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.

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