forked from prysmaticlabs/go-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_cache_test.go
133 lines (124 loc) · 2.79 KB
/
hash_cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ssz
import (
"bytes"
"encoding/binary"
"log"
"testing"
"time"
)
type junkObject struct {
D2Int64Slice [][]uint64
Uint uint64
Int64Slice []uint64
}
type tree struct {
First []*junkObject
Second []*junkObject
}
func generateJunkObject(size uint64) []*junkObject {
object := make([]*junkObject, size)
for i := uint64(0); i < uint64(len(object)); i++ {
d2Int64Slice := make([][]uint64, size)
is := make([]uint64, size)
uInt := uint64(time.Now().UnixNano())
is[i] = i
d2Int64Slice[i] = make([]uint64, size)
for j := uint64(0); j < uint64(len(object)); j++ {
d2Int64Slice[i][j] = i + j
}
object[i] = &junkObject{
D2Int64Slice: d2Int64Slice,
Uint: uInt,
Int64Slice: is,
}
}
return object
}
func TestCache_byHash(t *testing.T) {
byteSl := [][]byte{{0, 0}, {1, 1}}
useCache = false
mr, err := HashTreeRoot(byteSl)
if err != nil {
t.Fatal(err)
}
hs, err := HashedEncoding(byteSl)
if err != nil {
t.Fatal(err)
}
exists, _, err := hashCache.RootByEncodedHash(hs)
if err != nil {
t.Fatal(err)
}
if exists {
t.Error("Expected block info not to exist in empty cache")
}
useCache = true
if _, err := HashTreeRoot(byteSl); err != nil {
t.Fatal(err)
}
exists, fetchedInfo, err := hashCache.RootByEncodedHash(hs)
if err != nil {
t.Fatal(err)
}
if !exists {
t.Error("Expected blockInfo to exist")
}
if !bytes.Equal(mr[:], fetchedInfo.MerkleRoot) {
t.Errorf(
"Expected fetched info number to be %v, got %v",
mr,
fetchedInfo.MerkleRoot,
)
}
if fetchedInfo.Hash != hs {
t.Errorf(
"Expected fetched info hash to be %v, got %v",
hs,
fetchedInfo.Hash,
)
}
}
func BenchmarkHashWithoutCache(b *testing.B) {
useCache = false
First := generateJunkObject(100)
HashTreeRoot(&tree{First: First, Second: First})
for n := 0; n < b.N; n++ {
HashTreeRoot(&tree{First: First, Second: First})
}
}
func BenchmarkHashWithCache(b *testing.B) {
useCache = true
First := generateJunkObject(100)
type tree struct {
First []*junkObject
Second []*junkObject
}
HashTreeRoot(&tree{First: First, Second: First})
b.ResetTimer()
for n := 0; n < b.N; n++ {
HashTreeRoot(&tree{First: First, Second: First})
}
}
func TestBlockCache_maxSize(t *testing.T) {
maxCacheSize := int64(10000)
cache := newHashCache(maxCacheSize)
for i := uint64(0); i < uint64(maxCacheSize+1025); i++ {
rootNum := make([]byte, 8)
binary.LittleEndian.PutUint64(rootNum, i)
if err := cache.AddRoot(toBytes32(rootNum[:4]), []byte{1}); err != nil {
t.Fatal(err)
}
}
log.Printf(
"hash cache key size is %d, itemcount is %d",
maxCacheSize,
cache.hashCache.ItemCount(),
)
if int64(cache.hashCache.ItemCount()) > maxCacheSize {
t.Errorf(
"Expected hash cache key size to be %d, got %d",
maxCacheSize,
cache.hashCache.ItemCount(),
)
}
}