Skip to content

Commit

Permalink
Fix massif index from mmr index (#21)
Browse files Browse the repository at this point in the history
re: AB#9491

Co-authored-by: jgough <[email protected]>
  • Loading branch information
honourfish and jgough authored Jun 10, 2024
1 parent 33b2c93 commit 0c1bd8e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
24 changes: 3 additions & 21 deletions massifs/masssifreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,10 @@ func MassifIndexFromLeafIndex(massifHeight uint8, leafIndex uint64) uint64 {
// given the mmr index of the leaf.
//
// NOTE: if the mmrIndex is not a leaf node, then error is returned.
func MassifIndexFromMMRIndex(massifHeight uint8, mmrIndex uint64) (uint64, error) {
func MassifIndexFromMMRIndex(massifHeight uint8, mmrIndex uint64) uint64 {

// First check if the given mmrIndex is a leaf node.
//
// NOTE: leaf nodes are always on height 0.
height := mmr.IndexHeight(mmrIndex)
if height != 0 {
return 0, ErrNotleaf
}

// HeightSize returns the maximum number of nodes for a given height of MMR. Where the leaf nodes
// start on height 1.
mmrSize := mmr.HeightSize(uint64(massifHeight))

// now find the massif.
//
// for context, see: https://github.com/datatrails/epic-8120-scalable-proof-mechanisms/blob/main/mmr/forestrie-mmrblobs.md#blob-size
//
// Note: massif indexes start at 0.
// Note: mmr indexes starts at 0.
massifIndex := mmrIndex / mmrSize
leafIndex := mmr.LeafIndex(mmrIndex)

return massifIndex, nil
return MassifIndexFromLeafIndex(massifHeight, leafIndex)

}
64 changes: 53 additions & 11 deletions massifs/masssifreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ func TestMassifIndexFromLeafIndex(t *testing.T) {
// 1. a height of 3 and a mmr index of 4, returns a massif index of 0
// 2. a height of 3 and a mmr index of 15, returns a massif index of 2
// 3. a height of 3 and a mmr index of 10, returns a massif index of 1
// 4. a height of 5 and a mmr index of 33, returns a massif index of 1
// 5. a height of 5 and a mmr index of 70, returns a massif index of 1
// 6. a height of 3 and a mmr index of 12, returns not a leaf err
// 7. a height of 5 and a mmr index of 72, returns not a leaf err
// 8. a height of 14 and a mmr index of 16382, returns a massif index of 1
// 4. a height of 3 and a mmr index of 11, returns a massif index of 1
// 5. a height of 3 and a mmr index of 14, returns a massif index of 1
// 6. a height of 5 and a mmr index of 33, returns a massif index of 1
// 7. a height of 5 and a mmr index of 70, returns a massif index of 1
// 8. a height of 3 and a mmr index of 12, returns a massif index of 1
// 9. a height of 5 and a mmr index of 72, returns a massif index of 2
// 10. a height of 14 and a mmr index of 16382, returns a massif index of 0
// 11. a height of 14 and a mmr index of 16383, returns a massif index of 1
// 12. a height of 14 and a mmr index of 999418, retruns a massif index of 60
// 13. a height of 14 and a mmr index of 999419, retruns a massif index of 61
func TestMassifIndexFromMMRIndex(t *testing.T) {
type args struct {
massifHeight uint8
Expand Down Expand Up @@ -144,6 +149,22 @@ func TestMassifIndexFromMMRIndex(t *testing.T) {
},
expected: 1,
},
{
name: "height 3, mmr index 11",
args: args{
massifHeight: 3,
mmrIndex: 11,
},
expected: 1,
},
{
name: "height 3, mmr index 14",
args: args{
massifHeight: 3,
mmrIndex: 14,
},
expected: 1,
},
{
name: "height 5, mmr index 32",
args: args{
Expand Down Expand Up @@ -174,32 +195,53 @@ func TestMassifIndexFromMMRIndex(t *testing.T) {
massifHeight: 3,
mmrIndex: 12,
},
expected: 0,
err: ErrNotleaf,
expected: 1,
},
{
name: "height 5, mmr index 72",
args: args{
massifHeight: 5,
mmrIndex: 72,
},
expected: 0,
err: ErrNotleaf,
expected: 2,
},
{
name: "height 14, mmr index 16382",
args: args{
massifHeight: 14,
mmrIndex: 16382,
},
expected: 0,
},
{
name: "height 14, mmr index 16383",
args: args{
massifHeight: 14,
mmrIndex: 16383,
},
expected: 1,
},
{
name: "height 14, mmr index 999418",
args: args{
massifHeight: 14,
mmrIndex: 999418,
},
expected: 60,
},
{
name: "height 14, mmr index 999419",
args: args{
massifHeight: 14,
mmrIndex: 999419,
},
expected: 61,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := MassifIndexFromMMRIndex(test.args.massifHeight, test.args.mmrIndex)
actual := MassifIndexFromMMRIndex(test.args.massifHeight, test.args.mmrIndex)

assert.Equal(t, test.err, err)
assert.Equal(t, test.expected, actual)
})
}
Expand Down

0 comments on commit 0c1bd8e

Please sign in to comment.