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

Add decodeArgs() method for Event #33

Merged
merged 5 commits into from
Feb 14, 2022

Conversation

devictr
Copy link
Contributor

@devictr devictr commented Feb 8, 2022

Hey @esaulpaugh,

Here's my implementation of the decode function for Events.
I still need to add a few more tests for edge cases, but I wanted your opinion before doing so.
In a few words:

  • Indexed parameters are passed as a topics array, that we can easily decode using the usual decoding functions. However, dynamic types are not decodable as only a special hash of their encoding is stored, so we decode that instead.
  • Non-indexed parameters are all bundled in a data byte array, which we decode in one go. I'm not a fan of the data naming, but that's the terminology used in the Solidity docs, so I figured it's better to stay consistent.
  • We reconstruct and return a Tuple that contains all those parameters in the right order

This split between topics and data as input parameters is similar to what is done in ethereumj or to how things are presented in tools like etherscan.

Would that implementation work for you?
Feel free to also let me know about any code style changes you would like me to make, in order to match the project's choices in that regard.

@devictr devictr mentioned this pull request Feb 8, 2022
@esaulpaugh
Copy link
Owner

looks good so far. Other than more tests, all I can think to add right now is private static final ArrayType<ByteType, byte[]> BYTES_32 = TypeFactory.create("bytes32");

@esaulpaugh
Copy link
Owner

I would probably also eliminate decodedTopicsTuple and just use decodedTopics[topicIndex++]

@devictr
Copy link
Contributor Author

devictr commented Feb 10, 2022

@esaulpaugh Fixed your comments and added more tests

@esaulpaugh
Copy link
Owner

Okay, thanks. I will look at it.

@esaulpaugh
Copy link
Owner

esaulpaugh commented Feb 12, 2022

I'm thinking that if it's going to be doing decoding, Event should derive indexed and non-indexed TupleTypes only once, when constructed, and reuse them. Right? Unless there's a compelling reason not to, I think Event could just keep references to three TupleTypes: inputs, indexed, and nonIndexed.

And non-anonymous Events would calculate topics[0] once during construction and use it to verify the topics arrays it's decoding with Arrays.equals? new Keccak(256).digest(Strings.decode(getCanonicalSignature(), Strings.ASCII))

What do you think?

@devictr
Copy link
Contributor Author

devictr commented Feb 14, 2022

That sounds great! Adding those things now

@devictr
Copy link
Contributor Author

devictr commented Feb 14, 2022

@esaulpaugh Here you go! I opted to throw a RuntimeException when the signature hashes don't match for non-anonymous events, but I'm thinking we should probably create our own exception for that. What do you think? Maybe there's another standard exception we could use

@devictr
Copy link
Contributor Author

devictr commented Feb 14, 2022

I also refactored my code a bit and extracted some parts into more methods

@esaulpaugh
Copy link
Owner

I usually just throw IllegalArgumentException for anything attributable to the caller.

@devictr
Copy link
Contributor Author

devictr commented Feb 14, 2022

Fixed! If this looks good to you and you decide to merge it, would you mind cutting a release for it? That way I can merge some internal PR I have open as well 🙂
I'd also be happy to contribute more. If you open issues for things you need help with, I'd love to take a look when I have time

@esaulpaugh
Copy link
Owner

LGTM. I will be releasing version 6.0.0 with this included soon.

Thanks. After the release I will go through the abi-spec some more and see if there is anything else I'm missing.

@esaulpaugh esaulpaugh merged commit 3cc995a into esaulpaugh:master Feb 14, 2022
@devictr
Copy link
Contributor Author

devictr commented Feb 15, 2022

Great! Thank you

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.

2 participants