-
Notifications
You must be signed in to change notification settings - Fork 20.4k
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
abi: adding the method EventById and its test #19359
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.
See inline comments, it would be better to return a (possibly empty) slice rather than an error
accounts/abi/abi.go
Outdated
|
||
// EventById looks up a event by the topic hash | ||
// returns nil if none found | ||
func (abi *ABI) EventById(topic common.Hash) (*Event, 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.
I see two issues with this approach:
- The only kind of error that can arise is that there are no event with this topic, which is not an error a priori. It's only an error if the calling code considers no event to be an error, but we shouldn't be making this assumption at this stage.
- If there are more than one event with a given topic, you will only be returning one.
As a result, I recommend returning a slice of pointers, i.e. :
func (abi *ABI) EventById(topic common.Hash) []*Event {
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.
thx you for the review. First point makes complete sense to me.
However, on the second point, I think we can have at most one event per topic. The topic is the hash of the event name and arg types. Example from the test file, the topic is:
crypto.Keccak256Hash([]byte("received(address,uint256,bytes)"))
therefore shouldn't be the mapping topic -> event unique ? I can't see how we could get multiple Event struct
for a single topic (but having a collision).
accounts/abi/abi_test.go
Outdated
{"indexed":false,"name":"amount","type":"uint256"}, | ||
{"indexed":false,"name":"memo","type":"bytes"} | ||
] | ||
}]` |
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.
so yeah please test with 0 event, 1 event and more than one event
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.
as I said in my previous comment, a topic has a single event and an event has a single topic (hash is unique).
My test already includes a case for
- event can be found
- event cannot be found
Co-authored-by: Victor Tran <vu.tran54@gmail.com>
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.
@salanfe good point, so in that case let me backtrack and let's keep the error
field - I don't want to have a nil
returned without some sort of warning.
There was a PR from @tranvictor who did the same thing, I have merged both in this one, and you are both attributed as an author of course. Thanks again for your work!
👍 great thx |
adding the method
EventById
to the ABI, that allows to retrieve the event given a topic hash. Use case is similar to the methodMethodById
: given a transactoionReceipt with a log slice and the contract ABI,EventById
returns the corresponding event "object" matching the log's topic field.