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 pathconsistent_api_test.go
258 lines (212 loc) · 6.87 KB
/
consistent_api_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
package commander
// this is a test of the api more than anything, but putting it here because we touch
// the commander and this breaks any potential import loops
import (
"context"
"testing"
"github.com/Worldcoin/hubble-commander/api"
"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/dto"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/utils/consts"
"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type APIConsistencySuite struct {
*require.Assertions
suite.Suite
}
func (s *APIConsistencySuite) SetupSuite() {
s.Assertions = require.New(s.T())
}
//nolint:funlen
func (s *APIConsistencySuite) TestGetUserStatesPendingToBatched() {
testStorage, err := st.NewTestStorage()
s.NoError(err)
client, err := eth.NewTestClient()
s.NoError(err)
// we want to check that the state updates before the chain mines our batches
client.StopAutomine()
// we were updating this manually but that is no longer necessary
acct := client.Simulator.GetAccount()
acct.Nonce = nil
theapi := api.NewTestAPI(
testStorage.Storage,
client.Client,
)
commander := &Commander{
cfg: &config.Config{
Rollup: &config.RollupConfig{
MinTxsPerCommitment: 1,
MaxTxsPerCommitment: 1,
MinCommitmentsPerBatch: 1,
MaxCommitmentsPerBatch: 32,
},
},
storage: testStorage.Storage,
client: client.Client,
metrics: metrics.NewCommanderMetrics(),
}
feeReceiver := &models.UserState{
PubKeyID: 0,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(0),
Nonce: models.MakeUint256(0),
}
_, err = testStorage.Storage.StateTree.Set(0, feeReceiver)
s.NoError(err)
domain, err := client.Client.GetDomain()
s.NoError(err)
wallet, err := bls.NewRandomWallet(*domain)
s.NoError(err)
receiverWallet, err := bls.NewRandomWallet(*domain)
s.NoError(err)
err = testStorage.AccountTree.SetSingle(&models.AccountLeaf{
PubKeyID: 1,
PublicKey: *wallet.PublicKey(),
})
s.NoError(err)
userState := &models.UserState{
PubKeyID: 1,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(420),
Nonce: models.MakeUint256(0),
}
_, err = testStorage.StateTree.Set(1, userState)
s.NoError(err)
create2TransferWithoutSignature := dto.Create2Transfer{
FromStateID: ref.Uint32(1),
Amount: models.NewUint256(20),
Fee: models.NewUint256(10),
Nonce: models.NewUint256(0),
ToPublicKey: receiverWallet.PublicKey(),
}
create2Transfer, err := api.SignCreate2Transfer(wallet, create2TransferWithoutSignature)
s.NoError(err)
hash, err := theapi.SendTransaction(context.Background(), dto.MakeTransaction(*create2Transfer))
s.NoError(err)
s.NotNil(hash)
fetchedStates, err := theapi.GetUserStates(context.Background(), receiverWallet.PublicKey())
s.NoError(err)
s.Len(fetchedStates, 1)
s.Equal(dto.UserStateWithID{
StateID: consts.PendingID,
UserState: dto.UserState{
PubKeyID: consts.PendingID,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(20),
Nonce: models.MakeUint256(0),
},
}, fetchedStates[0])
nextPubkeyID, err := testStorage.AccountTree.NextBatchAccountPubKeyID()
s.NoError(err)
s.Equal(uint32(0x80000000), *nextPubkeyID) // see AccountBatchOffset
currentBatchType := batchtype.Create2Transfer
err = commander.rollupLoopIteration(context.Background(), ¤tBatchType)
s.NoError(err)
fetchedStates, err = theapi.GetUserStates(context.Background(), receiverWallet.PublicKey())
s.NoError(err)
s.Len(fetchedStates, 1)
s.Equal(dto.UserStateWithID{
StateID: 2,
UserState: dto.UserState{
PubKeyID: *nextPubkeyID,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(20),
Nonce: models.MakeUint256(0),
},
}, fetchedStates[0])
}
//nolint:funlen
func (s *APIConsistencySuite) TestConsistency() {
// a bunch of setup
testStorage, err := st.NewTestStorage()
s.NoError(err)
client, err := eth.NewTestClient()
s.NoError(err)
// we want to check that the state updates before the chain mines our batches
client.StopAutomine()
// we were updating this manually but that is no longer necessary
acct := client.Simulator.GetAccount()
acct.Nonce = nil
theapi := api.NewTestAPI(
testStorage.Storage,
client.Client,
)
commander := &Commander{
cfg: &config.Config{
Rollup: &config.RollupConfig{
MinTxsPerCommitment: 1,
MaxTxsPerCommitment: 1,
MinCommitmentsPerBatch: 1,
MaxCommitmentsPerBatch: 32,
},
},
storage: testStorage.Storage,
client: client.Client,
metrics: metrics.NewCommanderMetrics(),
}
feeReceiver := &models.UserState{
PubKeyID: 0,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(0),
Nonce: models.MakeUint256(0),
}
_, err = testStorage.Storage.StateTree.Set(0, feeReceiver)
s.NoError(err)
domain, err := client.Client.GetDomain()
s.NoError(err)
wallet, err := bls.NewRandomWallet(*domain)
s.NoError(err)
receiverWallet, err := bls.NewRandomWallet(*domain)
s.NoError(err)
err = testStorage.AccountTree.SetSingle(&models.AccountLeaf{
PubKeyID: 123,
PublicKey: *wallet.PublicKey(),
})
s.NoError(err)
userState := &models.UserState{
PubKeyID: 123,
TokenID: models.MakeUint256(0),
Balance: models.MakeUint256(420),
Nonce: models.MakeUint256(0),
}
_, err = testStorage.StateTree.Set(1, userState)
s.NoError(err)
create2TransferWithoutSignature := dto.Create2Transfer{
FromStateID: ref.Uint32(1),
Amount: models.NewUint256(20),
Fee: models.NewUint256(10),
Nonce: models.NewUint256(0),
ToPublicKey: receiverWallet.PublicKey(),
}
create2Transfer, err := api.SignCreate2Transfer(wallet, create2TransferWithoutSignature)
s.NoError(err)
// submit two transaction to the api which depend on each other
// check the nonce of the account
fetchedState, err := theapi.GetUserState(context.Background(), 1)
s.NoError(err)
s.Equal(fetchedState.Nonce, models.MakeUint256(0))
s.Equal(fetchedState.Balance, models.MakeUint256(420))
hash, err := theapi.SendTransaction(context.Background(), dto.MakeTransaction(*create2Transfer))
s.NoError(err)
s.NotNil(hash)
// run an iteration of the rollup loop here in such a way that it generates
// a commitment
currentBatchType := batchtype.Create2Transfer
err = commander.rollupLoopIteration(context.Background(), ¤tBatchType)
s.NoError(err)
fetchedState, err = theapi.GetUserState(context.Background(), 1)
s.NoError(err)
s.Equal(models.MakeUint256(1), fetchedState.Nonce)
s.Equal(models.MakeUint256(390), fetchedState.Balance)
}
func TestAPIConsistencySuite(t *testing.T) {
suite.Run(t, new(APIConsistencySuite))
}