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.

142 lines
5.9 KiB

  1. package eth
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. var rollupClient *RollupClient
  12. var auctionClient *AuctionClient
  13. var ethHashForge ethCommon.Hash
  14. var argsForge *RollupForgeBatchArgs
  15. var absoluteMaxL1L2BatchTimeout = int64(240)
  16. var maxTx = int64(512)
  17. var nLevels = int64(32)
  18. func TestRollupConstants(t *testing.T) {
  19. rollupConstants, err := rollupClient.RollupConstants()
  20. require.Nil(t, err)
  21. assert.Equal(t, absoluteMaxL1L2BatchTimeout, rollupConstants.AbsoluteMaxL1L2BatchTimeout)
  22. assert.Equal(t, auctionAddressConst, rollupConstants.HermezAuctionContract)
  23. assert.Equal(t, tokenERC777AddressConst, rollupConstants.TokenHEZ)
  24. assert.Equal(t, maxTx, rollupConstants.Verifiers[0].MaxTx)
  25. assert.Equal(t, nLevels, rollupConstants.Verifiers[0].NLevels)
  26. assert.Equal(t, governanceAddressConst, rollupConstants.HermezGovernanceDAOAddress)
  27. assert.Equal(t, safetyAddressConst, rollupConstants.SafetyAddress)
  28. assert.Equal(t, wdelayerAddressConst, rollupConstants.WithdrawDelayerContract)
  29. }
  30. func TestAddToken(t *testing.T) {
  31. feeAddToken := big.NewInt(10)
  32. // Addtoken
  33. _, err := rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
  34. require.Nil(t, err)
  35. }
  36. func TestRollupForgeBatch(t *testing.T) {
  37. // Register Coordinator
  38. forgerAddress := governanceAddressConst
  39. _, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
  40. require.Nil(t, err)
  41. // MultiBid
  42. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  43. require.Nil(t, err)
  44. slotSet := [6]bool{true, false, true, false, true, false}
  45. maxBid := new(big.Int)
  46. maxBid.SetString("15000000000000000000", 10)
  47. minBid := new(big.Int)
  48. minBid.SetString("11000000000000000000", 10)
  49. budget := new(big.Int)
  50. budget.SetString("45200000000000000000", 10)
  51. _, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
  52. require.Nil(t, err)
  53. // Add Blocks
  54. blockNum := int64(int(BLOCKSPERSLOT)*int(currentSlot+4) + genesisBlock)
  55. currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
  56. blocksToAdd := blockNum - currentBlockNum
  57. addBlocks(blocksToAdd, ethClientDialURL)
  58. // Forge
  59. args := new(RollupForgeBatchArgs)
  60. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  61. require.Nil(t, err)
  62. lenFeeIdxCoordinatorBytes := int(4)
  63. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  64. for i := 0; i < numFeeIdxCoordinator; i++ {
  65. var paddedFeeIdx [6]byte
  66. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  67. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  68. } else {
  69. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  70. }
  71. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  72. require.Nil(t, err)
  73. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  74. }
  75. l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
  76. require.Nil(t, err)
  77. numTxsL1 := len(l1CoordinatorBytes) / common.L1CoordinatorTxBytesLen
  78. for i := 0; i < numTxsL1; i++ {
  79. bytesL1Coordinator := l1CoordinatorBytes[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  80. var signature []byte
  81. v := bytesL1Coordinator[0]
  82. s := bytesL1Coordinator[1:33]
  83. r := bytesL1Coordinator[33:65]
  84. signature = append(signature, r[:]...)
  85. signature = append(signature, s[:]...)
  86. signature = append(signature, v)
  87. L1Tx, err := common.L1TxFromCoordinatorBytes(bytesL1Coordinator)
  88. require.Nil(t, err)
  89. args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, *L1Tx)
  90. args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
  91. }
  92. newStateRoot := new(big.Int)
  93. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  94. newExitRoot := big.NewInt(0)
  95. args.NewLastIdx = int64(256)
  96. args.NewStRoot = newStateRoot
  97. args.NewExitRoot = newExitRoot
  98. args.L1Batch = true
  99. args.VerifierIdx = 0
  100. args.ProofA[0] = big.NewInt(0)
  101. args.ProofA[1] = big.NewInt(0)
  102. args.ProofB[0][0] = big.NewInt(0)
  103. args.ProofB[0][1] = big.NewInt(0)
  104. args.ProofB[1][0] = big.NewInt(0)
  105. args.ProofB[1][1] = big.NewInt(0)
  106. args.ProofC[0] = big.NewInt(0)
  107. args.ProofC[1] = big.NewInt(0)
  108. argsForge = args
  109. _, err = rollupClient.RollupForgeBatch(argsForge)
  110. require.Nil(t, err)
  111. currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
  112. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  113. assert.Equal(t, int64(1), rollupEvents.ForgeBatch[0].BatchNum)
  114. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  115. }
  116. func TestRollupForgeBatchArgs(t *testing.T) {
  117. args, err := rollupClient.RollupForgeBatchArgs(ethHashForge)
  118. require.Nil(t, err)
  119. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  120. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  121. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  122. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  123. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  124. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  125. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  126. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  127. }