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.

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