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.

249 lines
6.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. 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. addrBidder1 := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
  58. addrBidder2 := ethCommon.HexToAddress("0xc27cadc437d067a6ec869502cc9f7F834cFc087a")
  59. addrForge := ethCommon.HexToAddress("0xCfAA413eEb796f328620a3630Ae39124cabcEa92")
  60. addrForge2 := ethCommon.HexToAddress("0x1fCb4ac309428feCc61B1C8cA5823C15A5e1a800")
  61. var timer timer
  62. clientSetup := NewClientSetupExample()
  63. clientSetup.AuctionVariables.ClosedAuctionSlots = 2
  64. clientSetup.AuctionVariables.OpenAuctionSlots = 4320
  65. clientSetup.AuctionVariables.DefaultSlotSetBid = [6]*big.Int{
  66. big.NewInt(1000), big.NewInt(1100), big.NewInt(1200),
  67. big.NewInt(1300), big.NewInt(1400), big.NewInt(1500)}
  68. c := NewClient(true, &timer, &addrBidder1, clientSetup)
  69. // Check several cases in which bid doesn't succed, and also do 2 successful bids.
  70. _, err := c.AuctionBid(0, big.NewInt(1))
  71. assert.Equal(t, errBidClosed, err)
  72. _, err = c.AuctionBid(4322, big.NewInt(1))
  73. assert.Equal(t, errBidNotOpen, err)
  74. // 101 % 6 = 5; defaultSlotSetBid[5] = 1500; 1500 + 10% = 1650
  75. _, err = c.AuctionBid(101, big.NewInt(1650))
  76. assert.Equal(t, errCoordNotReg, err)
  77. _, err = c.AuctionSetCoordinator(addrForge, "https://foo.bar")
  78. assert.Nil(t, err)
  79. _, err = c.AuctionBid(3, big.NewInt(1))
  80. assert.Equal(t, errBidBelowMin, err)
  81. _, err = c.AuctionBid(3, big.NewInt(1650))
  82. assert.Nil(t, err)
  83. c.CtlSetAddr(addrBidder2)
  84. _, err = c.AuctionSetCoordinator(addrForge2, "https://foo2.bar")
  85. assert.Nil(t, err)
  86. _, err = c.AuctionBid(3, big.NewInt(16))
  87. assert.Equal(t, errBidBelowMin, err)
  88. // 1650 + 10% = 1815
  89. _, err = c.AuctionBid(3, big.NewInt(1815))
  90. assert.Nil(t, err)
  91. c.CtlMineBlock()
  92. blockNum, err := c.EthCurrentBlock()
  93. require.Nil(t, err)
  94. auctionEvents, _, err := c.AuctionEventsByBlock(blockNum)
  95. require.Nil(t, err)
  96. assert.Equal(t, 2, len(auctionEvents.NewBid))
  97. }
  98. func TestClientRollup(t *testing.T) {
  99. token1Addr := ethCommon.HexToAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
  100. var timer timer
  101. clientSetup := NewClientSetupExample()
  102. c := NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
  103. // Add a token
  104. tx, err := c.RollupAddToken(token1Addr)
  105. require.Nil(t, err)
  106. assert.NotNil(t, tx)
  107. // Add some L1UserTxs
  108. // Create Accounts
  109. const N = 16
  110. var keys [N]*keys
  111. for i := 0; i < N; i++ {
  112. keys[i] = genKeys(int64(i))
  113. l1UserTx := common.L1Tx{
  114. FromIdx: nil,
  115. FromEthAddr: keys[i].Addr,
  116. FromBJJ: keys[i].BJJPublicKey,
  117. TokenID: common.TokenID(0),
  118. LoadAmount: big.NewInt(10 + int64(i)),
  119. }
  120. c.CtlAddL1TxUser(&l1UserTx)
  121. }
  122. c.CtlMineBlock()
  123. blockNum, err := c.EthCurrentBlock()
  124. require.Nil(t, err)
  125. rollupEvents, _, err := c.RollupEventsByBlock(blockNum)
  126. require.Nil(t, err)
  127. assert.Equal(t, N, len(rollupEvents.L1UserTx))
  128. assert.Equal(t, 1, len(rollupEvents.AddToken))
  129. // Forge a batch
  130. c.CtlAddBatch(&eth.RollupForgeBatchArgs{
  131. NewLastIdx: 0,
  132. NewStRoot: big.NewInt(1),
  133. NewExitRoot: big.NewInt(100),
  134. L1CoordinatorTxs: []*common.L1Tx{},
  135. L2TxsData: []*common.L2Tx{},
  136. FeeIdxCoordinator: make([]common.Idx, eth.RollupConstFeeIdxCoordinatorLen),
  137. VerifierIdx: 0,
  138. L1Batch: true,
  139. })
  140. c.CtlMineBlock()
  141. blockNumA, err := c.EthCurrentBlock()
  142. require.Nil(t, err)
  143. rollupEvents, hashA, err := c.RollupEventsByBlock(blockNumA)
  144. require.Nil(t, err)
  145. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  146. assert.Equal(t, 0, len(rollupEvents.AddToken))
  147. assert.Equal(t, 1, len(rollupEvents.ForgeBatch))
  148. // Simulate reorg discarding last mined block
  149. c.CtlRollback()
  150. c.CtlMineBlock()
  151. blockNumB, err := c.EthCurrentBlock()
  152. require.Nil(t, err)
  153. rollupEvents, hashB, err := c.RollupEventsByBlock(blockNumA)
  154. require.Nil(t, err)
  155. assert.Equal(t, 0, len(rollupEvents.L1UserTx))
  156. assert.Equal(t, 0, len(rollupEvents.AddToken))
  157. assert.Equal(t, 0, len(rollupEvents.ForgeBatch))
  158. assert.Equal(t, blockNumA, blockNumB)
  159. assert.NotEqual(t, hashA, hashB)
  160. // Forge again
  161. rollupForgeBatchArgs0 := &eth.RollupForgeBatchArgs{
  162. NewLastIdx: 0,
  163. NewStRoot: big.NewInt(1),
  164. NewExitRoot: big.NewInt(100),
  165. L1CoordinatorTxs: []*common.L1Tx{},
  166. L2TxsData: []*common.L2Tx{},
  167. FeeIdxCoordinator: make([]common.Idx, eth.RollupConstFeeIdxCoordinatorLen),
  168. VerifierIdx: 0,
  169. L1Batch: true,
  170. }
  171. c.CtlAddBatch(rollupForgeBatchArgs0)
  172. c.CtlMineBlock()
  173. // Retrieve ForgeBatchArguments starting from the events
  174. blockNum, err = c.EthCurrentBlock()
  175. require.Nil(t, err)
  176. rollupEvents, _, err = c.RollupEventsByBlock(blockNum)
  177. require.Nil(t, err)
  178. rollupForgeBatchArgs1, err := c.RollupForgeBatchArgs(rollupEvents.ForgeBatch[0].EthTxHash)
  179. require.Nil(t, err)
  180. assert.Equal(t, rollupForgeBatchArgs0, rollupForgeBatchArgs1)
  181. }
  182. type keys struct {
  183. BJJSecretKey *babyjub.PrivateKey
  184. BJJPublicKey *babyjub.PublicKey
  185. Addr ethCommon.Address
  186. }
  187. func genKeys(i int64) *keys {
  188. i++ // i = 0 doesn't work for the ecdsa key generation
  189. var sk babyjub.PrivateKey
  190. binary.LittleEndian.PutUint64(sk[:], uint64(i))
  191. // eth address
  192. var key ecdsa.PrivateKey
  193. key.D = big.NewInt(i) // only for testing
  194. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  195. key.Curve = ethCrypto.S256()
  196. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  197. return &keys{
  198. BJJSecretKey: &sk,
  199. BJJPublicKey: sk.Public(),
  200. Addr: addr,
  201. }
  202. }