This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtxs_batches_test.go
620 lines (492 loc) · 20 KB
/
txs_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package commander
import (
"context"
"testing"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/encoder"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/models/enums/result"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/testutils"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type TxsBatchesTestSuite struct {
*require.Assertions
suite.Suite
cmd *Commander
client *eth.TestClient
storage *st.TestStorage
txsCtx *executor.TxsContext
cfg *config.Config
wallets []bls.Wallet
}
func (s *TxsBatchesTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
s.cfg = config.GetTestConfig()
s.cfg.Rollup.MinTxsPerCommitment = 1
s.cfg.Rollup.MaxTxsPerCommitment = 32
s.cfg.Rollup.DisableSignatures = false
}
func (s *TxsBatchesTestSuite) SetupTest() {
var err error
s.storage, err = st.NewTestStorage()
s.NoError(err)
s.client = newClientWithGenesisState(s.T(), s.storage)
s.cmd = NewCommander(s.cfg, s.client.Blockchain)
s.cmd.client = s.client.Client
s.cmd.storage = s.storage.Storage
executionCtx := executor.NewTestExecutionContext(s.storage.Storage, s.client.Client, s.cfg.Rollup)
s.txsCtx, err = executor.NewTestTxsContext(executionCtx, batchtype.Transfer)
s.NoError(err)
err = s.cmd.addGenesisBatch()
s.NoError(err)
domain, err := s.client.GetDomain()
s.NoError(err)
s.wallets = testutils.GenerateWallets(s.Assertions, domain, 2)
setAccountLeaves(s.T(), s.storage.Storage, s.wallets)
}
func (s *TxsBatchesTestSuite) TearDownTest() {
stopCommander(s.cmd)
s.client.Close()
err := s.storage.Teardown()
s.NoError(err)
}
func (s *TxsBatchesTestSuite) TestUnsafeSyncBatches_DoesNotSyncExistingBatchTwice() {
tx := testutils.MakeTransfer(0, 1, 0, 400)
signTransfer(s.T(), &s.wallets[tx.FromStateID], &tx)
s.submitBatchInTx(&tx)
s.syncAllBlocks()
batches, err := s.cmd.storage.GetBatchesInRange(nil, nil)
s.NoError(err)
s.Len(batches, 2)
tx2 := testutils.MakeTransfer(1, 0, 0, 100)
signTransfer(s.T(), &s.wallets[tx2.FromStateID], &tx2)
s.submitBatchInTx(&tx2)
s.syncAllBlocks()
batches, err = s.cmd.storage.GetBatchesInRange(nil, nil)
s.NoError(err)
s.Len(batches, 3)
state0, err := s.cmd.storage.StateTree.Leaf(0)
s.NoError(err)
s.Equal(models.MakeUint256(710), state0.Balance)
state1, err := s.cmd.storage.StateTree.Leaf(1)
s.NoError(err)
s.Equal(models.MakeUint256(290), state1.Balance)
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_ReplaceLocalBatchWithRemoteOne() {
initialStateRoot, err := s.cmd.storage.StateTree.Root()
s.NoError(err)
remoteTx := testutils.MakeTransfer(0, 1, 0, 100)
s.setTransferHashAndSign(&remoteTx)
s.submitBatchInTx(&remoteTx)
localTx := testutils.MakeTransfer(0, 1, 0, 200)
s.setTransferHashAndSign(&localTx)
s.createTransferBatchLocally(&localTx)
batches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(batches, 1)
remoteBatch := batches[0]
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatch)
s.NoError(err)
// Correct batch stored
expectedBatch := remoteBatch.ToBatch(*initialStateRoot)
storedBatch, err := s.cmd.storage.GetBatch(remoteBatch.GetID())
s.NoError(err)
s.Equal(*expectedBatch, *storedBatch)
// Correct commitment stored
remoteTxBatch := remoteBatch.ToDecodedTxBatch()
remoteCommitment := remoteTxBatch.Commitments[0].ToDecodedCommitment()
expectedCommitment := models.TxCommitment{
CommitmentBase: models.CommitmentBase{
ID: models.CommitmentID{
BatchID: remoteTxBatch.ID,
IndexInBatch: 0,
},
Type: batchtype.Transfer,
PostStateRoot: remoteCommitment.StateRoot,
},
FeeReceiver: remoteCommitment.FeeReceiver,
CombinedSignature: remoteCommitment.CombinedSignature,
BodyHash: remoteTxBatch.Commitments[0].BodyHash(remoteTxBatch.AccountTreeRoot),
}
storedCommitment, err := s.cmd.storage.GetCommitment(&expectedCommitment.ID)
s.NoError(err)
s.Equal(expectedCommitment, *storedCommitment.ToTxCommitment())
// Correct tx stored
expectedTx := remoteTx
expectedTx.Signature = models.Signature{}
expectedTx.CommitmentSlot = models.NewCommitmentSlot(expectedCommitment.ID, 0)
transfer, err := s.cmd.storage.GetTransfer(remoteTx.Hash)
s.NoError(err)
s.Equal(expectedTx, *transfer)
// Previously stored tx moved back to mempool
pendingTransfers, err := s.cmd.storage.GetPendingTransactions(txtype.Transfer)
s.NoError(err)
s.Len(pendingTransfers, 1)
s.Equal(localTx, *pendingTransfers.ToTransferArray().At(0).ToTransfer())
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesBatchWithTooManyTxs() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.Transactions = append(commitment.Transactions, make([]byte, 32*encoder.TransferLength)...)
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.TooManyTx, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesBatchWithInvalidPostStateRoot() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.PostStateRoot = utils.RandomHash()
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.Ok, getDisputeResult(s.Assertions, s.client)) // invalid post state root emits Result.Ok
}
// This test checks that state witnesses needed for dispute tx are gathered correctly in case of a self transfer
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesFraudulentBatchWithSelfTransfer() {
tx := testutils.MakeTransfer(0, 0, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.PostStateRoot = common.Hash{1, 2, 3, 4}
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
// post state root is checked after applying the transfer in the SC dispute method, so we know that the witnesses were correct
s.Equal(result.Ok, getDisputeResult(s.Assertions, s.client)) // invalid post state root emits Result.Ok
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesCommitmentWithInvalidSignature() {
s.registerAccounts([]uint32{0, 1})
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.CombinedSignature[0] = 0 // break the signature
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.BadSignature, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesCommitmentWithSignatureInBadFormat() {
s.registerAccounts([]uint32{0, 1})
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.CombinedSignature = models.Signature{1, 2, 3}
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.BadPrecompileCall, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_RemovesExistingBatchAndDisputesFraudulentOne() {
remoteTx := testutils.MakeTransfer(0, 1, 0, 250)
s.submitInvalidBatchInTx(&remoteTx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.Transactions = append(commitment.Transactions, 1, 2, 3, 4)
})
localTx := testutils.MakeTransfer(0, 1, 0, 100)
localBatch := s.createTransferBatchLocally(&localTx)
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
remoteBatch := remoteBatches[0]
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatch)
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatch.GetID())
s.Equal(result.BadCompression, getDisputeResult(s.Assertions, s.client))
_, err = s.cmd.storage.GetBatch(localBatch.ID)
s.True(st.IsNotFoundError(err))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesFraudulentCommitmentAfterGenesisOne() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.Transactions = append(commitment.Transactions, 1, 2, 3, 4)
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.BadCompression, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesCommitmentWithInvalidFeeReceiverTokenID() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.FeeReceiver = 2
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.BadToTokenID, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesCommitmentWithoutTransfersAndInvalidPostStateRoot() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.Transactions = []byte{}
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.Ok, getDisputeResult(s.Assertions, s.client)) // invalid post state root emits Result.Ok
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesCommitmentWithNonexistentSender() {
tx := testutils.MakeTransfer(0, 1, 0, 100)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
tx.FromStateID = 10
encodedTx, err := encoder.EncodeTransferForCommitment(&tx)
s.NoError(err)
commitment.Transactions = encodedTx
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.NotEnoughTokenBalance, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesC2TWithNonRegisteredReceiverPublicKey() {
// Change batch type used in TxsContext
var err error
executionCtx := executor.NewTestExecutionContext(s.storage.Storage, s.client.Client, s.cfg.Rollup)
s.txsCtx, err = executor.NewTestTxsContext(executionCtx, batchtype.Create2Transfer)
s.NoError(err)
// Register public keys added to the account tree for signature disputes to work
s.registerAccounts([]uint32{0, 1})
tx := testutils.MakeCreate2Transfer(0, nil, 0, 100, s.wallets[0].PublicKey())
s.submitInvalidBatchInTx(&tx, func(storage *st.Storage, commitment *models.TxCommitmentWithTxs) {
// Fix post state root
_, err = storage.StateTree.Set(3, &models.UserState{
PubKeyID: 1234,
TokenID: models.MakeUint256(0),
Balance: tx.Amount,
Nonce: models.MakeUint256(0),
})
s.NoError(err)
var root *common.Hash
root, err = storage.StateTree.Root()
s.NoError(err)
commitment.PostStateRoot = *root
// Replace toStateID and toPubKeyID in C2T
tx.ToStateID = ref.Uint32(3)
var encodedTx []byte
encodedTx, err = encoder.EncodeCreate2TransferForCommitment(&tx, 1234)
s.NoError(err)
commitment.Transactions = encodedTx
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.NonexistentReceiver, getDisputeResult(s.Assertions, s.client))
}
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_DisputesBatchWithInvalidTokenAmount() {
tx := testutils.MakeTransfer(0, 1, 0, 10)
s.submitInvalidBatchInTx(&tx, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
tx.Amount = models.MakeUint256(0)
encodedTx, err := encoder.EncodeTransferForCommitment(&tx)
s.NoError(err)
commitment.Transactions = encodedTx
})
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.ErrorIs(err, ErrRollbackInProgress)
checkBatchAfterDispute(s.Assertions, s.cmd, remoteBatches[0].GetID())
s.Equal(result.InvalidTokenAmount, getDisputeResult(s.Assertions, s.client))
}
// TODO: what is this test testing? It claims to confirm we can send to
// nonexistent receivers but why would that be okay? And we call a method
// which creates the receiver(!) before doing anything.
func (s *TxsBatchesTestSuite) TestSyncRemoteBatch_AllowsTransferToNonexistentReceiver() {
tx := s.submitTransferToNonexistentReceiver()
s.syncAllBlocks()
expectedReceiverState := models.UserState{
PubKeyID: 0,
TokenID: models.MakeUint256(0),
Balance: tx.Amount,
Nonce: models.MakeUint256(0),
}
leaf, err := s.storage.StateTree.Leaf(tx.ToStateID)
s.NoError(err)
s.Equal(expectedReceiverState, leaf.UserState)
}
func (s *TxsBatchesTestSuite) submitTransferToNonexistentReceiver() models.Transfer {
tx := testutils.MakeTransfer(0, 3, 0, 100)
s.setTransferHashAndSign(&tx)
txController, txStorage, txsCtx := s.beginTransaction()
defer txController.Rollback(nil)
// Temporary set receiver StateLeaf for successful batch submission
_, err := txStorage.StateTree.Set(tx.ToStateID, &models.UserState{
PubKeyID: 0,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(0),
Nonce: models.MakeUint256(0),
})
s.NoError(err)
s.submitBatch(txStorage, txsCtx, &tx)
return tx
}
func (s *TxsBatchesTestSuite) TestUnsafeSyncBatches_SyncsBatchesBeforeInvalidOne() {
txs := []models.Transfer{
testutils.MakeTransfer(0, 1, 0, 250),
testutils.MakeTransfer(0, 1, 1, 100),
}
s.setTransferHashAndSign(&txs[0])
txController, txStorage, txsCtx := s.beginTransaction()
defer txController.Rollback(nil)
s.submitBatch(txStorage, txsCtx, &txs[0])
invalidBatch := s.submitBatch(txStorage, txsCtx, &txs[1])
s.cmd.invalidBatchID = &invalidBatch.ID
s.syncAllBlocks()
batches, err := s.cmd.storage.GetBatchesInRange(nil, nil)
s.NoError(err)
s.Len(batches, 2)
s.EqualValues(1, batches[1].ID.Uint64())
}
func (s *TxsBatchesTestSuite) syncAllBlocks() {
latestBlockNumber, err := s.client.GetLatestBlockNumber()
s.NoError(err)
err = s.cmd.unsafeSyncBatches(context.Background(), 0, *latestBlockNumber)
s.NoError(err)
}
func (s *TxsBatchesTestSuite) createTransferBatchLocally(tx *models.Transfer) *models.Batch {
err := s.cmd.storage.AddTransaction(tx)
s.NoError(err)
err = s.cmd.storage.AddMempoolTx(tx)
s.NoError(err)
pendingBatch, err := s.txsCtx.NewPendingBatch(batchtype.Transfer)
s.NoError(err)
commitments, err := s.txsCtx.CreateCommitments(context.Background())
s.NoError(err)
s.Len(commitments, 1)
err = s.cmd.storage.AddCommitment(commitments[0].ToCommitment())
s.NoError(err)
pendingBatch.TransactionHash = utils.RandomHash()
err = s.cmd.storage.AddBatch(pendingBatch)
s.NoError(err)
return pendingBatch
}
func (s *TxsBatchesTestSuite) submitBatch(
storage *st.Storage,
txsCtx *executor.TxsContext,
tx models.GenericTransaction,
) *models.Batch {
pendingBatch := submitInvalidTxsBatch(s.Assertions, storage, txsCtx, tx,
func(_ *st.Storage, _ *models.TxCommitmentWithTxs) {},
)
s.client.GetBackend().Commit()
return pendingBatch
}
// Submits batch on chain without adding it to storage
func (s *TxsBatchesTestSuite) submitBatchInTx(tx models.GenericTransaction) {
s.submitInvalidBatchInTx(tx, func(_ *st.Storage, _ *models.TxCommitmentWithTxs) {})
}
// Submits invalid batch on chain without adding it to storage
func (s *TxsBatchesTestSuite) submitInvalidBatchInTx(
tx models.GenericTransaction,
modifier func(storage *st.Storage, commitment *models.TxCommitmentWithTxs),
) {
txController, txStorage, txsCtx := s.beginTransaction()
defer txController.Rollback(nil)
submitInvalidTxsBatch(s.Assertions, txStorage, txsCtx, tx, modifier)
s.client.GetBackend().Commit()
}
func (s *TxsBatchesTestSuite) beginTransaction() (*db.TxController, *st.Storage, *executor.TxsContext) {
txController, txStorage := s.storage.BeginTransaction(st.TxOptions{})
executionCtx := executor.NewTestExecutionContext(txStorage, s.client.Client, s.cfg.Rollup)
txsCtx, err := executor.NewTestTxsContext(executionCtx, s.txsCtx.BatchType)
s.NoError(err)
return txController, txStorage, txsCtx
}
func submitInvalidTxsBatch(
s *require.Assertions,
storage *st.Storage,
txsCtx *executor.TxsContext,
tx models.GenericTransaction,
modifier func(storage *st.Storage, commitment *models.TxCommitmentWithTxs),
) *models.Batch {
err := storage.AddMempoolTx(tx)
s.NoError(err)
pendingBatch, err := txsCtx.NewPendingBatch(txsCtx.BatchType)
s.NoError(err)
commitments, err := txsCtx.CreateCommitments(context.Background())
s.NoError(err)
s.Len(commitments, 1)
modifier(storage, commitments[0].ToTxCommitmentWithTxs())
err = txsCtx.SubmitBatch(context.Background(), pendingBatch, commitments)
s.NoError(err)
return pendingBatch
}
func (s *TxsBatchesTestSuite) setTransferHashAndSign(txs ...*models.Transfer) {
for i := range txs {
signTransfer(s.T(), &s.wallets[txs[i].FromStateID], txs[i])
hash, err := encoder.HashTransfer(txs[i])
s.NoError(err)
txs[i].Hash = *hash
}
}
func checkBatchAfterDispute(s *require.Assertions, cmd *Commander, batchID models.Uint256) {
_, err := cmd.client.GetContractBatch(&batchID)
s.Error(err)
s.Equal(eth.MsgInvalidBatchID, err.Error())
batch, err := cmd.storage.GetBatch(batchID)
s.Nil(batch)
s.True(st.IsNotFoundError(err))
}
func getDisputeResult(s *require.Assertions, client *eth.TestClient) result.DisputeResult {
it, err := client.Rollup.FilterRollbackTriggered(nil)
s.NoError(err)
it.Next()
s.NoError(it.Error())
res := result.DisputeResult(it.Event.Result)
s.False(it.Next())
return res
}
func (s *TxsBatchesTestSuite) registerAccounts(pubKeyIDs []uint32) {
for i := range pubKeyIDs {
leaf, err := s.storage.AccountTree.Leaf(pubKeyIDs[i])
s.NoError(err)
pubKeyID, err := s.client.RegisterAccountAndWait(&leaf.PublicKey)
s.NoError(err)
s.Equal(pubKeyIDs[i], *pubKeyID)
}
}
func TestTxsBatchesTestSuite(t *testing.T) {
suite.Run(t, new(TxsBatchesTestSuite))
}