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

feat(pkg/mmr): Add basic MMR support #4154

Closed
wants to merge 19 commits into from
Closed

Conversation

dimartiro
Copy link
Contributor

@dimartiro dimartiro commented Aug 29, 2024

Changes

Add basic MMR support following the 1st iteration implementation proposed here

Features

  • Leaves accumulation
  • Root calculation
  • Commit changes to underlying storage
  • Basic testing

Tests

go test ./pkg/mmr

Issues

Closes: #4152

@dimartiro dimartiro self-assigned this Aug 29, 2024
@dimartiro dimartiro linked an issue Aug 29, 2024 that may be closed by this pull request
Copy link
Member

@EclesioMeloJunior EclesioMeloJunior left a comment

Choose a reason for hiding this comment

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

LGTM! Would like to see more tests over getElement, also can the storage be generic over MMRElement?

pkg/mmr/mmr_batch.go Outdated Show resolved Hide resolved
@dimartiro
Copy link
Contributor Author

Would like to see more tests over getElement

I'm gonna add more, thanks for the review 👍

also can the storage be generic over MMRElement?

You mean to use a generic instead of using MMRElement? basically MMRElement is an alias for []byte I wanted to keep it simple since it is an MVP and using []byte is easier for us since we don't need any special serializer or deserializer.
I think we can improve it later if we have any future special requirement to use a generic

pkg/mmr/memstorage.go Outdated Show resolved Hide resolved
pkg/mmr/memstorage.go Outdated Show resolved Hide resolved
pkg/mmr/mmr.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jimjbrettj jimjbrettj left a comment

Choose a reason for hiding this comment

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

Really nice work! I already looked over it, but if you could do a once over of the GP appendix E to make sure it seems compatible before merging that would be great.

@dimartiro
Copy link
Contributor Author

Really nice work! I already looked over it, but if you could do a once over of the GP appendix E to make sure it seems compatible before merging that would be great.

I've been checking, and if I'm not mistaken, it aligns well with the JAM GP.

image

The Append function describes how to add a new element in a generic way and how to merge it with the current peaks. If I understand correctly, it considers an MMR, which isn't necessarily limited to binary tries, which is why the function P is described based on n.

Regarding the encoding function, it essentially describes the process of bagging the peaks and calculating the final MMR root so we are ok with that too.

This is all based on my interpretation of the functions described in the GP, but in the future, we can compare our implementation with Parity’s to ensure we have the same behavior 😄

Comment on lines +25 to +27
// MergeFunc defines a function type used to merge two elements in the MMR.
// It takes two elements, `left` and `right`, and returns a pointer to the
// merged result or an error if the merge operation fails.
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment is not needed, the func signature is self-explanatory imo, maybe you can use it to explain how the merge is implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, but I wanted to add a comment since it is an exported type

pkg/mmr/mmr.go Outdated Show resolved Hide resolved
@dimartiro
Copy link
Contributor Author

Closed in favor of https://github.com/ChainSafe/go-jam/pull/38

@dimartiro dimartiro closed this Sep 10, 2024
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.

(BEEFY): MMR MVP
3 participants