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.

821 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.L1TxFromCoordinatorBytes(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, err := rollupClient.RollupForgeBatchArgs(ethHashForge)
  170. require.Nil(t, err)
  171. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  172. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  173. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  174. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  175. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  176. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  177. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  178. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  179. }
  180. func TestRollupUpdateForgeL1L2BatchTimeout(t *testing.T) {
  181. newForgeL1L2BatchTimeout := int64(222)
  182. _, err := rollupClient.RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout)
  183. require.Nil(t, err)
  184. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  185. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  186. assert.Equal(t, newForgeL1L2BatchTimeout, rollupEvents.UpdateForgeL1L2BatchTimeout[0].NewForgeL1L2BatchTimeout)
  187. }
  188. func TestRollupUpdateFeeAddToken(t *testing.T) {
  189. newFeeAddToken := big.NewInt(12)
  190. _, err := rollupClient.RollupUpdateFeeAddToken(newFeeAddToken)
  191. require.Nil(t, err)
  192. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  193. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  194. assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
  195. }
  196. func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
  197. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  198. require.Nil(t, err)
  199. key := genKeysBjj(2)
  200. fromIdxInt64 := int64(0)
  201. toIdxInt64 := int64(0)
  202. tokenIDUint32 := uint32(0)
  203. l1Tx := common.L1Tx{
  204. FromBJJ: key.BJJPublicKey,
  205. FromIdx: common.Idx(fromIdxInt64),
  206. ToIdx: common.Idx(toIdxInt64),
  207. LoadAmount: big.NewInt(10),
  208. TokenID: common.TokenID(tokenIDUint32),
  209. Amount: big.NewInt(0),
  210. }
  211. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  212. require.Nil(t, err)
  213. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  214. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  215. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  216. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  217. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  218. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  219. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  220. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  221. }
  222. func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
  223. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  224. require.Nil(t, err)
  225. key := genKeysBjj(1)
  226. fromIdxInt64 := int64(0)
  227. toIdxInt64 := int64(0)
  228. l1Tx := common.L1Tx{
  229. FromBJJ: key.BJJPublicKey,
  230. FromIdx: common.Idx(fromIdxInt64),
  231. ToIdx: common.Idx(toIdxInt64),
  232. LoadAmount: big.NewInt(10),
  233. TokenID: common.TokenID(tokenIDERC20),
  234. Amount: big.NewInt(0),
  235. }
  236. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  237. require.Nil(t, err)
  238. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  239. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  240. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  241. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  242. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  243. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  244. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  245. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  246. }
  247. func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
  248. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  249. require.Nil(t, err)
  250. key := genKeysBjj(3)
  251. fromIdxInt64 := int64(0)
  252. toIdxInt64 := int64(0)
  253. l1Tx := common.L1Tx{
  254. FromBJJ: key.BJJPublicKey,
  255. FromIdx: common.Idx(fromIdxInt64),
  256. ToIdx: common.Idx(toIdxInt64),
  257. LoadAmount: big.NewInt(10),
  258. TokenID: common.TokenID(tokenIDERC777),
  259. Amount: big.NewInt(0),
  260. }
  261. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  262. require.Nil(t, err)
  263. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  264. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  265. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  266. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  267. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  268. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  269. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  270. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  271. }
  272. func TestRollupL1UserTxETHDeposit(t *testing.T) {
  273. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  274. require.Nil(t, err)
  275. key := genKeysBjj(2)
  276. fromIdxInt64 := int64(0)
  277. toIdxInt64 := int64(0)
  278. tokenIDUint32 := uint32(0)
  279. l1Tx := common.L1Tx{
  280. FromBJJ: key.BJJPublicKey,
  281. FromIdx: common.Idx(fromIdxInt64),
  282. ToIdx: common.Idx(toIdxInt64),
  283. LoadAmount: big.NewInt(10),
  284. TokenID: common.TokenID(tokenIDUint32),
  285. Amount: big.NewInt(0),
  286. }
  287. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  288. require.Nil(t, err)
  289. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  290. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  291. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  292. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  293. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  294. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  295. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  296. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  297. }
  298. func TestRollupL1UserTxERC20Deposit(t *testing.T) {
  299. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  300. require.Nil(t, err)
  301. key := genKeysBjj(1)
  302. fromIdxInt64 := int64(0)
  303. toIdxInt64 := int64(0)
  304. l1Tx := common.L1Tx{
  305. FromBJJ: key.BJJPublicKey,
  306. FromIdx: common.Idx(fromIdxInt64),
  307. ToIdx: common.Idx(toIdxInt64),
  308. LoadAmount: big.NewInt(10),
  309. TokenID: common.TokenID(tokenIDERC20),
  310. Amount: big.NewInt(0),
  311. }
  312. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  313. require.Nil(t, err)
  314. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  315. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  316. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  317. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  318. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  319. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  320. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  321. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  322. }
  323. func TestRollupL1UserTxERC777Deposit(t *testing.T) {
  324. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  325. require.Nil(t, err)
  326. key := genKeysBjj(3)
  327. fromIdxInt64 := int64(0)
  328. toIdxInt64 := int64(0)
  329. l1Tx := common.L1Tx{
  330. FromBJJ: key.BJJPublicKey,
  331. FromIdx: common.Idx(fromIdxInt64),
  332. ToIdx: common.Idx(toIdxInt64),
  333. LoadAmount: big.NewInt(10),
  334. TokenID: common.TokenID(tokenIDERC777),
  335. Amount: big.NewInt(0),
  336. }
  337. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  338. require.Nil(t, err)
  339. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  340. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  341. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  342. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  343. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  344. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  345. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  346. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  347. }
  348. func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
  349. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  350. require.Nil(t, err)
  351. key := genKeysBjj(2)
  352. fromIdxInt64 := int64(0)
  353. toIdxInt64 := int64(0)
  354. tokenIDUint32 := uint32(0)
  355. l1Tx := common.L1Tx{
  356. FromBJJ: key.BJJPublicKey,
  357. FromIdx: common.Idx(fromIdxInt64),
  358. ToIdx: common.Idx(toIdxInt64),
  359. LoadAmount: big.NewInt(10),
  360. TokenID: common.TokenID(tokenIDUint32),
  361. Amount: big.NewInt(0),
  362. }
  363. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  364. require.Nil(t, err)
  365. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  366. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  367. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  368. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  369. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  370. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  371. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  372. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  373. }
  374. func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
  375. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  376. require.Nil(t, err)
  377. key := genKeysBjj(1)
  378. fromIdxInt64 := int64(0)
  379. toIdxInt64 := int64(0)
  380. l1Tx := common.L1Tx{
  381. FromBJJ: key.BJJPublicKey,
  382. FromIdx: common.Idx(fromIdxInt64),
  383. ToIdx: common.Idx(toIdxInt64),
  384. LoadAmount: big.NewInt(10),
  385. TokenID: common.TokenID(tokenIDERC20),
  386. Amount: big.NewInt(0),
  387. }
  388. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  389. require.Nil(t, err)
  390. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  391. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  392. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  393. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  394. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  395. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  396. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  397. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  398. }
  399. func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
  400. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  401. require.Nil(t, err)
  402. key := genKeysBjj(3)
  403. fromIdxInt64 := int64(0)
  404. toIdxInt64 := int64(0)
  405. l1Tx := common.L1Tx{
  406. FromBJJ: key.BJJPublicKey,
  407. FromIdx: common.Idx(fromIdxInt64),
  408. ToIdx: common.Idx(toIdxInt64),
  409. LoadAmount: big.NewInt(10),
  410. TokenID: common.TokenID(tokenIDERC777),
  411. Amount: big.NewInt(0),
  412. }
  413. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  414. require.Nil(t, err)
  415. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  416. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  417. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  418. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  419. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  420. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  421. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  422. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  423. }
  424. func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
  425. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  426. require.Nil(t, err)
  427. key := genKeysBjj(2)
  428. fromIdxInt64 := int64(0)
  429. toIdxInt64 := int64(0)
  430. tokenIDUint32 := uint32(0)
  431. l1Tx := common.L1Tx{
  432. FromBJJ: key.BJJPublicKey,
  433. FromIdx: common.Idx(fromIdxInt64),
  434. ToIdx: common.Idx(toIdxInt64),
  435. LoadAmount: big.NewInt(10),
  436. TokenID: common.TokenID(tokenIDUint32),
  437. Amount: big.NewInt(0),
  438. }
  439. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  440. require.Nil(t, err)
  441. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  442. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  443. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  444. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  445. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  446. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  447. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  448. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  449. }
  450. func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
  451. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  452. require.Nil(t, err)
  453. key := genKeysBjj(1)
  454. fromIdxInt64 := int64(0)
  455. toIdxInt64 := int64(0)
  456. l1Tx := common.L1Tx{
  457. FromBJJ: key.BJJPublicKey,
  458. FromIdx: common.Idx(fromIdxInt64),
  459. ToIdx: common.Idx(toIdxInt64),
  460. LoadAmount: big.NewInt(10),
  461. TokenID: common.TokenID(tokenIDERC20),
  462. Amount: big.NewInt(0),
  463. }
  464. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  465. require.Nil(t, err)
  466. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  467. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  468. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  469. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  470. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  471. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  472. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  473. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  474. }
  475. func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
  476. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  477. require.Nil(t, err)
  478. key := genKeysBjj(3)
  479. fromIdxInt64 := int64(0)
  480. toIdxInt64 := int64(0)
  481. l1Tx := common.L1Tx{
  482. FromBJJ: key.BJJPublicKey,
  483. FromIdx: common.Idx(fromIdxInt64),
  484. ToIdx: common.Idx(toIdxInt64),
  485. LoadAmount: big.NewInt(10),
  486. TokenID: common.TokenID(tokenIDERC777),
  487. Amount: big.NewInt(0),
  488. }
  489. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  490. require.Nil(t, err)
  491. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  492. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  493. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  494. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  495. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  496. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  497. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  498. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  499. }
  500. func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
  501. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  502. require.Nil(t, err)
  503. key := genKeysBjj(2)
  504. fromIdxInt64 := int64(0)
  505. toIdxInt64 := int64(0)
  506. tokenIDUint32 := uint32(0)
  507. l1Tx := common.L1Tx{
  508. FromBJJ: key.BJJPublicKey,
  509. FromIdx: common.Idx(fromIdxInt64),
  510. ToIdx: common.Idx(toIdxInt64),
  511. LoadAmount: big.NewInt(10),
  512. TokenID: common.TokenID(tokenIDUint32),
  513. Amount: big.NewInt(0),
  514. }
  515. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  516. require.Nil(t, err)
  517. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  518. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  519. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  520. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  521. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  522. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  523. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  524. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  525. }
  526. func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
  527. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  528. require.Nil(t, err)
  529. key := genKeysBjj(1)
  530. fromIdxInt64 := int64(0)
  531. toIdxInt64 := int64(0)
  532. l1Tx := common.L1Tx{
  533. FromBJJ: key.BJJPublicKey,
  534. FromIdx: common.Idx(fromIdxInt64),
  535. ToIdx: common.Idx(toIdxInt64),
  536. LoadAmount: big.NewInt(10),
  537. TokenID: common.TokenID(tokenIDERC20),
  538. Amount: big.NewInt(0),
  539. }
  540. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  541. require.Nil(t, err)
  542. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  543. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  544. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  545. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  546. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  547. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  548. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  549. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  550. }
  551. func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
  552. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  553. require.Nil(t, err)
  554. key := genKeysBjj(3)
  555. fromIdxInt64 := int64(0)
  556. toIdxInt64 := int64(0)
  557. l1Tx := common.L1Tx{
  558. FromBJJ: key.BJJPublicKey,
  559. FromIdx: common.Idx(fromIdxInt64),
  560. ToIdx: common.Idx(toIdxInt64),
  561. LoadAmount: big.NewInt(10),
  562. TokenID: common.TokenID(tokenIDERC777),
  563. Amount: big.NewInt(0),
  564. }
  565. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  566. require.Nil(t, err)
  567. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  568. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  569. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  570. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  571. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  572. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  573. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  574. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  575. }
  576. func TestRollupL1UserTxETHForceExit(t *testing.T) {
  577. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  578. require.Nil(t, err)
  579. key := genKeysBjj(2)
  580. fromIdxInt64 := int64(0)
  581. toIdxInt64 := int64(0)
  582. tokenIDUint32 := uint32(0)
  583. l1Tx := common.L1Tx{
  584. FromBJJ: key.BJJPublicKey,
  585. FromIdx: common.Idx(fromIdxInt64),
  586. ToIdx: common.Idx(toIdxInt64),
  587. LoadAmount: big.NewInt(10),
  588. TokenID: common.TokenID(tokenIDUint32),
  589. Amount: big.NewInt(0),
  590. }
  591. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
  592. require.Nil(t, err)
  593. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  594. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  595. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  596. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  597. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  598. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  599. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  600. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  601. }
  602. func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
  603. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  604. require.Nil(t, err)
  605. key := genKeysBjj(1)
  606. fromIdxInt64 := int64(0)
  607. toIdxInt64 := int64(0)
  608. l1Tx := common.L1Tx{
  609. FromBJJ: key.BJJPublicKey,
  610. FromIdx: common.Idx(fromIdxInt64),
  611. ToIdx: common.Idx(toIdxInt64),
  612. LoadAmount: big.NewInt(10),
  613. TokenID: common.TokenID(tokenIDERC20),
  614. Amount: big.NewInt(0),
  615. }
  616. _, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
  617. require.Nil(t, err)
  618. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  619. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  620. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  621. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  622. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  623. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  624. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  625. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  626. }
  627. func TestRollupL1UserTxERC777ForceExit(t *testing.T) {
  628. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  629. require.Nil(t, err)
  630. key := genKeysBjj(3)
  631. fromIdxInt64 := int64(0)
  632. toIdxInt64 := int64(0)
  633. fromIdx := new(common.Idx)
  634. *fromIdx = 0
  635. l1Tx := common.L1Tx{
  636. FromBJJ: key.BJJPublicKey,
  637. FromIdx: common.Idx(fromIdxInt64),
  638. ToIdx: common.Idx(toIdxInt64),
  639. LoadAmount: big.NewInt(10),
  640. TokenID: common.TokenID(tokenIDERC777),
  641. Amount: big.NewInt(0),
  642. }
  643. _, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
  644. require.Nil(t, err)
  645. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  646. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  647. assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
  648. assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
  649. assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
  650. assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
  651. assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
  652. assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
  653. }
  654. func TestRollupForgeBatch2(t *testing.T) {
  655. // Forge
  656. args := new(RollupForgeBatchArgs)
  657. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  658. require.Nil(t, err)
  659. lenFeeIdxCoordinatorBytes := int(4)
  660. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  661. for i := 0; i < numFeeIdxCoordinator; i++ {
  662. var paddedFeeIdx [6]byte
  663. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  664. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  665. } else {
  666. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  667. }
  668. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  669. require.Nil(t, err)
  670. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  671. }
  672. newStateRoot := new(big.Int)
  673. newStateRoot.SetString("0", 10)
  674. newExitRoot := new(big.Int)
  675. newExitRoot.SetString("6442511778188868333499919207091562876207840300369859025739972956758642594045", 10)
  676. args.NewLastIdx = int64(1000)
  677. args.NewStRoot = newStateRoot
  678. args.NewExitRoot = newExitRoot
  679. args.L1Batch = true
  680. args.VerifierIdx = 0
  681. args.ProofA[0] = big.NewInt(0)
  682. args.ProofA[1] = big.NewInt(0)
  683. args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  684. args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
  685. args.ProofC[0] = big.NewInt(0)
  686. args.ProofC[1] = big.NewInt(0)
  687. argsForge = args
  688. _, err = rollupClient.RollupForgeBatch(argsForge)
  689. require.Nil(t, err)
  690. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  691. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  692. assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
  693. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  694. }
  695. func TestRollupWithdrawMerkleProof(t *testing.T) {
  696. rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
  697. require.Nil(t, err)
  698. var pkComp babyjub.PublicKeyComp
  699. pkCompB, err := hex.DecodeString("9a0c13552c3a0b7b2b63ec4ab8a906b4af471ef3aa4463491ff08a0b489ac50f")
  700. require.Nil(t, err)
  701. pkCompL := common.SwapEndianness(pkCompB)
  702. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  703. require.Nil(t, err)
  704. pk, err := pkComp.Decompress()
  705. require.Nil(t, err)
  706. require.Nil(t, err)
  707. tokenID := uint32(1)
  708. numExitRoot := int64(2)
  709. fromIdx := int64(256)
  710. amount := big.NewInt(10)
  711. siblingBytes0, _ := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
  712. require.Nil(t, err)
  713. siblingBytes1, _ := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
  714. require.Nil(t, err)
  715. var siblings []*big.Int
  716. siblings = append(siblings, siblingBytes0)
  717. siblings = append(siblings, siblingBytes1)
  718. instantWithdraw := true
  719. _, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
  720. require.Nil(t, err)
  721. currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
  722. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  723. assert.Equal(t, uint64(fromIdx), rollupEvents.WithdrawEvent[0].Idx)
  724. assert.Equal(t, instantWithdraw, rollupEvents.WithdrawEvent[0].InstantWithdraw)
  725. assert.Equal(t, uint64(numExitRoot), rollupEvents.WithdrawEvent[0].NumExitRoot)
  726. }