hermez-node/test/zkproof/flows_test.go
test/zkproof/flows_test.goRaw
1package zkproof
2
3import (
4 "io/ioutil"
5 "os"
6 "strconv"
7 "testing"
8 "time"
9
10 ethCommon "github.com/ethereum/go-ethereum/common"
11 ethCrypto "github.com/ethereum/go-ethereum/crypto"
12 "github.com/hermeznetwork/hermez-node/batchbuilder"
13 "github.com/hermeznetwork/hermez-node/common"
14 dbUtils "github.com/hermeznetwork/hermez-node/db"
15 "github.com/hermeznetwork/hermez-node/db/historydb"
16 "github.com/hermeznetwork/hermez-node/db/l2db"
17 "github.com/hermeznetwork/hermez-node/db/statedb"
18 "github.com/hermeznetwork/hermez-node/log"
19 "github.com/hermeznetwork/hermez-node/test"
20 "github.com/hermeznetwork/hermez-node/test/til"
21 "github.com/hermeznetwork/hermez-node/test/txsets"
22 "github.com/hermeznetwork/hermez-node/txselector"
23 "github.com/jmoiron/sqlx"
24 "github.com/stretchr/testify/assert"
25 "github.com/stretchr/testify/require"
26)
27
28func addTokens(t *testing.T, tc *til.Context, db *sqlx.DB) {
29 var tokens []common.Token
30 for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
31 tokens = append(tokens, common.Token{
32 TokenID: common.TokenID(i + 1),
33 EthBlockNum: 1,
34 EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
35 Name: strconv.Itoa(i),
36 Symbol: strconv.Itoa(i),
37 Decimals: 18,
38 })
39 }
40
41 hdb := historydb.NewHistoryDB(db)
42 assert.NoError(t, hdb.AddBlock(&common.Block{
43 Num: 1,
44 }))
45 assert.NoError(t, hdb.AddTokens(tokens))
46}
47
48func addL2Txs(t *testing.T, l2DB *l2db.L2DB, poolL2Txs []common.PoolL2Tx) {
49 for i := 0; i < len(poolL2Txs); i++ {
50 err := l2DB.AddTxTest(&poolL2Txs[i])
51 if err != nil {
52 log.Error(err)
53 }
54 require.NoError(t, err)
55 }
56}
57
58func addAccCreationAuth(t *testing.T, tc *til.Context, l2DB *l2db.L2DB, chainID uint16, hermezContractAddr ethCommon.Address, username string) []byte {
59 user := tc.Users[username]
60 auth := &common.AccountCreationAuth{
61 EthAddr: user.Addr,
62 BJJ: user.BJJ.Public().Compress(),
63 }
64 err := auth.Sign(func(hash []byte) ([]byte, error) {
65 return ethCrypto.Sign(hash, user.EthSk)
66 }, chainID, hermezContractAddr)
67 assert.NoError(t, err)
68
69 err = l2DB.AddAccountCreationAuth(auth)
70 assert.NoError(t, err)
71 return auth.Signature
72}
73
74func initTxSelector(t *testing.T, chainID uint16, hermezContractAddr ethCommon.Address, coordUser *til.User) (*txselector.TxSelector, *l2db.L2DB, *statedb.StateDB) {
75 pass := os.Getenv("POSTGRES_PASS")
76 db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
77 require.NoError(t, err)
78 l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
79
80 dir, err := ioutil.TempDir("", "tmpSyncDB")
81 require.NoError(t, err)
82 defer assert.NoError(t, os.RemoveAll(dir))
83 syncStateDB, err := statedb.NewStateDB(dir, 128, statedb.TypeSynchronizer, 0)
84 require.NoError(t, err)
85
86 txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
87 require.NoError(t, err)
88 defer assert.NoError(t, os.RemoveAll(dir))
89
90 // use Til Coord keys for tests compatibility
91 coordAccount := &txselector.CoordAccount{
92 Addr: coordUser.Addr,
93 BJJ: coordUser.BJJ.Public().Compress(),
94 AccountCreationAuth: nil,
95 }
96 auth := common.AccountCreationAuth{
97 EthAddr: coordUser.Addr,
98 BJJ: coordUser.BJJ.Public().Compress(),
99 }
100 err = auth.Sign(func(hash []byte) ([]byte, error) {
101 return ethCrypto.Sign(hash, coordUser.EthSk)
102 }, chainID, hermezContractAddr)
103 assert.NoError(t, err)
104 coordAccount.AccountCreationAuth = auth.Signature
105
106 txsel, err := txselector.NewTxSelector(coordAccount, txselDir, syncStateDB, l2DB)
107 require.NoError(t, err)
108
109 test.WipeDB(l2DB.DB())
110
111 return txsel, l2DB, syncStateDB
112}
113
114func TestTxSelectorBatchBuilderZKInputs(t *testing.T) {
115 tc := til.NewContext(ChainID, common.RollupConstMaxL1UserTx)
116 // generate test transactions, the L1CoordinatorTxs generated by Til
117 // will be ignored at this test, as will be the TxSelector who
118 // generates them when needed
119 blocks, err := tc.GenerateBlocks(txsets.SetBlockchainMinimumFlow0)
120 require.NoError(t, err)
121
122 hermezContractAddr := ethCommon.HexToAddress("0xc344E203a046Da13b0B4467EB7B3629D0C99F6E6")
123 txsel, l2DBTxSel, syncStateDB := initTxSelector(t, ChainID, hermezContractAddr, tc.Users["Coord"])
124
125 bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB")
126 require.NoError(t, err)
127 bb, err := batchbuilder.NewBatchBuilder(bbDir, syncStateDB, 0, NLevels)
128 require.NoError(t, err)
129
130 // restart nonces of TilContext, as will be set by generating directly
131 // the PoolL2Txs for each specific batch with tc.GeneratePoolL2Txs
132 tc.RestartNonces()
133
134 // add tokens to HistoryDB to avoid breaking FK constrains
135 addTokens(t, tc, l2DBTxSel.DB())
136
137 selectionConfig := &txselector.SelectionConfig{
138 MaxL1UserTxs: 100, // TODO
139 TxProcessorConfig: txprocConfig,
140 }
141 configBatch := &batchbuilder.ConfigBatch{
142 // ForgerAddress:
143 TxProcessorConfig: txprocConfig,
144 }
145
146 // loop over the first 6 batches
147 expectedRoots := []string{"0", "0", "13644148972047617726265275926674266298636745191961029124811988256139761111521", "12433441613247342495680642890662773367605896324555599297255745922589338651261", "12433441613247342495680642890662773367605896324555599297255745922589338651261", "4191361650490017591061467288209836928064232431729236465872209988325272262963"}
148 for i := 0; i < 6; i++ {
149 log.Debugf("block:0 batch:%d", i+1)
150 var l1UserTxs []common.L1Tx
151 if blocks[0].Rollup.Batches[i].Batch.ForgeL1TxsNum != nil {
152 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[i].Batch.ForgeL1TxsNum])
153 }
154 // TxSelector select the transactions for the next Batch
155 coordIdxs, _, oL1UserTxs, oL1CoordTxs, oL2Txs, _, err := txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
156 require.NoError(t, err)
157 // BatchBuilder build Batch
158 zki, err := bb.BuildBatch(coordIdxs, configBatch, oL1UserTxs, oL1CoordTxs, oL2Txs)
159 require.NoError(t, err)
160 assert.Equal(t, expectedRoots[i], bb.LocalStateDB().MT.Root().BigInt().String())
161 sendProofAndCheckResp(t, zki)
162 }
163
164 log.Debug("block:0 batch:7")
165 // simulate the PoolL2Txs of the batch6
166 batchPoolL2 := `
167 Type: PoolL2
168 PoolTransferToEthAddr(1) A-B: 200 (126)
169 PoolTransferToEthAddr(0) B-C: 100 (126)`
170 l2Txs, err := tc.GeneratePoolL2Txs(batchPoolL2)
171 require.NoError(t, err)
172 // add AccountCreationAuths that will be used at the next batch
173 _ = addAccCreationAuth(t, tc, l2DBTxSel, ChainID, hermezContractAddr, "B")
174 _ = addAccCreationAuth(t, tc, l2DBTxSel, ChainID, hermezContractAddr, "C")
175 addL2Txs(t, l2DBTxSel, l2Txs) // Add L2s to TxSelector.L2DB
176 l1UserTxs := til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
177 // TxSelector select the transactions for the next Batch
178 coordIdxs, _, oL1UserTxs, oL1CoordTxs, oL2Txs, discardedL2Txs, err := txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
179 require.NoError(t, err)
180 // BatchBuilder build Batch
181 zki, err := bb.BuildBatch(coordIdxs, configBatch, oL1UserTxs, oL1CoordTxs, oL2Txs)
182 require.NoError(t, err)
183 assert.Equal(t, "7614010373759339299470010949167613050707822522530721724565424494781010548240", bb.LocalStateDB().MT.Root().BigInt().String())
184 sendProofAndCheckResp(t, zki)
185 err = l2DBTxSel.StartForging(common.TxIDsFromPoolL2Txs(oL2Txs), txsel.LocalAccountsDB().CurrentBatch())
186 require.NoError(t, err)
187 err = l2DBTxSel.UpdateTxsInfo(discardedL2Txs)
188 require.NoError(t, err)
189
190 log.Debug("block:0 batch:8")
191 // simulate the PoolL2Txs of the batch8
192 batchPoolL2 = `
193 Type: PoolL2
194 PoolTransfer(0) A-B: 100 (126)
195 PoolTransfer(0) C-A: 50 (126)
196 PoolTransfer(1) B-C: 100 (126)
197 PoolExit(0) A: 100 (126)`
198 l2Txs, err = tc.GeneratePoolL2Txs(batchPoolL2)
199 require.NoError(t, err)
200 addL2Txs(t, l2DBTxSel, l2Txs) // Add L2s to TxSelector.L2DB
201 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
202 // TxSelector select the transactions for the next Batch
203 coordIdxs, _, oL1UserTxs, oL1CoordTxs, oL2Txs, discardedL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
204 require.NoError(t, err)
205 // BatchBuilder build Batch
206 zki, err = bb.BuildBatch(coordIdxs, configBatch, oL1UserTxs, oL1CoordTxs, oL2Txs)
207 require.NoError(t, err)
208 assert.Equal(t, "21231789250434471575486264439945776732824482207853465397552873521865656677689", bb.LocalStateDB().MT.Root().BigInt().String())
209 sendProofAndCheckResp(t, zki)
210 err = l2DBTxSel.StartForging(common.TxIDsFromPoolL2Txs(l2Txs), txsel.LocalAccountsDB().CurrentBatch())
211 require.NoError(t, err)
212 err = l2DBTxSel.UpdateTxsInfo(discardedL2Txs)
213 require.NoError(t, err)
214
215 log.Debug("(batch9) block:1 batch:1")
216 // simulate the PoolL2Txs of the batch9
217 batchPoolL2 = `
218 Type: PoolL2
219 PoolTransfer(0) D-A: 300 (126)
220 PoolTransfer(0) B-D: 100 (126)`
221 l2Txs, err = tc.GeneratePoolL2Txs(batchPoolL2)
222 require.NoError(t, err)
223 addL2Txs(t, l2DBTxSel, l2Txs) // Add L2s to TxSelector.L2DB
224 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
225 // TxSelector select the transactions for the next Batch
226 coordIdxs, _, oL1UserTxs, oL1CoordTxs, oL2Txs, discardedL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
227 require.NoError(t, err)
228 // BatchBuilder build Batch
229 zki, err = bb.BuildBatch(coordIdxs, configBatch, oL1UserTxs, oL1CoordTxs, oL2Txs)
230 require.NoError(t, err)
231 assert.Equal(t, "11289313644810782435120113035387729451095637380468777086895109386127538554246", bb.LocalStateDB().MT.Root().BigInt().String())
232 sendProofAndCheckResp(t, zki)
233 err = l2DBTxSel.StartForging(common.TxIDsFromPoolL2Txs(l2Txs), txsel.LocalAccountsDB().CurrentBatch())
234 require.NoError(t, err)
235 err = l2DBTxSel.UpdateTxsInfo(discardedL2Txs)
236 require.NoError(t, err)
237
238 log.Debug("(batch10) block:1 batch:2")
239 l2Txs = []common.PoolL2Tx{}
240 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[1].Batch.ForgeL1TxsNum])
241 // TxSelector select the transactions for the next Batch
242 coordIdxs, _, oL1UserTxs, oL1CoordTxs, oL2Txs, discardedL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
243 require.NoError(t, err)
244 // BatchBuilder build Batch
245 zki, err = bb.BuildBatch(coordIdxs, configBatch, oL1UserTxs, oL1CoordTxs, oL2Txs)
246 require.NoError(t, err)
247 // same root as previous batch, as the L1CoordinatorTxs created by the
248 // Til set is not created by the TxSelector in this test
249 assert.Equal(t, "11289313644810782435120113035387729451095637380468777086895109386127538554246", bb.LocalStateDB().MT.Root().BigInt().String())
250 sendProofAndCheckResp(t, zki)
251 err = l2DBTxSel.StartForging(common.TxIDsFromPoolL2Txs(l2Txs), txsel.LocalAccountsDB().CurrentBatch())
252 require.NoError(t, err)
253 err = l2DBTxSel.UpdateTxsInfo(discardedL2Txs)
254 require.NoError(t, err)
255}