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

Make in-memory Merkle tree immutable #2701

Closed
wants to merge 5 commits into from

Conversation

pav-kv
Copy link
Contributor

@pav-kv pav-kv commented Apr 13, 2022

This change makes the inmemory.Tree Merkle tree type behave similarly to a regular Go slice.
It provides variadic Append* methods which most of the time reuse memory, but may reallocate
and copy data. It is safe to reuse all previous Tree objects for reads, and it is possible to roll
back and use for writes (with caution, knowing that it may rewrite hashes of any bigger Tree).

@pav-kv pav-kv force-pushed the immutable_merkle branch 2 times, most recently from af66de7 to 5c6d926 Compare April 14, 2022 16:21
@pav-kv pav-kv force-pushed the immutable_merkle branch from 5c6d926 to 914ee91 Compare April 20, 2022 10:31
@pav-kv pav-kv requested a review from jiggoha April 20, 2022 10:46
@pav-kv pav-kv force-pushed the immutable_merkle branch from 91737c9 to 59220f5 Compare April 20, 2022 13:29
@pav-kv pav-kv requested a review from phbnf April 20, 2022 13:39
@pav-kv pav-kv marked this pull request as ready for review April 20, 2022 13:39
@pav-kv pav-kv requested a review from a team as a code owner April 20, 2022 13:39
@pav-kv
Copy link
Contributor Author

pav-kv commented Apr 20, 2022

On a second thought, perhaps it's better to leave this type as is, so that it's simpler. It is used only for testing, so it should be intuitive.

The "feature" of this approach allows implementing transactions on Merkle trees. For example, in an in-memory Merkle tree storage. However, I don't think we would need it soon.

@pav-kv
Copy link
Contributor Author

pav-kv commented Apr 20, 2022

Making Append variadic is good though, so I'll just leave that, and remove the immutability. WDYT?

@pav-kv
Copy link
Contributor Author

pav-kv commented Apr 20, 2022

Please review #2706 instead.

@pav-kv pav-kv closed this Apr 20, 2022
@pav-kv pav-kv deleted the immutable_merkle branch April 20, 2022 15:14
@@ -124,16 +124,16 @@ var testProofs = []proofTestVector{
}},
}

func decodeHexStringOrPanic(hs string) []byte {
// hx decodes a hex string or panics.
func hx(hs string) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is unrelated to this CL, right?
Although I like the shortness, after this PR, it is unclear from looking at the name of this function wether is decodes or encodes a string in hex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this could be separated, I just did it as a drive-by because otherwise the lists of constants would look too verbose.

This file was old and ugly anyway, so #2707 refactors it. After the refactoring, hx is used only with constants that are inlined, e.g. hx("6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d"), and never used with variables like hx(someVariable). So it's obvious from context that it decodes, and the pros is that the name is short.

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