Skip to content

Commit

Permalink
Merge pull request #156 from 0xPolygon/feature/fixes_from_0.6.4-cdk8
Browse files Browse the repository at this point in the history
Cherry-pick fixes on release 0.6.4-cdk.8:
- #154: fix sanity check for genesis block
- #153: Synchronization of L1InfoTree events before genesis allows to resume
  • Loading branch information
joanestebanr authored Sep 4, 2024
2 parents 1937eaf + eca2559 commit add99c0
Show file tree
Hide file tree
Showing 13 changed files with 1,288 additions and 312 deletions.
25 changes: 4 additions & 21 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ func forkIDIntervals(ctx context.Context, st *state.State, etherman *etherman.Cl
if err != nil && !errors.Is(err, state.ErrStateNotSynchronized) {
return []state.ForkIDInterval{}, fmt.Errorf("error checking lastL1BlockSynced. Error: %v", err)
}
if lastBlock != nil {
// If lastBlock is below genesisBlock means state.ErrStateNotSynchronized (haven't started yet the sync process, is doing pregenesis sync)
if lastBlock != nil && lastBlock.BlockNumber >= genesisBlockNumber {
log.Info("Getting forkIDs intervals. Please wait...")
// Read Fork ID FROM POE SC
forkIntervals, err := etherman.GetForks(ctx, genesisBlockNumber, lastBlock.BlockNumber)
Expand Down Expand Up @@ -784,32 +785,14 @@ func forkIDIntervals(ctx context.Context, st *state.State, etherman *etherman.Cl
}
forkIDIntervals = forkIntervals
} else {
log.Debug("Getting all forkIDs")

// Get last L1 block number
bn, err := etherman.GetLatestBlockNumber(ctx)
if err != nil {
return []state.ForkIDInterval{}, fmt.Errorf("error getting latest block number. Error: %v", err)
}

// Get all forkIDs since genesis
forkIntervals, err := etherman.GetForks(ctx, genesisBlockNumber, bn)
log.Debug("Getting initial forkID")
forkIntervals, err := etherman.GetForks(ctx, genesisBlockNumber, genesisBlockNumber)
if err != nil {
return []state.ForkIDInterval{}, fmt.Errorf("error getting forks. Please check the configuration. Error: %v", err)
} else if len(forkIntervals) == 0 {
return []state.ForkIDInterval{}, fmt.Errorf("error: no forkID received. It should receive at least one, please check the configuration...")
}
forkIDIntervals = forkIntervals

log.Debugf("Retrieved %d forkIDs", len(forkIDIntervals))

log.Debug("Adding forkIDs to db and memory")
for _, forkID := range forkIDIntervals {
err = st.AddForkIDInterval(ctx, forkID, nil)
if err != nil {
log.Fatal("error adding forkID to db. Error: ", err)
}
}
}
}
return forkIDIntervals, nil
Expand Down
36 changes: 36 additions & 0 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ func (etherMan *Client) VerifyGenBlockNumber(ctx context.Context, genBlockNumber
return true, nil
}

// GetL1BlockUpgradeLxLy It returns the block genesis for LxLy before genesisBlock or error
func (etherMan *Client) GetL1BlockUpgradeLxLy(ctx context.Context, genesisBlock uint64) (uint64, error) {
it, err := etherMan.GlobalExitRootManager.FilterUpdateL1InfoTree(&bind.FilterOpts{
Start: 1,
End: &genesisBlock,
Context: ctx,
}, nil, nil)
if err != nil {
return uint64(0), err
}
for it.Next() {
log.Debugf("BlockNumber: %d Topics:L1InfoTree", it.Event.Raw.BlockNumber)
return it.Event.Raw.BlockNumber, nil
}
return uint64(0), ErrNotFound
}

// GetForks returns fork information
func (etherMan *Client) GetForks(ctx context.Context, genBlockNumber uint64, lastL1BlockSynced uint64) ([]state.ForkIDInterval, error) {
log.Debug("Getting forkIDs from blockNumber: ", genBlockNumber)
Expand Down Expand Up @@ -499,6 +516,25 @@ func (etherMan *Client) GetRollupInfoByBlockRange(ctx context.Context, fromBlock
return blocks, blocksOrder, nil
}

// GetRollupInfoByBlockRangePreviousRollupGenesis function retrieves the Rollup information that are included in all this ethereum blocks
// but it only retrieves the information from the previous rollup genesis block to the current block.
func (etherMan *Client) GetRollupInfoByBlockRangePreviousRollupGenesis(ctx context.Context, fromBlock uint64, toBlock *uint64) ([]Block, map[common.Hash][]Order, error) {
// Filter query
query := ethereum.FilterQuery{
FromBlock: new(big.Int).SetUint64(fromBlock),
Addresses: []common.Address{etherMan.l1Cfg.GlobalExitRootManagerAddr},
Topics: [][]common.Hash{{updateL1InfoTreeSignatureHash}},
}
if toBlock != nil {
query.ToBlock = new(big.Int).SetUint64(*toBlock)
}
blocks, blocksOrder, err := etherMan.readEvents(ctx, query)
if err != nil {
return nil, nil, err
}
return blocks, blocksOrder, nil
}

// Order contains the event order to let the synchronizer store the information following this order.
type Order struct {
Name EventOrder
Expand Down
28 changes: 0 additions & 28 deletions jsonrpc/mocks/mock_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions state/block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"fmt"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -15,6 +16,10 @@ type Block struct {
Checked bool
}

func (b *Block) String() string {
return fmt.Sprintf("BlockNumber: %d, BlockHash: %s, ParentHash: %s, ReceivedAt: %s", b.BlockNumber, b.BlockHash, b.ParentHash, b.ReceivedAt)
}

// NewBlock creates a block with the given data.
func NewBlock(blockNumber uint64) *Block {
return &Block{BlockNumber: blockNumber}
Expand Down
5 changes: 5 additions & 0 deletions state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/jackc/pgx/v4"
)

const (
// AutoDiscoverRollupManagerBlockNumber is the value to auto-discover the RollupManager creation block number
AutoDiscoverRollupManagerBlockNumber = uint64(0)
)

// Genesis contains the information to populate state on creation
type Genesis struct {
// RollupBlockNumber is the block number where the polygonZKEVM smc was deployed on L1
Expand Down
6 changes: 6 additions & 0 deletions synchronizer/common/syncinterfaces/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ type EthermanFullInterface interface {

EthermanGetLatestBatchNumber
GetFinalizedBlockNumber(ctx context.Context) (uint64, error)
EthermanPreRollup
}

type EthermanGetLatestBatchNumber interface {
GetLatestBatchNumber() (uint64, error)
}

type EthermanPreRollup interface {
GetL1BlockUpgradeLxLy(ctx context.Context, genesisBlock uint64) (uint64, error)
GetRollupInfoByBlockRangePreviousRollupGenesis(ctx context.Context, fromBlock uint64, toBlock *uint64) ([]etherman.Block, map[common.Hash][]etherman.Order, error)
}
126 changes: 126 additions & 0 deletions synchronizer/common/syncinterfaces/mocks/etherman_full_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit add99c0

Please sign in to comment.