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.

373 lines
14 KiB

Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
  1. package txselector
  2. import (
  3. "crypto/ecdsa"
  4. "fmt"
  5. "io/ioutil"
  6. "math/big"
  7. "os"
  8. "strconv"
  9. "testing"
  10. "time"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  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/txprocessor"
  22. "github.com/iden3/go-iden3-crypto/babyjub"
  23. "github.com/jmoiron/sqlx"
  24. "github.com/stretchr/testify/assert"
  25. "github.com/stretchr/testify/require"
  26. )
  27. func initTest(t *testing.T, chainID uint16, hermezContractAddr ethCommon.Address, testSet string) (*TxSelector, *til.Context) {
  28. pass := os.Getenv("POSTGRES_PASS")
  29. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  30. require.NoError(t, err)
  31. l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
  32. dir, err := ioutil.TempDir("", "tmpdb")
  33. require.NoError(t, err)
  34. defer assert.NoError(t, os.RemoveAll(dir))
  35. sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeTxSelector, 0)
  36. require.NoError(t, err)
  37. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  38. require.NoError(t, err)
  39. defer assert.NoError(t, os.RemoveAll(dir))
  40. // coordinator keys
  41. var ethSk ecdsa.PrivateKey
  42. ethSk.D = big.NewInt(int64(1)) // only for testing
  43. ethSk.PublicKey.X, ethSk.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(ethSk.D.Bytes())
  44. ethSk.Curve = ethCrypto.S256()
  45. addr := ethCrypto.PubkeyToAddress(ethSk.PublicKey)
  46. var bjj babyjub.PublicKeyComp
  47. err = bjj.UnmarshalText([]byte("c433f7a696b7aa3a5224efb3993baf0ccd9e92eecee0c29a3f6c8208a9e81d9e"))
  48. require.NoError(t, err)
  49. coordAccount := &CoordAccount{
  50. Addr: addr,
  51. BJJ: bjj,
  52. AccountCreationAuth: nil,
  53. }
  54. auth := common.AccountCreationAuth{
  55. EthAddr: addr,
  56. BJJ: bjj,
  57. }
  58. err = auth.Sign(func(hash []byte) ([]byte, error) {
  59. return ethCrypto.Sign(hash, &ethSk)
  60. }, chainID, hermezContractAddr)
  61. assert.NoError(t, err)
  62. coordAccount.AccountCreationAuth = auth.Signature
  63. txsel, err := NewTxSelector(coordAccount, txselDir, sdb, l2DB)
  64. require.NoError(t, err)
  65. test.WipeDB(txsel.l2db.DB())
  66. tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
  67. return txsel, tc
  68. }
  69. func addAccCreationAuth(t *testing.T, tc *til.Context, txsel *TxSelector, chainID uint16, hermezContractAddr ethCommon.Address, username string) []byte {
  70. user := tc.Users[username]
  71. auth := &common.AccountCreationAuth{
  72. EthAddr: user.Addr,
  73. BJJ: user.BJJ.Public().Compress(),
  74. }
  75. err := auth.Sign(func(hash []byte) ([]byte, error) {
  76. return ethCrypto.Sign(hash, user.EthSk)
  77. }, chainID, hermezContractAddr)
  78. assert.NoError(t, err)
  79. err = txsel.l2db.AddAccountCreationAuth(auth)
  80. assert.NoError(t, err)
  81. return auth.Signature
  82. }
  83. func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
  84. for i := 0; i < len(poolL2Txs); i++ {
  85. err := txsel.l2db.AddTxTest(&poolL2Txs[i])
  86. if err != nil {
  87. log.Error(err)
  88. }
  89. require.NoError(t, err)
  90. }
  91. }
  92. func addTokens(t *testing.T, tc *til.Context, db *sqlx.DB) {
  93. var tokens []common.Token
  94. for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
  95. tokens = append(tokens, common.Token{
  96. TokenID: common.TokenID(i + 1),
  97. EthBlockNum: 1,
  98. EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
  99. Name: strconv.Itoa(i),
  100. Symbol: strconv.Itoa(i),
  101. Decimals: 18,
  102. })
  103. }
  104. hdb := historydb.NewHistoryDB(db)
  105. assert.NoError(t, hdb.AddBlock(&common.Block{
  106. Num: 1,
  107. }))
  108. assert.NoError(t, hdb.AddTokens(tokens))
  109. }
  110. func checkBalance(t *testing.T, tc *til.Context, txsel *TxSelector, username string, tokenid int, expected string) {
  111. // Accounts.Idx does not match with the TxSelector tests as we are not
  112. // using the Til L1CoordinatorTxs (as are generated by the TxSelector
  113. // itself when processing the txs, so the Idxs does not match the Til
  114. // idxs). But the Idx is obtained through StateDB.GetIdxByEthAddrBJJ
  115. user := tc.Users[username]
  116. idx, err := txsel.localAccountsDB.GetIdxByEthAddrBJJ(user.Addr, user.BJJ.Public().Compress(), common.TokenID(tokenid))
  117. require.NoError(t, err)
  118. checkBalanceByIdx(t, txsel, idx, expected)
  119. }
  120. func checkBalanceByIdx(t *testing.T, txsel *TxSelector, idx common.Idx, expected string) {
  121. acc, err := txsel.localAccountsDB.GetAccount(idx)
  122. require.NoError(t, err)
  123. assert.Equal(t, expected, acc.Balance.String())
  124. }
  125. // checkSortedByNonce takes as input testAccNonces map, and the array of
  126. // common.PoolL2Txs, and checks if the nonces correspond to the accumulated
  127. // values of the map. Also increases the Nonces computed on the map.
  128. func checkSortedByNonce(t *testing.T, testAccNonces map[common.Idx]common.Nonce, txs []common.PoolL2Tx) {
  129. for _, tx := range txs {
  130. assert.True(t, testAccNonces[tx.FromIdx] == tx.Nonce,
  131. fmt.Sprintf("Idx: %d, expected: %d, tx.Nonce: %d",
  132. tx.FromIdx, testAccNonces[tx.FromIdx], tx.Nonce))
  133. testAccNonces[tx.FromIdx] = testAccNonces[tx.FromIdx] + 1
  134. }
  135. }
  136. func TestGetL2TxSelectionMinimumFlow0(t *testing.T) {
  137. chainID := uint16(0)
  138. hermezContractAddr := ethCommon.HexToAddress("0xc344E203a046Da13b0B4467EB7B3629D0C99F6E6")
  139. txsel, tc := initTest(t, chainID, hermezContractAddr, til.SetPool0)
  140. // generate test transactions, the L1CoordinatorTxs generated by Til
  141. // will be ignored at this test, as will be the TxSelector who
  142. // generates them when needed
  143. blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0)
  144. assert.NoError(t, err)
  145. // restart nonces of TilContext, as will be set by generating directly
  146. // the PoolL2Txs for each specific batch with tc.GeneratePoolL2Txs
  147. tc.RestartNonces()
  148. testAccNonces := make(map[common.Idx]common.Nonce)
  149. // add tokens to HistoryDB to avoid breaking FK constrains
  150. addTokens(t, tc, txsel.l2db.DB())
  151. tpc := txprocessor.Config{
  152. NLevels: 16,
  153. MaxFeeTx: 10,
  154. MaxTx: 20,
  155. MaxL1Tx: 10,
  156. ChainID: chainID,
  157. }
  158. selectionConfig := &SelectionConfig{
  159. MaxL1UserTxs: 5,
  160. TxProcessorConfig: tpc,
  161. }
  162. // coordIdxs, accAuths, l1UserTxs, l1CoordTxs, l2Txs, err
  163. log.Debug("block:0 batch:0")
  164. l1UserTxs := []common.L1Tx{}
  165. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err := txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  166. require.NoError(t, err)
  167. assert.Equal(t, 0, len(oL1UserTxs))
  168. assert.Equal(t, 0, len(oL1CoordTxs))
  169. assert.Equal(t, 0, len(oL2Txs))
  170. assert.Equal(t, common.BatchNum(1), txsel.localAccountsDB.CurrentBatch())
  171. assert.Equal(t, common.Idx(255), txsel.localAccountsDB.CurrentIdx())
  172. log.Debug("block:0 batch:1")
  173. l1UserTxs = []common.L1Tx{}
  174. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  175. require.NoError(t, err)
  176. assert.Equal(t, 0, len(oL1UserTxs))
  177. assert.Equal(t, 0, len(oL1CoordTxs))
  178. assert.Equal(t, 0, len(oL2Txs))
  179. assert.Equal(t, common.BatchNum(2), txsel.localAccountsDB.CurrentBatch())
  180. assert.Equal(t, common.Idx(255), txsel.localAccountsDB.CurrentIdx())
  181. log.Debug("block:0 batch:2")
  182. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum])
  183. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  184. require.NoError(t, err)
  185. assert.Equal(t, 2, len(oL1UserTxs))
  186. assert.Equal(t, 0, len(oL1CoordTxs))
  187. assert.Equal(t, 0, len(oL2Txs))
  188. assert.Equal(t, common.BatchNum(3), txsel.localAccountsDB.CurrentBatch())
  189. assert.Equal(t, common.Idx(257), txsel.localAccountsDB.CurrentIdx())
  190. checkBalance(t, tc, txsel, "A", 0, "500")
  191. checkBalance(t, tc, txsel, "C", 1, "0")
  192. log.Debug("block:0 batch:3")
  193. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum])
  194. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  195. require.NoError(t, err)
  196. assert.Equal(t, 1, len(oL1UserTxs))
  197. assert.Equal(t, 0, len(oL1CoordTxs))
  198. assert.Equal(t, 0, len(oL2Txs))
  199. assert.Equal(t, common.BatchNum(4), txsel.localAccountsDB.CurrentBatch())
  200. assert.Equal(t, common.Idx(258), txsel.localAccountsDB.CurrentIdx())
  201. checkBalance(t, tc, txsel, "A", 0, "500")
  202. checkBalance(t, tc, txsel, "A", 1, "500")
  203. checkBalance(t, tc, txsel, "C", 1, "0")
  204. log.Debug("block:0 batch:4")
  205. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum])
  206. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  207. require.NoError(t, err)
  208. assert.Equal(t, 0, len(oL1UserTxs))
  209. assert.Equal(t, 0, len(oL1CoordTxs))
  210. assert.Equal(t, 0, len(oL2Txs))
  211. assert.Equal(t, common.BatchNum(5), txsel.localAccountsDB.CurrentBatch())
  212. assert.Equal(t, common.Idx(258), txsel.localAccountsDB.CurrentIdx())
  213. checkBalance(t, tc, txsel, "A", 0, "500")
  214. checkBalance(t, tc, txsel, "A", 1, "500")
  215. checkBalance(t, tc, txsel, "C", 1, "0")
  216. log.Debug("block:0 batch:5")
  217. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum])
  218. _, _, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  219. require.NoError(t, err)
  220. assert.Equal(t, 1, len(oL1UserTxs))
  221. assert.Equal(t, 0, len(oL1CoordTxs))
  222. assert.Equal(t, 0, len(oL2Txs))
  223. assert.Equal(t, common.BatchNum(6), txsel.localAccountsDB.CurrentBatch())
  224. assert.Equal(t, common.Idx(259), txsel.localAccountsDB.CurrentIdx())
  225. checkBalance(t, tc, txsel, "A", 0, "600")
  226. checkBalance(t, tc, txsel, "A", 1, "500")
  227. checkBalance(t, tc, txsel, "B", 0, "400")
  228. checkBalance(t, tc, txsel, "C", 1, "0")
  229. log.Debug("block:0 batch:6")
  230. // simulate the PoolL2Txs of the batch6
  231. batchPoolL2 := `
  232. Type: PoolL2
  233. PoolTransfer(1) A-B: 200 (126)
  234. PoolTransfer(0) B-C: 100 (126)`
  235. poolL2Txs, err := tc.GeneratePoolL2Txs(batchPoolL2)
  236. require.NoError(t, err)
  237. // add AccountCreationAuths that will be used at the next batch
  238. accAuthSig0 := addAccCreationAuth(t, tc, txsel, chainID, hermezContractAddr, "B")
  239. accAuthSig1 := addAccCreationAuth(t, tc, txsel, chainID, hermezContractAddr, "C")
  240. // add ToEthAddr for the corresponent ToIdx, and remove ToIdx for Batches[6].L2Tx
  241. poolL2Txs[0].ToEthAddr = tc.Users["B"].Addr
  242. poolL2Txs[0].ToIdx = common.Idx(0)
  243. poolL2Txs[1].ToEthAddr = tc.Users["C"].Addr
  244. poolL2Txs[1].ToIdx = common.Idx(0)
  245. // add the PoolL2Txs to the l2DB
  246. addL2Txs(t, txsel, poolL2Txs)
  247. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
  248. coordIdxs, accAuths, oL1UserTxs, oL1CoordTxs, oL2Txs, err := txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  249. require.NoError(t, err)
  250. assert.Equal(t, []common.Idx{261, 262}, coordIdxs)
  251. assert.Equal(t, txsel.coordAccount.AccountCreationAuth, accAuths[0])
  252. assert.Equal(t, txsel.coordAccount.AccountCreationAuth, accAuths[1])
  253. assert.Equal(t, accAuthSig0, accAuths[2])
  254. assert.Equal(t, accAuthSig1, accAuths[3])
  255. assert.Equal(t, 1, len(oL1UserTxs))
  256. assert.Equal(t, 4, len(oL1CoordTxs))
  257. assert.Equal(t, 2, len(oL2Txs))
  258. assert.Equal(t, len(oL1CoordTxs), len(accAuths))
  259. assert.Equal(t, common.BatchNum(7), txsel.localAccountsDB.CurrentBatch())
  260. assert.Equal(t, common.Idx(264), txsel.localAccountsDB.CurrentIdx())
  261. checkBalanceByIdx(t, txsel, 261, "20") // CoordIdx for TokenID=1
  262. checkBalanceByIdx(t, txsel, 262, "10") // CoordIdx for TokenID=0
  263. checkBalance(t, tc, txsel, "A", 0, "600")
  264. checkBalance(t, tc, txsel, "A", 1, "280")
  265. checkBalance(t, tc, txsel, "B", 0, "290")
  266. checkBalance(t, tc, txsel, "B", 1, "200")
  267. checkBalance(t, tc, txsel, "C", 0, "100")
  268. checkBalance(t, tc, txsel, "D", 0, "800")
  269. checkSortedByNonce(t, testAccNonces, oL2Txs)
  270. err = txsel.l2db.StartForging(common.TxIDsFromPoolL2Txs(poolL2Txs), txsel.localAccountsDB.CurrentBatch())
  271. require.NoError(t, err)
  272. log.Debug("block:0 batch:7")
  273. // simulate the PoolL2Txs of the batch6
  274. batchPoolL2 = `
  275. Type: PoolL2
  276. PoolTransfer(0) A-B: 100 (126)
  277. PoolTransfer(0) C-A: 50 (126)
  278. PoolTransfer(1) B-C: 100 (126)
  279. PoolExit(0) A: 100 (126)`
  280. poolL2Txs, err = tc.GeneratePoolL2Txs(batchPoolL2)
  281. require.NoError(t, err)
  282. addL2Txs(t, txsel, poolL2Txs)
  283. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
  284. coordIdxs, accAuths, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  285. require.NoError(t, err)
  286. assert.Equal(t, []common.Idx{261, 262}, coordIdxs)
  287. assert.Equal(t, 0, len(accAuths))
  288. assert.Equal(t, 0, len(oL1UserTxs))
  289. assert.Equal(t, 0, len(oL1CoordTxs))
  290. assert.Equal(t, 4, len(oL2Txs))
  291. assert.Equal(t, len(oL1CoordTxs), len(accAuths))
  292. assert.Equal(t, common.BatchNum(8), txsel.localAccountsDB.CurrentBatch())
  293. assert.Equal(t, common.Idx(264), txsel.localAccountsDB.CurrentIdx())
  294. checkBalanceByIdx(t, txsel, 261, "30")
  295. checkBalanceByIdx(t, txsel, 262, "35")
  296. checkBalance(t, tc, txsel, "A", 0, "430")
  297. checkBalance(t, tc, txsel, "A", 1, "280")
  298. checkBalance(t, tc, txsel, "B", 0, "390")
  299. checkBalance(t, tc, txsel, "B", 1, "90")
  300. checkBalance(t, tc, txsel, "C", 0, "45")
  301. checkBalance(t, tc, txsel, "C", 1, "100")
  302. checkBalance(t, tc, txsel, "D", 0, "800")
  303. checkSortedByNonce(t, testAccNonces, oL2Txs)
  304. err = txsel.l2db.StartForging(common.TxIDsFromPoolL2Txs(poolL2Txs), txsel.localAccountsDB.CurrentBatch())
  305. require.NoError(t, err)
  306. log.Debug("block:1 batch:0")
  307. // simulate the PoolL2Txs of the batch6
  308. batchPoolL2 = `
  309. Type: PoolL2
  310. PoolTransfer(0) D-A: 300 (126)
  311. PoolTransfer(0) B-D: 100 (126)
  312. `
  313. poolL2Txs, err = tc.GeneratePoolL2Txs(batchPoolL2)
  314. require.NoError(t, err)
  315. addL2Txs(t, txsel, poolL2Txs)
  316. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
  317. coordIdxs, accAuths, oL1UserTxs, oL1CoordTxs, oL2Txs, err = txsel.GetL1L2TxSelection(selectionConfig, l1UserTxs)
  318. require.NoError(t, err)
  319. assert.Equal(t, []common.Idx{262}, coordIdxs)
  320. assert.Equal(t, 0, len(accAuths))
  321. assert.Equal(t, 4, len(oL1UserTxs))
  322. assert.Equal(t, 0, len(oL1CoordTxs))
  323. assert.Equal(t, 2, len(oL2Txs))
  324. assert.Equal(t, len(oL1CoordTxs), len(accAuths))
  325. assert.Equal(t, common.BatchNum(9), txsel.localAccountsDB.CurrentBatch())
  326. assert.Equal(t, common.Idx(264), txsel.localAccountsDB.CurrentIdx())
  327. checkBalanceByIdx(t, txsel, 261, "30")
  328. checkBalanceByIdx(t, txsel, 262, "75")
  329. checkBalance(t, tc, txsel, "A", 0, "730")
  330. checkBalance(t, tc, txsel, "A", 1, "280")
  331. checkBalance(t, tc, txsel, "B", 0, "380")
  332. checkBalance(t, tc, txsel, "B", 1, "90")
  333. checkBalance(t, tc, txsel, "C", 0, "845")
  334. checkBalance(t, tc, txsel, "C", 1, "100")
  335. checkBalance(t, tc, txsel, "D", 0, "470")
  336. checkSortedByNonce(t, testAccNonces, oL2Txs)
  337. err = txsel.l2db.StartForging(common.TxIDsFromPoolL2Txs(poolL2Txs), txsel.localAccountsDB.CurrentBatch())
  338. require.NoError(t, err)
  339. }