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.

636 lines
29 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 TestProcessTxsBalances(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. // generate test transactions from test.SetBlockchain0 code
  30. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  31. blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0)
  32. require.Nil(t, err)
  33. // Coordinator Idx where to send the fees
  34. coordIdxs := []common.Idx{256, 257}
  35. ptc := ProcessTxsConfig{
  36. NLevels: 32,
  37. MaxFeeTx: 64,
  38. MaxTx: 512,
  39. MaxL1Tx: 16,
  40. }
  41. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  42. _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  43. require.Nil(t, err)
  44. log.Debug("block:0 batch:1")
  45. l1UserTxs := []common.L1Tx{}
  46. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  47. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  48. require.Nil(t, err)
  49. log.Debug("block:0 batch:2")
  50. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum])
  51. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  52. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  53. require.Nil(t, err)
  54. checkBalance(t, tc, sdb, "A", 0, "500")
  55. log.Debug("block:0 batch:3")
  56. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum])
  57. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[3].L2Txs)
  58. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs)
  59. require.Nil(t, err)
  60. checkBalance(t, tc, sdb, "A", 0, "500")
  61. checkBalance(t, tc, sdb, "A", 1, "500")
  62. log.Debug("block:0 batch:4")
  63. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum])
  64. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[4].L2Txs)
  65. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs)
  66. require.Nil(t, err)
  67. checkBalance(t, tc, sdb, "A", 0, "500")
  68. checkBalance(t, tc, sdb, "A", 1, "500")
  69. log.Debug("block:0 batch:5")
  70. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum])
  71. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[5].L2Txs)
  72. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs)
  73. require.Nil(t, err)
  74. checkBalance(t, tc, sdb, "A", 0, "600")
  75. checkBalance(t, tc, sdb, "A", 1, "500")
  76. checkBalance(t, tc, sdb, "B", 0, "400")
  77. log.Debug("block:0 batch:6")
  78. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
  79. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[6].L2Txs)
  80. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[6].L1CoordinatorTxs, l2Txs)
  81. require.Nil(t, err)
  82. checkBalance(t, tc, sdb, "Coord", 0, "10")
  83. checkBalance(t, tc, sdb, "Coord", 1, "20")
  84. checkBalance(t, tc, sdb, "A", 0, "600")
  85. checkBalance(t, tc, sdb, "A", 1, "280")
  86. checkBalance(t, tc, sdb, "B", 0, "290")
  87. checkBalance(t, tc, sdb, "B", 1, "200")
  88. checkBalance(t, tc, sdb, "C", 0, "100")
  89. checkBalance(t, tc, sdb, "D", 0, "800")
  90. log.Debug("block:0 batch:7")
  91. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
  92. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[7].L2Txs)
  93. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[7].L1CoordinatorTxs, l2Txs)
  94. require.Nil(t, err)
  95. checkBalance(t, tc, sdb, "Coord", 0, "35")
  96. checkBalance(t, tc, sdb, "Coord", 1, "30")
  97. checkBalance(t, tc, sdb, "A", 0, "430")
  98. checkBalance(t, tc, sdb, "A", 1, "280")
  99. checkBalance(t, tc, sdb, "B", 0, "390")
  100. checkBalance(t, tc, sdb, "B", 1, "90")
  101. checkBalance(t, tc, sdb, "C", 0, "45")
  102. checkBalance(t, tc, sdb, "C", 1, "100")
  103. checkBalance(t, tc, sdb, "D", 0, "800")
  104. log.Debug("block:1 batch:0")
  105. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
  106. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  107. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  108. require.Nil(t, err)
  109. checkBalance(t, tc, sdb, "Coord", 0, "75")
  110. checkBalance(t, tc, sdb, "Coord", 1, "30")
  111. checkBalance(t, tc, sdb, "A", 0, "730")
  112. checkBalance(t, tc, sdb, "A", 1, "280")
  113. checkBalance(t, tc, sdb, "B", 0, "380")
  114. checkBalance(t, tc, sdb, "B", 1, "90")
  115. checkBalance(t, tc, sdb, "C", 0, "845")
  116. checkBalance(t, tc, sdb, "C", 1, "100")
  117. checkBalance(t, tc, sdb, "D", 0, "470")
  118. log.Debug("block:1 batch:1")
  119. l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[1].Batch.ForgeL1TxsNum])
  120. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  121. _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  122. require.Nil(t, err)
  123. // use Set of PoolL2 txs
  124. poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow0)
  125. assert.Nil(t, err)
  126. _, err = sdb.ProcessTxs(ptc, coordIdxs, []common.L1Tx{}, []common.L1Tx{}, poolL2Txs)
  127. require.Nil(t, err)
  128. checkBalance(t, tc, sdb, "Coord", 0, "105")
  129. checkBalance(t, tc, sdb, "Coord", 1, "40")
  130. checkBalance(t, tc, sdb, "A", 0, "510")
  131. checkBalance(t, tc, sdb, "A", 1, "170")
  132. checkBalance(t, tc, sdb, "B", 0, "480")
  133. checkBalance(t, tc, sdb, "B", 1, "190")
  134. checkBalance(t, tc, sdb, "C", 0, "845")
  135. checkBalance(t, tc, sdb, "C", 1, "100")
  136. checkBalance(t, tc, sdb, "D", 0, "360")
  137. checkBalance(t, tc, sdb, "F", 0, "100")
  138. }
  139. func TestProcessTxsSynchronizer(t *testing.T) {
  140. dir, err := ioutil.TempDir("", "tmpdb")
  141. require.Nil(t, err)
  142. defer assert.Nil(t, os.RemoveAll(dir))
  143. sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
  144. assert.Nil(t, err)
  145. // generate test transactions from test.SetBlockchain0 code
  146. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  147. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  148. require.Nil(t, err)
  149. assert.Equal(t, 31, len(blocks[0].Rollup.L1UserTxs))
  150. assert.Equal(t, 4, len(blocks[0].Rollup.Batches[0].L1CoordinatorTxs))
  151. assert.Equal(t, 0, len(blocks[0].Rollup.Batches[1].L1CoordinatorTxs))
  152. assert.Equal(t, 22, len(blocks[0].Rollup.Batches[2].L2Txs))
  153. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[0].L1CoordinatorTxs))
  154. assert.Equal(t, 62, len(blocks[1].Rollup.Batches[0].L2Txs))
  155. assert.Equal(t, 1, len(blocks[1].Rollup.Batches[1].L1CoordinatorTxs))
  156. assert.Equal(t, 8, len(blocks[1].Rollup.Batches[1].L2Txs))
  157. // Coordinator Idx where to send the fees
  158. coordIdxs := []common.Idx{256, 257, 258, 259}
  159. // Idx of user 'A'
  160. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  161. ptc := ProcessTxsConfig{
  162. NLevels: 32,
  163. MaxFeeTx: 64,
  164. MaxTx: 512,
  165. MaxL1Tx: 16,
  166. }
  167. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  168. // to create the Coordinator accounts to receive the fees
  169. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  170. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  171. require.Nil(t, err)
  172. assert.Equal(t, 4, len(ptOut.CreatedAccounts))
  173. assert.Equal(t, 0, len(ptOut.CollectedFees))
  174. log.Debug("block:0 batch:1")
  175. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  176. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs,
  177. blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  178. require.Nil(t, err)
  179. assert.Equal(t, 0, len(ptOut.ExitInfos))
  180. assert.Equal(t, 31, len(ptOut.CreatedAccounts))
  181. assert.Equal(t, 4, len(ptOut.CollectedFees))
  182. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  183. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  184. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  185. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  186. acc, err := sdb.GetAccount(idxA1)
  187. require.Nil(t, err)
  188. assert.Equal(t, "50", acc.Balance.String())
  189. log.Debug("block:0 batch:2")
  190. l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  191. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
  192. require.Nil(t, err)
  193. assert.Equal(t, 0, len(ptOut.ExitInfos))
  194. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  195. assert.Equal(t, 4, len(ptOut.CollectedFees))
  196. assert.Equal(t, "2", ptOut.CollectedFees[common.TokenID(0)].String())
  197. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  198. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  199. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  200. acc, err = sdb.GetAccount(idxA1)
  201. require.Nil(t, err)
  202. assert.Equal(t, "35", acc.Balance.String())
  203. log.Debug("block:1 batch:0")
  204. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  205. // before processing expect l2Txs[0:2].Nonce==0
  206. assert.Equal(t, common.Nonce(0), l2Txs[0].Nonce)
  207. assert.Equal(t, common.Nonce(0), l2Txs[1].Nonce)
  208. assert.Equal(t, common.Nonce(0), l2Txs[2].Nonce)
  209. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  210. require.Nil(t, err)
  211. // after processing expect l2Txs[0:2].Nonce!=0 and has expected value
  212. assert.Equal(t, common.Nonce(6), l2Txs[0].Nonce)
  213. assert.Equal(t, common.Nonce(7), l2Txs[1].Nonce)
  214. assert.Equal(t, common.Nonce(8), l2Txs[2].Nonce)
  215. assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
  216. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  217. assert.Equal(t, 4, len(ptOut.CollectedFees))
  218. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  219. assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
  220. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  221. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  222. acc, err = sdb.GetAccount(idxA1)
  223. require.Nil(t, err)
  224. assert.Equal(t, "57", acc.Balance.String())
  225. log.Debug("block:1 batch:1")
  226. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  227. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs,
  228. blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  229. require.Nil(t, err)
  230. assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
  231. assert.Equal(t, 1, len(ptOut.CreatedAccounts))
  232. assert.Equal(t, 4, len(ptOut.CollectedFees))
  233. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
  234. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
  235. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
  236. assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
  237. acc, err = sdb.GetAccount(idxA1)
  238. assert.Nil(t, err)
  239. assert.Equal(t, "77", acc.Balance.String())
  240. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  241. acc, err = sdb.GetAccount(idxB0)
  242. require.Nil(t, err)
  243. assert.Equal(t, "51", acc.Balance.String())
  244. // get balance of Coordinator account for TokenID==0
  245. acc, err = sdb.GetAccount(common.Idx(256))
  246. require.Nil(t, err)
  247. assert.Equal(t, "2", acc.Balance.String())
  248. }
  249. func TestProcessTxsBatchBuilder(t *testing.T) {
  250. dir, err := ioutil.TempDir("", "tmpdb")
  251. require.Nil(t, err)
  252. defer assert.Nil(t, os.RemoveAll(dir))
  253. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  254. assert.Nil(t, err)
  255. // generate test transactions from test.SetBlockchain0 code
  256. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  257. blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
  258. require.Nil(t, err)
  259. // Coordinator Idx where to send the fees
  260. coordIdxs := []common.Idx{256, 257, 258, 259}
  261. // Idx of user 'A'
  262. idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
  263. ptc := ProcessTxsConfig{
  264. NLevels: 32,
  265. MaxFeeTx: 64,
  266. MaxTx: 512,
  267. MaxL1Tx: 32,
  268. }
  269. // Process the 1st batch, which contains the L1CoordinatorTxs necessary
  270. // to create the Coordinator accounts to receive the fees
  271. log.Debug("block:0 batch:0, only L1CoordinatorTxs")
  272. ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
  273. require.Nil(t, err)
  274. // expect 0 at CreatedAccount, as is only computed when StateDB.Type==TypeSynchronizer
  275. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  276. log.Debug("block:0 batch:1")
  277. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
  278. ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  279. require.Nil(t, err)
  280. assert.Equal(t, 0, len(ptOut.ExitInfos))
  281. assert.Equal(t, 0, len(ptOut.CreatedAccounts))
  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. acc, err = sdb.GetAccount(idxA1)
  292. require.Nil(t, err)
  293. assert.Equal(t, "35", acc.Balance.String())
  294. log.Debug("block:1 batch:0")
  295. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
  296. _, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
  297. require.Nil(t, err)
  298. acc, err = sdb.GetAccount(idxA1)
  299. require.Nil(t, err)
  300. assert.Equal(t, "57", acc.Balance.String())
  301. log.Debug("block:1 batch:1")
  302. l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
  303. _, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
  304. require.Nil(t, err)
  305. acc, err = sdb.GetAccount(idxA1)
  306. assert.Nil(t, err)
  307. assert.Equal(t, "77", acc.Balance.String())
  308. idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
  309. acc, err = sdb.GetAccount(idxB0)
  310. require.Nil(t, err)
  311. assert.Equal(t, "51", acc.Balance.String())
  312. // get balance of Coordinator account for TokenID==0
  313. acc, err = sdb.GetAccount(common.Idx(256))
  314. require.Nil(t, err)
  315. assert.Equal(t, common.TokenID(0), acc.TokenID)
  316. assert.Equal(t, "2", acc.Balance.String())
  317. acc, err = sdb.GetAccount(common.Idx(257))
  318. require.Nil(t, err)
  319. assert.Equal(t, common.TokenID(1), acc.TokenID)
  320. assert.Equal(t, "2", acc.Balance.String())
  321. }
  322. func TestZKInputsGeneration(t *testing.T) {
  323. dir, err := ioutil.TempDir("", "tmpdb")
  324. require.Nil(t, err)
  325. defer assert.Nil(t, os.RemoveAll(dir))
  326. sdb, err := NewStateDB(dir, TypeBatchBuilder, 4)
  327. assert.Nil(t, err)
  328. set := `
  329. Type: Blockchain
  330. AddToken(1)
  331. CreateAccountDeposit(1) A: 10
  332. > batchL1
  333. CreateAccountCoordinator(1) B
  334. CreateAccountCoordinator(1) C
  335. > batchL1
  336. // idxs: A:258, B:256, C:257
  337. Transfer(1) A-B: 6 (1)
  338. Transfer(1) A-C: 2 (1)
  339. > batch
  340. > block
  341. `
  342. // generate test transactions from test.SetBlockchain0 code
  343. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  344. blocks, err := tc.GenerateBlocks(set)
  345. require.Nil(t, err)
  346. // Coordinator Idx where to send the fees
  347. coordIdxs := []common.Idx{256}
  348. ptc := ProcessTxsConfig{
  349. NLevels: 32,
  350. MaxFeeTx: 8,
  351. MaxTx: 32,
  352. MaxL1Tx: 16,
  353. }
  354. log.Debug("block:0 batch:0, only L1UserTx")
  355. _, err = sdb.ProcessTxs(ptc, nil, blocks[0].Rollup.L1UserTxs, nil, nil)
  356. require.Nil(t, err)
  357. log.Debug("block:0 batch:1, only L1CoordinatorTxs")
  358. _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, nil)
  359. require.Nil(t, err)
  360. log.Debug("block:0 batch:2, only L2Txs")
  361. l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
  362. ptOut, err := sdb.ProcessTxs(ptc, coordIdxs, nil, nil, l2Txs)
  363. require.Nil(t, err)
  364. checkBalance(t, tc, sdb, "A", 1, "2")
  365. s, err := json.Marshal(ptOut.ZKInputs)
  366. require.Nil(t, err)
  367. debug := false
  368. debug = true
  369. if debug {
  370. fmt.Println(string(s))
  371. }
  372. }
  373. func TestProcessTxsRootTestVectors(t *testing.T) {
  374. dir, err := ioutil.TempDir("", "tmpdb")
  375. require.Nil(t, err)
  376. defer assert.Nil(t, os.RemoveAll(dir))
  377. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  378. assert.Nil(t, err)
  379. // same values than in the js test
  380. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  381. assert.Nil(t, err)
  382. l1Txs := []common.L1Tx{
  383. {
  384. FromIdx: 0,
  385. LoadAmount: big.NewInt(16000000),
  386. Amount: big.NewInt(0),
  387. TokenID: 1,
  388. FromBJJ: bjj0,
  389. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  390. ToIdx: 0,
  391. Type: common.TxTypeCreateAccountDeposit,
  392. },
  393. }
  394. l2Txs := []common.PoolL2Tx{
  395. {
  396. FromIdx: 256,
  397. ToIdx: 256,
  398. TokenID: 1,
  399. Amount: big.NewInt(1000),
  400. Nonce: 0,
  401. Fee: 126,
  402. Type: common.TxTypeTransfer,
  403. },
  404. }
  405. ptc := ProcessTxsConfig{
  406. NLevels: 32,
  407. MaxFeeTx: 8,
  408. MaxTx: 32,
  409. MaxL1Tx: 16,
  410. }
  411. _, err = sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  412. require.Nil(t, err)
  413. assert.Equal(t, "9827704113668630072730115158977131501210702363656902211840117643154933433410", sdb.mt.Root().BigInt().String())
  414. }
  415. func TestZKInputsHashTestVector0(t *testing.T) {
  416. dir, err := ioutil.TempDir("", "tmpdb")
  417. require.Nil(t, err)
  418. defer assert.Nil(t, os.RemoveAll(dir))
  419. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  420. assert.Nil(t, err)
  421. // same values than in the js test
  422. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  423. assert.Nil(t, err)
  424. l1Txs := []common.L1Tx{
  425. {
  426. FromIdx: 0,
  427. // LoadAmount: big.NewInt(10400),
  428. LoadAmount: big.NewInt(16000000),
  429. Amount: big.NewInt(0),
  430. TokenID: 1,
  431. FromBJJ: bjj0,
  432. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  433. ToIdx: 0,
  434. Type: common.TxTypeCreateAccountDeposit,
  435. },
  436. }
  437. l2Txs := []common.PoolL2Tx{
  438. {
  439. FromIdx: 256,
  440. ToIdx: 256,
  441. TokenID: 1,
  442. Amount: big.NewInt(1000),
  443. Nonce: 0,
  444. Fee: 126,
  445. Type: common.TxTypeTransfer,
  446. },
  447. }
  448. ptc := ProcessTxsConfig{
  449. NLevels: 32,
  450. MaxFeeTx: 8,
  451. MaxTx: 32,
  452. MaxL1Tx: 16,
  453. }
  454. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  455. require.Nil(t, err)
  456. // check expected account keys values from tx inputs
  457. acc, err := sdb.GetAccount(common.Idx(256))
  458. require.Nil(t, err)
  459. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  460. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  461. // check that there no exist more accounts
  462. _, err = sdb.GetAccount(common.Idx(257))
  463. require.NotNil(t, err)
  464. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(256).BigInt()
  465. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  466. assert.Nil(t, err)
  467. // value from js test vector
  468. expectedToHash := "0000000000ff000000000100000000000000000000000000000000000000000000000000000000000000000015ba488d749f6b891d29d0bf3a72481ec812e4d4ecef2bf7a3fc64f3c010444200000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010003e87e00000100000000000000000000000000000000000000000000000000000000000000"
  469. // checks are splitted to find the difference easier
  470. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  471. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  472. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  473. h, err := ptOut.ZKInputs.HashGlobalData()
  474. require.Nil(t, err)
  475. // value from js test vector
  476. assert.Equal(t, "80757288244566854497474223360206077562032050734432637237701187686677568506", h.String())
  477. }
  478. func TestZKInputsHashTestVector1(t *testing.T) {
  479. dir, err := ioutil.TempDir("", "tmpdb")
  480. require.Nil(t, err)
  481. defer assert.Nil(t, os.RemoveAll(dir))
  482. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  483. assert.Nil(t, err)
  484. // same values than in the js test
  485. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  486. assert.Nil(t, err)
  487. bjj1, err := common.BJJFromStringWithChecksum("093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d")
  488. assert.Nil(t, err)
  489. l1Txs := []common.L1Tx{
  490. {
  491. FromIdx: 0,
  492. // LoadAmount: big.NewInt(10400),
  493. LoadAmount: big.NewInt(16000000),
  494. Amount: big.NewInt(0),
  495. TokenID: 1,
  496. FromBJJ: bjj0,
  497. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  498. ToIdx: 0,
  499. Type: common.TxTypeCreateAccountDeposit,
  500. },
  501. {
  502. FromIdx: 0,
  503. LoadAmount: big.NewInt(16000000),
  504. Amount: big.NewInt(0),
  505. TokenID: 1,
  506. FromBJJ: bjj1,
  507. FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
  508. ToIdx: 0,
  509. Type: common.TxTypeCreateAccountDeposit,
  510. },
  511. }
  512. l2Txs := []common.PoolL2Tx{
  513. {
  514. FromIdx: 257,
  515. ToIdx: 256,
  516. TokenID: 1,
  517. Amount: big.NewInt(1000),
  518. Nonce: 0,
  519. Fee: 137,
  520. Type: common.TxTypeTransfer,
  521. },
  522. }
  523. ptc := ProcessTxsConfig{
  524. NLevels: 32,
  525. MaxFeeTx: 8,
  526. MaxTx: 32,
  527. MaxL1Tx: 16,
  528. }
  529. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  530. require.Nil(t, err)
  531. // check expected account keys values from tx inputs
  532. acc, err := sdb.GetAccount(common.Idx(256))
  533. require.Nil(t, err)
  534. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  535. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  536. acc, err = sdb.GetAccount(common.Idx(257))
  537. require.Nil(t, err)
  538. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", acc.PublicKey.Compress().String())
  539. assert.Equal(t, "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF", acc.EthAddr.Hex())
  540. // check that there no exist more accounts
  541. _, err = sdb.GetAccount(common.Idx(258))
  542. require.NotNil(t, err)
  543. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(257).BigInt()
  544. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  545. assert.Nil(t, err)
  546. // value from js test vector
  547. expectedToHash := "0000000000ff0000000001010000000000000000000000000000000000000000000000000000000000000000304a3f3aef4f416cca887aab7265227449077627138345c2eb25bf8ff946b09500000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00000000000028a00000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000010003e88900000101000000000000000000000000000000000000000000000000000000000000"
  548. // checks are splitted to find the difference easier
  549. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  550. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  551. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  552. h, err := ptOut.ZKInputs.HashGlobalData()
  553. require.Nil(t, err)
  554. // value from js test vector
  555. assert.Equal(t, "10900521462378877053056992084240080637954406133884857263674494661625916419481", h.String())
  556. }