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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lock9
Copy link

@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.

return nil, respErr
}

block, err := s.chain.GetBlock(hash)
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 need some GetTrimmedBlock or alike, this one does extract all transactions and we don't need them (only hash is needed for GetAppExecResults()). But it's an optimization in its essence, current code will work fine.

for _, tx := range block.Transactions {
aers, err := s.chain.GetAppExecResults(tx.Hash(), trigger.Application)
if err != nil {
continue
Copy link
Member

Choose a reason for hiding this comment

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

Return internal error? This means some DB inconsistency and the result can be incorrect.

Copy link
Member

Choose a reason for hiding this comment

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

Vote up for error return.

}
}

// Also check for notifications from the block itself (PostPersist)
Copy link
Member

Choose a reason for hiding this comment

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

You need to keep the sequence correct and check for trigger.OnPersist before digging into transaction list.

Copy link
Member

Choose a reason for hiding this comment

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

We may additionally extend neorpc.NotificaitonFilter with trigger parameter, it may be useful in some cases, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

What are the use cases? People interested in block-level things (and not a lot happens there) probably pull block applogs directly and otherwise it's all Application.

}

// Also check for notifications from the block itself (PostPersist)
aers, err := s.chain.GetAppExecResults(block.Hash(), trigger.Application)
Copy link
Member

Choose a reason for hiding this comment

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

trigger.PostPersist here.

aers, err := s.chain.GetAppExecResults(block.Hash(), trigger.Application)
if err == nil && len(aers) > 0 {
aer := aers[0]
if aer.VMState == vmstate.Halt {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this code can be generalized in some way (helper function?), it's the same for blocks/transactions.

}

// testComparator is a helper type for notification filtering
type testComparator struct {
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 move these out into a separate file (along with the logic for appending to notifications.

Copy link
Member

Choose a reason for hiding this comment

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

Also, I'd say that testComparator/testContainer is suitable for testing code, but not for the production code. Let's rename it to something more appropriate.

params: `["` + genesisBlockHash + `", {"contract":"` + testContractHashLE + `", "name":"Transfer"}]`,
result: func(e *executor) any { return []state.ContainedNotificationEvent{} },
check: func(t *testing.T, e *executor, acc any) {
res, ok := acc.([]state.ContainedNotificationEvent)
Copy link
Member

Choose a reason for hiding this comment

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

Please check for expected contents as well.

@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.

for _, tx := range block.Transactions {
aers, err := s.chain.GetAppExecResults(tx.Hash(), trigger.Application)
if err != nil {
continue
Copy link
Member

Choose a reason for hiding this comment

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

Vote up for error return.

}
}

// Also check for notifications from the block itself (PostPersist)
Copy link
Member

Choose a reason for hiding this comment

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

We may additionally extend neorpc.NotificaitonFilter with trigger parameter, it may be useful in some cases, what do you think?

}

// testComparator is a helper type for notification filtering
type testComparator struct {
Copy link
Member

Choose a reason for hiding this comment

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

Also, I'd say that testComparator/testContainer is suitable for testing code, but not for the production code. Let's rename it to something more appropriate.

},
{
name: "positive with filter",
params: `["` + genesisBlockHash + `", {"contract":"` + testContractHashLE + `", "name":"Transfer"}]`,
Copy link
Member

Choose a reason for hiding this comment

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

Genesis block doesn't contain notifications from testContract. Let's use native Gas or Neo hash as a filter.

@@ -219,6 +220,7 @@ var rpcHandlers = map[string]func(*Server, params.Params) (any, *neorpc.Error){
"getblockhash": (*Server).getBlockHash,
"getblockheader": (*Server).getBlockHeader,
"getblockheadercount": (*Server).getBlockHeaderCount,
"getblocknotifications": (*Server).getBlockNotifications,
Copy link
Member

Choose a reason for hiding this comment

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

This extension should be documented at docs/rpc.md, see ### Extensions section.

@@ -219,6 +220,7 @@ var rpcHandlers = map[string]func(*Server, params.Params) (any, *neorpc.Error){
"getblockhash": (*Server).getBlockHash,
"getblockheader": (*Server).getBlockHeader,
"getblockheadercount": (*Server).getBlockHeaderCount,
"getblocknotifications": (*Server).getBlockNotifications,
Copy link
Member

Choose a reason for hiding this comment

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

This extension should be supported at the RPC client side (pkg/rpcclient/rpc.go).

@AnnaShaleva
Copy link
Member

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

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