-
Notifications
You must be signed in to change notification settings - Fork 437
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
Refactor TxDecoder
#7334
Refactor TxDecoder
#7334
Conversation
- Use dynamic for now
src/Nethermind/Nethermind.Serialization.Rlp/MyTxDecoder/BlobTxDecoder.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Serialization.Rlp/MyTxDecoder/ITxDecoder.cs
Outdated
Show resolved
Hide resolved
src/Nethermind/Nethermind.Serialization.Rlp/MyTxDecoder/OptimismTxDecoder.cs
Outdated
Show resolved
Hide resolved
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 went from one extreme to the other. From sharing everything in one class to extreme code duplication in multiple classes. Can we share the code that is the same for different decoders either by base class or some static methods?
src/Nethermind/Nethermind.Blockchain.Test/Validators/TxValidatorTests.cs
Show resolved
Hide resolved
- Only one use case which we can manually force
TxDecoder.Instance.Decode(ref decoderContext, ref _txBuffer, RlpBehaviors.AllowUnsigned); | ||
Hash256 _ = _txBuffer.Hash; // Force Hash evaluation |
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.
This was the only instance where the hash was evaluated eagerly. Instead of distorting the API to conform to this single use case, we force the hash evaluation immediately.
I tried using some form of template pattern where different transaction types can override the "default" behavior. In my opinion, this resulted in heavily coupled code with a lot of implicit assumptions since the "base class" methods resulted in a mix of abstract and virtual that always throw (ex. I think there are two main sources of duplication:
For the second case some higher order functions could help at the expense of performance. I will create a separate PR showing this approach and we can discuss if it is worth or not. |
|
During this refactor we explored an alternative design to the current RLP encoding/decoding API that is more declarative and results in less duplication: Essentially, we describe how to traverse a data structure (in this case a
Unfortunately I could not figure out a way to describe the traversal once and use it for both decoding and encoding, yet we are able to remove quite a bit of duplicated code. We might want to revisit this approach in the future. |
Partially solves #7313
Changes
TxDecoder
to dispatch (de/en)coding to appropriate sub (de/en)coders.Types of changes
What types of changes does your code introduce?
Testing
Requires testing
If yes, did you write tests?
Notes on testing
Tested against the existing test suite with a single breaking change
Documentation
Requires documentation update
Requires explanation in Release Notes
Remarks
The existing API for the
TxDecoder
remains untouched and only one (nonsensical) test was removed. The new design picks from a list of decoders the appropriate one based on the transaction type, throwing when the type is not recognized.This is a first step in a list of PRs, and its intentionally kept "small" to avoid having a single large refactoring PR.