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.

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