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.

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