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.

143 lines
6.0 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. var feeAddToken = new(big.Int)
  32. feeAddToken = big.NewInt(10)
  33. // Addtoken
  34. _, err := rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
  35. require.Nil(t, err)
  36. }
  37. func TestRollupForgeBatch(t *testing.T) {
  38. // Register Coordinator
  39. forgerAddress := governanceAddressConst
  40. _, err := auctionClient.AuctionSetCoordinator(forgerAddress, URL)
  41. require.Nil(t, err)
  42. // MultiBid
  43. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  44. require.Nil(t, err)
  45. slotSet := [6]bool{true, false, true, false, true, false}
  46. maxBid := new(big.Int)
  47. maxBid.SetString("15000000000000000000", 10)
  48. minBid := new(big.Int)
  49. minBid.SetString("11000000000000000000", 10)
  50. budget := new(big.Int)
  51. budget.SetString("45200000000000000000", 10)
  52. _, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
  53. require.Nil(t, err)
  54. // Add Blocks
  55. blockNum := int64(int(BLOCKSPERSLOT)*int(currentSlot+4) + genesisBlock)
  56. currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
  57. blocksToAdd := blockNum - currentBlockNum
  58. addBlocks(blocksToAdd, ethClientDialURL)
  59. // Forge
  60. args := new(RollupForgeBatchArgs)
  61. feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
  62. require.Nil(t, err)
  63. lenFeeIdxCoordinatorBytes := int(4)
  64. numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
  65. for i := 0; i < numFeeIdxCoordinator; i++ {
  66. var paddedFeeIdx [6]byte
  67. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  68. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  69. } else {
  70. copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  71. }
  72. FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  73. require.Nil(t, err)
  74. args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
  75. }
  76. l1CoordinatorBytes, err := hex.DecodeString("1c660323607bb113e586183609964a333d07ebe4bef3be82ec13af453bae9590bd7711cdb6abf42f176eadfbe5506fbef5e092e5543733f91b0061d9a7747fa10694a915a6470fa230de387b51e6f4db0b09787867778687b55197ad6d6a86eac000000001")
  77. require.Nil(t, err)
  78. numTxsL1 := len(l1CoordinatorBytes) / common.L1CoordinatorTxBytesLen
  79. for i := 0; i < numTxsL1; i++ {
  80. bytesL1Coordinator := l1CoordinatorBytes[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen]
  81. var signature []byte
  82. v := bytesL1Coordinator[0]
  83. s := bytesL1Coordinator[1:33]
  84. r := bytesL1Coordinator[33:65]
  85. signature = append(signature, r[:]...)
  86. signature = append(signature, s[:]...)
  87. signature = append(signature, v)
  88. L1Tx, err := common.L1TxFromCoordinatorBytes(bytesL1Coordinator)
  89. require.Nil(t, err)
  90. args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, L1Tx)
  91. args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
  92. }
  93. newStateRoot := new(big.Int)
  94. newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
  95. newExitRoot := big.NewInt(0)
  96. args.NewLastIdx = int64(256)
  97. args.NewStRoot = newStateRoot
  98. args.NewExitRoot = newExitRoot
  99. args.L1Batch = true
  100. args.VerifierIdx = 0
  101. args.ProofA[0] = big.NewInt(0)
  102. args.ProofA[1] = big.NewInt(0)
  103. args.ProofB[0][0] = big.NewInt(0)
  104. args.ProofB[0][1] = big.NewInt(0)
  105. args.ProofB[1][0] = big.NewInt(0)
  106. args.ProofB[1][1] = big.NewInt(0)
  107. args.ProofC[0] = big.NewInt(0)
  108. args.ProofC[1] = big.NewInt(0)
  109. argsForge = args
  110. _, err = rollupClient.RollupForgeBatch(argsForge)
  111. require.Nil(t, err)
  112. currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
  113. rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
  114. assert.Equal(t, int64(1), rollupEvents.ForgeBatch[0].BatchNum)
  115. ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
  116. }
  117. func TestRollupForgeBatchArgs(t *testing.T) {
  118. args, err := rollupClient.RollupForgeBatchArgs(ethHashForge)
  119. require.Nil(t, err)
  120. assert.Equal(t, argsForge.FeeIdxCoordinator, args.FeeIdxCoordinator)
  121. assert.Equal(t, argsForge.L1Batch, args.L1Batch)
  122. assert.Equal(t, argsForge.L1CoordinatorTxs, args.L1CoordinatorTxs)
  123. assert.Equal(t, argsForge.L1CoordinatorTxsAuths, args.L1CoordinatorTxsAuths)
  124. assert.Equal(t, argsForge.L2TxsData, args.L2TxsData)
  125. assert.Equal(t, argsForge.NewLastIdx, args.NewLastIdx)
  126. assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
  127. assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
  128. }