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.

525 lines
19 KiB

  1. package statedb
  2. import (
  3. "io/ioutil"
  4. "math/big"
  5. "os"
  6. "testing"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/log"
  10. "github.com/hermeznetwork/hermez-node/test/til"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func checkBalance(t *testing.T, tc *til.Context, sdb *StateDB, username string, tokenid int, expected string) {
  15. idx := tc.Users[username].Accounts[common.TokenID(tokenid)].Idx
  16. acc, err := sdb.GetAccount(idx)
  17. require.Nil(t, err)
  18. assert.Equal(t, expected, acc.Balance.String())
  19. }
  20. func TestCheckL1TxInvalidData(t *testing.T) {
  21. dir, err := ioutil.TempDir("", "tmpdb")
  22. require.Nil(t, err)
  23. defer assert.Nil(t, os.RemoveAll(dir))
  24. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  25. assert.Nil(t, err)
  26. set := `
  27. Type: Blockchain
  28. AddToken(1)
  29. CreateAccountDeposit(0) A: 10
  30. CreateAccountDeposit(0) B: 10
  31. CreateAccountDeposit(1) C: 10
  32. > batchL1
  33. > batchL1
  34. > block
  35. `
  36. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  37. blocks, err := tc.GenerateBlocks(set)
  38. require.Nil(t, err)
  39. ptc := ProcessTxsConfig{
  40. NLevels: 32,
  41. MaxFeeTx: 64,
  42. MaxTx: 512,
  43. MaxL1Tx: 16,
  44. }
  45. _, err = sdb.ProcessTxs(ptc, nil, blocks[0].Rollup.L1UserTxs, nil, nil)
  46. require.Nil(t, err)
  47. tx := common.L1Tx{
  48. FromIdx: 256,
  49. ToIdx: 257,
  50. Amount: big.NewInt(10),
  51. DepositAmount: big.NewInt(0),
  52. FromEthAddr: tc.Users["A"].Addr,
  53. UserOrigin: true,
  54. }
  55. sdb.computeEffectiveAmounts(&tx)
  56. assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
  57. assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
  58. // expect error due not enough funds
  59. tx = common.L1Tx{
  60. FromIdx: 256,
  61. ToIdx: 257,
  62. Amount: big.NewInt(11),
  63. DepositAmount: big.NewInt(0),
  64. FromEthAddr: tc.Users["A"].Addr,
  65. UserOrigin: true,
  66. }
  67. sdb.computeEffectiveAmounts(&tx)
  68. assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
  69. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  70. // expect no-error due not enough funds in a
  71. // CreateAccountDepositTransfer transction
  72. tx = common.L1Tx{
  73. FromIdx: 0,
  74. ToIdx: 257,
  75. Amount: big.NewInt(10),
  76. DepositAmount: big.NewInt(10),
  77. UserOrigin: true,
  78. }
  79. sdb.computeEffectiveAmounts(&tx)
  80. assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
  81. assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
  82. // expect error due not enough funds in a CreateAccountDepositTransfer
  83. // transction
  84. tx = common.L1Tx{
  85. FromIdx: 0,
  86. ToIdx: 257,
  87. Amount: big.NewInt(11),
  88. DepositAmount: big.NewInt(10),
  89. UserOrigin: true,
  90. }
  91. sdb.computeEffectiveAmounts(&tx)
  92. assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
  93. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  94. // expect error due not same TokenID
  95. tx = common.L1Tx{
  96. FromIdx: 256,
  97. ToIdx: 258,
  98. Amount: big.NewInt(5),
  99. DepositAmount: big.NewInt(0),
  100. FromEthAddr: tc.Users["A"].Addr,
  101. UserOrigin: true,
  102. }
  103. sdb.computeEffectiveAmounts(&tx)
  104. assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
  105. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  106. // expect error due not same EthAddr
  107. tx = common.L1Tx{
  108. FromIdx: 256,
  109. ToIdx: 257,
  110. Amount: big.NewInt(8),
  111. DepositAmount: big.NewInt(0),
  112. FromEthAddr: tc.Users["B"].Addr,
  113. UserOrigin: true,
  114. }
  115. sdb.computeEffectiveAmounts(&tx)
  116. assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
  117. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  118. }
  119. func TestProcessTxsBalances(t *testing.T) {
  120. dir, err := ioutil.TempDir("", "tmpdb")
  121. require.Nil(t, err)
  122. defer assert.Nil(t, os.RemoveAll(dir))
  123. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  124. assert.Nil(t, err)
  125. // generate test transactions from test.SetBlockchain0 code
  126. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  127. blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0)
  128. require.Nil(t, err)
  129. // Coordinator Idx where to send the fees
  130. coordIdxs := []common.Idx{256, 257}
  131. ptc := ProcessTxsConfig{
  132. NLevels: 32,
  133. MaxFeeTx: 64,
  134. MaxTx: 512,
  135. MaxL1Tx: 16,
  136. }
  137. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  138. _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  139. require.Nil(t, err)
  140. log.Debug("block:0 batch:1")
  141. l1UserTxs := []common.L1Tx{}
  142. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  143. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  144. require.Nil(t, err)
  145. log.Debug("block:0 batch:2")
  146. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum])
  147. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  148. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  149. require.Nil(t, err)
  150. checkBalance(t, tc, sdb, "A", 0, "500")
  151. log.Debug("block:0 batch:3")
  152. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum])
  153. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[3].L2Txs)
  154. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs)
  155. require.Nil(t, err)
  156. checkBalance(t, tc, sdb, "A", 0, "500")
  157. checkBalance(t, tc, sdb, "A", 1, "500")
  158. log.Debug("block:0 batch:4")
  159. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum])
  160. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[4].L2Txs)
  161. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs)
  162. require.Nil(t, err)
  163. checkBalance(t, tc, sdb, "A", 0, "500")
  164. checkBalance(t, tc, sdb, "A", 1, "500")
  165. log.Debug("block:0 batch:5")
  166. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum])
  167. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[5].L2Txs)
  168. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs)
  169. require.Nil(t, err)
  170. checkBalance(t, tc, sdb, "A", 0, "600")
  171. checkBalance(t, tc, sdb, "A", 1, "500")
  172. checkBalance(t, tc, sdb, "B", 0, "400")
  173. log.Debug("block:0 batch:6")
  174. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
  175. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[6].L2Txs)
  176. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[6].L1CoordinatorTxs, l2Txs)
  177. require.Nil(t, err)
  178. checkBalance(t, tc, sdb, "Coord", 0, "10")
  179. checkBalance(t, tc, sdb, "Coord", 1, "20")
  180. checkBalance(t, tc, sdb, "A", 0, "600")
  181. checkBalance(t, tc, sdb, "A", 1, "280")
  182. checkBalance(t, tc, sdb, "B", 0, "290")
  183. checkBalance(t, tc, sdb, "B", 1, "200")
  184. checkBalance(t, tc, sdb, "C", 0, "100")
  185. checkBalance(t, tc, sdb, "D", 0, "800")
  186. log.Debug("block:0 batch:7")
  187. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
  188. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[7].L2Txs)
  189. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[7].L1CoordinatorTxs, l2Txs)
  190. require.Nil(t, err)
  191. checkBalance(t, tc, sdb, "Coord", 0, "35")
  192. checkBalance(t, tc, sdb, "Coord", 1, "30")
  193. checkBalance(t, tc, sdb, "A", 0, "430")
  194. checkBalance(t, tc, sdb, "A", 1, "280")
  195. checkBalance(t, tc, sdb, "B", 0, "390")
  196. checkBalance(t, tc, sdb, "B", 1, "90")
  197. checkBalance(t, tc, sdb, "C", 0, "45")
  198. checkBalance(t, tc, sdb, "C", 1, "100")
  199. checkBalance(t, tc, sdb, "D", 0, "800")
  200. log.Debug("block:1 batch:0")
  201. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
  202. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  203. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  204. require.Nil(t, err)
  205. checkBalance(t, tc, sdb, "Coord", 0, "75")
  206. checkBalance(t, tc, sdb, "Coord", 1, "30")
  207. checkBalance(t, tc, sdb, "A", 0, "730")
  208. checkBalance(t, tc, sdb, "A", 1, "280")
  209. checkBalance(t, tc, sdb, "B", 0, "380")
  210. checkBalance(t, tc, sdb, "B", 1, "90")
  211. checkBalance(t, tc, sdb, "C", 0, "845")
  212. checkBalance(t, tc, sdb, "C", 1, "100")
  213. checkBalance(t, tc, sdb, "D", 0, "470")
  214. log.Debug("block:1 batch:1")
  215. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[1].Batch.ForgeL1TxsNum])
  216. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  217. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  218. require.Nil(t, err)
  219. // use Set of PoolL2 txs
  220. poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow1)
  221. assert.Nil(t, err)
  222. _, err = sdb.ProcessTxs(ptc, coordIdxs, []common.L1Tx{}, []common.L1Tx{}, poolL2Txs)
  223. require.Nil(t, err)
  224. checkBalance(t, tc, sdb, "Coord", 0, "105")
  225. checkBalance(t, tc, sdb, "Coord", 1, "40")
  226. checkBalance(t, tc, sdb, "A", 0, "510")
  227. checkBalance(t, tc, sdb, "A", 1, "170")
  228. checkBalance(t, tc, sdb, "B", 0, "480")
  229. checkBalance(t, tc, sdb, "B", 1, "190")
  230. checkBalance(t, tc, sdb, "C", 0, "845")
  231. checkBalance(t, tc, sdb, "C", 1, "100")
  232. checkBalance(t, tc, sdb, "D", 0, "360")
  233. checkBalance(t, tc, sdb, "F", 0, "100")
  234. }
  235. func TestProcessTxsSynchronizer(t *testing.T) {
  236. dir, err := ioutil.TempDir("", "tmpdb")
  237. require.Nil(t, err)
  238. defer assert.Nil(t, os.RemoveAll(dir))
  239. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  240. assert.Nil(t, err)
  241. // generate test transactions from test.SetBlockchain0 code
  242. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  243. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  244. require.Nil(t, err)
  245. assert.Equal(t, 31, len(blocks[0].Rollup.L1UserTxs))
  246. assert.Equal(t, 4, len(blocks[0].Rollup.Batches[0].L1CoordinatorTxs))
  247. assert.Equal(t, 0, len(blocks[0].Rollup.Batches[1].L1CoordinatorTxs))
  248. assert.Equal(t, 22, len(blocks[0].Rollup.Batches[2].L2Txs))
  249. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[0].L1CoordinatorTxs))
  250. assert.Equal(t, 62, len(blocks[1].Rollup.Batches[0].L2Txs))
  251. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[1].L1CoordinatorTxs))
  252. assert.Equal(t, 8, len(blocks[1].Rollup.Batches[1].L2Txs))
  253. // Coordinator Idx where to send the fees
  254. coordIdxs := []common.Idx{256, 257, 258, 259}
  255. // Idx of user 'A'
  256. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  257. ptc := ProcessTxsConfig{
  258. NLevels: 32,
  259. MaxFeeTx: 64,
  260. MaxTx: 512,
  261. MaxL1Tx: 32,
  262. }
  263. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  264. // to create the Coordinator accounts to receive the fees
  265. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  266. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  267. require.Nil(t, err)
  268. assert.Equal(t, 4, len(ptOut.CreatedAccounts))
  269. assert.Equal(t, 0, len(ptOut.CollectedFees))
  270. log.Debug("block:0 batch:1")
  271. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  272. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs,
  273. blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  274. require.Nil(t, err)
  275. assert.Equal(t, 0, len(ptOut.ExitInfos))
  276. assert.Equal(t, 31, len(ptOut.CreatedAccounts))
  277. assert.Equal(t, 4, len(ptOut.CollectedFees))
  278. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  279. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  280. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  281. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  282. acc, err := sdb.GetAccount(idxA1)
  283. require.Nil(t, err)
  284. assert.Equal(t, "50", acc.Balance.String())
  285. log.Debug("block:0 batch:2")
  286. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  287. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  288. require.Nil(t, err)
  289. assert.Equal(t, 0, len(ptOut.ExitInfos))
  290. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  291. assert.Equal(t, 4, len(ptOut.CollectedFees))
  292. assert.Equal(t, "2", ptOut.CollectedFees[common.TokenID(0)].String())
  293. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  294. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  295. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  296. acc, err = sdb.GetAccount(idxA1)
  297. require.Nil(t, err)
  298. assert.Equal(t, "35", acc.Balance.String())
  299. log.Debug("block:1 batch:0")
  300. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  301. // before processing expect l2Txs[0:2].Nonce==0
  302. assert.Equal(t, common.Nonce(0), l2Txs[0].Nonce)
  303. assert.Equal(t, common.Nonce(0), l2Txs[1].Nonce)
  304. assert.Equal(t, common.Nonce(0), l2Txs[2].Nonce)
  305. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  306. require.Nil(t, err)
  307. // after processing expect l2Txs[0:2].Nonce!=0 and has expected value
  308. assert.Equal(t, common.Nonce(6), l2Txs[0].Nonce)
  309. assert.Equal(t, common.Nonce(7), l2Txs[1].Nonce)
  310. assert.Equal(t, common.Nonce(8), l2Txs[2].Nonce)
  311. assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  312. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  313. assert.Equal(t, 4, len(ptOut.CollectedFees))
  314. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  315. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  316. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  317. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  318. acc, err = sdb.GetAccount(idxA1)
  319. require.Nil(t, err)
  320. assert.Equal(t, "57", acc.Balance.String())
  321. log.Debug("block:1 batch:1")
  322. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  323. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs,
  324. blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  325. require.Nil(t, err)
  326. assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  327. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  328. assert.Equal(t, 4, len(ptOut.CollectedFees))
  329. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  330. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  331. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  332. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  333. acc, err = sdb.GetAccount(idxA1)
  334. assert.Nil(t, err)
  335. assert.Equal(t, "77", acc.Balance.String())
  336. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  337. acc, err = sdb.GetAccount(idxB0)
  338. require.Nil(t, err)
  339. assert.Equal(t, "51", acc.Balance.String())
  340. // get balance of Coordinator account for TokenID==0
  341. acc, err = sdb.GetAccount(common.Idx(256))
  342. require.Nil(t, err)
  343. assert.Equal(t, "2", acc.Balance.String())
  344. }
  345. func TestProcessTxsBatchBuilder(t *testing.T) {
  346. dir, err := ioutil.TempDir("", "tmpdb")
  347. require.Nil(t, err)
  348. defer assert.Nil(t, os.RemoveAll(dir))
  349. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  350. assert.Nil(t, err)
  351. // generate test transactions from test.SetBlockchain0 code
  352. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  353. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  354. require.Nil(t, err)
  355. // Coordinator Idx where to send the fees
  356. coordIdxs := []common.Idx{256, 257, 258, 259}
  357. // Idx of user 'A'
  358. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  359. ptc := ProcessTxsConfig{
  360. NLevels: 32,
  361. MaxFeeTx: 64,
  362. MaxTx: 512,
  363. MaxL1Tx: 32,
  364. }
  365. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  366. // to create the Coordinator accounts to receive the fees
  367. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  368. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  369. require.Nil(t, err)
  370. // expect 0 at CreatedAccount, as is only computed when StateDB.Type==TypeSynchronizer
  371. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  372. log.Debug("block:0 batch:1")
  373. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  374. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  375. require.Nil(t, err)
  376. assert.Equal(t, 0, len(ptOut.ExitInfos))
  377. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  378. acc, err := sdb.GetAccount(idxA1)
  379. require.Nil(t, err)
  380. assert.Equal(t, "50", acc.Balance.String())
  381. log.Debug("block:0 batch:2")
  382. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  383. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  384. require.Nil(t, err)
  385. assert.Equal(t, 0, len(ptOut.ExitInfos))
  386. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  387. acc, err = sdb.GetAccount(idxA1)
  388. require.Nil(t, err)
  389. assert.Equal(t, "35", acc.Balance.String())
  390. log.Debug("block:1 batch:0")
  391. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  392. _, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  393. require.Nil(t, err)
  394. acc, err = sdb.GetAccount(idxA1)
  395. require.Nil(t, err)
  396. assert.Equal(t, "57", acc.Balance.String())
  397. log.Debug("block:1 batch:1")
  398. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  399. _, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  400. require.Nil(t, err)
  401. acc, err = sdb.GetAccount(idxA1)
  402. assert.Nil(t, err)
  403. assert.Equal(t, "77", acc.Balance.String())
  404. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  405. acc, err = sdb.GetAccount(idxB0)
  406. require.Nil(t, err)
  407. assert.Equal(t, "51", acc.Balance.String())
  408. // get balance of Coordinator account for TokenID==0
  409. acc, err = sdb.GetAccount(common.Idx(256))
  410. require.Nil(t, err)
  411. assert.Equal(t, common.TokenID(0), acc.TokenID)
  412. assert.Equal(t, "2", acc.Balance.String())
  413. acc, err = sdb.GetAccount(common.Idx(257))
  414. require.Nil(t, err)
  415. assert.Equal(t, common.TokenID(1), acc.TokenID)
  416. assert.Equal(t, "2", acc.Balance.String())
  417. }
  418. func TestProcessTxsRootTestVectors(t *testing.T) {
  419. dir, err := ioutil.TempDir("", "tmpdb")
  420. require.Nil(t, err)
  421. defer assert.Nil(t, os.RemoveAll(dir))
  422. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  423. assert.Nil(t, err)
  424. // same values than in the js test
  425. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  426. assert.Nil(t, err)
  427. l1Txs := []common.L1Tx{
  428. {
  429. FromIdx: 0,
  430. DepositAmount: big.NewInt(16000000),
  431. Amount: big.NewInt(0),
  432. TokenID: 1,
  433. FromBJJ: bjj0,
  434. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  435. ToIdx: 0,
  436. Type: common.TxTypeCreateAccountDeposit,
  437. UserOrigin: true,
  438. },
  439. }
  440. l2Txs := []common.PoolL2Tx{
  441. {
  442. FromIdx: 256,
  443. ToIdx: 256,
  444. TokenID: 1,
  445. Amount: big.NewInt(1000),
  446. Nonce: 0,
  447. Fee: 126,
  448. Type: common.TxTypeTransfer,
  449. },
  450. }
  451. ptc := ProcessTxsConfig{
  452. NLevels: 32,
  453. MaxFeeTx: 8,
  454. MaxTx: 32,
  455. MaxL1Tx: 16,
  456. }
  457. _, err = sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  458. require.Nil(t, err)
  459. assert.Equal(t, "9827704113668630072730115158977131501210702363656902211840117643154933433410", sdb.mt.Root().BigInt().String())
  460. }