mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-09 05:20:10 +00:00
## Description Upstream https://github.com/lazyledger/lazyledger-core/pull/351 to optimize merkle tree hashing ### Benchmarking: ``` benchmark old ns/op new ns/op delta BenchmarkHashAlternatives/recursive-8 22914 21949 -4.21% BenchmarkHashAlternatives/iterative-8 21634 21939 +1.41% benchmark old allocs new allocs delta BenchmarkHashAlternatives/recursive-8 398 200 -49.75% BenchmarkHashAlternatives/iterative-8 399 301 -24.56% benchmark old bytes new bytes delta BenchmarkHashAlternatives/recursive-8 19088 6496 -65.97% BenchmarkHashAlternatives/iterative-8 21776 13984 -35.78% ``` cc @odeke-em @cuonglm
45 lines
999 B
Go
45 lines
999 B
Go
package merkle
|
|
|
|
import (
|
|
// it is ok to use math/rand here: we do not need a cryptographically secure random
|
|
// number generator here and we can run the tests a bit faster
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestKeyPath(t *testing.T) {
|
|
var path KeyPath
|
|
keys := make([][]byte, 10)
|
|
alphanum := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
for d := 0; d < 1e4; d++ {
|
|
path = nil
|
|
|
|
for i := range keys {
|
|
enc := keyEncoding(rand.Intn(int(KeyEncodingMax)))
|
|
keys[i] = make([]byte, rand.Uint32()%20)
|
|
switch enc {
|
|
case KeyEncodingURL:
|
|
for j := range keys[i] {
|
|
keys[i][j] = alphanum[rand.Intn(len(alphanum))]
|
|
}
|
|
case KeyEncodingHex:
|
|
rand.Read(keys[i])
|
|
default:
|
|
panic("Unexpected encoding")
|
|
}
|
|
path = path.AppendKey(keys[i], enc)
|
|
}
|
|
|
|
res, err := KeyPathToKeys(path.String())
|
|
require.Nil(t, err)
|
|
require.Equal(t, len(keys), len(res))
|
|
|
|
for i, key := range keys {
|
|
require.Equal(t, key, res[i])
|
|
}
|
|
}
|
|
}
|