This repository was 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 pathdeposit_batches_test.go
210 lines (167 loc) · 5.94 KB
/
deposit_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
package commander
import (
"context"
"testing"
"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/commander/syncer"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/contracts/erc20"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/eth/deployer/rollup"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"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/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type DepositBatchesTestSuite struct {
*require.Assertions
suite.Suite
cmd *Commander
client *eth.TestClient
storage *st.TestStorage
depositSubtree models.PendingDepositSubtree
cfg *config.Config
}
func (s *DepositBatchesTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
s.cfg = config.GetTestConfig()
s.cfg.Rollup.MinTxsPerCommitment = 1
}
func (s *DepositBatchesTestSuite) 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
s.NoError(err)
err = s.cmd.addGenesisBatch()
s.NoError(err)
s.depositSubtree = models.PendingDepositSubtree{
ID: models.MakeUint256(1),
Root: utils.RandomHash(),
Deposits: testutils.GetFourDeposits(),
}
}
func newClientWithGenesisState(t *testing.T, storage *st.TestStorage) *eth.TestClient {
setStateLeaves(t, storage.Storage)
genesisRoot, err := storage.StateTree.Root()
require.NoError(t, err)
client, err := eth.NewConfiguredTestClient(&rollup.DeploymentConfig{
Params: rollup.Params{
GenesisStateRoot: genesisRoot,
},
}, ð.TestClientConfig{})
require.NoError(t, err)
return client
}
func (s *DepositBatchesTestSuite) TearDownTest() {
stopCommander(s.cmd)
s.client.Close()
err := s.storage.Teardown()
s.NoError(err)
}
func (s *DepositBatchesTestSuite) TestSyncRemoteBatch_SyncsDepositBatch() {
s.prepareDeposits()
s.submitDepositBatch(s.storage.Storage)
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
err = s.cmd.syncRemoteBatch(context.Background(), remoteBatches[0])
s.NoError(err)
batches, err := s.storage.GetBatchesInRange(nil, nil)
s.NoError(err)
s.Len(batches, 2)
depositBatch := remoteBatches[0].ToDecodedDepositBatch()
s.Equal(depositBatch.Hash, *batches[1].Hash)
s.Equal(depositBatch.Type, batches[1].Type)
}
func (s *DepositBatchesTestSuite) TestUnsafeSyncBatches_OmitsRolledBackBatch() {
s.prepareDeposits()
s.submitInvalidBatches()
latestBlock, err := s.client.GetLatestBlockNumber()
s.NoError(err)
// trigger dispute on fraudulent batch
err = s.cmd.unsafeSyncBatches(context.Background(), 0, *latestBlock)
s.ErrorIs(err, ErrRollbackInProgress)
depositBatch := s.submitDepositBatch(s.storage.Storage)
latestBlock, err = s.client.GetLatestBlockNumber()
s.NoError(err)
// try syncing already rolled back batch
err = s.cmd.unsafeSyncBatches(context.Background(), 0, *latestBlock)
s.NoError(err)
batches, err := s.storage.GetBatchesInRange(nil, nil)
s.NoError(err)
s.Len(batches, 2)
s.Equal(depositBatch.TransactionHash, batches[1].TransactionHash)
}
func (s *DepositBatchesTestSuite) submitInvalidBatches() {
previousRoot, err := s.storage.StateTree.Root()
s.NoError(err)
txController, txStorage := s.storage.BeginTransaction(st.TxOptions{})
defer txController.Rollback(nil)
executionCtx := executor.NewTestExecutionContext(txStorage, s.client.Client, s.cfg.Rollup)
txsCtx, err := executor.NewTestTxsContext(executionCtx, batchtype.Transfer)
s.NoError(err)
invalidTransfer := testutils.MakeTransfer(0, 1, 0, 100)
submitInvalidTxsBatch(s.Assertions, txStorage, txsCtx, &invalidTransfer, func(_ *st.Storage, commitment *models.TxCommitmentWithTxs) {
commitment.Transactions = append(commitment.Transactions, commitment.Transactions...)
})
s.client.Blockchain.GetBackend().Commit()
remoteBatches, err := s.client.GetAllBatches()
s.NoError(err)
s.Len(remoteBatches, 1)
txsSyncCtx, err := syncer.NewTestTxsContext(txStorage, s.client.Client, s.cfg.Rollup, txtype.Transfer)
s.NoError(err)
err = txsSyncCtx.UpdateExistingBatch(remoteBatches[0], *previousRoot)
s.NoError(err)
s.submitDepositBatch(txStorage)
}
func (s *DepositBatchesTestSuite) submitDepositBatch(storage *st.Storage) *models.Batch {
s.queueFourDeposits()
depositsCtx := executor.NewDepositsContext(
storage,
s.client.Client,
s.cfg.Rollup,
metrics.NewCommanderMetrics(),
context.Background(),
)
defer depositsCtx.Rollback(nil)
batch, _, err := depositsCtx.CreateAndSubmitBatch(context.Background())
s.NoError(err)
s.client.GetBackend().Commit()
return batch
}
func (s *DepositBatchesTestSuite) prepareDeposits() {
err := s.storage.AddPendingDepositSubtree(&s.depositSubtree)
s.NoError(err)
s.approveTokens()
}
func (s *DepositBatchesTestSuite) approveTokens() {
token, err := erc20.NewERC20(s.client.ExampleTokenAddress, s.client.GetBackend())
s.NoError(err)
_, err = token.Approve(s.client.GetAccount(), s.client.ChainState.DepositManager, utils.ParseEther("100"))
s.NoError(err)
s.client.GetBackend().Commit()
}
func (s *DepositBatchesTestSuite) queueFourDeposits() {
for i := 0; i < 4; i++ {
s.queueDeposit()
}
}
func (s *DepositBatchesTestSuite) queueDeposit() {
toPubKeyID := models.NewUint256(1)
tokenID := models.NewUint256(0)
l1Amount := models.NewUint256FromBig(*utils.ParseEther("10"))
_, _, err := s.client.QueueDepositAndWait(toPubKeyID, l1Amount, tokenID)
s.NoError(err)
}
func TestDepositBatchesTestSuite(t *testing.T) {
suite.Run(t, new(DepositBatchesTestSuite))
}