Browse Source

Bound the filter of init SC events by blocks

When querying the init event for each smart contract, bound the search from
block genesis-24h to block genesis.  Otherwise, this query in geth synced to
mainnet takes too long.
feature/fastsync-skip-updateeth
Eduard S 3 years ago
parent
commit
aa82efc868
9 changed files with 35 additions and 18 deletions
  1. +5
    -3
      eth/auction.go
  2. +1
    -1
      eth/auction_test.go
  3. +11
    -0
      eth/client.go
  4. +5
    -3
      eth/rollup.go
  5. +1
    -1
      eth/rollup_test.go
  6. +5
    -3
      eth/wdelayer.go
  7. +1
    -1
      eth/wdelayer_test.go
  8. +3
    -3
      synchronizer/synchronizer.go
  9. +3
    -3
      test/ethclient.go

+ 5
- 3
eth/auction.go

@ -260,7 +260,7 @@ type AuctionInterface interface {
AuctionConstants() (*common.AuctionConstants, error) AuctionConstants() (*common.AuctionConstants, error)
AuctionEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*AuctionEvents, error) AuctionEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*AuctionEvents, error)
AuctionEventInit() (*AuctionEventInitialize, int64, error)
AuctionEventInit(genesisBlockNum int64) (*AuctionEventInitialize, int64, error)
} }
// //
@ -809,12 +809,14 @@ var (
) )
// AuctionEventInit returns the initialize event with its corresponding block number // AuctionEventInit returns the initialize event with its corresponding block number
func (c *AuctionClient) AuctionEventInit() (*AuctionEventInitialize, int64, error) {
func (c *AuctionClient) AuctionEventInit(genesisBlockNum int64) (*AuctionEventInitialize, int64, error) {
query := ethereum.FilterQuery{ query := ethereum.FilterQuery{
Addresses: []ethCommon.Address{ Addresses: []ethCommon.Address{
c.address, c.address,
}, },
Topics: [][]ethCommon.Hash{{logAuctionInitialize}},
FromBlock: big.NewInt(max(0, genesisBlockNum-blocksPerDay)),
ToBlock: big.NewInt(genesisBlockNum),
Topics: [][]ethCommon.Hash{{logAuctionInitialize}},
} }
logs, err := c.client.client.FilterLogs(context.Background(), query) logs, err := c.client.client.FilterLogs(context.Background(), query)
if err != nil { if err != nil {

+ 1
- 1
eth/auction_test.go

@ -28,7 +28,7 @@ func TestAuctionGetCurrentSlotNumber(t *testing.T) {
} }
func TestAuctionEventInit(t *testing.T) { func TestAuctionEventInit(t *testing.T) {
auctionInit, blockNum, err := auctionClientTest.AuctionEventInit()
auctionInit, blockNum, err := auctionClientTest.AuctionEventInit(genesisBlock)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(18), blockNum) assert.Equal(t, int64(18), blockNum)
assert.Equal(t, donationAddressConst, auctionInit.DonationAddress) assert.Equal(t, donationAddressConst, auctionInit.DonationAddress)

+ 11
- 0
eth/client.go

@ -12,6 +12,17 @@ import (
var errTODO = fmt.Errorf("TODO: Not implemented yet") var errTODO = fmt.Errorf("TODO: Not implemented yet")
const (
blocksPerDay = (3600 * 24) / 15
)
func max(x, y int64) int64 {
if x > y {
return x
}
return y
}
// ClientInterface is the eth Client interface used by hermez-node modules to // ClientInterface is the eth Client interface used by hermez-node modules to
// interact with Ethereum Blockchain and smart contracts. // interact with Ethereum Blockchain and smart contracts.
type ClientInterface interface { type ClientInterface interface {

+ 5
- 3
eth/rollup.go

@ -273,7 +273,7 @@ type RollupInterface interface {
RollupConstants() (*common.RollupConstants, error) RollupConstants() (*common.RollupConstants, error)
RollupEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*RollupEvents, error) RollupEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*RollupEvents, error)
RollupForgeBatchArgs(ethCommon.Hash, uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error) RollupForgeBatchArgs(ethCommon.Hash, uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error)
RollupEventInit() (*RollupEventInitialize, int64, error)
RollupEventInit(genesisBlockNum int64) (*RollupEventInitialize, int64, error)
} }
// //
@ -749,12 +749,14 @@ var (
) )
// RollupEventInit returns the initialize event with its corresponding block number // RollupEventInit returns the initialize event with its corresponding block number
func (c *RollupClient) RollupEventInit() (*RollupEventInitialize, int64, error) {
func (c *RollupClient) RollupEventInit(genesisBlockNum int64) (*RollupEventInitialize, int64, error) {
query := ethereum.FilterQuery{ query := ethereum.FilterQuery{
Addresses: []ethCommon.Address{ Addresses: []ethCommon.Address{
c.address, c.address,
}, },
Topics: [][]ethCommon.Hash{{logHermezInitialize}},
FromBlock: big.NewInt(max(0, genesisBlockNum-blocksPerDay)),
ToBlock: big.NewInt(genesisBlockNum),
Topics: [][]ethCommon.Hash{{logHermezInitialize}},
} }
logs, err := c.client.client.FilterLogs(context.Background(), query) logs, err := c.client.client.FilterLogs(context.Background(), query)
if err != nil { if err != nil {

+ 1
- 1
eth/rollup_test.go

@ -56,7 +56,7 @@ func genKeysBjj(i int64) *keys {
} }
func TestRollupEventInit(t *testing.T) { func TestRollupEventInit(t *testing.T) {
rollupInit, blockNum, err := rollupClient.RollupEventInit()
rollupInit, blockNum, err := rollupClient.RollupEventInit(genesisBlock)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(19), blockNum) assert.Equal(t, int64(19), blockNum)
assert.Equal(t, uint8(10), rollupInit.ForgeL1L2BatchTimeout) assert.Equal(t, uint8(10), rollupInit.ForgeL1L2BatchTimeout)

+ 5
- 3
eth/wdelayer.go

@ -137,7 +137,7 @@ type WDelayerInterface interface {
WDelayerEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*WDelayerEvents, error) WDelayerEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*WDelayerEvents, error)
WDelayerConstants() (*common.WDelayerConstants, error) WDelayerConstants() (*common.WDelayerConstants, error)
WDelayerEventInit() (*WDelayerEventInitialize, int64, error)
WDelayerEventInit(genesisBlockNum int64) (*WDelayerEventInitialize, int64, error)
} }
// //
@ -415,12 +415,14 @@ var (
) )
// WDelayerEventInit returns the initialize event with its corresponding block number // WDelayerEventInit returns the initialize event with its corresponding block number
func (c *WDelayerClient) WDelayerEventInit() (*WDelayerEventInitialize, int64, error) {
func (c *WDelayerClient) WDelayerEventInit(genesisBlockNum int64) (*WDelayerEventInitialize, int64, error) {
query := ethereum.FilterQuery{ query := ethereum.FilterQuery{
Addresses: []ethCommon.Address{ Addresses: []ethCommon.Address{
c.address, c.address,
}, },
Topics: [][]ethCommon.Hash{{logWDelayerInitialize}},
FromBlock: big.NewInt(max(0, genesisBlockNum-blocksPerDay)),
ToBlock: big.NewInt(genesisBlockNum),
Topics: [][]ethCommon.Hash{{logWDelayerInitialize}},
} }
logs, err := c.client.client.FilterLogs(context.Background(), query) logs, err := c.client.client.FilterLogs(context.Background(), query)
if err != nil { if err != nil {

+ 1
- 1
eth/wdelayer_test.go

@ -18,7 +18,7 @@ var maxEmergencyModeTime = time.Hour * 24 * 7 * 26
var maxWithdrawalDelay = time.Hour * 24 * 7 * 2 var maxWithdrawalDelay = time.Hour * 24 * 7 * 2
func TestWDelayerInit(t *testing.T) { func TestWDelayerInit(t *testing.T) {
wDelayerInit, blockNum, err := wdelayerClientTest.WDelayerEventInit()
wDelayerInit, blockNum, err := wdelayerClientTest.WDelayerEventInit(genesisBlock)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(16), blockNum) assert.Equal(t, int64(16), blockNum)
assert.Equal(t, uint64(initWithdrawalDelay), wDelayerInit.InitialWithdrawalDelay) assert.Equal(t, uint64(initWithdrawalDelay), wDelayerInit.InitialWithdrawalDelay)

+ 3
- 3
synchronizer/synchronizer.go

@ -704,15 +704,15 @@ func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
func getInitialVariables(ethClient eth.ClientInterface, func getInitialVariables(ethClient eth.ClientInterface,
consts *common.SCConsts) (*common.SCVariables, *StartBlockNums, error) { consts *common.SCConsts) (*common.SCVariables, *StartBlockNums, error) {
rollupInit, rollupInitBlock, err := ethClient.RollupEventInit()
rollupInit, rollupInitBlock, err := ethClient.RollupEventInit(consts.Auction.GenesisBlockNum)
if err != nil { if err != nil {
return nil, nil, tracerr.Wrap(fmt.Errorf("RollupEventInit: %w", err)) return nil, nil, tracerr.Wrap(fmt.Errorf("RollupEventInit: %w", err))
} }
auctionInit, auctionInitBlock, err := ethClient.AuctionEventInit()
auctionInit, auctionInitBlock, err := ethClient.AuctionEventInit(consts.Auction.GenesisBlockNum)
if err != nil { if err != nil {
return nil, nil, tracerr.Wrap(fmt.Errorf("AuctionEventInit: %w", err)) return nil, nil, tracerr.Wrap(fmt.Errorf("AuctionEventInit: %w", err))
} }
wDelayerInit, wDelayerInitBlock, err := ethClient.WDelayerEventInit()
wDelayerInit, wDelayerInitBlock, err := ethClient.WDelayerEventInit(consts.Auction.GenesisBlockNum)
if err != nil { if err != nil {
return nil, nil, tracerr.Wrap(fmt.Errorf("WDelayerEventInit: %w", err)) return nil, nil, tracerr.Wrap(fmt.Errorf("WDelayerEventInit: %w", err))
} }

+ 3
- 3
test/ethclient.go

@ -1152,7 +1152,7 @@ func (c *Client) RollupEventsByBlock(blockNum int64,
} }
// RollupEventInit returns the initialize event with its corresponding block number // RollupEventInit returns the initialize event with its corresponding block number
func (c *Client) RollupEventInit() (*eth.RollupEventInitialize, int64, error) {
func (c *Client) RollupEventInit(genesisBlockNum int64) (*eth.RollupEventInitialize, int64, error) {
vars := c.blocks[0].Rollup.Vars vars := c.blocks[0].Rollup.Vars
return &eth.RollupEventInitialize{ return &eth.RollupEventInitialize{
ForgeL1L2BatchTimeout: uint8(vars.ForgeL1L2BatchTimeout), ForgeL1L2BatchTimeout: uint8(vars.ForgeL1L2BatchTimeout),
@ -1628,7 +1628,7 @@ func (c *Client) AuctionEventsByBlock(blockNum int64,
} }
// AuctionEventInit returns the initialize event with its corresponding block number // AuctionEventInit returns the initialize event with its corresponding block number
func (c *Client) AuctionEventInit() (*eth.AuctionEventInitialize, int64, error) {
func (c *Client) AuctionEventInit(genesisBlockNum int64) (*eth.AuctionEventInitialize, int64, error) {
vars := c.blocks[0].Auction.Vars vars := c.blocks[0].Auction.Vars
return &eth.AuctionEventInitialize{ return &eth.AuctionEventInitialize{
DonationAddress: vars.DonationAddress, DonationAddress: vars.DonationAddress,
@ -1863,7 +1863,7 @@ func (c *Client) WDelayerConstants() (*common.WDelayerConstants, error) {
} }
// WDelayerEventInit returns the initialize event with its corresponding block number // WDelayerEventInit returns the initialize event with its corresponding block number
func (c *Client) WDelayerEventInit() (*eth.WDelayerEventInitialize, int64, error) {
func (c *Client) WDelayerEventInit(genesisBlockNum int64) (*eth.WDelayerEventInitialize, int64, error) {
vars := c.blocks[0].WDelayer.Vars vars := c.blocks[0].WDelayer.Vars
return &eth.WDelayerEventInitialize{ return &eth.WDelayerEventInitialize{
InitialWithdrawalDelay: vars.WithdrawalDelay, InitialWithdrawalDelay: vars.WithdrawalDelay,

Loading…
Cancel
Save