Skip to content

Commit

Permalink
fix: GetTrieKey and GetTrieEntry leafindex calc (#20)
Browse files Browse the repository at this point in the history
Use the new (safe) mmr.LeafIndex method.

Also add comments in places where we use LeafCount directly indicating
why it is safe to do so

AB#9551

Co-authored-by: Robin Bryce <robin.bryce@datatrails.ai>
  • Loading branch information
robinbryce and Robin Bryce committed Jun 5, 2024
1 parent 6bea50d commit 33b2c93
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion massifs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/datatrails/go-datatrails-merklelog/massifs
go 1.22

require (
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.1
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.2
github.com/datatrails/go-datatrails-merklelog/mmrtesting v0.0.1
)

Expand Down
4 changes: 2 additions & 2 deletions massifs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/datatrails/go-datatrails-common v0.15.1 h1:wu3Gs6v7TkMLltzavPY2aHPniJabEiuqSJSHW79bX+4=
github.com/datatrails/go-datatrails-common v0.15.1/go.mod h1:lVLYVw5o+Wj+z8sn8bJBzp9qBCdYQ0DUX91+R5Gn73Q=
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.1 h1:XggMVejsJ72cAL/msjPhQUSQNeElSh/O1zZcvX4d4JU=
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.1/go.mod h1:+Oz8O6bns0rF6gr03xJzKTBzUzyskZ8Gics8/qeNzYk=
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.2 h1:Jxov4/onoFiCISLQNSPy/nyt3USAEvUZpEjlScHJYKI=
github.com/datatrails/go-datatrails-merklelog/mmr v0.0.2/go.mod h1:+Oz8O6bns0rF6gr03xJzKTBzUzyskZ8Gics8/qeNzYk=
github.com/datatrails/go-datatrails-merklelog/mmrtesting v0.0.1 h1:sIyXWKTadqmVEsPj66RlKwRKzNQ7hK9SH1fRjZFDCa8=
github.com/datatrails/go-datatrails-merklelog/mmrtesting v0.0.1/go.mod h1:KGdkOtamWG48EN4AXtTHPv6C0jJKrj840IMSkrD+egk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
16 changes: 11 additions & 5 deletions massifs/massifcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ func (mc *MassifContext) Get(i uint64) ([]byte, error) {
// GetTrieEntry gets the trie entry given the mmrIndex of its corresponding leaf node.
func (mc MassifContext) GetTrieEntry(mmrIndex uint64) ([]byte, error) {

trieIndex := mmr.LeafCount(mmrIndex+1) - 1
// Note: mmrIndex identifies an arbitrary node, so LeafIndex is necessary
trieIndex := mmr.LeafIndex(mmrIndex)

massifTrieIndex, err := mc.GetMassifTrieIndex(trieIndex)
if err != nil {
Expand All @@ -256,7 +257,8 @@ func (mc MassifContext) GetTrieEntry(mmrIndex uint64) ([]byte, error) {
// GetTrieKey gets the trie key given the mmrIndex of the trie entries corresponding leaf node.
func (mc MassifContext) GetTrieKey(mmrIndex uint64) ([]byte, error) {

trieIndex := mmr.LeafCount(mmrIndex+1) - 1
// Note: mmrIndex identifies an arbitrary node, so LeafIndex is necessary
trieIndex := mmr.LeafIndex(mmrIndex)

massifTrieIndex, err := mc.GetMassifTrieIndex(trieIndex)
if err != nil {
Expand Down Expand Up @@ -458,6 +460,9 @@ func (mc MassifContext) LastCommitUnixMS(idTimestampEpoch uint8) (int64, error)

// GetMassifLeafIndex returns the leafIndex into the whole log relative to the start of the massif leaf index.
func (mc MassifContext) GetMassifLeafIndex(leafIndex uint64) (uint64, error) {

// Note: FirstIndex is also the MMRSize at the end of the previous massif, so LeafCount is used.
// similarly RangeCount (below) will always return a valid MMRSize
firstLeafIndex := mmr.LeafCount(mc.Start.FirstIndex)
if leafIndex < firstLeafIndex {
return 0, fmt.Errorf("index %d: %w", leafIndex, ErrBeforeFirstLeaf)
Expand Down Expand Up @@ -490,10 +495,11 @@ func (mc MassifContext) GetLastIdTimestamp() uint64 {
// the number of leaves in the entire mmr call mmr.LeafCount directly)
func (mc MassifContext) MassifLeafCount() uint64 {

// Get the count of leaves in the entire mmr
// Get the count of leaves in the entire mmr, RangeCount always returns a valid MMRSize
count := mmr.LeafCount(mc.RangeCount())
// Subtract the number of leaves in the mmr defined by the end of the last blob
// to get the count of leaves in the current blob
// Subtract the number of leaves in the mmr defined by the end of the last
// blob to get the count of leaves in the current blob. FirstIndex is the
// valid MMRSize of the end of the preceding massif.
return count - mmr.LeafCount(mc.Start.FirstIndex)
}

Expand Down

0 comments on commit 33b2c93

Please sign in to comment.