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.

535 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. l1UserTxs = []common.L1Tx{
  131. {
  132. FromIdx: 0,
  133. DepositAmount: big.NewInt(16000000),
  134. Amount: big.NewInt(0),
  135. TokenID: 1,
  136. FromBJJ: users[0].BJJ.Public().Compress(),
  137. FromEthAddr: users[0].Addr,
  138. ToIdx: 0,
  139. Type: common.TxTypeCreateAccountDeposit,
  140. UserOrigin: true,
  141. },
  142. }
  143. l2Txs = []common.PoolL2Tx{
  144. {
  145. FromIdx: 256,
  146. ToIdx: 256,
  147. TokenID: 1,
  148. Amount: big.NewInt(1000),
  149. Nonce: 0,
  150. Fee: 0,
  151. Type: common.TxTypeTransfer,
  152. },
  153. }
  154. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  155. return users, []common.Idx{}, l1UserTxs, []common.L1Tx{}, l2Txs
  156. }
  157. // GenerateTxsZKInputs1 generates the transactions for the TestZKInputs1
  158. func GenerateTxsZKInputs1(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  159. // same values than in the js test
  160. users = GenerateJsUsers(t)
  161. l1UserTxs = []common.L1Tx{
  162. {
  163. FromIdx: 0,
  164. DepositAmount: big.NewInt(16000000),
  165. Amount: big.NewInt(0),
  166. TokenID: 1,
  167. FromBJJ: users[0].BJJ.Public().Compress(),
  168. FromEthAddr: users[0].Addr,
  169. ToIdx: 0,
  170. Type: common.TxTypeCreateAccountDeposit,
  171. UserOrigin: true,
  172. },
  173. {
  174. FromIdx: 0,
  175. DepositAmount: big.NewInt(16000000),
  176. Amount: big.NewInt(0),
  177. TokenID: 1,
  178. FromBJJ: users[1].BJJ.Public().Compress(),
  179. FromEthAddr: users[1].Addr,
  180. ToIdx: 0,
  181. Type: common.TxTypeCreateAccountDeposit,
  182. UserOrigin: true,
  183. },
  184. }
  185. l2Txs = []common.PoolL2Tx{
  186. {
  187. FromIdx: 256,
  188. ToIdx: 256,
  189. TokenID: 1,
  190. Amount: big.NewInt(1000),
  191. Nonce: 0,
  192. Fee: 126,
  193. Type: common.TxTypeTransfer,
  194. },
  195. }
  196. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  197. coordIdxs = []common.Idx{257}
  198. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  199. }
  200. // GenerateTxsZKInputs2 generates the transactions for the TestZKInputs2
  201. func GenerateTxsZKInputs2(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  202. // same values than in the js test
  203. users = GenerateJsUsers(t)
  204. l1UserTxs = []common.L1Tx{
  205. {
  206. FromIdx: 0,
  207. DepositAmount: big.NewInt(16000000),
  208. Amount: big.NewInt(0),
  209. TokenID: 1,
  210. FromBJJ: users[0].BJJ.Public().Compress(),
  211. FromEthAddr: users[0].Addr,
  212. ToIdx: 0,
  213. Type: common.TxTypeCreateAccountDeposit,
  214. UserOrigin: true,
  215. },
  216. {
  217. FromIdx: 0,
  218. DepositAmount: big.NewInt(16000000),
  219. Amount: big.NewInt(0),
  220. TokenID: 1,
  221. FromBJJ: users[1].BJJ.Public().Compress(),
  222. FromEthAddr: users[1].Addr,
  223. ToIdx: 0,
  224. Type: common.TxTypeCreateAccountDeposit,
  225. UserOrigin: true,
  226. },
  227. {
  228. FromIdx: 0,
  229. DepositAmount: big.NewInt(16000000),
  230. Amount: big.NewInt(0),
  231. TokenID: 1,
  232. FromBJJ: users[2].BJJ.Public().Compress(),
  233. FromEthAddr: users[2].Addr,
  234. ToIdx: 0,
  235. Type: common.TxTypeCreateAccountDeposit,
  236. UserOrigin: true,
  237. },
  238. {
  239. FromIdx: 0,
  240. DepositAmount: big.NewInt(16000000),
  241. Amount: big.NewInt(0),
  242. TokenID: 1,
  243. FromBJJ: users[3].BJJ.Public().Compress(),
  244. FromEthAddr: users[3].Addr,
  245. ToIdx: 0,
  246. Type: common.TxTypeCreateAccountDeposit,
  247. UserOrigin: true,
  248. },
  249. }
  250. l2Txs = []common.PoolL2Tx{
  251. {
  252. FromIdx: 256,
  253. ToIdx: 258,
  254. TokenID: 1,
  255. Amount: big.NewInt(1000),
  256. Nonce: 0,
  257. Fee: 126,
  258. Type: common.TxTypeTransfer,
  259. },
  260. {
  261. FromIdx: 256,
  262. ToIdx: 259,
  263. TokenID: 1,
  264. Amount: big.NewInt(1000),
  265. Nonce: 1,
  266. Fee: 126,
  267. Type: common.TxTypeTransfer,
  268. },
  269. }
  270. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  271. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  272. coordIdxs = []common.Idx{257}
  273. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  274. }
  275. // GenerateTxsZKInputs3 generates the transactions for the TestZKInputs3
  276. func GenerateTxsZKInputs3(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  277. // same values than in the js test
  278. users = GenerateJsUsers(t)
  279. l1UserTxs = []common.L1Tx{
  280. {
  281. FromIdx: 0,
  282. DepositAmount: big.NewInt(16000000),
  283. Amount: big.NewInt(0),
  284. TokenID: 1,
  285. FromBJJ: users[0].BJJ.Public().Compress(),
  286. FromEthAddr: users[0].Addr,
  287. ToIdx: 0,
  288. Type: common.TxTypeCreateAccountDeposit,
  289. UserOrigin: true,
  290. },
  291. {
  292. FromIdx: 0,
  293. DepositAmount: big.NewInt(16000000),
  294. Amount: big.NewInt(0),
  295. TokenID: 1,
  296. FromBJJ: users[1].BJJ.Public().Compress(),
  297. FromEthAddr: users[1].Addr,
  298. ToIdx: 0,
  299. Type: common.TxTypeCreateAccountDeposit,
  300. UserOrigin: true,
  301. },
  302. {
  303. FromIdx: 0,
  304. DepositAmount: big.NewInt(16000000),
  305. Amount: big.NewInt(0),
  306. TokenID: 1,
  307. FromBJJ: users[2].BJJ.Public().Compress(),
  308. FromEthAddr: users[2].Addr,
  309. ToIdx: 0,
  310. Type: common.TxTypeCreateAccountDeposit,
  311. UserOrigin: true,
  312. },
  313. {
  314. FromIdx: 0,
  315. DepositAmount: big.NewInt(16000000),
  316. Amount: big.NewInt(1000),
  317. TokenID: 1,
  318. FromBJJ: users[3].BJJ.Public().Compress(),
  319. FromEthAddr: users[3].Addr,
  320. ToIdx: 258,
  321. Type: common.TxTypeCreateAccountDepositTransfer,
  322. UserOrigin: true,
  323. },
  324. }
  325. l2Txs = []common.PoolL2Tx{
  326. {
  327. FromIdx: 256,
  328. ToIdx: 258,
  329. TokenID: 1,
  330. Amount: big.NewInt(1000),
  331. Nonce: 0,
  332. Fee: 126,
  333. Type: common.TxTypeTransfer,
  334. },
  335. {
  336. FromIdx: 256,
  337. ToIdx: 259,
  338. TokenID: 1,
  339. Amount: big.NewInt(1000),
  340. Nonce: 1,
  341. Fee: 126,
  342. Type: common.TxTypeTransfer,
  343. },
  344. }
  345. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  346. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  347. coordIdxs = []common.Idx{257}
  348. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  349. }
  350. // GenerateTxsZKInputs4 generates the transactions for the TestZKInputs4
  351. func GenerateTxsZKInputs4(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  352. // same values than in the js test
  353. users = GenerateJsUsers(t)
  354. l1UserTxs = []common.L1Tx{
  355. {
  356. FromIdx: 0,
  357. DepositAmount: big.NewInt(16000000),
  358. Amount: big.NewInt(0),
  359. TokenID: 1,
  360. FromBJJ: users[0].BJJ.Public().Compress(),
  361. FromEthAddr: users[0].Addr,
  362. ToIdx: 0,
  363. Type: common.TxTypeCreateAccountDeposit,
  364. UserOrigin: true,
  365. },
  366. {
  367. FromIdx: 0,
  368. DepositAmount: big.NewInt(16000000),
  369. Amount: big.NewInt(0),
  370. TokenID: 1,
  371. FromBJJ: users[1].BJJ.Public().Compress(),
  372. FromEthAddr: users[1].Addr,
  373. ToIdx: 0,
  374. Type: common.TxTypeCreateAccountDeposit,
  375. UserOrigin: true,
  376. },
  377. {
  378. FromIdx: 0,
  379. DepositAmount: big.NewInt(16000000),
  380. Amount: big.NewInt(0),
  381. TokenID: 1,
  382. FromBJJ: users[2].BJJ.Public().Compress(),
  383. FromEthAddr: users[2].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(1000),
  392. TokenID: 1,
  393. FromBJJ: users[3].BJJ.Public().Compress(),
  394. FromEthAddr: users[3].Addr,
  395. ToIdx: 258,
  396. Type: common.TxTypeCreateAccountDepositTransfer,
  397. UserOrigin: true,
  398. },
  399. {
  400. FromIdx: 258,
  401. DepositAmount: big.NewInt(16000000),
  402. Amount: big.NewInt(1000),
  403. TokenID: 1,
  404. FromEthAddr: users[2].Addr,
  405. ToIdx: 259,
  406. Type: common.TxTypeDepositTransfer,
  407. UserOrigin: true,
  408. },
  409. }
  410. l2Txs = []common.PoolL2Tx{
  411. {
  412. FromIdx: 256,
  413. ToIdx: 258,
  414. TokenID: 1,
  415. Amount: big.NewInt(1000),
  416. Nonce: 0,
  417. Fee: 126,
  418. Type: common.TxTypeTransfer,
  419. },
  420. {
  421. FromIdx: 256,
  422. ToIdx: 259,
  423. TokenID: 1,
  424. Amount: big.NewInt(1000),
  425. Nonce: 1,
  426. Fee: 126,
  427. Type: common.TxTypeTransfer,
  428. },
  429. }
  430. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  431. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  432. coordIdxs = []common.Idx{257}
  433. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  434. }
  435. // GenerateTxsZKInputs5 generates the transactions for the TestZKInputs5
  436. func GenerateTxsZKInputs5(t *testing.T, chainID uint16) (users []til.User, coordIdxs []common.Idx, l1UserTxs []common.L1Tx, l1CoordTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
  437. // same values than in the js test
  438. users = GenerateJsUsers(t)
  439. l1UserTxs = []common.L1Tx{
  440. {
  441. FromIdx: 0,
  442. DepositAmount: big.NewInt(16000000),
  443. Amount: big.NewInt(0),
  444. TokenID: 1,
  445. FromBJJ: users[0].BJJ.Public().Compress(),
  446. FromEthAddr: users[0].Addr,
  447. ToIdx: 0,
  448. Type: common.TxTypeCreateAccountDeposit,
  449. UserOrigin: true,
  450. },
  451. {
  452. FromIdx: 0,
  453. DepositAmount: big.NewInt(16000000),
  454. Amount: big.NewInt(0),
  455. TokenID: 1,
  456. FromBJJ: users[1].BJJ.Public().Compress(),
  457. FromEthAddr: users[1].Addr,
  458. ToIdx: 0,
  459. Type: common.TxTypeCreateAccountDeposit,
  460. UserOrigin: true,
  461. },
  462. {
  463. FromIdx: 257,
  464. DepositAmount: big.NewInt(0),
  465. Amount: big.NewInt(1000),
  466. TokenID: 1,
  467. FromBJJ: users[1].BJJ.Public().Compress(),
  468. FromEthAddr: users[1].Addr,
  469. ToIdx: 1,
  470. Type: common.TxTypeForceExit,
  471. UserOrigin: true,
  472. },
  473. }
  474. l2Txs = []common.PoolL2Tx{
  475. {
  476. FromIdx: 256,
  477. ToIdx: 257,
  478. TokenID: 1,
  479. Amount: big.NewInt(1000),
  480. Nonce: 0,
  481. Fee: 126,
  482. Type: common.TxTypeTransfer,
  483. },
  484. {
  485. FromIdx: 256,
  486. ToIdx: 1,
  487. TokenID: 1,
  488. Amount: big.NewInt(1000),
  489. Nonce: 1,
  490. Fee: 126,
  491. Type: common.TxTypeExit,
  492. },
  493. }
  494. l2Txs[0] = signL2Tx(t, chainID, users[0], l2Txs[0])
  495. l2Txs[1] = signL2Tx(t, chainID, users[0], l2Txs[1])
  496. coordIdxs = []common.Idx{257}
  497. return users, coordIdxs, l1UserTxs, []common.L1Tx{}, l2Txs
  498. }