Skip to content

Commit

Permalink
gas optimizations, readability
Browse files Browse the repository at this point in the history
  • Loading branch information
makramkd committed Jun 21, 2024
1 parent ef97195 commit 8111d24
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 71 deletions.
58 changes: 29 additions & 29 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -36,53 +36,53 @@ BurnWithFromMintTokenPool_lockOrBurn:test_PoolBurn_Success() (gas: 243659)
BurnWithFromMintTokenPool_lockOrBurn:test_Setup_Success() (gas: 24260)
CCIPCapabilityConfigurationSetup:test_getCapabilityConfiguration_Success() (gas: 9561)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeConfigDigest_Success() (gas: 70645)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_InitToRunning_Success() (gas: 350557)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_RunningToStaging_Success() (gas: 471502)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_StagingToRunning_Success() (gas: 440224)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_InitToRunning_Success() (gas: 350565)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_RunningToStaging_Success() (gas: 471510)
CCIPCapabilityConfiguration_ConfigStateMachine:test__computeNewConfigWithMeta_StagingToRunning_Success() (gas: 440232)
CCIPCapabilityConfiguration_ConfigStateMachine:test__groupByPluginType_TooManyOCR3Configs_Reverts() (gas: 37027)
CCIPCapabilityConfiguration_ConfigStateMachine:test__groupByPluginType_threeCommitConfigs_Reverts() (gas: 61083)
CCIPCapabilityConfiguration_ConfigStateMachine:test__groupByPluginType_threeExecutionConfigs_Reverts() (gas: 61003)
CCIPCapabilityConfiguration_ConfigStateMachine:test__groupByPluginType_threeCommitConfigs_Reverts() (gas: 61043)
CCIPCapabilityConfiguration_ConfigStateMachine:test__groupByPluginType_threeExecutionConfigs_Reverts() (gas: 60963)
CCIPCapabilityConfiguration_ConfigStateMachine:test__stateFromConfigLength_Success() (gas: 11764)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigStateTransition_Success() (gas: 9028)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_InitToRunning_Success() (gas: 303030)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_InitToRunning_Success() (gas: 303038)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_InitToRunning_WrongConfigCount_Reverts() (gas: 49619)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_NonExistentConfigTransition_Reverts() (gas: 32253)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_RunningToStaging_Success() (gas: 367508)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_RunningToStaging_Success() (gas: 367516)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_RunningToStaging_WrongConfigCount_Reverts() (gas: 120877)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_RunningToStaging_WrongConfigDigestBlueGreen_Reverts() (gas: 156973)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_StagingToRunning_Success() (gas: 367284)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_StagingToRunning_Success() (gas: 367292)
CCIPCapabilityConfiguration_ConfigStateMachine:test__validateConfigTransition_StagingToRunning_WrongConfigDigest_Reverts() (gas: 157040)
CCIPCapabilityConfiguration_ConfigStateMachine:test_getCapabilityConfiguration_Success() (gas: 9648)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_InitToRunning_Success() (gas: 1044424)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_InitToRunning_Success() (gas: 1044432)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_InvalidConfigLength_Reverts() (gas: 27539)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_InvalidConfigStateTransition_Reverts() (gas: 23118)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_RunningToStaging_Success() (gas: 1992342)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_StagingToRunning_Success() (gas: 2595924)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_RunningToStaging_Success() (gas: 1992348)
CCIPCapabilityConfiguration__updatePluginConfig:test__updatePluginConfig_StagingToRunning_Success() (gas: 2595930)
CCIPCapabilityConfiguration__updatePluginConfig:test_getCapabilityConfiguration_Success() (gas: 9626)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_CommitAndExecConfig_Success() (gas: 1834043)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_CommitConfigOnly_Success() (gas: 1055353)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_ExecConfigOnly_Success() (gas: 1055384)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_CommitAndExecConfig_Success() (gas: 1834017)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_CommitConfigOnly_Success() (gas: 1055344)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_ExecConfigOnly_Success() (gas: 1055375)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_OnlyCapabilityRegistryCanCall_Reverts() (gas: 9576)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_beforeCapabilityConfigSet_ZeroLengthConfig_Success() (gas: 16070)
CCIPCapabilityConfiguration_beforeCapabilityConfigSet:test_getCapabilityConfiguration_Success() (gas: 9626)
CCIPCapabilityConfiguration_chainConfig:test__applyChainConfigUpdates_FChainNotPositive_Reverts() (gas: 182901)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_addChainConfigs_Success() (gas: 342456)
CCIPCapabilityConfiguration_chainConfig:test__applyChainConfigUpdates_FChainNotPositive_Reverts() (gas: 182909)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_addChainConfigs_Success() (gas: 342472)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_nodeNotInRegistry_Reverts() (gas: 19116)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_removeChainConfigs_Success() (gas: 266058)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_removeChainConfigs_Success() (gas: 266071)
CCIPCapabilityConfiguration_chainConfig:test_applyChainConfigUpdates_selectorNotFound_Reverts() (gas: 14807)
CCIPCapabilityConfiguration_chainConfig:test_getCapabilityConfiguration_Success() (gas: 9604)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_ChainSelectorNotFound_Reverts() (gas: 285375)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_ChainSelectorNotSet_Reverts() (gas: 282493)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_FMustBePositive_Reverts() (gas: 283414)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_FTooHigh_Reverts() (gas: 283580)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_NodeNotInRegistry_Reverts() (gas: 287807)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_NotEnoughTransmitters_Reverts() (gas: 1068536)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_OfframpAddressCannotBeZero_Reverts() (gas: 282301)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_P2PIdsLengthNotMatching_Reverts() (gas: 284269)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_Success() (gas: 289214)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManyBootstrapP2PIds_Reverts() (gas: 285510)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManySigners_Reverts() (gas: 1120652)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManyTransmitters_Reverts() (gas: 1118992)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_ChainSelectorNotFound_Reverts() (gas: 285383)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_ChainSelectorNotSet_Reverts() (gas: 282501)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_FMustBePositive_Reverts() (gas: 283422)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_FTooHigh_Reverts() (gas: 283588)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_NodeNotInRegistry_Reverts() (gas: 287815)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_NotEnoughTransmitters_Reverts() (gas: 1068544)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_OfframpAddressCannotBeZero_Reverts() (gas: 282309)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_P2PIdsLengthNotMatching_Reverts() (gas: 284277)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_Success() (gas: 289222)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManyBootstrapP2PIds_Reverts() (gas: 285518)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManySigners_Reverts() (gas: 1120660)
CCIPCapabilityConfiguration_validateConfig:test__validateConfig_TooManyTransmitters_Reverts() (gas: 1119000)
CCIPCapabilityConfiguration_validateConfig:test_getCapabilityConfiguration_Success() (gas: 9540)
CCIPClientExample_sanity:test_ImmutableExamples_Success() (gas: 2133850)
CommitStore_constructor:test_Constructor_Success() (gas: 3091440)
Expand Down
54 changes: 14 additions & 40 deletions contracts/src/v0.8/ccip/capability/CCIPCapabilityConfiguration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ contract CCIPCapabilityConfiguration is ITypeAndVersion, ICapabilityConfiguratio
// Update contract state with new configuration if its valid.
// We won't run out of gas from this delete since the array is at most 2 elements long.
delete s_ocr3Configs[donId][pluginType];
for (uint256 i = 0; i < newConfigWithMeta.length; i++) {
for (uint256 i = 0; i < newConfigWithMeta.length; ++i) {
s_ocr3Configs[donId][pluginType].push(newConfigWithMeta[i]);
}
}
Expand Down Expand Up @@ -348,8 +348,8 @@ contract CCIPCapabilityConfiguration is ITypeAndVersion, ICapabilityConfiguratio
// access in the for loop below.
commitConfigs = new OCR3Config[](MAX_OCR3_CONFIGS_PER_PLUGIN);
execConfigs = new OCR3Config[](MAX_OCR3_CONFIGS_PER_PLUGIN);
uint8 commitCount;
uint8 execCount;
uint256 commitCount;
uint256 execCount;
for (uint256 i = 0; i < ocr3Configs.length; ++i) {
if (ocr3Configs[i].pluginType == PluginType.Commit) {
commitConfigs[commitCount] = ocr3Configs[i];
Expand All @@ -370,55 +370,29 @@ contract CCIPCapabilityConfiguration is ITypeAndVersion, ICapabilityConfiguratio
}

function _validateConfig(OCR3Config memory cfg) internal view {
if (cfg.chainSelector == 0) {
revert ChainSelectorNotSet();
}

if (cfg.pluginType != PluginType.Commit && cfg.pluginType != PluginType.Execution) {
revert InvalidPluginType();
}

if (cfg.chainSelector == 0) revert ChainSelectorNotSet();
if (cfg.pluginType != PluginType.Commit && cfg.pluginType != PluginType.Execution) revert InvalidPluginType();
// TODO: can we do more sophisticated validation than this?
if (cfg.offrampAddress.length == 0) {
revert OfframpAddressCannotBeZero();
}

// Check that the chain configuration is set.
if (!s_remoteChainSelectors.contains(cfg.chainSelector)) {
revert ChainSelectorNotFound(cfg.chainSelector);
}
if (cfg.offrampAddress.length == 0) revert OfframpAddressCannotBeZero();
if (!s_remoteChainSelectors.contains(cfg.chainSelector)) revert ChainSelectorNotFound(cfg.chainSelector);

// Some of these checks below are done in OCR2/3Base config validation, so we do them again here.
// Role DON OCR configs will have all the Role DON signers but only a subset of transmitters.
if (cfg.signers.length > MAX_NUM_ORACLES) {
revert TooManySigners();
}

if (cfg.transmitters.length > MAX_NUM_ORACLES) {
revert TooManyTransmitters();
}
if (cfg.signers.length > MAX_NUM_ORACLES) revert TooManySigners();
if (cfg.transmitters.length > MAX_NUM_ORACLES) revert TooManyTransmitters();

// We check for chain config presence above, so fChain here must be non-zero.
uint256 minTransmittersLength = 3 * s_chainConfigurations[cfg.chainSelector].fChain + 1;
if (cfg.transmitters.length < minTransmittersLength) {
revert NotEnoughTransmitters(cfg.transmitters.length, minTransmittersLength);
}

if (cfg.F == 0) {
revert FMustBePositive();
}

if (cfg.signers.length <= 3 * cfg.F) {
revert FTooHigh();
}
if (cfg.F == 0) revert FMustBePositive();
if (cfg.signers.length <= 3 * cfg.F) revert FTooHigh();

if (cfg.p2pIds.length != cfg.signers.length || cfg.p2pIds.length != cfg.transmitters.length) {
revert P2PIdsLengthNotMatching(cfg.p2pIds.length, cfg.signers.length, cfg.transmitters.length);
}

if (cfg.bootstrapP2PIds.length > cfg.p2pIds.length) {
revert TooManyBootstrapP2PIds();
}
if (cfg.bootstrapP2PIds.length > cfg.p2pIds.length) revert TooManyBootstrapP2PIds();

// Check that the readers are in the capability registry.
// TODO: check for duplicate signers, duplicate p2p ids, etc.
Expand Down Expand Up @@ -477,7 +451,7 @@ contract CCIPCapabilityConfiguration is ITypeAndVersion, ICapabilityConfiguratio
ChainConfigInfo[] calldata chainConfigAdds
) external onlyOwner {
// Process removals first.
for (uint256 i = 0; i < chainSelectorRemoves.length; i++) {
for (uint256 i = 0; i < chainSelectorRemoves.length; ++i) {
// check if the chain selector is in s_remoteChainSelectors first.
if (!s_remoteChainSelectors.contains(chainSelectorRemoves[i])) {
revert ChainSelectorNotFound(chainSelectorRemoves[i]);
Expand All @@ -490,7 +464,7 @@ contract CCIPCapabilityConfiguration is ITypeAndVersion, ICapabilityConfiguratio
}

// Process additions next.
for (uint256 i = 0; i < chainConfigAdds.length; i++) {
for (uint256 i = 0; i < chainConfigAdds.length; ++i) {
ChainConfig memory chainConfig = chainConfigAdds[i].chainConfig;
bytes32[] memory readers = chainConfig.readers;
uint64 chainSelector = chainConfigAdds[i].chainSelector;
Expand Down
Loading

0 comments on commit 8111d24

Please sign in to comment.