forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatches_test.go
275 lines (233 loc) · 7.19 KB
/
batches_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package db
import (
"errors"
"math/big"
"testing"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common"
)
func TestCanStoreAndRetrieveBatchHeader(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
batch := common.ExtBatch{
Header: &header,
}
err := db.AddBatch(&batch)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchHeader, err := db.GetBatchHeader(header.Hash())
if err != nil {
t.Errorf("stored batch but could not retrieve header. Cause: %s", err)
}
if batchHeader.Number.Cmp(header.Number) != 0 {
t.Errorf("batch header was not stored correctly")
}
}
func TestUnknownBatchHeaderReturnsNotFound(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := types.Header{}
_, err := db.GetBatchHeader(header.Hash())
if !errors.Is(err, errutil.ErrNotFound) {
t.Errorf("did not store batch header but was able to retrieve it")
}
}
func TestHigherNumberBatchBecomesBatchHeader(t *testing.T) { //nolint:dupl
db := NewInMemoryDB(nil, nil)
headerOne := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
batchOne := common.ExtBatch{
Header: &headerOne,
}
err := db.AddBatch(&batchOne)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
headerTwo := common.BatchHeader{
// We give the second header a higher number, making it the head.
Number: big.NewInt(0).Add(headerOne.Number, big.NewInt(1)),
}
batchTwo := common.ExtBatch{
Header: &headerTwo,
}
err = db.AddBatch(&batchTwo)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchHeader, err := db.GetHeadBatchHeader()
if err != nil {
t.Errorf("stored batch but could not retrieve header. Cause: %s", err)
}
if batchHeader.Number.Cmp(headerTwo.Number) != 0 {
t.Errorf("head batch was not set correctly")
}
}
func TestLowerNumberBatchDoesNotBecomeBatchHeader(t *testing.T) { //nolint:dupl
db := NewInMemoryDB(nil, nil)
headerOne := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
batchOne := common.ExtBatch{
Header: &headerOne,
}
err := db.AddBatch(&batchOne)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
headerTwo := common.BatchHeader{
// We give the second header a higher number, making it the head.
Number: big.NewInt(0).Sub(headerOne.Number, big.NewInt(1)),
}
batchTwo := common.ExtBatch{
Header: &headerTwo,
}
err = db.AddBatch(&batchTwo)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchHeader, err := db.GetHeadBatchHeader()
if err != nil {
t.Errorf("stored batch but could not retrieve header. Cause: %s", err)
}
if batchHeader.Number.Cmp(headerOne.Number) != 0 {
t.Errorf("head batch was not set correctly")
}
}
func TestHeadBatchHeaderIsNotSetInitially(t *testing.T) {
db := NewInMemoryDB(nil, nil)
_, err := db.GetHeadBatchHeader()
if !errors.Is(err, errutil.ErrNotFound) {
t.Errorf("head batch was set, but no batchs had been written")
}
}
func TestCanRetrieveBatchHashByNumber(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
batch := common.ExtBatch{
Header: &header,
}
err := db.AddBatch(&batch)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchHash, err := db.GetBatchHash(header.Number)
if err != nil {
t.Errorf("stored batch but could not retrieve headers hash by number. Cause: %s", err)
}
if *batchHash != header.Hash() {
t.Errorf("batch hash was not stored correctly against number")
}
}
func TestUnknownBatchNumberReturnsNotFound(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := types.Header{}
_, err := db.GetBatchHash(header.Number)
if !errors.Is(err, errutil.ErrNotFound) {
t.Errorf("did not store batch hash but was able to retrieve it")
}
}
func TestCanRetrieveBatchNumberByTxHash(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
txHash := gethcommon.BytesToHash([]byte("magicString"))
batch := common.ExtBatch{
Header: &header,
TxHashes: []gethcommon.Hash{txHash},
}
err := db.AddBatch(&batch)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchNumber, err := db.GetBatchNumber(txHash)
if err != nil {
t.Errorf("stored batch but could not retrieve headers number by transaction hash. Cause: %s", err)
}
if batchNumber.Cmp(header.Number) != 0 {
t.Errorf("batch number was not stored correctly against transaction hash")
}
}
func TestUnknownBatchTxHashReturnsNotFound(t *testing.T) {
db := NewInMemoryDB(nil, nil)
_, err := db.GetBatchNumber(gethcommon.BytesToHash([]byte("magicString")))
if !errors.Is(err, errutil.ErrNotFound) {
t.Errorf("did not store batch number but was able to retrieve it")
}
}
func TestCanRetrieveBatchTransactions(t *testing.T) {
db := NewInMemoryDB(nil, nil)
header := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
txHashes := []gethcommon.Hash{gethcommon.BytesToHash([]byte("magicStringOne")), gethcommon.BytesToHash([]byte("magicStringTwo"))}
batch := common.ExtBatch{
Header: &header,
TxHashes: txHashes,
}
err := db.AddBatch(&batch)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
batchTxs, err := db.GetBatchTxs(header.Hash())
if err != nil {
t.Errorf("stored batch but could not retrieve headers transactions. Cause: %s", err)
}
if len(batchTxs) != len(txHashes) {
t.Errorf("batch transactions were not stored correctly")
}
for idx, batchTx := range batchTxs {
if batchTx != txHashes[idx] {
t.Errorf("batch transactions were not stored correctly")
}
}
}
func TestTransactionsForUnknownBatchReturnsNotFound(t *testing.T) {
db := NewInMemoryDB(nil, nil)
_, err := db.GetBatchNumber(gethcommon.BytesToHash([]byte("magicString")))
if !errors.Is(err, errutil.ErrNotFound) {
t.Errorf("did not store batch number but was able to retrieve it")
}
}
func TestCanRetrieveTotalNumberOfTransactions(t *testing.T) {
db := NewInMemoryDB(nil, nil)
headerOne := common.BatchHeader{
Number: big.NewInt(batchNumber),
}
txHashesOne := []gethcommon.Hash{gethcommon.BytesToHash([]byte("magicStringOne")), gethcommon.BytesToHash([]byte("magicStringTwo"))}
batchOne := common.ExtBatch{
Header: &headerOne,
TxHashes: txHashesOne,
}
err := db.AddBatch(&batchOne)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
headerTwo := common.BatchHeader{
Number: big.NewInt(batchNumber + 1),
}
txHashesTwo := []gethcommon.Hash{gethcommon.BytesToHash([]byte("magicStringThree")), gethcommon.BytesToHash([]byte("magicStringFour"))}
batchTwo := common.ExtBatch{
Header: &headerTwo,
TxHashes: txHashesTwo,
}
err = db.AddBatch(&batchTwo)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
totalTxs, err := db.GetTotalTransactions()
if err != nil {
t.Errorf("was not able to read total number of transactions. Cause: %s", err)
}
if int(totalTxs.Int64()) != len(txHashesOne)+len(txHashesTwo) {
t.Errorf("total number of batch transactions was not stored correctly")
}
}
// todo (#718) - add tests of writing and reading extbatches.