Skip to content

Commit

Permalink
refactor: retrieve contract addresses from l1
Browse files Browse the repository at this point in the history
  • Loading branch information
MorettiGeorgiev committed Oct 4, 2024
1 parent 9881886 commit a551f76
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 14 deletions.
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ var (
Usage: "Check the contract address on the L1",
Value: true,
}
L1ContractAddressRetrieveFlag = cli.BoolFlag{
Name: "zkevm.l1-contract-address-retrieve",
Usage: "Retrieve the contracts addresses from the L1",
Value: true,
}
RebuildTreeAfterFlag = cli.Uint64Flag{
Name: "zkevm.rebuild-tree-after",
Usage: "Rebuild the state tree after this many blocks behind",
Expand Down
68 changes: 57 additions & 11 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,17 +848,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {

log.Info("Rollup ID", "rollupId", cfg.L1RollupId)

// check contract addresses in config against L1
if cfg.Zk.L1ContractAddressCheck {
success, err := l1ContractAddressCheck(ctx, cfg.Zk, backend.l1Syncer)
if !success || err != nil {
//log.Warn("Contract address check failed", "success", success, "err", err)
panic("Contract address check failed")
}
log.Info("Contract address check passed")
} else {
log.Info("Contract address check skipped")
}
// Check if L1 contracts addresses should be retrieved from the L1 chain
l1ContractAddressProcess(ctx, cfg.Zk, backend.l1Syncer)

l1InfoTreeSyncer := syncer.NewL1Syncer(
ctx,
Expand Down Expand Up @@ -1554,6 +1545,57 @@ func checkPortIsFree(addr string) (free bool) {
return false
}

func l1ContractAddressProcess(ctx context.Context, cfg *ethconfig.Zk, l1BlockSyncer *syncer.L1Syncer) {
if cfg.L1ContractAddressRetrieve {
l1ContractAddress(ctx, cfg, l1BlockSyncer)
return
}
l1ContractAddressValidate(ctx, cfg, l1BlockSyncer)
}

func l1ContractAddress(ctx context.Context, cfg *ethconfig.Zk, l1BlockSyncer *syncer.L1Syncer) {
if err := l1ContractAdressFromZKevm(ctx, cfg, l1BlockSyncer); err != nil {
panic("Failed to retrieve contract addresses from L1")
}
log.Info("Contract addresses retrieved from L1")
}

func l1ContractAdressFromZKevm(ctx context.Context, cfg *ethconfig.Zk, l1BlockSyncer *syncer.L1Syncer) error {
l1AddrRollup, err := l1BlockSyncer.CallRollupManager(ctx, &cfg.AddressZkevm)
if err != nil {
return err
}
cfg.AddressRollup = l1AddrRollup

l1AddrGerManager, err := l1BlockSyncer.CallGlobalExitRootManager(ctx, &cfg.AddressZkevm)
if err != nil {
return err
}
cfg.AddressGerManager = l1AddrGerManager

l1AddrSequencer, err := l1BlockSyncer.CallTrustedSequencer(ctx, &cfg.AddressZkevm)
if err != nil {
return err
}
cfg.AddressSequencer = l1AddrSequencer

return nil
}

func l1ContractAddressValidate(ctx context.Context, cfg *ethconfig.Zk, l1BlockSyncer *syncer.L1Syncer) {
if !cfg.L1ContractAddressCheck {
log.Info("Contract address check skipped")
return
}

success, err := l1ContractAddressCheck(ctx, cfg, l1BlockSyncer)
if !success || err != nil {
panic("Contract address check failed")
}

log.Info("Contract address check passed")
}

func l1ContractAddressCheck(ctx context.Context, cfg *ethconfig.Zk, l1BlockSyncer *syncer.L1Syncer) (bool, error) {
l1AddrRollup, err := l1BlockSyncer.CallRollupManager(ctx, &cfg.AddressZkevm)
if err != nil {
Expand All @@ -1564,6 +1606,7 @@ func l1ContractAddressCheck(ctx context.Context, cfg *ethconfig.Zk, l1BlockSynce
log.Warn("L1 contract address check failed (AddressRollup)", "expected", cfg.AddressRollup, "actual", l1AddrRollup)
return false, nil
}
log.Warn("🚨 zkevm.address-rollup configuration parameter is deprecated and it will be removed in upcoming releases")

if cfg.AddressAdmin != (libcommon.Address{}) {
log.Warn("🚨 zkevm.address-admin configuration parameter is deprecated and it will be removed in upcoming releases")
Expand All @@ -1577,6 +1620,7 @@ func l1ContractAddressCheck(ctx context.Context, cfg *ethconfig.Zk, l1BlockSynce
log.Warn("L1 contract address check failed (AddressGerManager)", "expected", cfg.AddressGerManager, "actual", l1AddrGerManager)
return false, nil
}
log.Warn("🚨 zkevm.address-ger-manager configuration parameter is deprecated and it will be removed in upcoming releases")

l1AddrSequencer, err := l1BlockSyncer.CallTrustedSequencer(ctx, &cfg.AddressZkevm)
if err != nil {
Expand All @@ -1586,5 +1630,7 @@ func l1ContractAddressCheck(ctx context.Context, cfg *ethconfig.Zk, l1BlockSynce
log.Warn("L1 contract address check failed (AddressSequencer)", "expected", cfg.AddressSequencer, "actual", l1AddrSequencer)
return false, nil
}
log.Warn("🚨 zkevm.address-sequencer configuration parameter is deprecated and it will be removed in upcoming releases")

return true, nil
}
1 change: 1 addition & 0 deletions eth/ethconfig/config_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Zk struct {
AddressZkevm common.Address
AddressGerManager common.Address
L1ContractAddressCheck bool
L1ContractAddressRetrieve bool
L1RollupId uint64
L1BlockRange uint64
L1QueryDelay uint64
Expand Down
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ var DefaultFlags = []cli.Flag{
&utils.L1FirstBlockFlag,
&utils.L1FinalizedBlockRequirementFlag,
&utils.L1ContractAddressCheckFlag,
&utils.L1ContractAddressRetrieveFlag,
&utils.RpcRateLimitsFlag,
&utils.RpcGetBatchWitnessConcurrencyLimitFlag,
&utils.DatastreamVersionFlag,
Expand Down
5 changes: 2 additions & 3 deletions turbo/cli/flags_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
L1FirstBlock: ctx.Uint64(utils.L1FirstBlockFlag.Name),
L1FinalizedBlockRequirement: ctx.Uint64(utils.L1FinalizedBlockRequirementFlag.Name),
L1ContractAddressCheck: ctx.Bool(utils.L1ContractAddressCheckFlag.Name),
L1ContractAddressRetrieve: ctx.Bool(utils.L1ContractAddressRetrieveFlag.Name),
RpcGetBatchWitnessConcurrencyLimit: ctx.Int(utils.RpcGetBatchWitnessConcurrencyLimitFlag.Name),
DatastreamVersion: ctx.Int(utils.DatastreamVersionFlag.Name),
RebuildTreeAfter: ctx.Uint64(utils.RebuildTreeAfterFlag.Name),
Expand Down Expand Up @@ -215,10 +216,7 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
}
}

checkFlag(utils.AddressSequencerFlag.Name, cfg.AddressSequencer)
checkFlag(utils.AddressRollupFlag.Name, cfg.AddressRollup)
checkFlag(utils.AddressZkevmFlag.Name, cfg.AddressZkevm)
checkFlag(utils.AddressGerManagerFlag.Name, cfg.AddressGerManager)

checkFlag(utils.L1ChainIdFlag.Name, cfg.L1ChainId)
checkFlag(utils.L1RpcUrlFlag.Name, cfg.L1RpcUrl)
Expand All @@ -230,4 +228,5 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
checkFlag(utils.L1QueryDelayFlag.Name, cfg.L1QueryDelay)
checkFlag(utils.TxPoolRejectSmartContractDeployments.Name, cfg.TxPoolRejectSmartContractDeployments)
checkFlag(utils.L1ContractAddressCheckFlag.Name, cfg.L1ContractAddressCheck)
checkFlag(utils.L1ContractAddressRetrieveFlag.Name, cfg.L1ContractAddressCheck)
}

0 comments on commit a551f76

Please sign in to comment.