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.

847 lines
35 KiB

  1. package eth
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "math/big"
  8. "testing"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. var rollupClient *RollupClient
  17. var auctionClient *AuctionClient
  18. var ethHashForge ethCommon.Hash
  19. var argsForge *RollupForgeBatchArgs
  20. var absoluteMaxL1L2BatchTimeout = int64(240)
  21. var maxTx = int64(512)
  22. var nLevels = int64(32)
  23. var tokenIDERC777 uint32
  24. var tokenHEZID uint32
  25. type keys struct {
  26. BJJSecretKey *babyjub.PrivateKey
  27. BJJPublicKey *babyjub.PublicKey
  28. Addr ethCommon.Address
  29. }
  30. func genKeysBjj(i int64) *keys {
  31. i++ // i = 0 doesn't work for the ecdsa key generation
  32. var sk babyjub.PrivateKey
  33. binary.LittleEndian.PutUint64(sk[:], uint64(i))
  34. // eth address
  35. var key ecdsa.PrivateKey
  36. key.D = big.NewInt(i) // only for testing
  37. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  38. key.Curve = ethCrypto.S256()
  39. return &keys{
  40. BJJSecretKey: &sk,
  41. BJJPublicKey: sk.Public(),
  42. }
  43. }
  44. func TestRollupConstants(t *testing.T) {
  45. rollupConstants, err := rollupClient.RollupConstants()
  46. require.Nil(t, err)
  47. assert.Equal(t, absoluteMaxL1L2BatchTimeout, rollupConstants.AbsoluteMaxL1L2BatchTimeout)
  48. assert.Equal(t, auctionAddressConst, rollupConstants.HermezAuctionContract)
  49. assert.Equal(t, tokenHEZAddressConst, rollupConstants.TokenHEZ)
  50. assert.Equal(t, maxTx, rollupConstants.Verifiers[0].MaxTx)
  51. assert.Equal(t, nLevels, rollupConstants.Verifiers[0].NLevels)
  52. assert.Equal(t, governanceAddressConst, rollupConstants.HermezGovernanceDAOAddress)
  53. assert.Equal(t, safetyAddressConst, rollupConstants.SafetyAddress)
  54. assert.Equal(t, wdelayerAddressConst, rollupConstants.WithdrawDelayerContract)
  55. }
  56. func TestRollupRegisterTokensCount(t *testing.T) {
  57. registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
  58. require.Nil(t, err)
  59. assert.Equal(t, big.NewInt(1), registerTokensCount)
  60. }
  61. func TestRollupAddToken(t *testing.T) {
  62. feeAddToken := big.NewInt(10)
  63. // Addtoken ERC20Permit
  64. registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
  65. require.Nil(t, err)
  66. _, err = rollupClient.RollupAddToken(tokenHEZAddressConst, feeAddToken, deadline)
  67. require.Nil(t, err)
  68. currentBlockNum, err := rollupClient.client.EthLastBlock()
  69. require.Nil(t, err)
  70. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  71. require.Nil(t, err)
  72. assert.Equal(t, tokenHEZAddressConst, rollupEvents.AddToken[0].TokenAddress)
  73. assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
  74. tokenHEZID = rollupEvents.AddToken[0].TokenID
  75. }
  76. func TestRollupForgeBatch(t *testing.T) {
  77. chainid, _ := auctionClient.client.Client().ChainID(context.Background())
  78. // Register Coordinator
  79. forgerAddress := governanceAddressConst
  80. _, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
  81. require.Nil(t, err)
  82. // MultiBid
  83. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  84. require.Nil(t, err)
  85. slotSet := [6]bool{true, false, true, false, true, false}
  86. maxBid := new(big.Int)
  87. maxBid.SetString("15000000000000000000", 10)
  88. minBid := new(big.Int)
  89. minBid.SetString("11000000000000000000", 10)
  90. budget := new(big.Int)
  91. budget.SetString("45200000000000000000", 10)
  92. _, err = auctionClient.AuctionMultiBid(budget, currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, deadline)
  93. require.Nil(t, err)
  94. // Add Blocks
  95. blockNum := int64(int(blocksPerSlot)*int(currentSlot+4) + int(genesisBlock))
  96. currentBlockNum, err := auctionClient.client.EthLastBlock()
  97. require.Nil(t, err)
  98. blocksToAdd := blockNum - currentBlockNum
  99. addBlocks(blocksToAdd, ethClientDialURL)
  100. // Forge
  101. args := new(RollupForgeBatchArgs)
  102. args.FeeIdxCoordinator = []common.Idx{} // When encoded, 64 times the 0 idx means that no idx to collect fees is specified.
  103. l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
  104. require.Nil(t, err)
  105. numTxsL1 := len(l1CoordinatorBytes) / common.L1CoordinatorTxBytesLen
  106. for i := 0; i < numTxsL1; i++ {
  107. bytesL1Coordinator := l1CoordinatorBytes[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  108. var signature []byte
  109. v := bytesL1Coordinator[0]
  110. s := bytesL1Coordinator[1:33]
  111. r := bytesL1Coordinator[33:65]
  112. signature = append(signature, r[:]...)
  113. signature = append(signature, s[:]...)
  114. signature = append(signature, v)
  115. l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, chainid, rollupClient.address)
  116. require.Nil(t, err)
  117. args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, *l1Tx)
  118. args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
  119. }
  120. args.L2TxsData = []common.L2Tx{}
  121. newStateRoot := new(big.Int)
  122. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  123. newExitRoot := new(big.Int)
  124. bytesNumExitRoot, err := hex.DecodeString("10a89d5fe8d488eda1ba371d633515739933c706c210c604f5bd209180daa43b")
  125. require.Nil(t, err)
  126. newExitRoot.SetBytes(bytesNumExitRoot)
  127. args.NewLastIdx = int64(256)
  128. args.NewStRoot = newStateRoot
  129. args.NewExitRoot = newExitRoot
  130. args.L1Batch = true
  131. args.VerifierIdx = 0
  132. args.ProofA[0] = big.NewInt(0)
  133. args.ProofA[1] = big.NewInt(0)
  134. args.ProofB[0][0] = big.NewInt(0)
  135. args.ProofB[0][1] = big.NewInt(0)
  136. args.ProofB[1][0] = big.NewInt(0)
  137. args.ProofB[1][1] = big.NewInt(0)
  138. args.ProofC[0] = big.NewInt(0)
  139. args.ProofC[1] = big.NewInt(0)
  140. argsForge = args
  141. _, err = rollupClient.RollupForgeBatch(argsForge)
  142. require.Nil(t, err)
  143. currentBlockNum, err = rollupClient.client.EthLastBlock()
  144. require.Nil(t, err)
  145. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  146. require.Nil(t, err)
  147. assert.Equal(t, int64(1), rollupEvents.ForgeBatch[0].BatchNum)
  148. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  149. }
  150. func TestRollupForgeBatchArgs(t *testing.T) {
  151. args, sender, err := rollupClient.RollupForgeBatchArgs(ethHashForge)
  152. require.Nil(t, err)
  153. assert.Equal(t, *sender, rollupClient.client.account.Address)
  154. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  155. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  156. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  157. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  158. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  159. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  160. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  161. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  162. }
  163. func TestRollupUpdateForgeL1L2BatchTimeout(t *testing.T) {
  164. newForgeL1L2BatchTimeout := int64(222)
  165. _, err := rollupClient.RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout)
  166. require.Nil(t, err)
  167. currentBlockNum, err := rollupClient.client.EthLastBlock()
  168. require.Nil(t, err)
  169. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  170. require.Nil(t, err)
  171. assert.Equal(t, newForgeL1L2BatchTimeout, rollupEvents.UpdateForgeL1L2BatchTimeout[0].NewForgeL1L2BatchTimeout)
  172. }
  173. func TestRollupUpdateFeeAddToken(t *testing.T) {
  174. newFeeAddToken := big.NewInt(12)
  175. _, err := rollupClient.RollupUpdateFeeAddToken(newFeeAddToken)
  176. require.Nil(t, err)
  177. currentBlockNum, err := rollupClient.client.EthLastBlock()
  178. require.Nil(t, err)
  179. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  180. require.Nil(t, err)
  181. assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
  182. }
  183. func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
  184. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  185. require.Nil(t, err)
  186. key := genKeysBjj(2)
  187. fromIdxInt64 := int64(0)
  188. toIdxInt64 := int64(0)
  189. tokenIDUint32 := uint32(0)
  190. l1Tx := common.L1Tx{
  191. FromBJJ: key.BJJPublicKey,
  192. FromIdx: common.Idx(fromIdxInt64),
  193. ToIdx: common.Idx(toIdxInt64),
  194. LoadAmount: big.NewInt(10),
  195. TokenID: common.TokenID(tokenIDUint32),
  196. Amount: big.NewInt(0),
  197. }
  198. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  199. require.Nil(t, err)
  200. currentBlockNum, err := rollupClient.client.EthLastBlock()
  201. require.Nil(t, err)
  202. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  203. require.Nil(t, err)
  204. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  205. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  206. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  207. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  208. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  209. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  210. }
  211. func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
  212. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  213. require.Nil(t, err)
  214. key := genKeysBjj(1)
  215. fromIdxInt64 := int64(0)
  216. toIdxInt64 := int64(0)
  217. l1Tx := common.L1Tx{
  218. FromBJJ: key.BJJPublicKey,
  219. FromIdx: common.Idx(fromIdxInt64),
  220. ToIdx: common.Idx(toIdxInt64),
  221. LoadAmount: big.NewInt(10),
  222. TokenID: common.TokenID(tokenHEZID),
  223. Amount: big.NewInt(0),
  224. }
  225. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  226. require.Nil(t, err)
  227. currentBlockNum, err := rollupClient.client.EthLastBlock()
  228. require.Nil(t, err)
  229. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  230. require.Nil(t, err)
  231. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  232. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  233. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  234. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  235. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  236. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  237. }
  238. func TestRollupL1UserTxERC20PermitCreateAccountDeposit(t *testing.T) {
  239. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  240. require.Nil(t, err)
  241. key := genKeysBjj(3)
  242. fromIdxInt64 := int64(0)
  243. toIdxInt64 := int64(0)
  244. l1Tx := common.L1Tx{
  245. FromBJJ: key.BJJPublicKey,
  246. FromIdx: common.Idx(fromIdxInt64),
  247. ToIdx: common.Idx(toIdxInt64),
  248. LoadAmount: big.NewInt(10),
  249. TokenID: common.TokenID(tokenIDERC777),
  250. Amount: big.NewInt(0),
  251. }
  252. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  253. require.Nil(t, err)
  254. currentBlockNum, err := rollupClient.client.EthLastBlock()
  255. require.Nil(t, err)
  256. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  257. require.Nil(t, err)
  258. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  259. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  260. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  261. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  262. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  263. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  264. }
  265. func TestRollupL1UserTxETHDeposit(t *testing.T) {
  266. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  267. require.Nil(t, err)
  268. key := genKeysBjj(2)
  269. fromIdxInt64 := int64(0)
  270. toIdxInt64 := int64(0)
  271. tokenIDUint32 := uint32(0)
  272. l1Tx := common.L1Tx{
  273. FromBJJ: key.BJJPublicKey,
  274. FromIdx: common.Idx(fromIdxInt64),
  275. ToIdx: common.Idx(toIdxInt64),
  276. LoadAmount: big.NewInt(10),
  277. TokenID: common.TokenID(tokenIDUint32),
  278. Amount: big.NewInt(0),
  279. }
  280. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  281. require.Nil(t, err)
  282. currentBlockNum, err := rollupClient.client.EthLastBlock()
  283. require.Nil(t, err)
  284. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  285. require.Nil(t, err)
  286. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  287. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  288. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  289. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  290. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  291. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  292. }
  293. func TestRollupL1UserTxERC20Deposit(t *testing.T) {
  294. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  295. require.Nil(t, err)
  296. key := genKeysBjj(1)
  297. fromIdxInt64 := int64(0)
  298. toIdxInt64 := int64(0)
  299. l1Tx := common.L1Tx{
  300. FromBJJ: key.BJJPublicKey,
  301. FromIdx: common.Idx(fromIdxInt64),
  302. ToIdx: common.Idx(toIdxInt64),
  303. LoadAmount: big.NewInt(10),
  304. TokenID: common.TokenID(tokenHEZID),
  305. Amount: big.NewInt(0),
  306. }
  307. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  308. require.Nil(t, err)
  309. currentBlockNum, err := rollupClient.client.EthLastBlock()
  310. require.Nil(t, err)
  311. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  312. require.Nil(t, err)
  313. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  314. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  315. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  316. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  317. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  318. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  319. }
  320. func TestRollupL1UserTxERC20PermitDeposit(t *testing.T) {
  321. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  322. require.Nil(t, err)
  323. key := genKeysBjj(3)
  324. fromIdxInt64 := int64(0)
  325. toIdxInt64 := int64(0)
  326. l1Tx := common.L1Tx{
  327. FromBJJ: key.BJJPublicKey,
  328. FromIdx: common.Idx(fromIdxInt64),
  329. ToIdx: common.Idx(toIdxInt64),
  330. LoadAmount: big.NewInt(10),
  331. TokenID: common.TokenID(tokenIDERC777),
  332. Amount: big.NewInt(0),
  333. }
  334. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  335. require.Nil(t, err)
  336. currentBlockNum, err := rollupClient.client.EthLastBlock()
  337. require.Nil(t, err)
  338. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  339. require.Nil(t, err)
  340. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  341. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  342. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  343. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  344. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  345. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  346. }
  347. func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
  348. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  349. require.Nil(t, err)
  350. key := genKeysBjj(2)
  351. fromIdxInt64 := int64(0)
  352. toIdxInt64 := int64(0)
  353. tokenIDUint32 := uint32(0)
  354. l1Tx := common.L1Tx{
  355. FromBJJ: key.BJJPublicKey,
  356. FromIdx: common.Idx(fromIdxInt64),
  357. ToIdx: common.Idx(toIdxInt64),
  358. LoadAmount: big.NewInt(10),
  359. TokenID: common.TokenID(tokenIDUint32),
  360. Amount: big.NewInt(0),
  361. }
  362. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  363. require.Nil(t, err)
  364. currentBlockNum, err := rollupClient.client.EthLastBlock()
  365. require.Nil(t, err)
  366. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  367. require.Nil(t, err)
  368. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  369. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  370. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  371. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  372. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  373. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  374. }
  375. func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
  376. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  377. require.Nil(t, err)
  378. key := genKeysBjj(1)
  379. fromIdxInt64 := int64(0)
  380. toIdxInt64 := int64(0)
  381. l1Tx := common.L1Tx{
  382. FromBJJ: key.BJJPublicKey,
  383. FromIdx: common.Idx(fromIdxInt64),
  384. ToIdx: common.Idx(toIdxInt64),
  385. LoadAmount: big.NewInt(10),
  386. TokenID: common.TokenID(tokenHEZID),
  387. Amount: big.NewInt(0),
  388. }
  389. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  390. require.Nil(t, err)
  391. currentBlockNum, err := rollupClient.client.EthLastBlock()
  392. require.Nil(t, err)
  393. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  394. require.Nil(t, err)
  395. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  396. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  397. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  398. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  399. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  400. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  401. }
  402. func TestRollupL1UserTxERC20PermitDepositTransfer(t *testing.T) {
  403. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  404. require.Nil(t, err)
  405. key := genKeysBjj(3)
  406. fromIdxInt64 := int64(0)
  407. toIdxInt64 := int64(0)
  408. l1Tx := common.L1Tx{
  409. FromBJJ: key.BJJPublicKey,
  410. FromIdx: common.Idx(fromIdxInt64),
  411. ToIdx: common.Idx(toIdxInt64),
  412. LoadAmount: big.NewInt(10),
  413. TokenID: common.TokenID(tokenIDERC777),
  414. Amount: big.NewInt(0),
  415. }
  416. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  417. require.Nil(t, err)
  418. currentBlockNum, err := rollupClient.client.EthLastBlock()
  419. require.Nil(t, err)
  420. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  421. require.Nil(t, err)
  422. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  423. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  424. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  425. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  426. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  427. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  428. }
  429. func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
  430. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  431. require.Nil(t, err)
  432. key := genKeysBjj(2)
  433. fromIdxInt64 := int64(0)
  434. toIdxInt64 := int64(0)
  435. tokenIDUint32 := uint32(0)
  436. l1Tx := common.L1Tx{
  437. FromBJJ: key.BJJPublicKey,
  438. FromIdx: common.Idx(fromIdxInt64),
  439. ToIdx: common.Idx(toIdxInt64),
  440. LoadAmount: big.NewInt(10),
  441. TokenID: common.TokenID(tokenIDUint32),
  442. Amount: big.NewInt(0),
  443. }
  444. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  445. require.Nil(t, err)
  446. currentBlockNum, err := rollupClient.client.EthLastBlock()
  447. require.Nil(t, err)
  448. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  449. require.Nil(t, err)
  450. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  451. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  452. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  453. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  454. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  455. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  456. }
  457. func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
  458. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  459. require.Nil(t, err)
  460. key := genKeysBjj(1)
  461. fromIdxInt64 := int64(0)
  462. toIdxInt64 := int64(0)
  463. l1Tx := common.L1Tx{
  464. FromBJJ: key.BJJPublicKey,
  465. FromIdx: common.Idx(fromIdxInt64),
  466. ToIdx: common.Idx(toIdxInt64),
  467. LoadAmount: big.NewInt(10),
  468. TokenID: common.TokenID(tokenHEZID),
  469. Amount: big.NewInt(0),
  470. }
  471. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  472. require.Nil(t, err)
  473. currentBlockNum, err := rollupClient.client.EthLastBlock()
  474. require.Nil(t, err)
  475. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  476. require.Nil(t, err)
  477. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  478. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  479. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  480. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  481. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  482. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  483. }
  484. func TestRollupL1UserTxERC20PermitCreateAccountDepositTransfer(t *testing.T) {
  485. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  486. require.Nil(t, err)
  487. key := genKeysBjj(3)
  488. fromIdxInt64 := int64(0)
  489. toIdxInt64 := int64(0)
  490. l1Tx := common.L1Tx{
  491. FromBJJ: key.BJJPublicKey,
  492. FromIdx: common.Idx(fromIdxInt64),
  493. ToIdx: common.Idx(toIdxInt64),
  494. LoadAmount: big.NewInt(10),
  495. TokenID: common.TokenID(tokenIDERC777),
  496. Amount: big.NewInt(0),
  497. }
  498. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  499. require.Nil(t, err)
  500. currentBlockNum, err := rollupClient.client.EthLastBlock()
  501. require.Nil(t, err)
  502. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  503. require.Nil(t, err)
  504. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  505. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  506. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  507. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  508. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  509. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  510. }
  511. func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
  512. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  513. require.Nil(t, err)
  514. key := genKeysBjj(2)
  515. fromIdxInt64 := int64(0)
  516. toIdxInt64 := int64(0)
  517. tokenIDUint32 := uint32(0)
  518. l1Tx := common.L1Tx{
  519. FromBJJ: key.BJJPublicKey,
  520. FromIdx: common.Idx(fromIdxInt64),
  521. ToIdx: common.Idx(toIdxInt64),
  522. LoadAmount: big.NewInt(10),
  523. TokenID: common.TokenID(tokenIDUint32),
  524. Amount: big.NewInt(0),
  525. }
  526. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  527. require.Nil(t, err)
  528. currentBlockNum, err := rollupClient.client.EthLastBlock()
  529. require.Nil(t, err)
  530. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  531. require.Nil(t, err)
  532. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  533. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  534. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  535. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  536. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  537. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  538. }
  539. func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
  540. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  541. require.Nil(t, err)
  542. key := genKeysBjj(1)
  543. fromIdxInt64 := int64(0)
  544. toIdxInt64 := int64(0)
  545. l1Tx := common.L1Tx{
  546. FromBJJ: key.BJJPublicKey,
  547. FromIdx: common.Idx(fromIdxInt64),
  548. ToIdx: common.Idx(toIdxInt64),
  549. LoadAmount: big.NewInt(10),
  550. TokenID: common.TokenID(tokenHEZID),
  551. Amount: big.NewInt(0),
  552. }
  553. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  554. require.Nil(t, err)
  555. currentBlockNum, err := rollupClient.client.EthLastBlock()
  556. require.Nil(t, err)
  557. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  558. require.Nil(t, err)
  559. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  560. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  561. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  562. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  563. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  564. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  565. }
  566. func TestRollupL1UserTxERC20PermitForceTransfer(t *testing.T) {
  567. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  568. require.Nil(t, err)
  569. key := genKeysBjj(3)
  570. fromIdxInt64 := int64(0)
  571. toIdxInt64 := int64(0)
  572. l1Tx := common.L1Tx{
  573. FromBJJ: key.BJJPublicKey,
  574. FromIdx: common.Idx(fromIdxInt64),
  575. ToIdx: common.Idx(toIdxInt64),
  576. LoadAmount: big.NewInt(10),
  577. TokenID: common.TokenID(tokenIDERC777),
  578. Amount: big.NewInt(0),
  579. }
  580. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  581. require.Nil(t, err)
  582. currentBlockNum, err := rollupClient.client.EthLastBlock()
  583. require.Nil(t, err)
  584. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  585. require.Nil(t, err)
  586. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  587. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  588. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  589. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  590. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  591. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  592. }
  593. func TestRollupL1UserTxETHForceExit(t *testing.T) {
  594. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  595. require.Nil(t, err)
  596. key := genKeysBjj(2)
  597. fromIdxInt64 := int64(0)
  598. toIdxInt64 := int64(0)
  599. tokenIDUint32 := uint32(0)
  600. l1Tx := common.L1Tx{
  601. FromBJJ: key.BJJPublicKey,
  602. FromIdx: common.Idx(fromIdxInt64),
  603. ToIdx: common.Idx(toIdxInt64),
  604. LoadAmount: big.NewInt(10),
  605. TokenID: common.TokenID(tokenIDUint32),
  606. Amount: big.NewInt(0),
  607. }
  608. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  609. require.Nil(t, err)
  610. currentBlockNum, err := rollupClient.client.EthLastBlock()
  611. require.Nil(t, err)
  612. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  613. require.Nil(t, err)
  614. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  615. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  616. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  617. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  618. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  619. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  620. }
  621. func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
  622. rollupClientAux2, err := NewRollupClient(ethereumClientAux2, hermezRollupAddressConst, tokenHEZ)
  623. require.Nil(t, err)
  624. key := genKeysBjj(1)
  625. fromIdxInt64 := int64(0)
  626. toIdxInt64 := int64(0)
  627. l1Tx := common.L1Tx{
  628. FromBJJ: key.BJJPublicKey,
  629. FromIdx: common.Idx(fromIdxInt64),
  630. ToIdx: common.Idx(toIdxInt64),
  631. LoadAmount: big.NewInt(10),
  632. TokenID: common.TokenID(tokenHEZID),
  633. Amount: big.NewInt(0),
  634. }
  635. _, err = rollupClientAux2.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenHEZID, toIdxInt64)
  636. require.Nil(t, err)
  637. currentBlockNum, err := rollupClient.client.EthLastBlock()
  638. require.Nil(t, err)
  639. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  640. require.Nil(t, err)
  641. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  642. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  643. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  644. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  645. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  646. assert.Equal(t, rollupClientAux2.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  647. }
  648. func TestRollupL1UserTxERC20PermitForceExit(t *testing.T) {
  649. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  650. require.Nil(t, err)
  651. key := genKeysBjj(3)
  652. fromIdxInt64 := int64(0)
  653. toIdxInt64 := int64(0)
  654. fromIdx := new(common.Idx)
  655. *fromIdx = 0
  656. l1Tx := common.L1Tx{
  657. FromBJJ: key.BJJPublicKey,
  658. FromIdx: common.Idx(fromIdxInt64),
  659. ToIdx: common.Idx(toIdxInt64),
  660. LoadAmount: big.NewInt(10),
  661. TokenID: common.TokenID(tokenIDERC777),
  662. Amount: big.NewInt(0),
  663. }
  664. _, err = rollupClientAux.RollupL1UserTxERC20Permit(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64, deadline)
  665. require.Nil(t, err)
  666. currentBlockNum, err := rollupClient.client.EthLastBlock()
  667. require.Nil(t, err)
  668. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  669. require.Nil(t, err)
  670. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  671. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  672. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  673. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  674. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  675. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  676. }
  677. func TestRollupForgeBatch2(t *testing.T) {
  678. // Forge
  679. args := new(RollupForgeBatchArgs)
  680. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  681. require.Nil(t, err)
  682. lenFeeIdxCoordinatorBytes := int(4)
  683. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  684. for i := 0; i < numFeeIdxCoordinator; i++ {
  685. var paddedFeeIdx [6]byte
  686. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  687. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  688. } else {
  689. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  690. }
  691. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  692. require.Nil(t, err)
  693. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  694. }
  695. newStateRoot := new(big.Int)
  696. newStateRoot.SetString("0", 10)
  697. newExitRoot := new(big.Int)
  698. newExitRoot.SetString("4694629460381124336935185586347620040847956843554725549791403956105308092690", 10)
  699. args.NewLastIdx = int64(1000)
  700. args.NewStRoot = newStateRoot
  701. args.NewExitRoot = newExitRoot
  702. args.L1Batch = true
  703. args.VerifierIdx = 0
  704. args.ProofA[0] = big.NewInt(0)
  705. args.ProofA[1] = big.NewInt(0)
  706. args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  707. args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  708. args.ProofC[0] = big.NewInt(0)
  709. args.ProofC[1] = big.NewInt(0)
  710. argsForge = args
  711. _, err = rollupClient.RollupForgeBatch(argsForge)
  712. require.Nil(t, err)
  713. currentBlockNum, err := rollupClient.client.EthLastBlock()
  714. require.Nil(t, err)
  715. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  716. require.Nil(t, err)
  717. assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
  718. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  719. }
  720. func TestRollupWithdrawMerkleProof(t *testing.T) {
  721. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZ)
  722. require.Nil(t, err)
  723. var pkComp babyjub.PublicKeyComp
  724. pkCompBE, err := hex.DecodeString("adc3b754f8da621967b073a787bef8eec7052f2ba712b23af57d98f65beea8b2")
  725. require.Nil(t, err)
  726. pkCompLE := common.SwapEndianness(pkCompBE)
  727. copy(pkComp[:], pkCompLE)
  728. // err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  729. // require.Nil(t, err)
  730. pk, err := pkComp.Decompress()
  731. require.Nil(t, err)
  732. require.Nil(t, err)
  733. tokenID := uint32(1)
  734. numExitRoot := int64(2)
  735. fromIdx := int64(256)
  736. amount := big.NewInt(10)
  737. // siblingBytes0, err := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
  738. // require.Nil(t, err)
  739. // siblingBytes1, err := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
  740. // require.Nil(t, err)
  741. var siblings []*big.Int
  742. // siblings = append(siblings, siblingBytes0)
  743. // siblings = append(siblings, siblingBytes1)
  744. instantWithdraw := true
  745. _, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
  746. require.Nil(t, err)
  747. currentBlockNum, err := rollupClient.client.EthLastBlock()
  748. require.Nil(t, err)
  749. rollupEvents, _, err := rollupClient.RollupEventsByBlock(currentBlockNum)
  750. require.Nil(t, err)
  751. assert.Equal(t, uint64(fromIdx), rollupEvents.Withdraw[0].Idx)
  752. assert.Equal(t, instantWithdraw, rollupEvents.Withdraw[0].InstantWithdraw)
  753. assert.Equal(t, uint64(numExitRoot), rollupEvents.Withdraw[0].NumExitRoot)
  754. }