From dcca9ae2d6c983dc6b6607cc6f68e7f1e35b56f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Fri, 6 Dec 2024 16:40:24 +0100 Subject: [PATCH] fix: calculate getLastGER based on UpdateL1InfoTree event --- cmd/importcombinedjson.go | 2 +- rollup/rollup_pessimistic_proofs.go | 38 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/cmd/importcombinedjson.go b/cmd/importcombinedjson.go index def9bb6..08b6b48 100644 --- a/cmd/importcombinedjson.go +++ b/cmd/importcombinedjson.go @@ -94,7 +94,7 @@ func importCombinedJson(cliCtx *cli.Context) error { return fmt.Errorf("failed to retrieve batch l2 data %w", err) } - lastGlobalExitRoot, err = r.GetLastGlobalExitRoot(rollupManager.GERAddr, client) + lastGlobalExitRoot, err = r.GetLastGlobalExitRoot(rollupManager, client) if err != nil { return fmt.Errorf("failed to retrieve batch l2 data %w", err) } diff --git a/rollup/rollup_pessimistic_proofs.go b/rollup/rollup_pessimistic_proofs.go index 273c9fd..a8f44a9 100644 --- a/rollup/rollup_pessimistic_proofs.go +++ b/rollup/rollup_pessimistic_proofs.go @@ -3,15 +3,18 @@ package rollup import ( "bytes" "context" + "errors" "fmt" "math/big" "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana-paris/polygonzkevmbridgev2" - "github.com/0xPolygon/cdk-contracts-tooling/contracts/banana-paris/polygonzkevmglobalexitroot" + "github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmglobalexitrootv2" "github.com/0xPolygon/cdk-contracts-tooling/contracts/pessimistic-proofs/polygonpessimisticconsensus" + "github.com/0xPolygon/cdk-contracts-tooling/rollupmanager" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) @@ -114,18 +117,43 @@ func (r *RollupPessimisticProofs) GetBatchL2Data(client bind.ContractBackend) (s } // GetLastGlobalExitRoot retrieves the last global exit root from global exit root manager -func (r *RollupPessimisticProofs) GetLastGlobalExitRoot(gerAddr common.Address, client bind.ContractBackend) (common.Hash, error) { - gerContract, err := polygonzkevmglobalexitroot.NewPolygonzkevmglobalexitroot(gerAddr, client) +func (r *RollupPessimisticProofs) GetLastGlobalExitRoot(rm *rollupmanager.RollupManager, client bind.ContractBackend) (common.Hash, error) { + gerContract, err := polygonzkevmglobalexitrootv2.NewPolygonzkevmglobalexitrootv2(rm.GERAddr, client) if err != nil { return common.Hash{}, err } - lastGER, err := gerContract.GetLastGlobalExitRoot(nil) + if r.CreationBlock == 0 { + return common.Hash{}, errors.New("rollup creation block number is zero") + } + + endBlock := r.CreationBlock - 1 + iter, err := gerContract.FilterUpdateL1InfoTree( + &bind.FilterOpts{ + Start: rm.UpdateToULxLyBlock, + End: &endBlock, + }, nil, nil) + + if err != nil { + return common.Hash{}, err + } + + // We need to grab the latest UpdateL1InfoTree event + var globalExitRoot common.Hash + for iter.Next() { + globalExitRoot = common.BytesToHash( + crypto.Keccak256( + iter.Event.MainnetExitRoot[:], + iter.Event.RollupExitRoot[:], + )) + } + + err = iter.Close() if err != nil { return common.Hash{}, err } - return common.BytesToHash(lastGER[:]), nil + return common.BytesToHash(globalExitRoot[:]), nil } type preEIP155Transaction struct {