You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
14 KiB

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