Skip to content

Commit

Permalink
Same in Suffix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pav-kv committed Mar 13, 2021
1 parent 93af584 commit 85dc187
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
8 changes: 2 additions & 6 deletions storage/cache/subtree_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ func TestRepopulateLogSubtree(t *testing.T) {
t.Fatalf("merkle tree update failed: %v", err)
}

nodeID := tree.NewNodeIDFromPrefix(s.Prefix, logStrataDepth, numLeaves-1, logStrataDepth, maxLogDepth)
_, sfx := nodeID.Split(len(s.Prefix), int(s.Depth))
sfxKey := sfx.String()
sfxKey := toSuffix(compact.NewNodeID(0, uint64(numLeaves)-1))
s.Leaves[sfxKey] = leafHash
if numLeaves == 1<<uint(defaultLogStrata[0]) {
s.InternalNodeCount = uint32(len(cmtStorage.InternalNodes))
Expand Down Expand Up @@ -285,9 +283,7 @@ func BenchmarkRepopulateLogSubtree(b *testing.B) {
for i := 0; i < 256; i++ {
leaf := []byte(fmt.Sprintf("leaf %d", i))
hash := hasher.HashLeaf(leaf)
nodeID := tree.NewNodeIDFromPrefix(s.Prefix, logStrataDepth, int64(i), logStrataDepth, maxLogDepth)
_, sfx := nodeID.Split(len(s.Prefix), int(s.Depth))
s.Leaves[sfx.String()] = hash
s.Leaves[toSuffix(compact.NewNodeID(0, uint64(i)))] = hash
}

for n := 0; n < b.N; n++ {
Expand Down
37 changes: 20 additions & 17 deletions storage/tree/suffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package tree
import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"math/big"
"testing"
Expand Down Expand Up @@ -72,26 +73,23 @@ func TestParseSuffix(t *testing.T) {
}
}

func TestSplitParseSuffixRoundtrip(t *testing.T) {
func TestToSuffixParseRoundtrip(t *testing.T) {
for _, tc := range []struct {
prefix []byte
index int64
level int
index uint64
wantPath []byte
}{
// Because we're using logStrataDepth below we'll always get a one byte
// suffix path.
{prefix: h2b(""), index: 1, wantPath: h2b("01")},
{prefix: h2b("00"), index: 1, wantPath: h2b("01")},
{prefix: h2b("00"), level: 4, index: 10, wantPath: h2b("a0")},
{prefix: h2b("abcd"), index: 99, wantPath: h2b("63")},
{prefix: h2b("98765432"), index: 27, wantPath: h2b("1b")},
{prefix: h2b("12345678"), index: 253, wantPath: h2b("fd")},
{index: 1, wantPath: h2b("01")},
{level: 4, index: 10, wantPath: h2b("a0")},
{level: 3, index: 3, wantPath: h2b("18")},
{index: 99, wantPath: h2b("63")},
{index: 27, wantPath: h2b("1b")},
{index: 253, wantPath: h2b("fd")},
} {
nodeID := NewNodeIDFromPrefix(tc.prefix, logStrataDepth-tc.level, tc.index, logStrataDepth, maxLogDepth)
sfx := nodeID.Suffix(len(tc.prefix), logStrataDepth)
sfx := toSuffix(tc.level, tc.index)
sfxKey := sfx.String()

sfxP, err := ParseSuffix(sfxKey)
if err != nil {
t.Errorf("ParseSuffix(%s): %v", sfxKey, err)
Expand Down Expand Up @@ -139,17 +137,15 @@ func TestSuffixKeyEquals(t *testing.T) {
continue
}

nodeID := NewNodeIDFromPrefix(tc.prefix, logStrataDepth, tc.leafIndex, logStrataDepth, maxLogDepth)
sfxB := nodeID.Suffix(len(tc.prefix), logStrataDepth)
sfxBKey := sfxB.String()
sfxBKey := toSuffix(0, uint64(tc.leafIndex)).String()
sfxBBytes, err := base64.StdEncoding.DecodeString(sfxBKey)
if err != nil {
t.Errorf("splitNodeID(%v): _, %v", nodeID, err)
t.Errorf("splitNodeID(%v): _, %v", tc.leafIndex, err)
continue
}

if got, want := sfxBBytes, tc.want; !bytes.Equal(got, want) {
t.Errorf("[%x, %v].splitNodeID(%v, %v): %v.Serialize(): %x, want %x", nodeID.Path, nodeID.PrefixLenBits, len(tc.prefix), logStrataDepth, sfxB, got, want)
t.Errorf("[%x, %v].splitNodeID(%v): %v.Serialize(): %x, want %x", tc.leafIndex, len(tc.prefix), logStrataDepth, sfxBKey, got, want)
continue
}
}
Expand Down Expand Up @@ -306,3 +302,10 @@ func TestCacheIsolation(t *testing.T) {
t.Fatalf("cache instances are not immutable")
}
}

func toSuffix(level int, index uint64) *Suffix {
depth := logStrataDepth - level
var bytes [8]byte
binary.BigEndian.PutUint64(bytes[:], index<<(maxLogDepth-depth))
return NewSuffix(uint8(logStrataDepth), bytes[:])
}

0 comments on commit 85dc187

Please sign in to comment.