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.

832 lines
34 KiB

  1. package statedb
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math/big"
  8. "os"
  9. "testing"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/hermeznetwork/hermez-node/log"
  13. "github.com/hermeznetwork/hermez-node/test/til"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func checkBalance(t *testing.T, tc *til.Context, sdb *StateDB, username string, tokenid int, expected string) {
  18. idx := tc.Users[username].Accounts[common.TokenID(tokenid)].Idx
  19. acc, err := sdb.GetAccount(idx)
  20. require.Nil(t, err)
  21. assert.Equal(t, expected, acc.Balance.String())
  22. }
  23. func TestCheckL1TxInvalidData(t *testing.T) {
  24. dir, err := ioutil.TempDir("", "tmpdb")
  25. require.Nil(t, err)
  26. defer assert.Nil(t, os.RemoveAll(dir))
  27. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  28. assert.Nil(t, err)
  29. set := `
  30. Type: Blockchain
  31. AddToken(1)
  32. CreateAccountDeposit(0) A: 10
  33. CreateAccountDeposit(0) B: 10
  34. CreateAccountDeposit(1) C: 10
  35. > batchL1
  36. > batchL1
  37. > block
  38. `
  39. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  40. blocks, err := tc.GenerateBlocks(set)
  41. require.Nil(t, err)
  42. ptc := ProcessTxsConfig{
  43. NLevels: 32,
  44. MaxFeeTx: 64,
  45. MaxTx: 512,
  46. MaxL1Tx: 16,
  47. }
  48. _, err = sdb.ProcessTxs(ptc, nil, blocks[0].Rollup.L1UserTxs, nil, nil)
  49. require.Nil(t, err)
  50. tx := common.L1Tx{
  51. FromIdx: 256,
  52. ToIdx: 257,
  53. Amount: big.NewInt(10),
  54. LoadAmount: big.NewInt(0),
  55. FromEthAddr: tc.Users["A"].Addr,
  56. UserOrigin: true,
  57. }
  58. sdb.computeEffectiveAmounts(&tx)
  59. assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
  60. assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
  61. // expect error due not enough funds
  62. tx = common.L1Tx{
  63. FromIdx: 256,
  64. ToIdx: 257,
  65. Amount: big.NewInt(11),
  66. LoadAmount: big.NewInt(0),
  67. FromEthAddr: tc.Users["A"].Addr,
  68. UserOrigin: true,
  69. }
  70. sdb.computeEffectiveAmounts(&tx)
  71. assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
  72. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  73. // expect no-error due not enough funds in a
  74. // CreateAccountDepositTransfer transction
  75. tx = common.L1Tx{
  76. FromIdx: 0,
  77. ToIdx: 257,
  78. Amount: big.NewInt(10),
  79. LoadAmount: big.NewInt(10),
  80. UserOrigin: true,
  81. }
  82. sdb.computeEffectiveAmounts(&tx)
  83. assert.Equal(t, big.NewInt(10), tx.EffectiveLoadAmount)
  84. assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
  85. // expect error due not enough funds in a CreateAccountDepositTransfer
  86. // transction
  87. tx = common.L1Tx{
  88. FromIdx: 0,
  89. ToIdx: 257,
  90. Amount: big.NewInt(11),
  91. LoadAmount: big.NewInt(10),
  92. UserOrigin: true,
  93. }
  94. sdb.computeEffectiveAmounts(&tx)
  95. assert.Equal(t, big.NewInt(10), tx.EffectiveLoadAmount)
  96. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  97. // expect error due not same TokenID
  98. tx = common.L1Tx{
  99. FromIdx: 256,
  100. ToIdx: 258,
  101. Amount: big.NewInt(5),
  102. LoadAmount: big.NewInt(0),
  103. FromEthAddr: tc.Users["A"].Addr,
  104. UserOrigin: true,
  105. }
  106. sdb.computeEffectiveAmounts(&tx)
  107. assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
  108. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  109. // expect error due not same EthAddr
  110. tx = common.L1Tx{
  111. FromIdx: 256,
  112. ToIdx: 257,
  113. Amount: big.NewInt(8),
  114. LoadAmount: big.NewInt(0),
  115. FromEthAddr: tc.Users["B"].Addr,
  116. UserOrigin: true,
  117. }
  118. sdb.computeEffectiveAmounts(&tx)
  119. assert.Equal(t, big.NewInt(0), tx.EffectiveLoadAmount)
  120. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  121. }
  122. func TestProcessTxsBalances(t *testing.T) {
  123. dir, err := ioutil.TempDir("", "tmpdb")
  124. require.Nil(t, err)
  125. defer assert.Nil(t, os.RemoveAll(dir))
  126. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  127. assert.Nil(t, err)
  128. // generate test transactions from test.SetBlockchain0 code
  129. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  130. blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0)
  131. require.Nil(t, err)
  132. // Coordinator Idx where to send the fees
  133. coordIdxs := []common.Idx{256, 257}
  134. ptc := ProcessTxsConfig{
  135. NLevels: 32,
  136. MaxFeeTx: 64,
  137. MaxTx: 512,
  138. MaxL1Tx: 16,
  139. }
  140. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  141. _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  142. require.Nil(t, err)
  143. log.Debug("block:0 batch:1")
  144. l1UserTxs := []common.L1Tx{}
  145. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  146. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  147. require.Nil(t, err)
  148. log.Debug("block:0 batch:2")
  149. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum])
  150. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  151. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  152. require.Nil(t, err)
  153. checkBalance(t, tc, sdb, "A", 0, "500")
  154. log.Debug("block:0 batch:3")
  155. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum])
  156. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[3].L2Txs)
  157. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs)
  158. require.Nil(t, err)
  159. checkBalance(t, tc, sdb, "A", 0, "500")
  160. checkBalance(t, tc, sdb, "A", 1, "500")
  161. log.Debug("block:0 batch:4")
  162. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum])
  163. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[4].L2Txs)
  164. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs)
  165. require.Nil(t, err)
  166. checkBalance(t, tc, sdb, "A", 0, "500")
  167. checkBalance(t, tc, sdb, "A", 1, "500")
  168. log.Debug("block:0 batch:5")
  169. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum])
  170. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[5].L2Txs)
  171. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs)
  172. require.Nil(t, err)
  173. checkBalance(t, tc, sdb, "A", 0, "600")
  174. checkBalance(t, tc, sdb, "A", 1, "500")
  175. checkBalance(t, tc, sdb, "B", 0, "400")
  176. log.Debug("block:0 batch:6")
  177. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
  178. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[6].L2Txs)
  179. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[6].L1CoordinatorTxs, l2Txs)
  180. require.Nil(t, err)
  181. checkBalance(t, tc, sdb, "Coord", 0, "10")
  182. checkBalance(t, tc, sdb, "Coord", 1, "20")
  183. checkBalance(t, tc, sdb, "A", 0, "600")
  184. checkBalance(t, tc, sdb, "A", 1, "280")
  185. checkBalance(t, tc, sdb, "B", 0, "290")
  186. checkBalance(t, tc, sdb, "B", 1, "200")
  187. checkBalance(t, tc, sdb, "C", 0, "100")
  188. checkBalance(t, tc, sdb, "D", 0, "800")
  189. log.Debug("block:0 batch:7")
  190. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
  191. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[7].L2Txs)
  192. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[7].L1CoordinatorTxs, l2Txs)
  193. require.Nil(t, err)
  194. checkBalance(t, tc, sdb, "Coord", 0, "35")
  195. checkBalance(t, tc, sdb, "Coord", 1, "30")
  196. checkBalance(t, tc, sdb, "A", 0, "430")
  197. checkBalance(t, tc, sdb, "A", 1, "280")
  198. checkBalance(t, tc, sdb, "B", 0, "390")
  199. checkBalance(t, tc, sdb, "B", 1, "90")
  200. checkBalance(t, tc, sdb, "C", 0, "45")
  201. checkBalance(t, tc, sdb, "C", 1, "100")
  202. checkBalance(t, tc, sdb, "D", 0, "800")
  203. log.Debug("block:1 batch:0")
  204. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
  205. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  206. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  207. require.Nil(t, err)
  208. checkBalance(t, tc, sdb, "Coord", 0, "75")
  209. checkBalance(t, tc, sdb, "Coord", 1, "30")
  210. checkBalance(t, tc, sdb, "A", 0, "730")
  211. checkBalance(t, tc, sdb, "A", 1, "280")
  212. checkBalance(t, tc, sdb, "B", 0, "380")
  213. checkBalance(t, tc, sdb, "B", 1, "90")
  214. checkBalance(t, tc, sdb, "C", 0, "845")
  215. checkBalance(t, tc, sdb, "C", 1, "100")
  216. checkBalance(t, tc, sdb, "D", 0, "470")
  217. log.Debug("block:1 batch:1")
  218. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[1].Batch.ForgeL1TxsNum])
  219. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  220. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  221. require.Nil(t, err)
  222. // use Set of PoolL2 txs
  223. poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow1)
  224. assert.Nil(t, err)
  225. _, err = sdb.ProcessTxs(ptc, coordIdxs, []common.L1Tx{}, []common.L1Tx{}, poolL2Txs)
  226. require.Nil(t, err)
  227. checkBalance(t, tc, sdb, "Coord", 0, "105")
  228. checkBalance(t, tc, sdb, "Coord", 1, "40")
  229. checkBalance(t, tc, sdb, "A", 0, "510")
  230. checkBalance(t, tc, sdb, "A", 1, "170")
  231. checkBalance(t, tc, sdb, "B", 0, "480")
  232. checkBalance(t, tc, sdb, "B", 1, "190")
  233. checkBalance(t, tc, sdb, "C", 0, "845")
  234. checkBalance(t, tc, sdb, "C", 1, "100")
  235. checkBalance(t, tc, sdb, "D", 0, "360")
  236. checkBalance(t, tc, sdb, "F", 0, "100")
  237. }
  238. func TestProcessTxsSynchronizer(t *testing.T) {
  239. dir, err := ioutil.TempDir("", "tmpdb")
  240. require.Nil(t, err)
  241. defer assert.Nil(t, os.RemoveAll(dir))
  242. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  243. assert.Nil(t, err)
  244. // generate test transactions from test.SetBlockchain0 code
  245. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  246. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  247. require.Nil(t, err)
  248. assert.Equal(t, 31, len(blocks[0].Rollup.L1UserTxs))
  249. assert.Equal(t, 4, len(blocks[0].Rollup.Batches[0].L1CoordinatorTxs))
  250. assert.Equal(t, 0, len(blocks[0].Rollup.Batches[1].L1CoordinatorTxs))
  251. assert.Equal(t, 22, len(blocks[0].Rollup.Batches[2].L2Txs))
  252. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[0].L1CoordinatorTxs))
  253. assert.Equal(t, 62, len(blocks[1].Rollup.Batches[0].L2Txs))
  254. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[1].L1CoordinatorTxs))
  255. assert.Equal(t, 8, len(blocks[1].Rollup.Batches[1].L2Txs))
  256. // Coordinator Idx where to send the fees
  257. coordIdxs := []common.Idx{256, 257, 258, 259}
  258. // Idx of user 'A'
  259. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  260. ptc := ProcessTxsConfig{
  261. NLevels: 32,
  262. MaxFeeTx: 64,
  263. MaxTx: 512,
  264. MaxL1Tx: 16,
  265. }
  266. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  267. // to create the Coordinator accounts to receive the fees
  268. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  269. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  270. require.Nil(t, err)
  271. assert.Equal(t, 4, len(ptOut.CreatedAccounts))
  272. assert.Equal(t, 0, len(ptOut.CollectedFees))
  273. log.Debug("block:0 batch:1")
  274. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  275. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs,
  276. blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  277. require.Nil(t, err)
  278. assert.Equal(t, 0, len(ptOut.ExitInfos))
  279. assert.Equal(t, 31, len(ptOut.CreatedAccounts))
  280. assert.Equal(t, 4, len(ptOut.CollectedFees))
  281. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  282. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  283. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  284. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  285. acc, err := sdb.GetAccount(idxA1)
  286. require.Nil(t, err)
  287. assert.Equal(t, "50", acc.Balance.String())
  288. log.Debug("block:0 batch:2")
  289. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  290. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  291. require.Nil(t, err)
  292. assert.Equal(t, 0, len(ptOut.ExitInfos))
  293. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  294. assert.Equal(t, 4, len(ptOut.CollectedFees))
  295. assert.Equal(t, "2", ptOut.CollectedFees[common.TokenID(0)].String())
  296. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  297. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  298. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  299. acc, err = sdb.GetAccount(idxA1)
  300. require.Nil(t, err)
  301. assert.Equal(t, "35", acc.Balance.String())
  302. log.Debug("block:1 batch:0")
  303. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  304. // before processing expect l2Txs[0:2].Nonce==0
  305. assert.Equal(t, common.Nonce(0), l2Txs[0].Nonce)
  306. assert.Equal(t, common.Nonce(0), l2Txs[1].Nonce)
  307. assert.Equal(t, common.Nonce(0), l2Txs[2].Nonce)
  308. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  309. require.Nil(t, err)
  310. // after processing expect l2Txs[0:2].Nonce!=0 and has expected value
  311. assert.Equal(t, common.Nonce(6), l2Txs[0].Nonce)
  312. assert.Equal(t, common.Nonce(7), l2Txs[1].Nonce)
  313. assert.Equal(t, common.Nonce(8), l2Txs[2].Nonce)
  314. assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  315. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  316. assert.Equal(t, 4, len(ptOut.CollectedFees))
  317. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  318. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  319. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  320. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  321. acc, err = sdb.GetAccount(idxA1)
  322. require.Nil(t, err)
  323. assert.Equal(t, "57", acc.Balance.String())
  324. log.Debug("block:1 batch:1")
  325. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  326. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs,
  327. blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  328. require.Nil(t, err)
  329. assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  330. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  331. assert.Equal(t, 4, len(ptOut.CollectedFees))
  332. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  333. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  334. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  335. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  336. acc, err = sdb.GetAccount(idxA1)
  337. assert.Nil(t, err)
  338. assert.Equal(t, "77", acc.Balance.String())
  339. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  340. acc, err = sdb.GetAccount(idxB0)
  341. require.Nil(t, err)
  342. assert.Equal(t, "51", acc.Balance.String())
  343. // get balance of Coordinator account for TokenID==0
  344. acc, err = sdb.GetAccount(common.Idx(256))
  345. require.Nil(t, err)
  346. assert.Equal(t, "2", acc.Balance.String())
  347. }
  348. func TestProcessTxsBatchBuilder(t *testing.T) {
  349. dir, err := ioutil.TempDir("", "tmpdb")
  350. require.Nil(t, err)
  351. defer assert.Nil(t, os.RemoveAll(dir))
  352. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  353. assert.Nil(t, err)
  354. // generate test transactions from test.SetBlockchain0 code
  355. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  356. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  357. require.Nil(t, err)
  358. // Coordinator Idx where to send the fees
  359. coordIdxs := []common.Idx{256, 257, 258, 259}
  360. // Idx of user 'A'
  361. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  362. ptc := ProcessTxsConfig{
  363. NLevels: 32,
  364. MaxFeeTx: 64,
  365. MaxTx: 512,
  366. MaxL1Tx: 32,
  367. }
  368. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  369. // to create the Coordinator accounts to receive the fees
  370. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  371. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  372. require.Nil(t, err)
  373. // expect 0 at CreatedAccount, as is only computed when StateDB.Type==TypeSynchronizer
  374. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  375. log.Debug("block:0 batch:1")
  376. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  377. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  378. require.Nil(t, err)
  379. assert.Equal(t, 0, len(ptOut.ExitInfos))
  380. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  381. acc, err := sdb.GetAccount(idxA1)
  382. require.Nil(t, err)
  383. assert.Equal(t, "50", acc.Balance.String())
  384. log.Debug("block:0 batch:2")
  385. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  386. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  387. require.Nil(t, err)
  388. assert.Equal(t, 0, len(ptOut.ExitInfos))
  389. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  390. acc, err = sdb.GetAccount(idxA1)
  391. require.Nil(t, err)
  392. assert.Equal(t, "35", acc.Balance.String())
  393. log.Debug("block:1 batch:0")
  394. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  395. _, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  396. require.Nil(t, err)
  397. acc, err = sdb.GetAccount(idxA1)
  398. require.Nil(t, err)
  399. assert.Equal(t, "57", acc.Balance.String())
  400. log.Debug("block:1 batch:1")
  401. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  402. _, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  403. require.Nil(t, err)
  404. acc, err = sdb.GetAccount(idxA1)
  405. assert.Nil(t, err)
  406. assert.Equal(t, "77", acc.Balance.String())
  407. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  408. acc, err = sdb.GetAccount(idxB0)
  409. require.Nil(t, err)
  410. assert.Equal(t, "51", acc.Balance.String())
  411. // get balance of Coordinator account for TokenID==0
  412. acc, err = sdb.GetAccount(common.Idx(256))
  413. require.Nil(t, err)
  414. assert.Equal(t, common.TokenID(0), acc.TokenID)
  415. assert.Equal(t, "2", acc.Balance.String())
  416. acc, err = sdb.GetAccount(common.Idx(257))
  417. require.Nil(t, err)
  418. assert.Equal(t, common.TokenID(1), acc.TokenID)
  419. assert.Equal(t, "2", acc.Balance.String())
  420. }
  421. func TestZKInputsGeneration(t *testing.T) {
  422. dir, err := ioutil.TempDir("", "tmpdb")
  423. require.Nil(t, err)
  424. defer assert.Nil(t, os.RemoveAll(dir))
  425. sdb, err := NewStateDB(dir, TypeBatchBuilder, 4)
  426. assert.Nil(t, err)
  427. set := `
  428. Type: Blockchain
  429. AddToken(1)
  430. CreateAccountDeposit(1) A: 10
  431. > batchL1
  432. CreateAccountCoordinator(1) B
  433. CreateAccountCoordinator(1) C
  434. > batchL1
  435. // idxs: A:258, B:256, C:257
  436. Transfer(1) A-B: 6 (1)
  437. Transfer(1) A-C: 2 (1)
  438. > batch
  439. > block
  440. `
  441. // generate test transactions from test.SetBlockchain0 code
  442. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  443. blocks, err := tc.GenerateBlocks(set)
  444. require.Nil(t, err)
  445. // Coordinator Idx where to send the fees
  446. coordIdxs := []common.Idx{256}
  447. ptc := ProcessTxsConfig{
  448. NLevels: 32,
  449. MaxFeeTx: 8,
  450. MaxTx: 32,
  451. MaxL1Tx: 16,
  452. }
  453. log.Debug("block:0 batch:0, only L1UserTx")
  454. _, err = sdb.ProcessTxs(ptc, nil, blocks[0].Rollup.L1UserTxs, nil, nil)
  455. require.Nil(t, err)
  456. log.Debug("block:0 batch:1, only L1CoordinatorTxs")
  457. _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, nil)
  458. require.Nil(t, err)
  459. log.Debug("block:0 batch:2, only L2Txs")
  460. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  461. ptOut, err := sdb.ProcessTxs(ptc, coordIdxs, nil, nil, l2Txs)
  462. require.Nil(t, err)
  463. checkBalance(t, tc, sdb, "A", 1, "2")
  464. s, err := json.Marshal(ptOut.ZKInputs)
  465. require.Nil(t, err)
  466. debug := false
  467. debug = true
  468. if debug {
  469. fmt.Println(string(s))
  470. }
  471. }
  472. func TestProcessTxsRootTestVectors(t *testing.T) {
  473. dir, err := ioutil.TempDir("", "tmpdb")
  474. require.Nil(t, err)
  475. defer assert.Nil(t, os.RemoveAll(dir))
  476. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  477. assert.Nil(t, err)
  478. // same values than in the js test
  479. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  480. assert.Nil(t, err)
  481. l1Txs := []common.L1Tx{
  482. {
  483. FromIdx: 0,
  484. LoadAmount: big.NewInt(16000000),
  485. Amount: big.NewInt(0),
  486. TokenID: 1,
  487. FromBJJ: bjj0,
  488. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  489. ToIdx: 0,
  490. Type: common.TxTypeCreateAccountDeposit,
  491. UserOrigin: true,
  492. },
  493. }
  494. l2Txs := []common.PoolL2Tx{
  495. {
  496. FromIdx: 256,
  497. ToIdx: 256,
  498. TokenID: 1,
  499. Amount: big.NewInt(1000),
  500. Nonce: 0,
  501. Fee: 126,
  502. Type: common.TxTypeTransfer,
  503. },
  504. }
  505. ptc := ProcessTxsConfig{
  506. NLevels: 32,
  507. MaxFeeTx: 8,
  508. MaxTx: 32,
  509. MaxL1Tx: 16,
  510. }
  511. _, err = sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  512. require.Nil(t, err)
  513. assert.Equal(t, "9827704113668630072730115158977131501210702363656902211840117643154933433410", sdb.mt.Root().BigInt().String())
  514. }
  515. func TestCircomTest(t *testing.T) {
  516. dir, err := ioutil.TempDir("", "tmpdb")
  517. require.Nil(t, err)
  518. defer assert.Nil(t, os.RemoveAll(dir))
  519. sdb, err := NewStateDB(dir, TypeBatchBuilder, 8)
  520. assert.Nil(t, err)
  521. // same values than in the js test
  522. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  523. assert.Nil(t, err)
  524. l1Txs := []common.L1Tx{
  525. {
  526. FromIdx: 0,
  527. // LoadAmount: big.NewInt(10400),
  528. LoadAmount: big.NewInt(16000000),
  529. Amount: big.NewInt(0),
  530. TokenID: 1,
  531. FromBJJ: bjj0,
  532. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  533. ToIdx: 0,
  534. Type: common.TxTypeCreateAccountDeposit,
  535. UserOrigin: true,
  536. },
  537. }
  538. l2Txs := []common.PoolL2Tx{
  539. {
  540. FromIdx: 256,
  541. ToIdx: 256,
  542. TokenID: 1,
  543. Amount: big.NewInt(1000),
  544. Nonce: 0,
  545. Fee: 126,
  546. Type: common.TxTypeTransfer,
  547. },
  548. }
  549. ptc := ProcessTxsConfig{
  550. NLevels: 8,
  551. MaxFeeTx: 2,
  552. MaxTx: 5,
  553. MaxL1Tx: 2,
  554. }
  555. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  556. require.Nil(t, err)
  557. // check expected account keys values from tx inputs
  558. acc, err := sdb.GetAccount(common.Idx(256))
  559. require.Nil(t, err)
  560. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  561. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  562. // check that there no exist more accounts
  563. _, err = sdb.GetAccount(common.Idx(257))
  564. require.NotNil(t, err)
  565. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(256).BigInt()
  566. s, err := json.Marshal(ptOut.ZKInputs)
  567. require.Nil(t, err)
  568. debug := false
  569. if debug {
  570. fmt.Println("\nCopy&Paste into js circom test:\n let zkInput = JSON.parse(`" + string(s) + "`);")
  571. h, err := ptOut.ZKInputs.HashGlobalData()
  572. require.Nil(t, err)
  573. fmt.Printf(`
  574. const output={
  575. hashGlobalInputs: "%s",
  576. };
  577. await circuit.assertOut(w, output);
  578. `, h.String())
  579. fmt.Println("")
  580. }
  581. }
  582. func TestZKInputsHashTestVector0(t *testing.T) {
  583. dir, err := ioutil.TempDir("", "tmpdb")
  584. require.Nil(t, err)
  585. defer assert.Nil(t, os.RemoveAll(dir))
  586. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  587. assert.Nil(t, err)
  588. // same values than in the js test
  589. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  590. assert.Nil(t, err)
  591. l1Txs := []common.L1Tx{
  592. {
  593. FromIdx: 0,
  594. LoadAmount: big.NewInt(16000000),
  595. Amount: big.NewInt(0),
  596. TokenID: 1,
  597. FromBJJ: bjj0,
  598. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  599. ToIdx: 0,
  600. Type: common.TxTypeCreateAccountDeposit,
  601. UserOrigin: true,
  602. },
  603. }
  604. l2Txs := []common.PoolL2Tx{
  605. {
  606. FromIdx: 256,
  607. ToIdx: 256,
  608. TokenID: 1,
  609. Amount: big.NewInt(1000),
  610. Nonce: 0,
  611. Fee: 126,
  612. Type: common.TxTypeTransfer,
  613. },
  614. }
  615. ptc := ProcessTxsConfig{
  616. NLevels: 32,
  617. MaxFeeTx: 8,
  618. MaxTx: 32,
  619. MaxL1Tx: 16,
  620. }
  621. // skip first batch to do the test with BatchNum=1
  622. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  623. require.Nil(t, err)
  624. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  625. require.Nil(t, err)
  626. // check expected account keys values from tx inputs
  627. acc, err := sdb.GetAccount(common.Idx(256))
  628. require.Nil(t, err)
  629. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  630. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  631. // check that there no exist more accounts
  632. _, err = sdb.GetAccount(common.Idx(257))
  633. require.NotNil(t, err)
  634. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(256).BigInt()
  635. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  636. assert.Nil(t, err)
  637. // value from js test vector
  638. expectedToHash := "0000000000ff000000000100000000000000000000000000000000000000000000000000000000000000000015ba488d749f6b891d29d0bf3a72481ec812e4d4ecef2bf7a3fc64f3c010444200000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010003e87e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001"
  639. // checks are splitted to find the difference easier
  640. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  641. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  642. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  643. h, err := ptOut.ZKInputs.HashGlobalData()
  644. require.Nil(t, err)
  645. // value from js test vector
  646. assert.Equal(t, "4356692423721763303547321618014315464040324829724049399065961225345730555597", h.String())
  647. }
  648. func TestZKInputsHashTestVector1(t *testing.T) {
  649. dir, err := ioutil.TempDir("", "tmpdb")
  650. require.Nil(t, err)
  651. defer assert.Nil(t, os.RemoveAll(dir))
  652. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  653. assert.Nil(t, err)
  654. // same values than in the js test
  655. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  656. assert.Nil(t, err)
  657. bjj1, err := common.BJJFromStringWithChecksum("093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d")
  658. assert.Nil(t, err)
  659. l1Txs := []common.L1Tx{
  660. {
  661. FromIdx: 0,
  662. // LoadAmount: big.NewInt(10400),
  663. LoadAmount: big.NewInt(16000000),
  664. Amount: big.NewInt(0),
  665. TokenID: 1,
  666. FromBJJ: bjj0,
  667. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  668. ToIdx: 0,
  669. Type: common.TxTypeCreateAccountDeposit,
  670. UserOrigin: true,
  671. },
  672. {
  673. FromIdx: 0,
  674. LoadAmount: big.NewInt(16000000),
  675. Amount: big.NewInt(0),
  676. TokenID: 1,
  677. FromBJJ: bjj1,
  678. FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
  679. ToIdx: 0,
  680. Type: common.TxTypeCreateAccountDeposit,
  681. UserOrigin: true,
  682. },
  683. }
  684. l2Txs := []common.PoolL2Tx{
  685. {
  686. FromIdx: 257,
  687. ToIdx: 256,
  688. TokenID: 1,
  689. Amount: big.NewInt(1000),
  690. Nonce: 0,
  691. Fee: 137,
  692. Type: common.TxTypeTransfer,
  693. },
  694. }
  695. ptc := ProcessTxsConfig{
  696. NLevels: 32,
  697. MaxFeeTx: 8,
  698. MaxTx: 32,
  699. MaxL1Tx: 16,
  700. }
  701. // skip first batch to do the test with BatchNum=1
  702. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  703. require.Nil(t, err)
  704. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  705. require.Nil(t, err)
  706. // check expected account keys values from tx inputs
  707. acc, err := sdb.GetAccount(common.Idx(256))
  708. require.Nil(t, err)
  709. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  710. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  711. acc, err = sdb.GetAccount(common.Idx(257))
  712. require.Nil(t, err)
  713. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", acc.PublicKey.Compress().String())
  714. assert.Equal(t, "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF", acc.EthAddr.Hex())
  715. // check that there no exist more accounts
  716. _, err = sdb.GetAccount(common.Idx(258))
  717. require.NotNil(t, err)
  718. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(257).BigInt()
  719. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  720. assert.Nil(t, err)
  721. // value from js test vector
  722. expectedToHash := "0000000000ff0000000001010000000000000000000000000000000000000000000000000000000000000000304a3f3aef4f416cca887aab7265227449077627138345c2eb25bf8ff946b09500000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00000000000028a000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000010003e889000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000000001"
  723. // checks are splitted to find the difference easier
  724. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  725. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  726. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  727. h, err := ptOut.ZKInputs.HashGlobalData()
  728. require.Nil(t, err)
  729. // value from js test vector
  730. assert.Equal(t, "20293112365009290386650039345314592436395562810005523677125576447132206192598", h.String())
  731. }