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
/
mempool_test.go
365 lines (289 loc) · 9.67 KB
/
mempool_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
package storage
import (
"testing"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
"github.com/Worldcoin/hubble-commander/models/stored"
"github.com/Worldcoin/hubble-commander/testutils"
"github.com/Worldcoin/hubble-commander/utils/consts"
"github.com/dgraph-io/badger/v3"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type MempoolTestSuite struct {
*require.Assertions
suite.Suite
storage *TestStorage
}
func (s *MempoolTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *MempoolTestSuite) SetupTest() {
var err error
s.storage, err = NewTestStorage()
s.NoError(err)
err = s.storage.AccountTree.SetSingle(&models.AccountLeaf{
PubKeyID: 123,
PublicKey: models.PublicKey{1, 2, 3},
})
s.NoError(err)
// sender
_, err = s.storage.StateTree.Set(
1,
&models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(100),
Nonce: models.MakeUint256(0),
},
)
s.NoError(err)
// receiver
_, err = s.storage.StateTree.Set(
2,
&models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(0),
Nonce: models.MakeUint256(0),
},
)
s.NoError(err)
}
func (s *MempoolTestSuite) TearDownTest() {
err := s.storage.Teardown()
s.NoError(err)
}
// TODO: this should not be a unit test, this test belongs on the api/ directory
// the api test will test the exact same thing but make the code less brittle
func (s *MempoolTestSuite) TestMempool_UsesPendingBalance() {
// stateID=2 starts with Balance=0, so the API does not accept any transfers
// from it until it receives some money
transfer := testutils.NewTransfer(2, 1, 0, 10)
err := s.storage.AddMempoolTx(transfer)
s.ErrorContains(err, "balance too low")
transfer = testutils.NewTransfer(1, 2, 0, 10)
err = s.storage.AddMempoolTx(transfer)
s.NoError(err)
// we have 10 but this txn also pays a fee of 10 so it still fails
transfer = testutils.NewTransfer(2, 1, 0, 10)
err = s.storage.AddMempoolTx(transfer)
s.ErrorContains(err, "balance too low")
transfer = testutils.NewTransfer(1, 2, 1, 10)
err = s.storage.AddMempoolTx(transfer)
s.NoError(err)
// now that our pending balance is high enough it is accepted
transfer = testutils.NewTransfer(2, 1, 0, 10)
err = s.storage.AddMempoolTx(transfer)
s.NoError(err)
}
func (s *MempoolTestSuite) TestMempool_LowestNoncePendingTxs() {
txs := []*models.Transfer{
testutils.NewTransfer(1, 2, 0, 10),
testutils.NewTransfer(1, 2, 1, 10),
testutils.NewTransfer(2, 1, 0, 10),
}
for _, tx := range txs {
s.NotNil(tx)
err := s.storage.AddMempoolTx(tx)
s.NoError(err)
}
// it skips the second txn from 1->2
pendingTxs, err := s.storage.lowestNoncePendingTxs()
s.NoError(err)
s.Len(pendingTxs, 2)
s.Equal(uint32(1), pendingTxs[0].FromStateID)
s.Equal(models.MakeUint256(0), pendingTxs[0].Nonce)
s.Equal(uint32(2), pendingTxs[1].FromStateID)
s.Equal(models.MakeUint256(0), pendingTxs[1].Nonce)
}
func (s *MempoolTestSuite) TestMempool_PeekEmptyMempool() {
mempoolHeap, err := s.storage.NewMempoolHeap(txtype.Transfer)
s.NoError(err)
s.NotNil(mempoolHeap)
// if there's nothing in the mempool you won't crash when you ask for the next Tx
firstTx := mempoolHeap.PeekHighestFeeExecutableTx()
s.Nil(firstTx)
}
func (s *MempoolTestSuite) randomPublicKey() *models.PublicKey {
domain := bls.Domain{1, 2, 3, 4}
wallet, err := bls.NewRandomWallet(domain)
s.NoError(err)
return wallet.PublicKey()
}
// skips all pending state maintenance
func (s *MempoolTestSuite) rawInsert(tx models.GenericTransaction) {
pendingTx := stored.NewPendingTx(tx)
err := s.storage.UnsafeInsertPendingTxSkipValidation(pendingTx)
s.NoError(err)
}
func (s *MempoolTestSuite) TestMempool_MigrateState() {
destKey := s.randomPublicKey()
s.rawInsert(testutils.NewCreate2Transfer(1, nil, 0, 10, destKey))
s.rawInsert(testutils.NewCreate2Transfer(1, nil, 1, 10, destKey))
pendingState, err := s.storage.GetPendingC2TState(destKey)
s.NoError(err)
s.Nil(pendingState)
migrated, err := s.storage.alreadyRanPubKeyMigration()
s.NoError(err)
s.False(migrated)
err = s.storage.MigratePubKeyPendingState()
s.NoError(err)
migrated, err = s.storage.alreadyRanPubKeyMigration()
s.NoError(err)
s.True(migrated)
pendingState, err = s.storage.GetPendingC2TState(destKey)
s.NoError(err)
s.Equal(
models.UserState{
PubKeyID: consts.PendingID,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(20),
Nonce: models.MakeUint256(0),
},
*pendingState,
)
}
func TestMempoolTestSuite(t *testing.T) {
suite.Run(t, new(MempoolTestSuite))
}
type ConflictTestSuite struct {
*require.Assertions
suite.Suite
rollupStorage *Storage
apiStorage *Storage
}
func (s *ConflictTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
func (s *ConflictTestSuite) SetupTest() {
memoryDB, err := db.NewInMemoryDatabase()
s.NoError(err)
database := &Database{
Badger: memoryDB,
}
rollupStorage, err := newStorageFromDatabase(database)
s.NoError(err)
s.NotNil(rollupStorage)
s.rollupStorage = rollupStorage
apiStorage, err := newStorageFromDatabase(database)
s.NoError(err)
s.NotNil(apiStorage)
s.apiStorage = apiStorage
_, err = apiStorage.StateTree.Set(
1,
&models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(100),
Nonce: models.MakeUint256(0),
},
)
s.NoError(err)
_, err = apiStorage.StateTree.Set(
2,
&models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(1),
Balance: models.MakeUint256(0),
Nonce: models.MakeUint256(0),
},
)
s.NoError(err)
// confirm this immediately shows up in rollupStorage, they share a db
leaf, err := s.rollupStorage.StateTree.Leaf(1)
s.NoError(err)
s.Equal(models.MakeUint256(100), leaf.UserState.Balance)
}
func (s *ConflictTestSuite) TestConflict_ConflictsWithGet() {
// nextTxForAccount goes to some lengths to not call txn.Get() in order to avoid
// badger.ErrConflict, this tests confirms that is a real concern.
// (I) open rollupTx and do nothing with it
rollupTxController, txRollupStorage := s.rollupStorage.BeginTransaction(TxOptions{})
s.NotNil(rollupTxController)
s.NotNil(txRollupStorage)
// (II) open apiTx and insert a transaction
apiTxController, txAPIStorage := s.apiStorage.BeginTransaction(TxOptions{})
s.NotNil(apiTxController)
s.NotNil(txAPIStorage)
transfer := testutils.NewTransfer(1, 2, 0, 10)
err := txAPIStorage.unsafeAddMempoolTx(transfer) // the safe version Commit()s
s.NoError(err)
// (III) txn.Get() in rollupTx
key := pendingTxKey(1, 0)
_, err = txRollupStorage.rawLookup(key)
s.ErrorIs(err, badger.ErrKeyNotFound) // the API has not yet Commit()
// (IV) do a write so that badger knows to try to fail this tx in case of conflict
key = pendingTxKey(10, 10)
err = txRollupStorage.rawSet(key, []byte("hello"))
s.NoError(err)
// (V) commit apiTX
err = apiTxController.Commit()
s.NoError(err)
// (VI) commit rollupTx and notice that it fails
err = rollupTxController.Commit()
s.ErrorIs(err, badger.ErrConflict)
}
// now, confirm that there is no conflict even if the rollup loop has "read" the key.
// in TestConflict_ConflictsWithGet we saw that a call to `rawLookup` in the rollup loop
// will cause a conflict to occur. Here we throw a transaction into the mempool which is not
// executable (the balance is too low) and check that the rollup builds a batch which correctly
// excludes the transaction without causing a conflict.
func (s *ConflictTestSuite) TestConflict_NoConflict() {
transfer := testutils.NewTransfer(1, 2, 0, 10)
err := s.apiStorage.AddMempoolTx(transfer)
s.NoError(err)
// stateID=2 starts with a balance of 0 so this transaction is not
// executable
transfer = testutils.NewTransfer(2, 1, 0, 10)
{
// go through some hoops to insert the transaction because the normal
// code paths very much so do not want to accept transactions which the
// user does not have enough funds to pay for
pending := stored.NewPendingTx(transfer)
key := pendingTxKey(2, 0)
err = s.apiStorage.rawSet(key, pending.Bytes())
s.NoError(err)
err = s.apiStorage.UnsafeSetPendingState(2, models.MakeUint256(1), models.MakeUint256(0))
s.NoError(err)
}
// (I) we start the rollupTx
rollupTxController, txRollupStorage := s.rollupStorage.BeginTransaction(TxOptions{})
s.NotNil(rollupTxController)
s.NotNil(txRollupStorage)
// (II) we start the apiTx
apiTxController, txAPIStorage := s.apiStorage.BeginTransaction(TxOptions{})
s.NotNil(apiTxController)
s.NotNil(txAPIStorage)
// (III) we read out some transactions in the rollup loop
mempoolHeap, err := txRollupStorage.NewMempoolHeap(txtype.Transfer)
s.NoError(err)
s.NotNil(mempoolHeap)
firstTx := mempoolHeap.PeekHighestFeeExecutableTx()
s.NotNil(firstTx)
s.Equal(uint32(1), firstTx.GetFromStateID())
// this attempts to read out txns from both accounts
// this is the line which will cause a conflict if mempool contains a bug
err = mempoolHeap.DropHighestFeeExecutableTx()
s.NoError(err)
err = mempoolHeap.Savepoint()
s.NoError(err)
// (IV) we insert some new txns from the api
transfer = testutils.NewTransfer(1, 2, 1, 40)
err = txAPIStorage.AddMempoolTx(transfer)
s.NoError(err)
transfer = testutils.NewTransfer(2, 1, 1, 10)
err = txAPIStorage.AddMempoolTx(transfer)
s.NoError(err)
err = apiTxController.Commit()
s.NoError(err)
// (VII) we cleanly commit the rollup txn
err = rollupTxController.Commit()
s.NoError(err)
}
// TODO: are there more likely scenarios where this might conflict?
func TestConflictTestSuite(t *testing.T) {
suite.Run(t, new(ConflictTestSuite))
}