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.

558 lines
15 KiB

  1. // Package txsets contains Til sets of transactions & Transactions generation
  2. // that are used at tests of other packages of hermez-node
  3. //nolint:gomnd
  4. package txsets
  5. import (
  6. "encoding/hex"
  7. "math/big"
  8. "strconv"
  9. "testing"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/hermeznetwork/hermez-node/test/til"
  13. "github.com/iden3/go-iden3-crypto/babyjub"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. // The methods from this file are used at txprocessor package to test the
  18. // ZKInputs generation & at tests of the test/zkproof to test the integration
  19. // of the ZKInputs generation with the proof server
  20. // GenerateJsUsers generates the same values than in the js test
  21. func GenerateJsUsers(t *testing.T) []til.User {
  22. // same values than in the js test
  23. // skJsHex is equivalent to the 0000...000i js private key in commonjs
  24. skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f",
  25. "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819",
  26. "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707",
  27. "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"}
  28. addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf",
  29. "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf",
  30. "0x6813eb9362372eef6200f3b1dbc3f819671cba69",
  31. "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"}
  32. var users []til.User
  33. for i := 0; i < len(skJsHex); i++ {
  34. skJs, err := hex.DecodeString(skJsHex[i])
  35. require.NoError(t, err)
  36. var sk babyjub.PrivateKey
  37. copy(sk[:], skJs)
  38. // bjj := sk.Public()
  39. user := til.User{
  40. Name: strconv.Itoa(i),
  41. BJJ: &sk,
  42. Addr: ethCommon.HexToAddress(addrHex[i]),
  43. }
  44. users = append(users, user)
  45. }
  46. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021",
  47. users[0].BJJ.Public().String())
  48. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909",
  49. users[1].BJJ.Public().String())
  50. assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11",
  51. users[2].BJJ.Public().String())
  52. assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b",
  53. users[3].BJJ.Public().String())
  54. return users
  55. }
  56. func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx {
  57. toSign, err := l2Tx.HashToSign(chainID)
  58. require.NoError(t, err)
  59. sig := user.BJJ.SignPoseidon(toSign)
  60. l2Tx.Signature = sig.Compress()
  61. return l2Tx
  62. }
  63. // GenerateTxsZKInputsHash0 generates the transactions for the TestZKInputsHash0
  64. func GenerateTxsZKInputsHash0(t *testing.T, chainID uint16) (users []til.User,
  65. coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
  66. l2Txs []common.PoolL2Tx) {
  67. // same values than in the js test
  68. users = GenerateJsUsers(t)
  69. l1UserTxs = []common.L1Tx{
  70. {
  71. FromIdx: 0,
  72. DepositAmount: big.NewInt(16000000),
  73. Amount: big.NewInt(0),
  74. TokenID: 1,
  75. FromBJJ: users[0].BJJ.Public().Compress(),
  76. FromEthAddr: users[0].Addr,
  77. ToIdx: 0,
  78. Type: common.TxTypeCreateAccountDeposit,
  79. UserOrigin: true,
  80. },
  81. }
  82. l2Txs = []common.PoolL2Tx{
  83. {
  84. FromIdx: 256,
  85. ToIdx: 256,
  86. TokenID: 1,
  87. Amount: big.NewInt(1000),
  88. Nonce: 0,
  89. Fee: 126,
  90. Type: common.TxTypeTransfer,
  91. },
  92. }
  93. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  94. return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
  95. }
  96. // GenerateTxsZKInputsHash1 generates the transactions for the TestZKInputsHash1
  97. func GenerateTxsZKInputsHash1(t *testing.T, chainID uint16) (users []til.User,
  98. coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
  99. l2Txs []common.PoolL2Tx) {
  100. // same values than in the js test
  101. users = GenerateJsUsers(t)
  102. l1UserTxs = []common.L1Tx{
  103. {
  104. FromIdx: 0,
  105. DepositAmount: big.NewInt(16000000),
  106. Amount: big.NewInt(0),
  107. TokenID: 1,
  108. FromBJJ: users[0].BJJ.Public().Compress(),
  109. FromEthAddr: users[0].Addr,
  110. ToIdx: 0,
  111. Type: common.TxTypeCreateAccountDeposit,
  112. UserOrigin: true,
  113. },
  114. {
  115. FromIdx: 0,
  116. DepositAmount: big.NewInt(16000000),
  117. Amount: big.NewInt(0),
  118. TokenID: 1,
  119. FromBJJ: users[1].BJJ.Public().Compress(),
  120. FromEthAddr: users[1].Addr,
  121. ToIdx: 0,
  122. Type: common.TxTypeCreateAccountDeposit,
  123. UserOrigin: true,
  124. },
  125. }
  126. l2Txs = []common.PoolL2Tx{
  127. {
  128. FromIdx: 257,
  129. ToIdx: 256,
  130. TokenID: 1,
  131. Amount: big.NewInt(1000),
  132. Nonce: 0,
  133. Fee: 137,
  134. Type: common.TxTypeTransfer,
  135. },
  136. }
  137. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  138. return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
  139. }
  140. // GenerateTxsZKInputs0 generates the transactions for the TestZKInputs0
  141. func GenerateTxsZKInputs0(t *testing.T, chainID uint16) (users []til.User,
  142. coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx,
  143. l2Txs []common.PoolL2Tx) {
  144. // same values than in the js test
  145. users = GenerateJsUsers(t)
  146. depositAmount, err := common.Float40(10400).BigInt()
  147. require.Nil(t, err)
  148. l1UserTxs = []common.L1Tx{
  149. {
  150. FromIdx: 0,
  151. DepositAmount: depositAmount,
  152. Amount: big.NewInt(0),
  153. TokenID: 1,
  154. FromBJJ: users[0].BJJ.Public().Compress(),
  155. FromEthAddr: users[0].Addr,
  156. ToIdx: 0,
  157. Type: common.TxTypeCreateAccountDeposit,
  158. UserOrigin: true,
  159. },
  160. }
  161. l2Txs = []common.PoolL2Tx{
  162. {
  163. FromIdx: 256,
  164. ToIdx: 256,
  165. TokenID: 1,
  166. Amount: big.NewInt(1000),
  167. Nonce: 0,
  168. Fee: 0,
  169. Type: common.TxTypeTransfer,
  170. },
  171. }
  172. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  173. return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
  174. }
  175. // GenerateTxsZKInputs1 generates the transactions for the TestZKInputs1
  176. func GenerateTxsZKInputs1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
  177. l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  178. // same values than in the js test
  179. users = GenerateJsUsers(t)
  180. l1UserTxs = []common.L1Tx{
  181. {
  182. FromIdx: 0,
  183. DepositAmount: big.NewInt(16000000),
  184. Amount: big.NewInt(0),
  185. TokenID: 1,
  186. FromBJJ: users[0].BJJ.Public().Compress(),
  187. FromEthAddr: users[0].Addr,
  188. ToIdx: 0,
  189. Type: common.TxTypeCreateAccountDeposit,
  190. UserOrigin: true,
  191. },
  192. {
  193. FromIdx: 0,
  194. DepositAmount: big.NewInt(16000000),
  195. Amount: big.NewInt(0),
  196. TokenID: 1,
  197. FromBJJ: users[1].BJJ.Public().Compress(),
  198. FromEthAddr: users[1].Addr,
  199. ToIdx: 0,
  200. Type: common.TxTypeCreateAccountDeposit,
  201. UserOrigin: true,
  202. },
  203. }
  204. l2Txs = []common.PoolL2Tx{
  205. {
  206. FromIdx: 256,
  207. ToIdx: 256,
  208. TokenID: 1,
  209. Amount: big.NewInt(1000),
  210. Nonce: 0,
  211. Fee: 126,
  212. Type: common.TxTypeTransfer,
  213. },
  214. }
  215. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  216. coordIdxs = []common.Idx{257}
  217. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  218. }
  219. // GenerateTxsZKInputs2 generates the transactions for the TestZKInputs2
  220. func GenerateTxsZKInputs2(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
  221. l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  222. // same values than in the js test
  223. users = GenerateJsUsers(t)
  224. l1UserTxs = []common.L1Tx{
  225. {
  226. FromIdx: 0,
  227. DepositAmount: big.NewInt(16000000),
  228. Amount: big.NewInt(0),
  229. TokenID: 1,
  230. FromBJJ: users[0].BJJ.Public().Compress(),
  231. FromEthAddr: users[0].Addr,
  232. ToIdx: 0,
  233. Type: common.TxTypeCreateAccountDeposit,
  234. UserOrigin: true,
  235. },
  236. {
  237. FromIdx: 0,
  238. DepositAmount: big.NewInt(16000000),
  239. Amount: big.NewInt(0),
  240. TokenID: 1,
  241. FromBJJ: users[1].BJJ.Public().Compress(),
  242. FromEthAddr: users[1].Addr,
  243. ToIdx: 0,
  244. Type: common.TxTypeCreateAccountDeposit,
  245. UserOrigin: true,
  246. },
  247. {
  248. FromIdx: 0,
  249. DepositAmount: big.NewInt(16000000),
  250. Amount: big.NewInt(0),
  251. TokenID: 1,
  252. FromBJJ: users[2].BJJ.Public().Compress(),
  253. FromEthAddr: users[2].Addr,
  254. ToIdx: 0,
  255. Type: common.TxTypeCreateAccountDeposit,
  256. UserOrigin: true,
  257. },
  258. {
  259. FromIdx: 0,
  260. DepositAmount: big.NewInt(16000000),
  261. Amount: big.NewInt(0),
  262. TokenID: 1,
  263. FromBJJ: users[3].BJJ.Public().Compress(),
  264. FromEthAddr: users[3].Addr,
  265. ToIdx: 0,
  266. Type: common.TxTypeCreateAccountDeposit,
  267. UserOrigin: true,
  268. },
  269. }
  270. l2Txs = []common.PoolL2Tx{
  271. {
  272. FromIdx: 256,
  273. ToIdx: 258,
  274. TokenID: 1,
  275. Amount: big.NewInt(1000),
  276. Nonce: 0,
  277. Fee: 126,
  278. Type: common.TxTypeTransfer,
  279. },
  280. {
  281. FromIdx: 256,
  282. ToIdx: 259,
  283. TokenID: 1,
  284. Amount: big.NewInt(1000),
  285. Nonce: 1,
  286. Fee: 126,
  287. Type: common.TxTypeTransfer,
  288. },
  289. }
  290. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  291. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  292. coordIdxs = []common.Idx{257}
  293. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  294. }
  295. // GenerateTxsZKInputs3 generates the transactions for the TestZKInputs3
  296. func GenerateTxsZKInputs3(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
  297. l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  298. // same values than in the js test
  299. users = GenerateJsUsers(t)
  300. l1UserTxs = []common.L1Tx{
  301. {
  302. FromIdx: 0,
  303. DepositAmount: big.NewInt(16000000),
  304. Amount: big.NewInt(0),
  305. TokenID: 1,
  306. FromBJJ: users[0].BJJ.Public().Compress(),
  307. FromEthAddr: users[0].Addr,
  308. ToIdx: 0,
  309. Type: common.TxTypeCreateAccountDeposit,
  310. UserOrigin: true,
  311. },
  312. {
  313. FromIdx: 0,
  314. DepositAmount: big.NewInt(16000000),
  315. Amount: big.NewInt(0),
  316. TokenID: 1,
  317. FromBJJ: users[1].BJJ.Public().Compress(),
  318. FromEthAddr: users[1].Addr,
  319. ToIdx: 0,
  320. Type: common.TxTypeCreateAccountDeposit,
  321. UserOrigin: true,
  322. },
  323. {
  324. FromIdx: 0,
  325. DepositAmount: big.NewInt(16000000),
  326. Amount: big.NewInt(0),
  327. TokenID: 1,
  328. FromBJJ: users[2].BJJ.Public().Compress(),
  329. FromEthAddr: users[2].Addr,
  330. ToIdx: 0,
  331. Type: common.TxTypeCreateAccountDeposit,
  332. UserOrigin: true,
  333. },
  334. {
  335. FromIdx: 0,
  336. DepositAmount: big.NewInt(16000000),
  337. Amount: big.NewInt(1000),
  338. TokenID: 1,
  339. FromBJJ: users[3].BJJ.Public().Compress(),
  340. FromEthAddr: users[3].Addr,
  341. ToIdx: 258,
  342. Type: common.TxTypeCreateAccountDepositTransfer,
  343. UserOrigin: true,
  344. },
  345. }
  346. l2Txs = []common.PoolL2Tx{
  347. {
  348. FromIdx: 256,
  349. ToIdx: 258,
  350. TokenID: 1,
  351. Amount: big.NewInt(1000),
  352. Nonce: 0,
  353. Fee: 126,
  354. Type: common.TxTypeTransfer,
  355. },
  356. {
  357. FromIdx: 256,
  358. ToIdx: 259,
  359. TokenID: 1,
  360. Amount: big.NewInt(1000),
  361. Nonce: 1,
  362. Fee: 126,
  363. Type: common.TxTypeTransfer,
  364. },
  365. }
  366. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  367. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  368. coordIdxs = []common.Idx{257}
  369. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  370. }
  371. // GenerateTxsZKInputs4 generates the transactions for the TestZKInputs4
  372. func GenerateTxsZKInputs4(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
  373. l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  374. // same values than in the js test
  375. users = GenerateJsUsers(t)
  376. l1UserTxs = []common.L1Tx{
  377. {
  378. FromIdx: 0,
  379. DepositAmount: big.NewInt(16000000),
  380. Amount: big.NewInt(0),
  381. TokenID: 1,
  382. FromBJJ: users[0].BJJ.Public().Compress(),
  383. FromEthAddr: users[0].Addr,
  384. ToIdx: 0,
  385. Type: common.TxTypeCreateAccountDeposit,
  386. UserOrigin: true,
  387. },
  388. {
  389. FromIdx: 0,
  390. DepositAmount: big.NewInt(16000000),
  391. Amount: big.NewInt(0),
  392. TokenID: 1,
  393. FromBJJ: users[1].BJJ.Public().Compress(),
  394. FromEthAddr: users[1].Addr,
  395. ToIdx: 0,
  396. Type: common.TxTypeCreateAccountDeposit,
  397. UserOrigin: true,
  398. },
  399. {
  400. FromIdx: 0,
  401. DepositAmount: big.NewInt(16000000),
  402. Amount: big.NewInt(0),
  403. TokenID: 1,
  404. FromBJJ: users[2].BJJ.Public().Compress(),
  405. FromEthAddr: users[2].Addr,
  406. ToIdx: 0,
  407. Type: common.TxTypeCreateAccountDeposit,
  408. UserOrigin: true,
  409. },
  410. {
  411. FromIdx: 0,
  412. DepositAmount: big.NewInt(16000000),
  413. Amount: big.NewInt(1000),
  414. TokenID: 1,
  415. FromBJJ: users[3].BJJ.Public().Compress(),
  416. FromEthAddr: users[3].Addr,
  417. ToIdx: 258,
  418. Type: common.TxTypeCreateAccountDepositTransfer,
  419. UserOrigin: true,
  420. },
  421. {
  422. FromIdx: 258,
  423. DepositAmount: big.NewInt(16000000),
  424. Amount: big.NewInt(1000),
  425. TokenID: 1,
  426. FromEthAddr: users[2].Addr,
  427. ToIdx: 259,
  428. Type: common.TxTypeDepositTransfer,
  429. UserOrigin: true,
  430. },
  431. }
  432. l2Txs = []common.PoolL2Tx{
  433. {
  434. FromIdx: 256,
  435. ToIdx: 258,
  436. TokenID: 1,
  437. Amount: big.NewInt(1000),
  438. Nonce: 0,
  439. Fee: 126,
  440. Type: common.TxTypeTransfer,
  441. },
  442. {
  443. FromIdx: 256,
  444. ToIdx: 259,
  445. TokenID: 1,
  446. Amount: big.NewInt(1000),
  447. Nonce: 1,
  448. Fee: 126,
  449. Type: common.TxTypeTransfer,
  450. },
  451. }
  452. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  453. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  454. coordIdxs = []common.Idx{257}
  455. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  456. }
  457. // GenerateTxsZKInputs5 generates the transactions for the TestZKInputs5
  458. func GenerateTxsZKInputs5(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx,
  459. l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  460. // same values than in the js test
  461. users = GenerateJsUsers(t)
  462. l1UserTxs = []common.L1Tx{
  463. {
  464. FromIdx: 0,
  465. DepositAmount: big.NewInt(16000000),
  466. Amount: big.NewInt(0),
  467. TokenID: 1,
  468. FromBJJ: users[0].BJJ.Public().Compress(),
  469. FromEthAddr: users[0].Addr,
  470. ToIdx: 0,
  471. Type: common.TxTypeCreateAccountDeposit,
  472. UserOrigin: true,
  473. },
  474. {
  475. FromIdx: 0,
  476. DepositAmount: big.NewInt(16000000),
  477. Amount: big.NewInt(0),
  478. TokenID: 1,
  479. FromBJJ: users[1].BJJ.Public().Compress(),
  480. FromEthAddr: users[1].Addr,
  481. ToIdx: 0,
  482. Type: common.TxTypeCreateAccountDeposit,
  483. UserOrigin: true,
  484. },
  485. {
  486. FromIdx: 257,
  487. DepositAmount: big.NewInt(0),
  488. Amount: big.NewInt(1000),
  489. TokenID: 1,
  490. FromBJJ: users[1].BJJ.Public().Compress(),
  491. FromEthAddr: users[1].Addr,
  492. ToIdx: 1,
  493. Type: common.TxTypeForceExit,
  494. UserOrigin: true,
  495. },
  496. }
  497. l2Txs = []common.PoolL2Tx{
  498. {
  499. FromIdx: 256,
  500. ToIdx: 257,
  501. TokenID: 1,
  502. Amount: big.NewInt(1000),
  503. Nonce: 0,
  504. Fee: 126,
  505. Type: common.TxTypeTransfer,
  506. },
  507. {
  508. FromIdx: 256,
  509. ToIdx: 1,
  510. TokenID: 1,
  511. Amount: big.NewInt(1000),
  512. Nonce: 1,
  513. Fee: 126,
  514. Type: common.TxTypeExit,
  515. },
  516. }
  517. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  518. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  519. coordIdxs = []common.Idx{257}
  520. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  521. }