-
Notifications
You must be signed in to change notification settings - Fork 81
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
return nil, respErr | ||
} | ||
|
||
block, err := s.chain.GetBlock(hash) |
There was a problem hiding this comment.
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.
pkg/services/rpcsrv/server.go
Outdated
for _, tx := range block.Transactions { | ||
aers, err := s.chain.GetAppExecResults(tx.Hash(), trigger.Application) | ||
if err != nil { | ||
continue |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pkg/services/rpcsrv/server.go
Outdated
} | ||
} | ||
|
||
// Also check for notifications from the block itself (PostPersist) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
pkg/services/rpcsrv/server.go
Outdated
} | ||
|
||
// testComparator is a helper type for notification filtering | ||
type testComparator struct { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
pkg/services/rpcsrv/server_test.go
Outdated
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) |
There was a problem hiding this comment.
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.
RPC client needs to be updated to support this as well. |
There was a problem hiding this 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
for _, tx := range block.Transactions { | ||
aers, err := s.chain.GetAppExecResults(tx.Hash(), trigger.Application) | ||
if err != nil { | ||
continue |
There was a problem hiding this comment.
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.
pkg/services/rpcsrv/server.go
Outdated
} | ||
} | ||
|
||
// Also check for notifications from the block itself (PostPersist) |
There was a problem hiding this comment.
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?
pkg/services/rpcsrv/server.go
Outdated
} | ||
|
||
// testComparator is a helper type for notification filtering | ||
type testComparator struct { |
There was a problem hiding this comment.
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.
Please, ensure testing jobs are passing. Currently, unit-tests, project linter and DCO checks are failing. |
Hey guys, got some busy days ahead. I'll come back to this in one or two weeks max. Sorry for the delay |
c1849a5
to
dee62f4
Compare
dee62f4
to
cb76197
Compare
PR Updated. Changes:
Still pending:
About trigger filters: I don't know/don't have an opinion. |
return nil | ||
} | ||
|
||
func (b *TrimmedBlock) ToStackItem() stackitem.Item { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, remove it. We're only interested in proper deserialization from bytes.
Codecov ReportAttention: Patch coverage is
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. |
@@ -248,6 +248,10 @@ block. It can be removed in future versions, but at the moment you can use it | |||
to see how much GAS is burned with a particular block (because system fees are | |||
burned). | |||
|
|||
#### `getblocknotifications` call | |||
|
|||
This method returns notifications from a block organized by trigger type. Supports filtering by contract and event name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, preserve common line width (~80 symbols).
@@ -221,7 +235,6 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int { | |||
return size | |||
} | |||
|
|||
// ToStackItem converts Block to stackitem.Item. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless change.
type auxTrimmedBlockOut struct { | ||
TxHashes []util.Uint256 `json:"tx"` | ||
} | ||
|
||
// auxTrimmedBlockIn is used for JSON i/o. | ||
type auxTrimmedBlockIn struct { | ||
TxHashes []json.RawMessage `json:"tx"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON marshallers are not needed for this structure. DecodeBinary is enough for our purposes.
return block, br.Err | ||
} | ||
|
||
func (b TrimmedBlock) MarshalJSON() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also should be removed, why do we need this code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I considered adding a get trimmed block endpoint. I also had some issues to debug the RPC endpoints (it doesn't stop on the debugger). I had an error that I didn't know where it was coming from. Later I realized that it was due to the notification object serialization, not the block. The same for the 'toStackItem'. I didn't know if that was being used (now I know).
I also got confused because the tests 'were passing', when in practice, they weren't. I 'suspected' that this was related to some inner serilization / deserialization that I wasn't aware of. That's why I included these methods. I'll remove this and other parts that aren't being used.
return nil | ||
} | ||
|
||
func (b *TrimmedBlock) ToStackItem() stackitem.Item { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, remove it. We're only interested in proper deserialization from bytes.
@@ -18,3 +24,46 @@ func checkInt32(i int) error { | |||
} | |||
return nil | |||
} | |||
|
|||
func (c notificationComparatorFilter) EventID() neorpc.EventID { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All exported methods require a comment.
@@ -18,3 +24,46 @@ func checkInt32(i int) error { | |||
} | |||
return nil | |||
} | |||
|
|||
func (c notificationComparatorFilter) EventID() neorpc.EventID { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're trying to avoid util.go
files as much as possible. Let's create a dedicated file for these structures and name this file accordingly.
@@ -18,3 +24,46 @@ func checkInt32(i int) error { | |||
} | |||
return nil | |||
} | |||
|
|||
func (c notificationComparatorFilter) EventID() neorpc.EventID { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/notificationComparatorFilter/notificationEventComparator
@@ -18,3 +24,46 @@ func checkInt32(i int) error { | |||
} | |||
return nil | |||
} | |||
|
|||
func (c notificationComparatorFilter) EventID() neorpc.EventID { | |||
return c.id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c.id
field is not needed, remove it. notificatoinEventComparator
works only with ContainedNotificatoinEvent
, hence you may return neorpc.NotificationEventID
here.
Container: container, | ||
NotificationEvent: evt, | ||
} | ||
if filter == nil || rpcevent.Matches(¬ificationComparatorFilter{ |
There was a problem hiding this comment.
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.
Seems that we don't need it for now, so nothing should be done wrt this question. |
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. |
Closes #3779.
It works but I don't know if this is the best way to do it.