Skip to content

Commit

Permalink
Optimize hashChildren global var out
Browse files Browse the repository at this point in the history
  • Loading branch information
pav-kv committed Apr 27, 2022
1 parent 77f1b57 commit 09877c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
11 changes: 5 additions & 6 deletions compact/range_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import (
"testing"
)

var (
hashChildren = func(_, _ []byte) []byte { return []byte("fake-hash") }
factory = &RangeFactory{Hash: hashChildren}
)
var factory = &RangeFactory{Hash: func(_, _ []byte) []byte {
return []byte("fake-hash")
}}

func TestAppendRangeErrors(t *testing.T) {
anotherFactory := &RangeFactory{Hash: hashChildren}
anotherFactory := &RangeFactory{Hash: factory.Hash}

nonEmpty1, _ := factory.NewRange(7, 8, [][]byte{[]byte("hash")})
nonEmpty2, _ := factory.NewRange(0, 6, [][]byte{[]byte("hash0"), []byte("hash1")})
Expand Down Expand Up @@ -100,7 +99,7 @@ func TestEqual(t *testing.T) {
hashes: [][]byte{[]byte("hash 1"), []byte("hash 2")},
},
rhs: &Range{
f: &RangeFactory{Hash: hashChildren},
f: &RangeFactory{Hash: factory.Hash},
begin: 17,
end: 23,
hashes: [][]byte{[]byte("hash 1"), []byte("hash 2")},
Expand Down
9 changes: 3 additions & 6 deletions compact/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import (
"github.com/transparency-dev/merkle/testonly"
)

var (
hashChildren = rfc6962.DefaultHasher.HashChildren
factory = &compact.RangeFactory{Hash: hashChildren}
)
var factory = &compact.RangeFactory{Hash: rfc6962.DefaultHasher.HashChildren}

// leafData returns test leaf data that depends on the passed in leaf index.
func leafData(index uint64) []byte {
Expand Down Expand Up @@ -75,7 +72,7 @@ func newTree(t *testing.T, size uint64) (*tree, compact.VisitFn) {
// Compute internal node hashes.
for lvl := 1; lvl < levels; lvl++ {
for i := range nodes[lvl] {
nodes[lvl][i].hash = hashChildren(nodes[lvl-1][i*2].hash, nodes[lvl-1][i*2+1].hash)
nodes[lvl][i].hash = factory.Hash(nodes[lvl-1][i*2].hash, nodes[lvl-1][i*2+1].hash)
}
}

Expand All @@ -91,7 +88,7 @@ func (tr *tree) rootHash() []byte {
if hash == nil {
hash = root
} else {
hash = hashChildren(root, hash)
hash = factory.Hash(root, hash)
}
}
}
Expand Down

0 comments on commit 09877c5

Please sign in to comment.