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

(De)serialization of Merkle Nodes #98

Merged
merged 53 commits into from
Jun 23, 2023
Merged

(De)serialization of Merkle Nodes #98

merged 53 commits into from
Jun 23, 2023

Conversation

emlautarom1
Copy link
Contributor

@emlautarom1 emlautarom1 commented Jun 21, 2023

Add a new namespace Merkle which describes Merkle constructs. For now, we describe Merkle Nodes and their corresponding (de)serialization methods. The current nodes are:

  • Branch: 1 byte for type + dirty, 2 bytes for nibbles, optional 32 bytes for keccak
  • Extension: 1 byte for type + dirty, var-length for path (usually it will be a nibble or a few)
  • Leaf: 1 byte for type + dirty, var-length for path, 32 bytes for keccak

Test suite is included (98% coverage).

@emlautarom1
Copy link
Contributor Author

WriteTo should have the semantics of returning the actual payload that was written (sliced if needed to make it shorter than max). WriteToWithLeftover is the one that returns what is left

All nodes now have both a WriteTo (returns the actual used bytes) and a WriteToWithLeftover (return a the leftover to be used by another writer)

@emlautarom1
Copy link
Contributor Author

Serialization tests should assert var output = WriteTo(stackalloc ...) with X.ReadFrom(output)

All serialization tests now work like this, and they also validate that output is exactly the bytes used during WriteTo.

Copy link
Contributor

@Scooletz Scooletz left a comment

Choose a reason for hiding this comment

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

One comment about the leftover. Let me know what you think

public Span<byte> WriteTo(Span<byte> output)
{
Bytes.CopyTo(output);
return output.Slice(Size);
Copy link
Contributor

Choose a reason for hiding this comment

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

If they write with Leftover it's up to the caller to get the actual payload? I'd provide just WriteTo (the usual case will be the caller serializing and playing with the payload) or the two. If needed, later we can refactor towards a struct that handles both cases;

@emlautarom1
Copy link
Contributor Author

Included support for optional Keccaks on Branch. If no Keccak is provided, then the default (Keccak.Zero) is used. (De)serialization takes this into consideration, so the size of a Branch with no Keccak is smaller that the one with it.

@github-actions
Copy link

Code Coverage

Package Line Rate Branch Rate Health
Paprika 89% 79%
Summary 89% (1754 / 1974) 79% (431 / 548)

Minimum allowed line rate is 75%

Copy link
Contributor

@Scooletz Scooletz left a comment

Choose a reason for hiding this comment

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

LET'S GO!

@Scooletz Scooletz merged commit 95b2c34 into main Jun 23, 2023
2 checks passed
@Scooletz Scooletz deleted the feature/merkle_node branch June 23, 2023 07:47
@Scooletz Scooletz mentioned this pull request Jun 26, 2023
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ethereum An Ethereum specific work item that requires a good understanding of Eth
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants