Implement conversion from PartialMerkleTree
to TieredSmt
#209
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are a few things I don't like about the current implementation. This PR serves to get the conversation going about the best way to implement the PMT -> TSMT conversion.
Problem statement
It is not possible to convert a
PartialMerkleTree
into aTieredSmt
because the PMT stores only values (accessed byNodeIndex
), whereas theTieredSmt
associates a key to every value. Therefore, to convert PMT -> TSMT, we must provide the key associated with each value in the PMT.Current solution
The current solution uses
TryFrom<(PMT, BTreeMap)> for TieredSmt
, where theBTreeMap
provides the "value -> key" information.I also used an iterator-based approach (with a helper from
itertools
), as it is the most efficient approach; for all other solutions I tried, I was forced to create one or moreVec<>
along the way, or traversepmt.leaves()
twice. However, the downside is that the expression in thetry_from()
is a little difficult to parse.Finally, it is not clear from the type
BTreeMap<Digest, Digest>
what it is supposed to do. The problem I faced was thatWord
(the actual type for values in the tree) doesn't implementOrd
, and so cannot be used as the key to aBTreeMap
. The workaround is to store values asDigest
, and convert them back toWord
at some point in the process. I think we need a better solution for that.