Skip to content

Commit

Permalink
fixed some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixFan1992 committed Feb 18, 2024
1 parent 41fd1ec commit 88d3dce
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 91 deletions.
137 changes: 69 additions & 68 deletions integration-tests/actions/automationv2/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"testing"
"time"

"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/automation_registrar_wrapper2_2"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/lib/pq"
Expand All @@ -25,6 +22,9 @@ import (
"golang.org/x/sync/errgroup"
"gopkg.in/guregu/null.v4"

"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/automation_registrar_wrapper2_1"

ocr2keepers20config "github.com/smartcontractkit/chainlink-automation/pkg/v2/config"
ocr2keepers30config "github.com/smartcontractkit/chainlink-automation/pkg/v3/config"

Expand All @@ -35,7 +35,6 @@ import (
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
"github.com/smartcontractkit/chainlink/integration-tests/contracts/ethereum"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/automation_registrar_wrapper2_1"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/keeper_registrar_wrapper2_0"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
Expand Down Expand Up @@ -413,7 +412,6 @@ func (a *AutomationTest) SetConfigOnRegistry() error {
donNodes := a.NodeDetails[1:]
S := make([]int, len(donNodes))
oracleIdentities := make([]confighelper.OracleIdentityExtra, len(donNodes))
var offC []byte
var signerOnchainPublicKeys []types.OnchainPublicKey
var transmitterAccounts []types.Account
var f uint8
Expand Down Expand Up @@ -472,58 +470,12 @@ func (a *AutomationTest) SetConfigOnRegistry() error {

switch a.RegistrySettings.RegistryVersion {
case ethereum.RegistryVersion_2_0:
offC, err = json.Marshal(ocr2keepers20config.OffchainConfig{
TargetProbability: a.PluginConfig.TargetProbability,
TargetInRounds: a.PluginConfig.TargetInRounds,
PerformLockoutWindow: a.PluginConfig.PerformLockoutWindow,
GasLimitPerReport: a.PluginConfig.GasLimitPerReport,
GasOverheadPerUpkeep: a.PluginConfig.GasOverheadPerUpkeep,
MinConfirmations: a.PluginConfig.MinConfirmations,
MaxUpkeepBatchSize: a.PluginConfig.MaxUpkeepBatchSize,
})
if err != nil {
return errors.Join(err, fmt.Errorf("failed to marshal plugin config"))
}

signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = ocr2.ContractSetConfigArgsForTests(
a.PublicConfig.DeltaProgress, a.PublicConfig.DeltaResend,
a.PublicConfig.DeltaRound, a.PublicConfig.DeltaGrace,
a.PublicConfig.DeltaStage, uint8(a.PublicConfig.RMax),
S, oracleIdentities, offC,
a.PublicConfig.MaxDurationQuery, a.PublicConfig.MaxDurationObservation,
1200*time.Millisecond,
a.PublicConfig.MaxDurationShouldAcceptAttestedReport,
a.PublicConfig.MaxDurationShouldTransmitAcceptedReport,
a.PublicConfig.F, a.PublicConfig.OnchainConfig,
)
signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = calculateOCR2ConfigArgs(a, S, oracleIdentities)
if err != nil {
return errors.Join(err, fmt.Errorf("failed to build config args"))
}

case ethereum.RegistryVersion_2_1, ethereum.RegistryVersion_2_2:
offC, err = json.Marshal(ocr2keepers30config.OffchainConfig{
TargetProbability: a.PluginConfig.TargetProbability,
TargetInRounds: a.PluginConfig.TargetInRounds,
PerformLockoutWindow: a.PluginConfig.PerformLockoutWindow,
GasLimitPerReport: a.PluginConfig.GasLimitPerReport,
GasOverheadPerUpkeep: a.PluginConfig.GasOverheadPerUpkeep,
MinConfirmations: a.PluginConfig.MinConfirmations,
MaxUpkeepBatchSize: a.PluginConfig.MaxUpkeepBatchSize,
})
if err != nil {
return errors.Join(err, fmt.Errorf("failed to marshal plugin config"))
}

signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = ocr3.ContractSetConfigArgsForTests(
a.PublicConfig.DeltaProgress, a.PublicConfig.DeltaResend, a.PublicConfig.DeltaInitial,
a.PublicConfig.DeltaRound, a.PublicConfig.DeltaGrace, a.PublicConfig.DeltaCertifiedCommitRequest,
a.PublicConfig.DeltaStage, a.PublicConfig.RMax,
S, oracleIdentities, offC,
a.PublicConfig.MaxDurationQuery, a.PublicConfig.MaxDurationObservation,
a.PublicConfig.MaxDurationShouldAcceptAttestedReport,
a.PublicConfig.MaxDurationShouldTransmitAcceptedReport,
a.PublicConfig.F, a.PublicConfig.OnchainConfig,
)
signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = calculateOCR3ConfigArgs(a, S, oracleIdentities)
if err != nil {
return errors.Join(err, fmt.Errorf("failed to build config args"))
}
Expand Down Expand Up @@ -568,6 +520,69 @@ func (a *AutomationTest) SetConfigOnRegistry() error {
return nil
}

func calculateOCR2ConfigArgs(a *AutomationTest, S []int, oracleIdentities []confighelper.OracleIdentityExtra) (
signers []types.OnchainPublicKey,
transmitters []types.Account,
f_ uint8,
onchainConfig_ []byte,
offchainConfigVersion uint64,
offchainConfig []byte,
err error,
) {
offC, _ := json.Marshal(ocr2keepers20config.OffchainConfig{
TargetProbability: a.PluginConfig.TargetProbability,
TargetInRounds: a.PluginConfig.TargetInRounds,
PerformLockoutWindow: a.PluginConfig.PerformLockoutWindow,
GasLimitPerReport: a.PluginConfig.GasLimitPerReport,
GasOverheadPerUpkeep: a.PluginConfig.GasOverheadPerUpkeep,
MinConfirmations: a.PluginConfig.MinConfirmations,
MaxUpkeepBatchSize: a.PluginConfig.MaxUpkeepBatchSize,
})

return ocr2.ContractSetConfigArgsForTests(
a.PublicConfig.DeltaProgress, a.PublicConfig.DeltaResend,
a.PublicConfig.DeltaRound, a.PublicConfig.DeltaGrace,
a.PublicConfig.DeltaStage, uint8(a.PublicConfig.RMax),
S, oracleIdentities, offC,
a.PublicConfig.MaxDurationQuery, a.PublicConfig.MaxDurationObservation,
1200*time.Millisecond,
a.PublicConfig.MaxDurationShouldAcceptAttestedReport,
a.PublicConfig.MaxDurationShouldTransmitAcceptedReport,
a.PublicConfig.F, a.PublicConfig.OnchainConfig,
)
}

func calculateOCR3ConfigArgs(a *AutomationTest, S []int, oracleIdentities []confighelper.OracleIdentityExtra) (
signers []types.OnchainPublicKey,
transmitters []types.Account,
f_ uint8,
onchainConfig_ []byte,
offchainConfigVersion uint64,
offchainConfig []byte,
err error,
) {
offC, _ := json.Marshal(ocr2keepers30config.OffchainConfig{
TargetProbability: a.PluginConfig.TargetProbability,
TargetInRounds: a.PluginConfig.TargetInRounds,
PerformLockoutWindow: a.PluginConfig.PerformLockoutWindow,
GasLimitPerReport: a.PluginConfig.GasLimitPerReport,
GasOverheadPerUpkeep: a.PluginConfig.GasOverheadPerUpkeep,
MinConfirmations: a.PluginConfig.MinConfirmations,
MaxUpkeepBatchSize: a.PluginConfig.MaxUpkeepBatchSize,
})

return ocr3.ContractSetConfigArgsForTests(
a.PublicConfig.DeltaProgress, a.PublicConfig.DeltaResend, a.PublicConfig.DeltaInitial,
a.PublicConfig.DeltaRound, a.PublicConfig.DeltaGrace, a.PublicConfig.DeltaCertifiedCommitRequest,
a.PublicConfig.DeltaStage, a.PublicConfig.RMax,
S, oracleIdentities, offC,
a.PublicConfig.MaxDurationQuery, a.PublicConfig.MaxDurationObservation,
a.PublicConfig.MaxDurationShouldAcceptAttestedReport,
a.PublicConfig.MaxDurationShouldTransmitAcceptedReport,
a.PublicConfig.F, a.PublicConfig.OnchainConfig,
)
}

func (a *AutomationTest) RegisterUpkeeps(upkeepConfigs []UpkeepConfig) ([]common.Hash, error) {
var registrarABI *abi.ABI
var err error
Expand All @@ -590,7 +605,7 @@ func (a *AutomationTest) RegisterUpkeeps(upkeepConfigs []UpkeepConfig) ([]common
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to pack registrar request"))
}
case ethereum.RegistryVersion_2_1:
case ethereum.RegistryVersion_2_1, ethereum.RegistryVersion_2_2: // 2.1 and 2.2 use the same registrar
registrarABI, err = automation_registrar_wrapper2_1.AutomationRegistrarMetaData.GetAbi()
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to get registrar abi"))
Expand All @@ -604,20 +619,6 @@ func (a *AutomationTest) RegisterUpkeeps(upkeepConfigs []UpkeepConfig) ([]common
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to pack registrar request"))
}
case ethereum.RegistryVersion_2_2:
registrarABI, err = automation_registrar_wrapper2_2.AutomationRegistrarMetaData.GetAbi()
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to get registrar abi"))
}
registrationRequest, err = registrarABI.Pack(
"register", upkeepConfig.UpkeepName, upkeepConfig.EncryptedEmail,
upkeepConfig.UpkeepContract, upkeepConfig.GasLimit, upkeepConfig.AdminAddress,
upkeepConfig.TriggerType, upkeepConfig.CheckData, upkeepConfig.TriggerConfig,
upkeepConfig.OffchainConfig, upkeepConfig.FundingAmount,
common.HexToAddress(a.ChainClient.GetDefaultWallet().Address()))
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to pack registrar request"))
}
default:
return nil, fmt.Errorf("v2.0, v2.1, and v2.2 are the only supported versions")
}
Expand Down
42 changes: 20 additions & 22 deletions integration-tests/contracts/contract_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,21 +1134,11 @@ func (e *EthereumContractDeployer) DeployKeeperRegistry(
}, err

case eth_contracts.RegistryVersion_2_1:
automationForwarderLogicAddr, _, _, err := e.client.DeployContract("automationForwarderLogic", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return automationForwarderLogic.DeployAutomationForwarderLogic(auth, backend)
})

automationForwarderLogicAddr, err := deployAutomationForwarderLogic(e.client)
if err != nil {
return nil, err
}

if err := e.client.WaitForEvents(); err != nil {
return nil, err
}

registryLogicBAddr, _, _, err := e.client.DeployContract("KeeperRegistryLogicB2_1", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
Expand Down Expand Up @@ -1221,21 +1211,11 @@ func (e *EthereumContractDeployer) DeployKeeperRegistry(
address: address,
}, err
case eth_contracts.RegistryVersion_2_2:
automationForwarderLogicAddr, _, _, err := e.client.DeployContract("automationForwarderLogic", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return automationForwarderLogic.DeployAutomationForwarderLogic(auth, backend)
})

automationForwarderLogicAddr, err := deployAutomationForwarderLogic(e.client)
if err != nil {
return nil, err
}

if err := e.client.WaitForEvents(); err != nil {
return nil, err
}

registryLogicBAddr, _, _, err := e.client.DeployContract("AutomationRegistryLogicB2_2", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
Expand Down Expand Up @@ -1312,6 +1292,24 @@ func (e *EthereumContractDeployer) DeployKeeperRegistry(
}
}

func deployAutomationForwarderLogic(client blockchain.EVMClient) (*common.Address, error) {
automationForwarderLogicAddr, _, _, err := client.DeployContract("automationForwarderLogic", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
return automationForwarderLogic.DeployAutomationForwarderLogic(auth, backend)
})

if err != nil {
return nil, err
}

if err := client.WaitForEvents(); err != nil {
return nil, err
}
return automationForwarderLogicAddr, nil
}

// LoadKeeperRegistry returns deployed on given address EthereumKeeperRegistry
func (e *EthereumContractDeployer) LoadKeeperRegistry(address common.Address, registryVersion eth_contracts.KeeperRegistryVersion) (KeeperRegistry, error) {
switch registryVersion {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/contracts/ethereum_keeper_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (rcs *KeeperRegistrySettings) EncodeOnChainConfig(registrar string, registr

return encodedOnchainConfig, err
} else if rcs.RegistryVersion == ethereum.RegistryVersion_2_2 {
chainModuleBaseAddr, _, _, err := client.DeployContract("ChainModuleBase", func(
chainModuleBaseAddr, _, _, _ := client.DeployContract("ChainModuleBase", func(
auth *bind.TransactOpts,
backend bind.ContractBackend,
) (common.Address, *types.Transaction, interface{}, error) {
Expand Down

0 comments on commit 88d3dce

Please sign in to comment.