Skip to content

fix: use canonical transaction encoding for transactions in payloads and root calculations#240

Merged
fmoletta merged 7 commits into
mainfrom
impl-cannonical-encoding
Aug 12, 2024
Merged

fix: use canonical transaction encoding for transactions in payloads and root calculations#240
fmoletta merged 7 commits into
mainfrom
impl-cannonical-encoding

Conversation

@fmoletta

@fmoletta fmoletta commented Aug 9, 2024

Copy link
Copy Markdown
Contributor

Motivation

In a previous PR, transaction encoding and decoding for non legacy transactions was changed from tx_type || rlp(tx_fieldss) to rlp(bytes(tx_type || rlp(tx_fields))). While this is the correct encoding used when encoding and decoding a block with transactions, it removed the implementation of the "canonical" encoding that is used when decoding an execution payload and computing transaction roots. This PR aims to fix this issue by implementing the previous canonical encoding separately from the rlp encoding and updating the doc to avoid future confusions and problems

Description

  • Implement encode_canonical & decode_canonical for Transaction and use it to decode transactions in execution payloads and encode transactions when computing the transactions root.

  • Update documentation for rlp implementation of transactions to clearly state that it is encoding the transaction into a bytes object.

  • Other:

    • Revert rlp encoding of inner transactions to only encode the transaction fields and move type and bytes encoding to the implementation of encoding for the transaction enum. Reasoning: This allows us to use the rlp encoding for the inner transactions for both the canonical and bytes encoding of the transaction enum, and also fixes rlpdecode and rlpencode not being consistent with one another)

Closes None, but is needed for #51 to work

With this fix we can now successfully validate and execute all payloads with kurtosis local network 🚀

kurtosis-clip-cut.mov

@fmoletta fmoletta marked this pull request as ready for review August 12, 2024 13:54
@fmoletta fmoletta requested a review from a team as a code owner August 12, 2024 13:54
@fmoletta fmoletta changed the title fix: use canonical transaction encoding for transactions for payloads and root calculations fix: use canonical transaction encoding for transactions in payloads and root calculations Aug 12, 2024
// Value: tx_type || RLP(tx) if tx_type != 0
// RLP(tx) else
trie.insert(idx.encode_to_vec(), tx.encode_to_vec());
trie.insert(idx.encode_to_vec(), tx.encode_canonical_to_vec());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the previous change added a regression to the transaction root calculation. Maybe we can add some unit test for this method to avoid this from happening again in the future?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are we not validating this in EF tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The ef tests provide the blocks with the transaction root already set so it is always valid

@mpaulucci mpaulucci left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🙌

@fmoletta fmoletta added this pull request to the merge queue Aug 12, 2024
Merged via the queue into main with commit 191ad7c Aug 12, 2024
@fmoletta fmoletta deleted the impl-cannonical-encoding branch August 12, 2024 18:47
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