Skip to content

Commit

Permalink
Sync L1InfoRoot since upgrade to LxLy rollup manager (0xPolygonHermez…
Browse files Browse the repository at this point in the history
…#3397)

* Find etrog update and if it's before genesis block synchronizer previous L1InfoRoot
  • Loading branch information
joanestebanr authored Mar 5, 2024
1 parent 6225e52 commit badc2cb
Show file tree
Hide file tree
Showing 19 changed files with 1,268 additions and 164 deletions.
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,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
51 changes: 49 additions & 2 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import (
"golang.org/x/crypto/sha3"
)

const (
// ETrogUpgradeVersion is the version of the LxLy upgrade
ETrogUpgradeVersion = 2
)

var (
// Events RollupManager
setBatchFeeSignatureHash = crypto.Keccak256Hash([]byte("SetBatchFee(uint256)"))
Expand Down Expand Up @@ -209,6 +214,7 @@ func NewClient(cfg Config, l1Config L1Config) (*Client, error) {
log.Errorf("error connecting to %s: %+v", cfg.URL, err)
return nil, err
}

// Create smc clients
zkevm, err := polygonzkevm.NewPolygonzkevm(l1Config.ZkEVMAddr, ethClient)
if err != nil {
Expand Down Expand Up @@ -267,7 +273,7 @@ func NewClient(cfg Config, l1Config L1Config) (*Client, error) {
}
log.Debug("rollupID: ", rollupID)

return &Client{
client := &Client{
EthClient: ethClient,
ZkEVM: zkevm,
EtrogZKEVM: etrogZkevm,
Expand All @@ -285,7 +291,8 @@ func NewClient(cfg Config, l1Config L1Config) (*Client, error) {
l1Cfg: l1Config,
cfg: cfg,
auth: map[common.Address]bind.TransactOpts{},
}, nil
}
return client, nil
}

// VerifyGenBlockNumber verifies if the genesis Block Number is valid
Expand Down Expand Up @@ -340,6 +347,27 @@ func (etherMan *Client) VerifyGenBlockNumber(ctx context.Context, genBlockNumber
return true, nil
}

// GetL1BlockUpgradeLxLy It returns the block genesis for LxLy before genesisBlock or error
// TODO: Check if all RPC providers support this range of blocks
func (etherMan *Client) GetL1BlockUpgradeLxLy(ctx context.Context, genesisBlock uint64) (uint64, error) {
it, err := etherMan.RollupManager.FilterInitialized(&bind.FilterOpts{
Start: 1,
End: &genesisBlock,
Context: ctx,
})
if err != nil {
return uint64(0), err
}
for it.Next() {
log.Debugf("BlockNumber: %d Topics:Initialized(%d)", it.Event.Raw.BlockNumber, it.Event.Version)
if it.Event.Version == ETrogUpgradeVersion { // 2 is ETROG (LxLy upgrade)
log.Infof("LxLy upgrade found at blockNumber: %d", 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 @@ -475,6 +503,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
21 changes: 21 additions & 0 deletions synchronizer/common/syncinterfaces/block_range_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package syncinterfaces

import (
"context"

"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
)

type ProcessBlockRangeL1BlocksMode bool

const (
StoreL1Blocks ProcessBlockRangeL1BlocksMode = true
NoStoreL1Blocks ProcessBlockRangeL1BlocksMode = false
)

type BlockRangeProcessor interface {
ProcessBlockRange(ctx context.Context, blocks []etherman.Block, order map[common.Hash][]etherman.Order) error
ProcessBlockRangeSingleDbTx(ctx context.Context, blocks []etherman.Block, order map[common.Hash][]etherman.Order, storeBlocks ProcessBlockRangeL1BlocksMode, dbTx pgx.Tx) error
}
6 changes: 6 additions & 0 deletions synchronizer/common/syncinterfaces/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ type EthermanFullInterface interface {
GetTrustedSequencerURL() (string, error)
VerifyGenBlockNumber(ctx context.Context, genBlockNumber uint64) (bool, error)
GetLatestVerifiedBatchNum() (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)
}
14 changes: 14 additions & 0 deletions synchronizer/common/syncinterfaces/l1_event_processor_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package syncinterfaces

import (
"context"

"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/actions"
"github.com/jackc/pgx/v4"
)

type L1EventProcessorManager interface {
Process(ctx context.Context, forkId actions.ForkIdType, order etherman.Order, block *etherman.Block, dbTx pgx.Tx) error
Get(forkId actions.ForkIdType, event etherman.EventOrder) actions.L1EventProcessor
}
142 changes: 142 additions & 0 deletions synchronizer/common/syncinterfaces/mocks/block_range_processor.go

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

Loading

0 comments on commit badc2cb

Please sign in to comment.