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.

240 lines
6.5 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. type timer struct {
  18. time int64
  19. }
  20. func (t *timer) Time() int64 {
  21. currentTime := t.time
  22. t.time++
  23. return currentTime
  24. }
  25. func TestClientInterface(t *testing.T) {
  26. var c eth.ClientInterface
  27. var timer timer
  28. clientSetup := NewClientSetupExample()
  29. client := NewClient(true, &timer, ethCommon.Address{}, clientSetup)
  30. c = client
  31. require.NotNil(t, c)
  32. }
  33. func TestClientEth(t *testing.T) {
  34. var timer timer
  35. clientSetup := NewClientSetupExample()
  36. c := NewClient(true, &timer, ethCommon.Address{}, clientSetup)
  37. blockNum, err := c.EthCurrentBlock()
  38. require.Nil(t, err)
  39. assert.Equal(t, int64(0), blockNum)
  40. block, err := c.EthBlockByNumber(context.TODO(), 0)
  41. require.Nil(t, err)
  42. assert.Equal(t, int64(0), block.EthBlockNum)
  43. assert.Equal(t, time.Unix(0, 0), block.Timestamp)
  44. assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000000", block.Hash.Hex())
  45. assert.Equal(t, int64(0), c.blockNum)
  46. // Mine some empty blocks
  47. c.CtlMineBlock()
  48. assert.Equal(t, int64(1), c.blockNum)
  49. c.CtlMineBlock()
  50. assert.Equal(t, int64(2), c.blockNum)
  51. block, err = c.EthBlockByNumber(context.TODO(), 2)
  52. require.Nil(t, err)
  53. assert.Equal(t, int64(2), block.EthBlockNum)
  54. assert.Equal(t, time.Unix(2, 0), block.Timestamp)
  55. }
  56. func TestClientAuction(t *testing.T) {
  57. addrWithdraw := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
  58. addrForge := ethCommon.HexToAddress("0xCfAA413eEb796f328620a3630Ae39124cabcEa92")
  59. addrForge2 := ethCommon.HexToAddress("0x1fCb4ac309428feCc61B1C8cA5823C15A5e1a800")
  60. var timer timer
  61. clientSetup := NewClientSetupExample()
  62. clientSetup.AuctionVariables.ClosedAuctionSlots = 2
  63. clientSetup.AuctionVariables.OpenAuctionSlots = 100
  64. c := NewClient(true, &timer, addrWithdraw, clientSetup)
  65. _, err := c.AuctionBid(0, big.NewInt(1), addrForge)
  66. assert.Equal(t, errBidClosed, err)
  67. _, err = c.AuctionBid(102, big.NewInt(1), addrForge)
  68. assert.Equal(t, errBidNotOpen, err)
  69. _, err = c.AuctionBid(101, big.NewInt(16), addrForge)
  70. assert.Equal(t, errCoordNotReg, err)
  71. _, err = c.AuctionRegisterCoordinator(addrForge, "https://foo.bar")
  72. assert.Nil(t, err)
  73. _, err = c.AuctionBid(3, big.NewInt(1), addrForge)
  74. assert.Equal(t, errBidBelowMin, err)
  75. _, err = c.AuctionBid(3, big.NewInt(16), addrForge)
  76. assert.Nil(t, err)
  77. _, err = c.AuctionRegisterCoordinator(addrForge2, "https://foo2.bar")
  78. assert.Nil(t, err)
  79. _, err = c.AuctionBid(3, big.NewInt(16), addrForge2)
  80. assert.Equal(t, errBidBelowMin, err)
  81. _, err = c.AuctionBid(3, big.NewInt(17), addrForge2)
  82. assert.Nil(t, err)
  83. c.CtlMineBlock()
  84. blockNum, err := c.EthCurrentBlock()
  85. require.Nil(t, err)
  86. auctionEvents, _, err := c.AuctionEventsByBlock(blockNum)
  87. require.Nil(t, err)
  88. assert.Equal(t, 2, len(auctionEvents.NewBid))
  89. }
  90. func TestClientRollup(t *testing.T) {
  91. token1Addr := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
  92. var timer timer
  93. clientSetup := NewClientSetupExample()
  94. c := NewClient(true, &timer, ethCommon.Address{}, clientSetup)
  95. // Add a token
  96. tx, err := c.RollupAddToken(token1Addr)
  97. require.Nil(t, err)
  98. assert.NotNil(t, tx)
  99. // Add some L1UserTxs
  100. // Create Accounts
  101. const N = 16
  102. var keys [N]*keys
  103. for i := 0; i < N; i++ {
  104. keys[i] = genKeys(int64(i))
  105. l1UserTx := common.L1Tx{
  106. FromIdx: common.Idx(0),
  107. FromEthAddr: keys[i].Addr,
  108. FromBJJ: keys[i].BJJPublicKey,
  109. TokenID: common.TokenID(0),
  110. LoadAmount: big.NewInt(10 + int64(i)),
  111. }
  112. c.CtlAddL1TxUser(&l1UserTx)
  113. }
  114. c.CtlMineBlock()
  115. blockNum, err := c.EthCurrentBlock()
  116. require.Nil(t, err)
  117. rollupEvents, _, err := c.RollupEventsByBlock(blockNum)
  118. require.Nil(t, err)
  119. assert.Equal(t, N, len(rollupEvents.L1UserTx))
  120. assert.Equal(t, 1, len(rollupEvents.AddToken))
  121. // Forge a batch
  122. c.CtlAddBatch(&eth.RollupForgeBatchArgs{
  123. NewLastIdx: 0,
  124. NewStRoot: big.NewInt(1),
  125. NewExitRoot: big.NewInt(100),
  126. L1CoordinatorTxs: []*common.L1Tx{},
  127. L2Txs: []*common.L2Tx{},
  128. FeeIdxCoordinator: make([]common.Idx, eth.FeeIdxCoordinatorLen),
  129. VerifierIdx: 0,
  130. L1Batch: true,
  131. })
  132. c.CtlMineBlock()
  133. blockNumA, err := c.EthCurrentBlock()
  134. require.Nil(t, err)
  135. rollupEvents, hashA, err := c.RollupEventsByBlock(blockNumA)
  136. require.Nil(t, err)
  137. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  138. assert.Equal(t, 0, len(rollupEvents.AddToken))
  139. assert.Equal(t, 1, len(rollupEvents.ForgeBatch))
  140. // Simulate reorg discarding last mined block
  141. c.CtlRollback()
  142. c.CtlMineBlock()
  143. blockNumB, err := c.EthCurrentBlock()
  144. require.Nil(t, err)
  145. rollupEvents, hashB, err := c.RollupEventsByBlock(blockNumA)
  146. require.Nil(t, err)
  147. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  148. assert.Equal(t, 0, len(rollupEvents.AddToken))
  149. assert.Equal(t, 0, len(rollupEvents.ForgeBatch))
  150. assert.Equal(t, blockNumA, blockNumB)
  151. assert.NotEqual(t, hashA, hashB)
  152. // Forge again
  153. rollupForgeBatchArgs0 := &eth.RollupForgeBatchArgs{
  154. NewLastIdx: 0,
  155. NewStRoot: big.NewInt(1),
  156. NewExitRoot: big.NewInt(100),
  157. L1CoordinatorTxs: []*common.L1Tx{},
  158. L2Txs: []*common.L2Tx{},
  159. FeeIdxCoordinator: make([]common.Idx, eth.FeeIdxCoordinatorLen),
  160. VerifierIdx: 0,
  161. L1Batch: true,
  162. }
  163. c.CtlAddBatch(rollupForgeBatchArgs0)
  164. c.CtlMineBlock()
  165. // Retrieve ForgeBatchArguments starting from the events
  166. blockNum, err = c.EthCurrentBlock()
  167. require.Nil(t, err)
  168. rollupEvents, _, err = c.RollupEventsByBlock(blockNum)
  169. require.Nil(t, err)
  170. rollupForgeBatchArgs1, err := c.RollupForgeBatchArgs(rollupEvents.ForgeBatch[0].EthTxHash)
  171. require.Nil(t, err)
  172. assert.Equal(t, rollupForgeBatchArgs0, rollupForgeBatchArgs1)
  173. }
  174. type keys struct {
  175. BJJSecretKey *babyjub.PrivateKey
  176. BJJPublicKey *babyjub.PublicKey
  177. Addr ethCommon.Address
  178. }
  179. func genKeys(i int64) *keys {
  180. i++ // i = 0 doesn't work for the ecdsa key generation
  181. var sk babyjub.PrivateKey
  182. binary.LittleEndian.PutUint64(sk[:], uint64(i))
  183. // eth address
  184. var key ecdsa.PrivateKey
  185. key.D = big.NewInt(i) // only for testing
  186. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  187. key.Curve = ethCrypto.S256()
  188. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  189. return &keys{
  190. BJJSecretKey: &sk,
  191. BJJPublicKey: sk.Public(),
  192. Addr: addr,
  193. }
  194. }