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.

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