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.

845 lines
34 KiB

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