forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unwind_test.go
191 lines (132 loc) · 4.33 KB
/
unwind_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
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
180
181
182
183
184
185
186
187
188
189
190
191
package stagedsync
import (
"testing"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/stretchr/testify/assert"
)
func TestUnwindStackLoadFromDb(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
stack2 := NewPersistentUnwindStack()
for i := range stages {
err := stack2.AddFromDB(db, stages[i])
assert.NoError(t, err)
}
assert.Equal(t, stack.unwindStack, stack2.unwindStack)
assert.Equal(t, len(stages), len(stack2.unwindStack))
}
func TestUnwindStackLoadFromDbAfterDone(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
u := stack.Pop()
assert.NotNil(t, u)
err := u.Done(db)
assert.NoError(t, err)
stack2 := NewPersistentUnwindStack()
for i := range stages {
err := stack2.AddFromDB(db, stages[i])
assert.NoError(t, err)
}
assert.Equal(t, stack.unwindStack, stack2.unwindStack)
assert.Equal(t, len(stages)-1, len(stack2.unwindStack))
}
func TestUnwindStackLoadFromDbNoDone(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
u := stack.Pop()
assert.NotNil(t, u)
stack2 := NewPersistentUnwindStack()
for i := range stages {
err := stack2.AddFromDB(db, stages[i])
assert.NoError(t, err)
}
assert.NotEqual(t, stack.unwindStack, stack2.unwindStack)
assert.Equal(t, len(stages), len(stack2.unwindStack))
}
func TestUnwindStackPopAndEmpty(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
assert.False(t, stack.Empty())
u := stack.Pop()
assert.NotNil(t, u)
assert.False(t, stack.Empty())
u = stack.Pop()
assert.NotNil(t, u)
assert.False(t, stack.Empty())
u = stack.Pop()
assert.NotNil(t, u)
assert.True(t, stack.Empty())
u = stack.Pop()
assert.Nil(t, u)
assert.True(t, stack.Empty())
}
func TestUnwindOverrideWithLower(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
assert.Equal(t, 3, len(stack.unwindStack))
err := stack.Add(UnwindState{stages[0], 5}, db)
assert.NoError(t, err)
// we append if the next unwind is to the lower block
assert.Equal(t, 4, len(stack.unwindStack))
}
func TestUnwindOverrideWithHigher(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
assert.Equal(t, 3, len(stack.unwindStack))
err := stack.Add(UnwindState{stages[0], 105}, db)
assert.NoError(t, err)
// we ignore if next unwind is to the higher block
assert.Equal(t, 3, len(stack.unwindStack))
}
func TestUnwindOverrideWithTheSame(t *testing.T) {
db := ethdb.NewMemDatabase()
stack := NewPersistentUnwindStack()
stages := []stages.SyncStage{stages.Bodies, stages.Headers, stages.Execution}
points := []uint64{10, 20, 30}
for i := range stages {
err := stack.Add(UnwindState{stages[i], points[i]}, db)
assert.NoError(t, err)
}
assert.Equal(t, 3, len(stack.unwindStack))
err := stack.Add(UnwindState{stages[0], 10}, db)
assert.NoError(t, err)
// we ignore if next unwind is to the higher block
assert.Equal(t, 3, len(stack.unwindStack))
}