forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeacon_state_bellatrix_test.go
144 lines (104 loc) · 4.66 KB
/
beacon_state_bellatrix_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
134
135
136
137
138
139
140
141
142
143
144
package spectests
import (
"encoding/hex"
"os"
"testing"
ssz "github.com/ferranbt/fastssz"
"github.com/stretchr/testify/require"
)
const TestFileName = "fixtures/beacon_state_bellatrix.ssz" // https://goerli.beaconcha.in/slot/4744352
func TestBeaconHeader_SingleProof(t *testing.T) {
data, err := os.ReadFile(TestFileName)
require.NoError(t, err)
sszState := BeaconStateBellatrix{}
err = sszState.UnmarshalSSZ(data)
require.NoError(t, err)
object := sszState.LatestBlockHeader
objectTree, err := object.GetTree()
require.NoError(t, err)
proofAtIndex := 8
proof, err := objectTree.Prove(proofAtIndex)
require.NoError(t, err)
require.Equal(t, hex.EncodeToString(proof.Leaf), "a064480000000000000000000000000000000000000000000000000000000000")
require.Equal(t, proofAtIndex, proof.Index)
require.Equal(t, 3, len(proof.Hashes), "proof hashes length incorrect")
require.Equal(t, hex.EncodeToString(proof.Hashes[0]), "7859010000000000000000000000000000000000000000000000000000000000")
require.Equal(t, hex.EncodeToString(proof.Hashes[1]), "007c0d1e0260fb9a6fa86a39569aaebc9a95aaab0180f2865da2fc25180e2242")
require.Equal(t, hex.EncodeToString(proof.Hashes[2]), "98a517b0aa099cdfd06cdcfe71869417a00f168e8bec03ee3fa2135af2396bb6")
}
func TestBeaconHeader_MultiProof(t *testing.T) {
data, err := os.ReadFile(TestFileName)
require.NoError(t, err)
sszState := BeaconStateBellatrix{}
err = sszState.UnmarshalSSZ(data)
require.NoError(t, err)
object := sszState.LatestBlockHeader
objectTree, err := object.GetTree()
require.NoError(t, err)
proofAtIndices := []int{8, 9, 13}
multiProof, err := objectTree.ProveMulti(proofAtIndices)
require.NoError(t, err)
require.Equal(t, 3, len(multiProof.Leaves), "multi proof leaf hashes length incorrect")
require.Equal(t, hex.EncodeToString(multiProof.Leaves[0]), "a064480000000000000000000000000000000000000000000000000000000000")
require.Equal(t, hex.EncodeToString(multiProof.Leaves[1]), "7859010000000000000000000000000000000000000000000000000000000000")
require.Equal(t, hex.EncodeToString(multiProof.Leaves[2]), "0000000000000000000000000000000000000000000000000000000000000000")
require.Equal(t, proofAtIndices, multiProof.Indices)
require.Equal(t, 3, len(multiProof.Hashes), "proof hashes length incorrect")
require.Equal(t, hex.EncodeToString(multiProof.Hashes[0]), "445fab586d7d52993d7713c29da316d7e0fe04fd053983198af93fb131ce02ed")
require.Equal(t, hex.EncodeToString(multiProof.Hashes[1]), "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b")
require.Equal(t, hex.EncodeToString(multiProof.Hashes[2]), "007c0d1e0260fb9a6fa86a39569aaebc9a95aaab0180f2865da2fc25180e2242")
}
func TestBeaconState_BlockRootAtIndexProof(t *testing.T) {
data, err := os.ReadFile(TestFileName)
require.NoError(t, err)
sszState := BeaconStateBellatrix{}
err = sszState.UnmarshalSSZ(data)
require.NoError(t, err)
// index of first block_root field in the beacon state
leavesStart := 303104
// let's prove block roof at position 4 in the block roots array
index := leavesStart + 3
expectedLeaf := sszState.BlockRoots[3]
tree, err := sszState.GetTree()
require.NoError(t, err)
proof, err := tree.Prove(index)
require.NoError(t, err)
// check that the block root hash at the index matches what is in the beacon state
require.Equal(t, expectedLeaf, proof.Leaf)
root, err := sszState.HashTreeRoot()
require.NoError(t, err)
ok, err := ssz.VerifyProof(root[:], proof)
require.NoError(t, err, "failed to verify proof")
require.True(t, ok, "incorrect proof verification")
}
func TestBeaconState_BlockRootsProof(t *testing.T) {
data, err := os.ReadFile(TestFileName)
require.NoError(t, err)
sszState := BeaconStateBellatrix{}
err = sszState.UnmarshalSSZ(data)
require.NoError(t, err)
index := 37
tree, err := sszState.GetTree()
require.NoError(t, err)
root, err := sszState.HashTreeRoot()
require.NoError(t, err)
// This is required to set the node values as the tree is hashed. Ideally should be done as part of GetTree() or Prove()
tree.Hash()
proof, err := tree.Prove(index)
require.NoError(t, err)
ok, err := ssz.VerifyProof(root[:], proof)
require.NoError(t, err, "failed to verify proof")
require.True(t, ok, "incorrect proof verification")
}
func TestBeaconStateTree_HashTreeRoot(t *testing.T) {
data, err := os.ReadFile(TestFileName)
require.NoError(t, err)
sszState := BeaconStateBellatrix{}
err = sszState.UnmarshalSSZ(data)
require.NoError(t, err)
tree, err := sszState.GetTree()
require.NoError(t, err)
hash := tree.Hash()
// taken from https://goerli.beaconcha.in/slot/4744352 - stateRoot field
require.Equal(t, "c4a9c5ebf637c089db599574b568bb679b385c1984f08410707db08e03d7ae52", hex.EncodeToString(hash))
}