Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get block notifications API #3781

Closed
wants to merge 1 commit into from

Conversation

lock9
Copy link
Contributor

@lock9 lock9 commented Jan 6, 2025

Closes #3779.

It works but I don't know if this is the best way to do it.

Copy link
Member

@roman-khimov roman-khimov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one overall.

pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server_test.go Outdated Show resolved Hide resolved
@roman-khimov
Copy link
Member

RPC client needs to be updated to support this as well.

Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A useful extension.

pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server_test.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/server.go Show resolved Hide resolved
pkg/services/rpcsrv/server.go Show resolved Hide resolved
@AnnaShaleva
Copy link
Member

Please, ensure testing jobs are passing. Currently, unit-tests, project linter and DCO checks are failing.

@lock9
Copy link
Contributor Author

lock9 commented Jan 15, 2025

Hey guys, got some busy days ahead. I'll come back to this in one or two weeks max. Sorry for the delay

@lock9 lock9 force-pushed the block-notifications branch 2 times, most recently from dee62f4 to cb76197 Compare January 20, 2025 06:34
@lock9
Copy link
Contributor Author

lock9 commented Jan 20, 2025

PR Updated.

Changes:

  • Added GetTrimmedBlock and TrimmedBlock
    • Note: There's already the concept of 'trimmed block' and this name may not be ideal
  • Added a new struct "BlockNotifications", grouping notifications by trigger type
  • Updated the tests to use the neo token hash
  • Updated the tests to check the notification content
  • Changed the name of some functions/types
  • Moved reusable logic to the utils file
  • Added the new endpoint to the rpc docs file
  • Updated the client class to support the new endpoint

Still pending:

  • RPC Client tests

About trigger filters: I don't know/don't have an opinion.

pkg/core/block/block.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 20, 2025

Codecov Report

Attention: Patch coverage is 61.25000% with 62 lines in your changes missing coverage. Please review.

Project coverage is 82.99%. Comparing base (16a6a59) to head (d012a6b).

Files with missing lines Patch % Lines
pkg/core/block/block.go 45.33% 33 Missing and 8 partials ⚠️
pkg/services/rpcsrv/server.go 67.56% 9 Missing and 3 partials ⚠️
pkg/rpcclient/rpc.go 0.00% 6 Missing ⚠️
pkg/core/dao/dao.go 72.72% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3781      +/-   ##
==========================================
- Coverage   83.14%   82.99%   -0.16%     
==========================================
  Files         335      335              
  Lines       46896    47056     +160     
==========================================
+ Hits        38994    39054      +60     
- Misses       6310     6399      +89     
- Partials     1592     1603      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

docs/rpc.md Outdated Show resolved Hide resolved
@@ -221,7 +235,6 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
return size
}

// ToStackItem converts Block to stackitem.Item.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still relevant.

pkg/core/block/block.go Outdated Show resolved Hide resolved
pkg/core/block/block.go Outdated Show resolved Hide resolved
pkg/core/block/block.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/util.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/util.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/util.go Outdated Show resolved Hide resolved
pkg/services/rpcsrv/util.go Outdated Show resolved Hide resolved
Container: container,
NotificationEvent: evt,
}
if filter == nil || rpcevent.Matches(&notificationComparatorFilter{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter == nil check is excessive. rpcevent.Matche perfectly handle this case.

@AnnaShaleva
Copy link
Member

About trigger filters: I don't know/don't have an opinion.

Seems that we don't need it for now, so nothing should be done wrt this question.

@AnnaShaleva
Copy link
Member

Please, format commits according to https://github.com/nspcc-dev/.github/blob/master/git.md#commits. See https://github.com/nspcc-dev/neo-go/pull/3787/commits for example.

Also, please ensure your commits have valid signed-off-by signature.

pkg/core/dao/dao.go Outdated Show resolved Hide resolved
Add new RPC method to retrieve block notifications organized by trigger type.

Signed-off-by: Ricardo Prado <[email protected]>
@lock9 lock9 force-pushed the block-notifications branch from d012a6b to 3d27ef8 Compare February 10, 2025 15:23
@@ -221,7 +235,6 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
return size
}

// ToStackItem converts Block to stackitem.Item.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still relevant.

@@ -123,6 +124,55 @@ func TestPutGetBlock(t *testing.T) {
require.Error(t, err)
}

func TestGetTrimmedBlock(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover, fails to test.

@@ -3149,3 +3149,8 @@ func (bc *Blockchain) GetStoragePrice() int64 {
}
return bc.contracts.Policy.GetStoragePriceInternal(bc.dao)
}

// GetTrimmedBlock returns a block with only the header and transaction hashes.
func (bc *Blockchain) GetTrimmedBlock(hash util.Uint256) (*block.TrimmedBlock, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be fixed, fails to build.


// BlockNotifications represents notifications from a block organized by trigger type.
type BlockNotifications struct {
PrePersistNotifications []state.ContainedNotificationEvent `json:"prepersist,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we better have onpersist instead of prepersist in JSON, while pre is technically more correct here we have on used in trigger, method and many other places, so it'd be a bit more consistent this way.

Comment on lines +7 to +11
// BlockNotifications represents notifications from a block organized by trigger type.
type BlockNotifications struct {
PrePersistNotifications []state.ContainedNotificationEvent `json:"prepersist,omitempty"`
TxNotifications []state.ContainedNotificationEvent `json:"transactions,omitempty"`
PostPersistNotifications []state.ContainedNotificationEvent `json:"postpersist,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still drop Notifications from all of the field names, it's excessive. Actually following trigger names makes total sense here, OnPersist/Application/PostPersist like in https://github.com/nspcc-dev/neo-go/blob/8d728b4ec179e6354c21fc7e2c4f8a7c4df7312e/pkg/smartcontract/trigger/trigger_type.go

notifications := &result.BlockNotifications{}

aers, err := s.chain.GetAppExecResults(block.Hash(), trigger.OnPersist)
if err == nil && len(aers) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still relevant.

}

aers, err = s.chain.GetAppExecResults(block.Hash(), trigger.PostPersist)
if err == nil && len(aers) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also.

NotificationEvent: evt,
}
if rpcevent.Matches(&notificationEventComparator{
filter: *filter,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this panic on nil filter? Probably something was a bit different when #3781 (comment) was made.

@roman-khimov
Copy link
Member

I can't push to linkd-academy:block-notifications, so this one is replaced with #3805 (which includes changes from this PR), thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to fetch all notifications from a block (with filters)
3 participants