From 33b2c9367ae25e3eac500f680d2a6d6e06bfe9a7 Mon Sep 17 00:00:00 2001 From: robinbryce Date: Wed, 5 Jun 2024 10:16:19 +0100 Subject: [PATCH] fix: GetTrieKey and GetTrieEntry leafindex calc (#20) 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 --- massifs/go.mod | 2 +- massifs/go.sum | 4 ++-- massifs/massifcontext.go | 16 +++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/massifs/go.mod b/massifs/go.mod index bbe1ab7..753f969 100644 --- a/massifs/go.mod +++ b/massifs/go.mod @@ -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 ) diff --git a/massifs/go.sum b/massifs/go.sum index 8951388..3ff0ec6 100644 --- a/massifs/go.sum +++ b/massifs/go.sum @@ -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= diff --git a/massifs/massifcontext.go b/massifs/massifcontext.go index 7061d2e..ed4e5ea 100644 --- a/massifs/massifcontext.go +++ b/massifs/massifcontext.go @@ -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 { @@ -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 { @@ -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) @@ -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) }