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.

595 lines
20 KiB

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