Skip to content

Commit

Permalink
NodeID: Remove unused BigInt format (#2379)
Browse files Browse the repository at this point in the history
This format was only used by HStar2 which was recently removed.
  • Loading branch information
pav-kv authored Mar 2, 2021
1 parent 3bc7219 commit 6b396b7
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 117 deletions.
1 change: 0 additions & 1 deletion storage/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type PrepareSubtreeWriteFunc func(*storagepb.SubtreeProto) error
var (
NewNodeIDFromHash = tree.NewNodeIDFromHash
NewNodeIDFromPrefix = tree.NewNodeIDFromPrefix
NewNodeIDFromBigInt = tree.NewNodeIDFromBigInt
NewNodeIDForTreeCoords = tree.NewNodeIDForTreeCoords
NewNodeIDFromPrefixSuffix = tree.NewNodeIDFromPrefixSuffix

Expand Down
1 change: 1 addition & 0 deletions storage/storagepb/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
"testing"

_ "github.com/golang/glog"
"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/google/trillian/merkle/smt"
"github.com/google/trillian/storage/storagepb"
Expand Down
52 changes: 0 additions & 52 deletions storage/tree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"math/bits"
"strconv"
"strings"

"github.com/golang/glog"
)

const hexChars = "0123456789abcdef"
Expand Down Expand Up @@ -158,50 +155,6 @@ func NewNodeIDFromPrefix(prefix []byte, depth int, index int64, subDepth, totalD
}
}

// NewNodeIDFromBigInt creates a new node for a given depth and index, where
// the index can exceed a 64-bit range. The total tree depth must be provided.
// This occurs in the sparse Merkle tree implementation for maps as the lower
// levels have up 2^(hash size) entries. For log trees see NewNodeIDForTreeCoords.
func NewNodeIDFromBigInt(depth int, index *big.Int, totalDepth int) NodeID {
if got, want := totalDepth%8, 0; got != want {
panic(fmt.Sprintf("storage NewNodeFromBitInt(): totalDepth mod 8: %v, want %v", got, want))
}

if totalDepth == 0 {
panic("storage NewNodeFromBitInt(): totalDepth must not be zero")
}

// Put index in the LSB bits of path.
// This code more-or-less pinched from nat.go in the golang math/big package:
const _S = bits.UintSize / 8
path := make([]byte, totalDepth/8)

iBits := index.Bits()
i := len(path)
loop:
for _, d := range iBits {
for j := 0; j < _S; j++ {
i--
if i < 0 {
break loop
}
path[i] = byte(d)
d >>= 8
}
}

// TODO(gdbelvin): consider masking off insignificant bits past depth.
if glog.V(5) {
glog.Infof("NewNodeIDFromBigInt(%v, %x, %v): %v, %x",
depth, index, totalDepth, depth, path)
}

return NodeID{
Path: path,
PrefixLenBits: depth,
}
}

// NewNodeIDFromID2 constructs a NodeID from a NodeID2.
func NewNodeIDFromID2(id NodeID2) NodeID {
path := make([]byte, bytesForBits(int(id.BitLen())))
Expand All @@ -217,11 +170,6 @@ func (n NodeID) ToNodeID2() NodeID2 {
return NewNodeID2(string(n.Path), uint(n.PrefixLenBits))
}

// BigInt returns the big.Int for this node.
func (n NodeID) BigInt() *big.Int {
return new(big.Int).SetBytes(n.Path)
}

// NewNodeIDForTreeCoords creates a new NodeID for a Tree node with a specified depth and
// index.
// This method is used exclusively by the Log, and, since the Log model grows upwards from the
Expand Down
67 changes: 3 additions & 64 deletions storage/tree/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"testing"

_ "github.com/golang/glog"
)

func TestMaskLeft(t *testing.T) {
Expand Down Expand Up @@ -121,57 +122,6 @@ func TestNewNodeIDFromPrefixPanic(t *testing.T) {
}
}

func TestNewNodeIDFromBigInt(t *testing.T) {
for _, tc := range []struct {
depth int
index *big.Int
totalDepth int
wantPath []byte
wantDepth int
}{
{256, new(big.Int).SetBytes(h2b("00")), 256, h2b("0000000000000000000000000000000000000000000000000000000000000000"), 256},
{256, new(big.Int).SetBytes(h2b("01")), 256, h2b("0000000000000000000000000000000000000000000000000000000000000001"), 256},
{
8, new(big.Int).SetBytes(h2b("4100000000000000000000000000000000000000000000000000000000000000")), 256,
h2b("4100000000000000000000000000000000000000000000000000000000000000"), 8,
},
} {
n := NewNodeIDFromBigInt(tc.depth, tc.index, tc.totalDepth)
if got, want := n.Path, tc.wantPath; !bytes.Equal(got, want) {
t.Errorf("NewNodeIDFromBigInt(%v, %x, %v): %x, want %x",
tc.depth, tc.index.Bytes(), tc.totalDepth, got, want)
}
if got, want := n.PrefixLenBits, tc.wantDepth; got != want {
t.Errorf("NewNodeIDFromBigInt(%v, %x, %v): depth %v, want %v",
tc.depth, tc.index.Bytes(), tc.totalDepth, got, want)
}
// We should be able to get the same big.Int back. This is used in
// the HStar2 implementation so should be tested.
if got, want := n.BigInt(), tc.index; want.Cmp(got) != 0 {
t.Errorf("NewNodeIDFromBigInt(%v, %x, %v):got:\n%v, want:\n%v",
tc.depth, tc.index.Bytes(), tc.totalDepth, got, want)
}
}
}

func TestNewNodeIDFromBigIntPanic(t *testing.T) {
for b := 0; b < 64; b++ {
t.Run(fmt.Sprintf("%dbits", b), func(t *testing.T) {
// Only multiples of 8 bits should be accepted. This method also
// fails for 0 bits, unlike NewNodeIDFromPrefix.
want := (b%8 != 0) || b == 0
// Unfortunately we have to test for panics.
defer func() {
got := recover()
if (got != nil && !want) || (got == nil && want) {
t.Errorf("Incorrect panic behaviour (b=%d) got: %v, want: %v", b, got, want)
}
}()
_ = NewNodeIDFromBigInt(12, big.NewInt(234), b)
})
}
}

// goldenSplitData is golden test data for operations which split up a given
// NodeID in some way.
var goldenSplitData = []struct {
Expand Down Expand Up @@ -637,7 +587,7 @@ func TestString(t *testing.T) {
wantKey: "112:7468697320697320612068617368",
},
{
n: NewNodeIDFromBigInt(5, big.NewInt(20), 16),
n: NewNodeIDFromPrefix(nil, 5, 20, 0, 16),
want: "00000",
wantKey: "5:00",
},
Expand Down Expand Up @@ -837,14 +787,3 @@ func BenchmarkSuffix(b *testing.B) {
_ = n.Suffix(10, 176)
}
}

func runBenchmarkNewNodeIDFromBigInt(b *testing.B, f func(int, *big.Int, int) NodeID) {
b.Helper()
for i := 0; i < b.N; i++ {
_ = f(256, new(big.Int).SetBytes(h2b("00")), 256)
}
}

func BenchmarkNewNodeIDFromBigIntNew(b *testing.B) {
runBenchmarkNewNodeIDFromBigInt(b, NewNodeIDFromBigInt)
}

0 comments on commit 6b396b7

Please sign in to comment.