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.

820 lines
35 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 tokenIDERC20 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, tokenERC777AddressConst, 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 TestAddToken(t *testing.T) {
  61. var feeAddToken = new(big.Int)
  62. feeAddToken = big.NewInt(10)
  63. // Addtoken ERC20
  64. registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
  65. require.Nil(t, err)
  66. _, err = rollupClient.RollupAddToken(tokenERC20AddressConst, feeAddToken)
  67. require.Nil(t, err)
  68. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  69. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  70. assert.Equal(t, tokenERC20AddressConst, rollupEvents.AddToken[0].TokenAddress)
  71. assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
  72. tokenIDERC20 = rollupEvents.AddToken[0].TokenID
  73. // Addtoken ERC777
  74. registerTokensCount, err = rollupClient.RollupRegisterTokensCount()
  75. require.Nil(t, err)
  76. _, err = rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
  77. require.Nil(t, err)
  78. currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
  79. rollupEvents, _, _ = rollupClient.RollupEventsByBlock(currentBlockNum)
  80. assert.Equal(t, tokenERC777AddressConst, rollupEvents.AddToken[0].TokenAddress)
  81. assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
  82. tokenIDERC777 = rollupEvents.AddToken[0].TokenID
  83. }
  84. func TestRollupForgeBatch(t *testing.T) {
  85. // Register Coordinator
  86. forgerAddress := governanceAddressConst
  87. _, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
  88. require.Nil(t, err)
  89. // MultiBid
  90. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  91. require.Nil(t, err)
  92. slotSet := [6]bool{true, false, true, false, true, false}
  93. maxBid := new(big.Int)
  94. maxBid.SetString("15000000000000000000", 10)
  95. minBid := new(big.Int)
  96. minBid.SetString("11000000000000000000", 10)
  97. budget := new(big.Int)
  98. budget.SetString("45200000000000000000", 10)
  99. _, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
  100. require.Nil(t, err)
  101. // Add Blocks
  102. blockNum := int64(int(BLOCKSPERSLOT)*int(currentSlot+4) + genesisBlock)
  103. currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
  104. blocksToAdd := blockNum - currentBlockNum
  105. addBlocks(blocksToAdd, ethClientDialURL)
  106. // Forge
  107. args := new(RollupForgeBatchArgs)
  108. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  109. require.Nil(t, err)
  110. lenFeeIdxCoordinatorBytes := int(4)
  111. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  112. for i := 0; i < numFeeIdxCoordinator; i++ {
  113. var paddedFeeIdx [6]byte
  114. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  115. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  116. } else {
  117. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  118. }
  119. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  120. require.Nil(t, err)
  121. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  122. }
  123. l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
  124. require.Nil(t, err)
  125. numTxsL1 := len(l1CoordinatorBytes) / common.L1CoordinatorTxBytesLen
  126. for i := 0; i < numTxsL1; i++ {
  127. bytesL1Coordinator := l1CoordinatorBytes[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  128. var signature []byte
  129. v := bytesL1Coordinator[0]
  130. s := bytesL1Coordinator[1:33]
  131. r := bytesL1Coordinator[33:65]
  132. signature = append(signature, r[:]...)
  133. signature = append(signature, s[:]...)
  134. signature = append(signature, v)
  135. L1Tx, err := common.L1TxFromCoordinatorBytes(bytesL1Coordinator)
  136. require.Nil(t, err)
  137. args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, L1Tx)
  138. args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
  139. }
  140. newStateRoot := new(big.Int)
  141. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  142. newExitRoot := new(big.Int)
  143. bytesNumExitRoot, err := hex.DecodeString("10a89d5fe8d488eda1ba371d633515739933c706c210c604f5bd209180daa43b")
  144. require.Nil(t, err)
  145. newExitRoot.SetBytes(bytesNumExitRoot)
  146. args.NewLastIdx = int64(256)
  147. args.NewStRoot = newStateRoot
  148. args.NewExitRoot = newExitRoot
  149. args.L1Batch = true
  150. args.VerifierIdx = 0
  151. args.ProofA[0] = big.NewInt(0)
  152. args.ProofA[1] = big.NewInt(0)
  153. args.ProofB[0][0] = big.NewInt(0)
  154. args.ProofB[0][1] = big.NewInt(0)
  155. args.ProofB[1][0] = big.NewInt(0)
  156. args.ProofB[1][1] = big.NewInt(0)
  157. args.ProofC[0] = big.NewInt(0)
  158. args.ProofC[1] = big.NewInt(0)
  159. argsForge = args
  160. _, err = rollupClient.RollupForgeBatch(argsForge)
  161. require.Nil(t, err)
  162. currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
  163. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  164. assert.Equal(t, int64(1), rollupEvents.ForgeBatch[0].BatchNum)
  165. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  166. }
  167. func TestRollupForgeBatchArgs(t *testing.T) {
  168. args, err := rollupClient.RollupForgeBatchArgs(ethHashForge)
  169. require.Nil(t, err)
  170. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  171. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  172. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  173. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  174. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  175. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  176. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  177. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  178. }
  179. func TestRollupUpdateForgeL1L2BatchTimeout(t *testing.T) {
  180. newForgeL1L2BatchTimeout := uint8(222)
  181. _, err := rollupClient.RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout)
  182. require.Nil(t, err)
  183. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  184. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  185. assert.Equal(t, newForgeL1L2BatchTimeout, rollupEvents.UpdateForgeL1L2BatchTimeout[0].NewForgeL1L2BatchTimeout)
  186. }
  187. func TestRollupUpdateFeeAddToken(t *testing.T) {
  188. newFeeAddToken := big.NewInt(12)
  189. _, err := rollupClient.RollupUpdateFeeAddToken(newFeeAddToken)
  190. require.Nil(t, err)
  191. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  192. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  193. assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
  194. }
  195. func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
  196. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  197. require.Nil(t, err)
  198. key := genKeysBjj(2)
  199. fromIdxInt64 := int64(0)
  200. toIdxInt64 := int64(0)
  201. tokenIDUint32 := uint32(0)
  202. l1Tx := common.L1Tx{
  203. FromBJJ: key.BJJPublicKey,
  204. FromIdx: common.Idx(fromIdxInt64),
  205. ToIdx: common.Idx(toIdxInt64),
  206. LoadAmount: big.NewInt(10),
  207. TokenID: common.TokenID(tokenIDUint32),
  208. Amount: big.NewInt(0),
  209. }
  210. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  211. require.Nil(t, err)
  212. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  213. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  214. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  215. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  216. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  217. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  218. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  219. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  220. }
  221. func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
  222. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  223. require.Nil(t, err)
  224. key := genKeysBjj(1)
  225. fromIdxInt64 := int64(0)
  226. toIdxInt64 := int64(0)
  227. l1Tx := common.L1Tx{
  228. FromBJJ: key.BJJPublicKey,
  229. FromIdx: common.Idx(fromIdxInt64),
  230. ToIdx: common.Idx(toIdxInt64),
  231. LoadAmount: big.NewInt(10),
  232. TokenID: common.TokenID(tokenIDERC20),
  233. Amount: big.NewInt(0),
  234. }
  235. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  236. require.Nil(t, err)
  237. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  238. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  239. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  240. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  241. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  242. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  243. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  244. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  245. }
  246. func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
  247. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  248. require.Nil(t, err)
  249. key := genKeysBjj(3)
  250. fromIdxInt64 := int64(0)
  251. toIdxInt64 := int64(0)
  252. l1Tx := common.L1Tx{
  253. FromBJJ: key.BJJPublicKey,
  254. FromIdx: common.Idx(fromIdxInt64),
  255. ToIdx: common.Idx(toIdxInt64),
  256. LoadAmount: big.NewInt(10),
  257. TokenID: common.TokenID(tokenIDERC777),
  258. Amount: big.NewInt(0),
  259. }
  260. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  261. require.Nil(t, err)
  262. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  263. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  264. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  265. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  266. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  267. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  268. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  269. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  270. }
  271. func TestRollupL1UserTxETHDeposit(t *testing.T) {
  272. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  273. require.Nil(t, err)
  274. key := genKeysBjj(2)
  275. fromIdxInt64 := int64(0)
  276. toIdxInt64 := int64(0)
  277. tokenIDUint32 := uint32(0)
  278. l1Tx := common.L1Tx{
  279. FromBJJ: key.BJJPublicKey,
  280. FromIdx: common.Idx(fromIdxInt64),
  281. ToIdx: common.Idx(toIdxInt64),
  282. LoadAmount: big.NewInt(10),
  283. TokenID: common.TokenID(tokenIDUint32),
  284. Amount: big.NewInt(0),
  285. }
  286. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  287. require.Nil(t, err)
  288. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  289. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  290. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  291. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  292. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  293. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  294. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  295. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  296. }
  297. func TestRollupL1UserTxERC20Deposit(t *testing.T) {
  298. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  299. require.Nil(t, err)
  300. key := genKeysBjj(1)
  301. fromIdxInt64 := int64(0)
  302. toIdxInt64 := int64(0)
  303. l1Tx := common.L1Tx{
  304. FromBJJ: key.BJJPublicKey,
  305. FromIdx: common.Idx(fromIdxInt64),
  306. ToIdx: common.Idx(toIdxInt64),
  307. LoadAmount: big.NewInt(10),
  308. TokenID: common.TokenID(tokenIDERC20),
  309. Amount: big.NewInt(0),
  310. }
  311. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  312. require.Nil(t, err)
  313. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  314. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  315. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  316. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  317. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  318. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  319. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  320. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  321. }
  322. func TestRollupL1UserTxERC777Deposit(t *testing.T) {
  323. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  324. require.Nil(t, err)
  325. key := genKeysBjj(3)
  326. fromIdxInt64 := int64(0)
  327. toIdxInt64 := int64(0)
  328. l1Tx := common.L1Tx{
  329. FromBJJ: key.BJJPublicKey,
  330. FromIdx: common.Idx(fromIdxInt64),
  331. ToIdx: common.Idx(toIdxInt64),
  332. LoadAmount: big.NewInt(10),
  333. TokenID: common.TokenID(tokenIDERC777),
  334. Amount: big.NewInt(0),
  335. }
  336. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  337. require.Nil(t, err)
  338. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  339. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  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, tokenHEZAddressConst)
  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, _ := rollupClient.client.EthCurrentBlock()
  365. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  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. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  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(tokenIDERC20),
  385. Amount: big.NewInt(0),
  386. }
  387. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  388. require.Nil(t, err)
  389. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  390. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  391. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  392. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  393. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  394. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  395. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  396. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  397. }
  398. func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
  399. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  400. require.Nil(t, err)
  401. key := genKeysBjj(3)
  402. fromIdxInt64 := int64(0)
  403. toIdxInt64 := int64(0)
  404. l1Tx := common.L1Tx{
  405. FromBJJ: key.BJJPublicKey,
  406. FromIdx: common.Idx(fromIdxInt64),
  407. ToIdx: common.Idx(toIdxInt64),
  408. LoadAmount: big.NewInt(10),
  409. TokenID: common.TokenID(tokenIDERC777),
  410. Amount: big.NewInt(0),
  411. }
  412. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  413. require.Nil(t, err)
  414. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  415. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  416. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  417. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  418. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  419. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  420. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  421. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  422. }
  423. func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
  424. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  425. require.Nil(t, err)
  426. key := genKeysBjj(2)
  427. fromIdxInt64 := int64(0)
  428. toIdxInt64 := int64(0)
  429. tokenIDUint32 := uint32(0)
  430. l1Tx := common.L1Tx{
  431. FromBJJ: key.BJJPublicKey,
  432. FromIdx: common.Idx(fromIdxInt64),
  433. ToIdx: common.Idx(toIdxInt64),
  434. LoadAmount: big.NewInt(10),
  435. TokenID: common.TokenID(tokenIDUint32),
  436. Amount: big.NewInt(0),
  437. }
  438. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  439. require.Nil(t, err)
  440. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  441. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  442. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  443. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  444. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  445. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  446. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  447. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  448. }
  449. func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
  450. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  451. require.Nil(t, err)
  452. key := genKeysBjj(1)
  453. fromIdxInt64 := int64(0)
  454. toIdxInt64 := int64(0)
  455. l1Tx := common.L1Tx{
  456. FromBJJ: key.BJJPublicKey,
  457. FromIdx: common.Idx(fromIdxInt64),
  458. ToIdx: common.Idx(toIdxInt64),
  459. LoadAmount: big.NewInt(10),
  460. TokenID: common.TokenID(tokenIDERC20),
  461. Amount: big.NewInt(0),
  462. }
  463. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  464. require.Nil(t, err)
  465. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  466. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  467. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  468. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  469. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  470. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  471. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  472. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  473. }
  474. func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
  475. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  476. require.Nil(t, err)
  477. key := genKeysBjj(3)
  478. fromIdxInt64 := int64(0)
  479. toIdxInt64 := int64(0)
  480. l1Tx := common.L1Tx{
  481. FromBJJ: key.BJJPublicKey,
  482. FromIdx: common.Idx(fromIdxInt64),
  483. ToIdx: common.Idx(toIdxInt64),
  484. LoadAmount: big.NewInt(10),
  485. TokenID: common.TokenID(tokenIDERC777),
  486. Amount: big.NewInt(0),
  487. }
  488. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  489. require.Nil(t, err)
  490. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  491. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  492. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  493. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  494. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  495. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  496. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  497. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  498. }
  499. func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
  500. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  501. require.Nil(t, err)
  502. key := genKeysBjj(2)
  503. fromIdxInt64 := int64(0)
  504. toIdxInt64 := int64(0)
  505. tokenIDUint32 := uint32(0)
  506. l1Tx := common.L1Tx{
  507. FromBJJ: key.BJJPublicKey,
  508. FromIdx: common.Idx(fromIdxInt64),
  509. ToIdx: common.Idx(toIdxInt64),
  510. LoadAmount: big.NewInt(10),
  511. TokenID: common.TokenID(tokenIDUint32),
  512. Amount: big.NewInt(0),
  513. }
  514. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  515. require.Nil(t, err)
  516. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  517. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  518. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  519. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  520. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  521. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  522. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  523. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  524. }
  525. func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
  526. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  527. require.Nil(t, err)
  528. key := genKeysBjj(1)
  529. fromIdxInt64 := int64(0)
  530. toIdxInt64 := int64(0)
  531. l1Tx := common.L1Tx{
  532. FromBJJ: key.BJJPublicKey,
  533. FromIdx: common.Idx(fromIdxInt64),
  534. ToIdx: common.Idx(toIdxInt64),
  535. LoadAmount: big.NewInt(10),
  536. TokenID: common.TokenID(tokenIDERC20),
  537. Amount: big.NewInt(0),
  538. }
  539. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  540. require.Nil(t, err)
  541. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  542. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  543. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  544. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  545. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  546. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  547. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  548. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  549. }
  550. func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
  551. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  552. require.Nil(t, err)
  553. key := genKeysBjj(3)
  554. fromIdxInt64 := int64(0)
  555. toIdxInt64 := int64(0)
  556. l1Tx := common.L1Tx{
  557. FromBJJ: key.BJJPublicKey,
  558. FromIdx: common.Idx(fromIdxInt64),
  559. ToIdx: common.Idx(toIdxInt64),
  560. LoadAmount: big.NewInt(10),
  561. TokenID: common.TokenID(tokenIDERC777),
  562. Amount: big.NewInt(0),
  563. }
  564. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  565. require.Nil(t, err)
  566. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  567. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  568. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  569. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  570. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  571. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  572. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  573. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  574. }
  575. func TestRollupL1UserTxETHForceExit(t *testing.T) {
  576. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  577. require.Nil(t, err)
  578. key := genKeysBjj(2)
  579. fromIdxInt64 := int64(0)
  580. toIdxInt64 := int64(0)
  581. tokenIDUint32 := uint32(0)
  582. l1Tx := common.L1Tx{
  583. FromBJJ: key.BJJPublicKey,
  584. FromIdx: common.Idx(fromIdxInt64),
  585. ToIdx: common.Idx(toIdxInt64),
  586. LoadAmount: big.NewInt(10),
  587. TokenID: common.TokenID(tokenIDUint32),
  588. Amount: big.NewInt(0),
  589. }
  590. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  591. require.Nil(t, err)
  592. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  593. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  594. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  595. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  596. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  597. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  598. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  599. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  600. }
  601. func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
  602. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  603. require.Nil(t, err)
  604. key := genKeysBjj(1)
  605. fromIdxInt64 := int64(0)
  606. toIdxInt64 := int64(0)
  607. l1Tx := common.L1Tx{
  608. FromBJJ: key.BJJPublicKey,
  609. FromIdx: common.Idx(fromIdxInt64),
  610. ToIdx: common.Idx(toIdxInt64),
  611. LoadAmount: big.NewInt(10),
  612. TokenID: common.TokenID(tokenIDERC20),
  613. Amount: big.NewInt(0),
  614. }
  615. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  616. require.Nil(t, err)
  617. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  618. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  619. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  620. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  621. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  622. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  623. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  624. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  625. }
  626. func TestRollupL1UserTxERC777ForceExit(t *testing.T) {
  627. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  628. require.Nil(t, err)
  629. key := genKeysBjj(3)
  630. fromIdxInt64 := int64(0)
  631. toIdxInt64 := int64(0)
  632. fromIdx := new(common.Idx)
  633. *fromIdx = 0
  634. l1Tx := common.L1Tx{
  635. FromBJJ: key.BJJPublicKey,
  636. FromIdx: common.Idx(fromIdxInt64),
  637. ToIdx: common.Idx(toIdxInt64),
  638. LoadAmount: big.NewInt(10),
  639. TokenID: common.TokenID(tokenIDERC777),
  640. Amount: big.NewInt(0),
  641. }
  642. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  643. require.Nil(t, err)
  644. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  645. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  646. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  647. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  648. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  649. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  650. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  651. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  652. }
  653. func TestRollupForgeBatch2(t *testing.T) {
  654. // Forge
  655. args := new(RollupForgeBatchArgs)
  656. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  657. require.Nil(t, err)
  658. lenFeeIdxCoordinatorBytes := int(4)
  659. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  660. for i := 0; i < numFeeIdxCoordinator; i++ {
  661. var paddedFeeIdx [6]byte
  662. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  663. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  664. } else {
  665. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  666. }
  667. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  668. require.Nil(t, err)
  669. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  670. }
  671. newStateRoot := new(big.Int)
  672. newStateRoot.SetString("0", 10)
  673. newExitRoot := new(big.Int)
  674. newExitRoot.SetString("6442511778188868333499919207091562876207840300369859025739972956758642594045", 10)
  675. args.NewLastIdx = int64(1000)
  676. args.NewStRoot = newStateRoot
  677. args.NewExitRoot = newExitRoot
  678. args.L1Batch = true
  679. args.VerifierIdx = 0
  680. args.ProofA[0] = big.NewInt(0)
  681. args.ProofA[1] = big.NewInt(0)
  682. args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  683. args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  684. args.ProofC[0] = big.NewInt(0)
  685. args.ProofC[1] = big.NewInt(0)
  686. argsForge = args
  687. _, err = rollupClient.RollupForgeBatch(argsForge)
  688. require.Nil(t, err)
  689. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  690. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  691. assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
  692. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  693. }
  694. func TestRollupWithdrawMerkleProof(t *testing.T) {
  695. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  696. require.Nil(t, err)
  697. var pkComp babyjub.PublicKeyComp
  698. pkCompB, err := hex.DecodeString("9a0c13552c3a0b7b2b63ec4ab8a906b4af471ef3aa4463491ff08a0b489ac50f")
  699. require.Nil(t, err)
  700. pkCompL := common.SwapEndianness(pkCompB)
  701. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  702. require.Nil(t, err)
  703. pk, err := pkComp.Decompress()
  704. require.Nil(t, err)
  705. require.Nil(t, err)
  706. tokenID := uint32(1)
  707. numExitRoot := int64(2)
  708. fromIdx := int64(256)
  709. amount := big.NewInt(10)
  710. siblingBytes0, _ := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
  711. require.Nil(t, err)
  712. siblingBytes1, _ := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
  713. require.Nil(t, err)
  714. var siblings []*big.Int
  715. siblings = append(siblings, siblingBytes0)
  716. siblings = append(siblings, siblingBytes1)
  717. instantWithdraw := true
  718. _, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
  719. require.Nil(t, err)
  720. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  721. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  722. assert.Equal(t, uint64(fromIdx), rollupEvents.WithdrawEvent[0].Idx)
  723. assert.Equal(t, instantWithdraw, rollupEvents.WithdrawEvent[0].InstantWithdraw)
  724. assert.Equal(t, uint64(numExitRoot), rollupEvents.WithdrawEvent[0].NumExitRoot)
  725. }