From ba1a8cd0111349b2db74e32372735d82aca69c55 Mon Sep 17 00:00:00 2001 From: Graham Goh Date: Tue, 17 Dec 2024 13:47:37 +1100 Subject: [PATCH] refactor(mcms): handle nil scenario Some scenarios will throw nil pointer exception without using the ok pattern check. We should handle those properly. --- .../transfer_to_mcms_with_timelock.go | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/deployment/common/changeset/transfer_to_mcms_with_timelock.go b/deployment/common/changeset/transfer_to_mcms_with_timelock.go index afba1afa48b..fbb4b6b45cd 100644 --- a/deployment/common/changeset/transfer_to_mcms_with_timelock.go +++ b/deployment/common/changeset/transfer_to_mcms_with_timelock.go @@ -218,25 +218,46 @@ type RenounceTimelockDeployerConfig struct { ChainSel uint64 } +func (cfg RenounceTimelockDeployerConfig) Validate() error { + if err := deployment.IsValidChainSelector(cfg.ChainSel); err != nil { + return fmt.Errorf("invalid chain selector: %w", err) + } + return nil +} + // RenounceTimelockDeployer revokes the deployer key from administering the contract. func RenounceTimelockDeployer(e deployment.Environment, cfg RenounceTimelockDeployerConfig) (deployment.ChangesetOutput, error) { + if err := cfg.Validate(); err != nil { + return deployment.ChangesetOutput{}, err + } + contracts, err := MaybeLoadMCMSWithTimelockState(e, []uint64{cfg.ChainSel}) if err != nil { return deployment.ChangesetOutput{}, err } - tl := contracts[cfg.ChainSel].Timelock + contract, ok := contracts[cfg.ChainSel] + if !ok { + return deployment.ChangesetOutput{}, fmt.Errorf("mcms contracts not found on chain %d", cfg.ChainSel) + } + tl := contract.Timelock if tl == nil { return deployment.ChangesetOutput{}, fmt.Errorf("timelock not found on chain %d", cfg.ChainSel) } - admin, err := tl.ADMINROLE(&bind.CallOpts{}) + admin, err := tl.ADMINROLE(&bind.CallOpts{Context: e.GetContext()}) if err != nil { return deployment.ChangesetOutput{}, fmt.Errorf("failed to get admin role: %w", err) } - tx, err := tl.RenounceRole(e.Chains[cfg.ChainSel].DeployerKey, admin, e.Chains[cfg.ChainSel].DeployerKey.From) + + chain, ok := e.Chains[cfg.ChainSel] + if !ok { + return deployment.ChangesetOutput{}, fmt.Errorf("chain %d not found", cfg.ChainSel) + } + + tx, err := tl.RenounceRole(chain.DeployerKey, admin, chain.DeployerKey.From) if err != nil { return deployment.ChangesetOutput{}, fmt.Errorf("failed to revoke deployer key: %w", err) } - if _, err := deployment.ConfirmIfNoError(e.Chains[cfg.ChainSel], tx, err); err != nil { + if _, err := deployment.ConfirmIfNoError(chain, tx, err); err != nil { return deployment.ChangesetOutput{}, err } e.Logger.Infof("revoked deployer key from owning contract %s", tl.Address().Hex())