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.

220 lines
5.9 KiB

  1. package test
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "encoding/binary"
  6. "math/big"
  7. "testing"
  8. "time"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/hermeznetwork/hermez-node/eth"
  13. "github.com/iden3/go-iden3-crypto/babyjub"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. var clientSetup *ClientSetup
  18. func init() {
  19. rollupConstants := &eth.RollupConstants{}
  20. rollupVariables := &eth.RollupVariables{
  21. MaxTxVerifiers: make([]int, 0),
  22. TokenHEZ: ethCommon.Address{},
  23. GovernanceAddress: ethCommon.Address{},
  24. SafetyBot: ethCommon.Address{},
  25. ConsensusContract: ethCommon.Address{},
  26. WithdrawalContract: ethCommon.Address{},
  27. FeeAddToken: big.NewInt(1),
  28. ForgeL1Timeout: 16,
  29. FeeL1UserTx: big.NewInt(2),
  30. }
  31. auctionConstants := &eth.AuctionConstants{}
  32. auctionVariables := &eth.AuctionVariables{
  33. DonationAddress: ethCommon.Address{},
  34. BootCoordinator: ethCommon.Address{},
  35. MinBidEpoch: [6]*big.Int{
  36. big.NewInt(10), big.NewInt(11), big.NewInt(12),
  37. big.NewInt(13), big.NewInt(14), big.NewInt(15)},
  38. ClosedAuctionSlots: 0,
  39. OpenAuctionSlots: 0,
  40. AllocationRatio: [3]uint8{},
  41. Outbidding: 0,
  42. SlotDeadline: 0,
  43. }
  44. clientSetup = &ClientSetup{
  45. RollupConstants: rollupConstants,
  46. RollupVariables: rollupVariables,
  47. AuctionConstants: auctionConstants,
  48. AuctionVariables: auctionVariables,
  49. }
  50. }
  51. type timer struct {
  52. time int64
  53. }
  54. func (t *timer) Time() int64 {
  55. currentTime := t.time
  56. t.time++
  57. return currentTime
  58. }
  59. func TestClientInterface(t *testing.T) {
  60. var c eth.ClientInterface
  61. var timer timer
  62. client := NewClient(true, &timer, clientSetup)
  63. c = client
  64. require.NotNil(t, c)
  65. }
  66. func TestEthClient(t *testing.T) {
  67. token1Addr := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
  68. var timer timer
  69. c := NewClient(true, &timer, clientSetup)
  70. blockNum, err := c.EthCurrentBlock()
  71. require.Nil(t, err)
  72. assert.Equal(t, int64(0), blockNum)
  73. block, err := c.EthBlockByNumber(context.TODO(), 0)
  74. require.Nil(t, err)
  75. assert.Equal(t, int64(0), block.EthBlockNum)
  76. assert.Equal(t, time.Unix(0, 0), block.Timestamp)
  77. assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", block.Hash.Hex())
  78. assert.Equal(t, int64(0), c.blockNum)
  79. // Mine some empty blocks
  80. c.CtlMineBlock()
  81. assert.Equal(t, int64(1), c.blockNum)
  82. c.CtlMineBlock()
  83. assert.Equal(t, int64(2), c.blockNum)
  84. block, err = c.EthBlockByNumber(context.TODO(), 2)
  85. require.Nil(t, err)
  86. assert.Equal(t, int64(2), block.EthBlockNum)
  87. assert.Equal(t, time.Unix(2, 0), block.Timestamp)
  88. // Add a token
  89. tx, err := c.RollupAddToken(token1Addr)
  90. require.Nil(t, err)
  91. assert.NotNil(t, tx)
  92. // Add some L1UserTxs
  93. // Create Accounts
  94. const N = 16
  95. var keys [N]*keys
  96. for i := 0; i < N; i++ {
  97. keys[i] = genKeys(int64(i))
  98. l1UserTx := common.L1Tx{
  99. FromIdx: common.Idx(0),
  100. FromEthAddr: keys[i].Addr,
  101. FromBJJ: keys[i].BJJPublicKey,
  102. TokenID: common.TokenID(0),
  103. LoadAmount: big.NewInt(10 + int64(i)),
  104. }
  105. c.CtlAddL1TxUser(&l1UserTx)
  106. }
  107. c.CtlMineBlock()
  108. blockNum, err = c.EthCurrentBlock()
  109. require.Nil(t, err)
  110. rollupEvents, _, err := c.RollupEventsByBlock(blockNum)
  111. require.Nil(t, err)
  112. assert.Equal(t, N, len(rollupEvents.L1UserTx))
  113. assert.Equal(t, 1, len(rollupEvents.AddToken))
  114. // Forge a batch
  115. c.CtlAddBatch(&eth.RollupForgeBatchArgs{
  116. NewLastIdx: 0,
  117. NewStRoot: big.NewInt(1),
  118. NewExitRoot: big.NewInt(100),
  119. L1CoordinatorTxs: []*common.L1Tx{},
  120. L2Txs: []*common.L2Tx{},
  121. FeeIdxCoordinator: make([]common.Idx, eth.FeeIdxCoordinatorLen),
  122. VerifierIdx: 0,
  123. L1Batch: true,
  124. })
  125. c.CtlMineBlock()
  126. blockNumA, err := c.EthCurrentBlock()
  127. require.Nil(t, err)
  128. rollupEvents, hashA, err := c.RollupEventsByBlock(blockNumA)
  129. require.Nil(t, err)
  130. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  131. assert.Equal(t, 0, len(rollupEvents.AddToken))
  132. assert.Equal(t, 1, len(rollupEvents.ForgeBatch))
  133. // Simulate reorg discarding last mined block
  134. c.CtlRollback()
  135. c.CtlMineBlock()
  136. blockNumB, err := c.EthCurrentBlock()
  137. require.Nil(t, err)
  138. rollupEvents, hashB, err := c.RollupEventsByBlock(blockNumA)
  139. require.Nil(t, err)
  140. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  141. assert.Equal(t, 0, len(rollupEvents.AddToken))
  142. assert.Equal(t, 0, len(rollupEvents.ForgeBatch))
  143. assert.Equal(t, blockNumA, blockNumB)
  144. assert.NotEqual(t, hashA, hashB)
  145. // Forge again
  146. rollupForgeBatchArgs0 := &eth.RollupForgeBatchArgs{
  147. NewLastIdx: 0,
  148. NewStRoot: big.NewInt(1),
  149. NewExitRoot: big.NewInt(100),
  150. L1CoordinatorTxs: []*common.L1Tx{},
  151. L2Txs: []*common.L2Tx{},
  152. FeeIdxCoordinator: make([]common.Idx, eth.FeeIdxCoordinatorLen),
  153. VerifierIdx: 0,
  154. L1Batch: true,
  155. }
  156. c.CtlAddBatch(rollupForgeBatchArgs0)
  157. c.CtlMineBlock()
  158. // Retrieve ForgeBatchArguments starting from the events
  159. blockNum, err = c.EthCurrentBlock()
  160. require.Nil(t, err)
  161. rollupEvents, _, err = c.RollupEventsByBlock(blockNum)
  162. require.Nil(t, err)
  163. rollupForgeBatchArgs1, err := c.RollupForgeBatchArgs(rollupEvents.ForgeBatch[0].EthTxHash)
  164. require.Nil(t, err)
  165. assert.Equal(t, rollupForgeBatchArgs0, rollupForgeBatchArgs1)
  166. }
  167. type keys struct {
  168. BJJSecretKey *babyjub.PrivateKey
  169. BJJPublicKey *babyjub.PublicKey
  170. Addr ethCommon.Address
  171. }
  172. func genKeys(i int64) *keys {
  173. i++ // i = 0 doesn't work for the ecdsa key generation
  174. var sk babyjub.PrivateKey
  175. binary.LittleEndian.PutUint64(sk[:], uint64(i))
  176. // eth address
  177. var key ecdsa.PrivateKey
  178. key.D = big.NewInt(i) // only for testing
  179. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  180. key.Curve = ethCrypto.S256()
  181. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  182. return &keys{
  183. BJJSecretKey: &sk,
  184. BJJPublicKey: sk.Public(),
  185. Addr: addr,
  186. }
  187. }