Repository navigation
Expand file tree
/
Copy pathcanonical_state_test.go
More file actions
179 lines (168 loc) · 5.46 KB
/
Copy pathcanonical_state_test.go
File metadata and controls
179 lines (168 loc) · 5.46 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package ethmonitor
import (
"encoding/json"
"fmt"
"math/big"
"testing"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)
func canonicalTestBlock(num int64) *Block {
return &Block{
Block: types.NewBlockWithHeader(&types.Header{
Number: big.NewInt(num),
BlockHash: common.BigToHash(big.NewInt(num)),
ParentHash: common.BigToHash(big.NewInt(num - 1)),
Time: uint64(num),
}),
Event: Added,
OK: true,
}
}
func pushCanonicalTestBlock(t *testing.T, chain *Chain, input *Block) *Block {
t.Helper()
chain.push(input)
block := chain.Head()
require.NotNil(t, block)
require.Equal(t, input.Hash(), block.Hash())
return block
}
// The interface keeps the before-fix proof executable without the new method.
func canonicalTestState(t *testing.T, block *Block) (uint64, bool) {
t.Helper()
state, ok := any(block).(interface{ CanonicalState() (uint64, bool) })
if !ok {
return 0, false
}
return state.CanonicalState()
}
func TestBlockCanonicalStateRetention(t *testing.T) {
for _, depth := range []int64{1, 3} {
t.Run(fmt.Sprintf("evictionDepth=%d", depth), func(t *testing.T) {
chain := newChain(10, false)
block := pushCanonicalTestBlock(t, chain, canonicalTestBlock(100))
copy := chain.Blocks().Copy()[0]
incarnation, canonical := canonicalTestState(t, block)
require.Positive(t, incarnation)
require.True(t, canonical)
for num := int64(101); num <= 109+depth; num++ {
pushCanonicalTestBlock(t, chain, canonicalTestBlock(num))
}
require.Nil(t, chain.GetBlock(block.Hash()))
for _, snapshot := range []*Block{block, copy} {
got, canonical := canonicalTestState(t, snapshot)
require.Equal(t, incarnation, got)
require.True(t, canonical, "retention eviction was mistaken for removal")
}
})
}
}
func TestBlockCanonicalStateReadoption(t *testing.T) {
chain := newChain(10, false)
input := canonicalTestBlock(100)
block := pushCanonicalTestBlock(t, chain, input)
shallow := *block
copy := chain.Blocks().Copy()[0]
incarnation, canonical := canonicalTestState(t, block)
require.Positive(t, incarnation)
require.True(t, canonical)
removed := *chain.pop()
removed.Event = Removed
for _, snapshot := range []*Block{block, &shallow, copy, &removed} {
got, canonical := canonicalTestState(t, snapshot)
require.Equal(t, incarnation, got)
require.False(t, canonical, "snapshot did not observe the actual removal")
}
// Reusing either the original input or a removed snapshot must create a fresh
// owned incarnation without mutating old Added/Removed event copies.
for _, reused := range []*Block{input, copy} {
fresh := pushCanonicalTestBlock(t, chain, reused)
got, canonical := canonicalTestState(t, fresh)
require.Greater(t, got, incarnation)
require.True(t, canonical)
for _, snapshot := range []*Block{block, &shallow, copy, &removed} {
got, canonical := canonicalTestState(t, snapshot)
require.Equal(t, incarnation, got)
require.False(t, canonical, "fresh readoption revived an old event")
}
chain.pop()
}
}
func TestBlockCanonicalStateBootstrap(t *testing.T) {
for _, count := range []int{1, 3} {
for _, serialized := range []bool{false, true} {
t.Run(fmt.Sprintf("blocks=%d/JSON=%v", count, serialized), func(t *testing.T) {
inputs := make(Blocks, count)
for i := range inputs {
inputs[i] = canonicalTestBlock(int64(100 + i))
}
chain := newChain(10, true)
if serialized {
data, err := json.Marshal(inputs)
require.NoError(t, err)
require.NoError(t, chain.BootstrapFromBlocksJSON(data))
} else {
require.NoError(t, chain.BootstrapFromBlocks(inputs))
}
for _, block := range chain.Blocks().Copy() {
incarnation, canonical := canonicalTestState(t, block)
require.Positive(t, incarnation)
require.True(t, canonical)
data, err := json.Marshal(block)
require.NoError(t, err)
require.NotContains(t, string(data), "incarnation")
require.NoError(t, json.Unmarshal(data, block))
incarnation, canonical = canonicalTestState(t, block)
require.Zero(t, incarnation, "serialized state preserved runtime ownership")
require.False(t, canonical)
}
})
}
}
}
func TestBlockCanonicalStateConcurrentRemoval(t *testing.T) {
chain := newChain(10, false)
input := canonicalTestBlock(100)
block := pushCanonicalTestBlock(t, chain, input)
incarnation, _ := canonicalTestState(t, block)
require.Positive(t, incarnation)
state := any(block).(interface{ CanonicalState() (uint64, bool) })
start, done := make(chan struct{}), make(chan struct{})
result := make(chan error, 1)
go func() {
close(start)
removed := false
for {
got, canonical := state.CanonicalState()
if got != incarnation || (removed && canonical) {
result <- fmt.Errorf("old incarnation changed or revived: id=%d canonical=%v", got, canonical)
return
}
removed = removed || !canonical
select {
case <-done:
result <- nil
return
default:
}
}
}()
<-start
for i := 0; i < 100; i++ {
chain.pop()
pushCanonicalTestBlock(t, chain, input)
}
close(done)
require.NoError(t, <-result)
got, canonical := state.CanonicalState()
require.Equal(t, incarnation, got)
require.False(t, canonical)
}
func TestBlockCanonicalStateUntracked(t *testing.T) {
for _, block := range []*Block{nil, canonicalTestBlock(100)} {
incarnation, canonical := canonicalTestState(t, block)
require.Zero(t, incarnation)
require.False(t, canonical)
}
}