Skip to content

Commit

Permalink
add ✨ art ✨ and getActiveIndex getCandidateIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Sep 27, 2024
1 parent 5cf7ccb commit c12b5ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
16 changes: 8 additions & 8 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -817,22 +817,22 @@ RMNHome__validateStaticAndDynamicConfig:test_validateStaticAndDynamicConfig_Dupl
RMNHome__validateStaticAndDynamicConfig:test_validateStaticAndDynamicConfig_MinObserversTooHigh_reverts() (gas: 20810)
RMNHome__validateStaticAndDynamicConfig:test_validateStaticAndDynamicConfig_OutOfBoundsNodesLength_reverts() (gas: 137268)
RMNHome__validateStaticAndDynamicConfig:test_validateStaticAndDynamicConfig_OutOfBoundsObserverNodeIndex_reverts() (gas: 20472)
RMNHome_getConfigDigests:test_getConfigDigests_success() (gas: 1077910)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_ConfigDigestMismatch_reverts() (gas: 23866)
RMNHome_getConfigDigests:test_getConfigDigests_success() (gas: 1077745)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_ConfigDigestMismatch_reverts() (gas: 23857)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_NoOpStateTransitionNotAllowed_reverts() (gas: 10575)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_OnlyOwner_reverts() (gas: 10936)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_success() (gas: 1083149)
RMNHome_revokeCandidate:test_revokeCandidate_ConfigDigestMismatch_reverts() (gas: 19090)
RMNHome_promoteCandidateAndRevokeActive:test_promoteCandidateAndRevokeActive_success() (gas: 1083071)
RMNHome_revokeCandidate:test_revokeCandidate_ConfigDigestMismatch_reverts() (gas: 19063)
RMNHome_revokeCandidate:test_revokeCandidate_OnlyOwner_reverts() (gas: 10963)
RMNHome_revokeCandidate:test_revokeCandidate_RevokingZeroDigestNotAllowed_reverts() (gas: 10606)
RMNHome_revokeCandidate:test_revokeCandidate_success() (gas: 28201)
RMNHome_setCandidate:test_setCandidate_ConfigDigestMismatch_reverts() (gas: 594772)
RMNHome_revokeCandidate:test_revokeCandidate_success() (gas: 28147)
RMNHome_setCandidate:test_setCandidate_ConfigDigestMismatch_reverts() (gas: 594679)
RMNHome_setCandidate:test_setCandidate_OnlyOwner_reverts() (gas: 15177)
RMNHome_setCandidate:test_setCandidate_success() (gas: 588430)
RMNHome_setCandidate:test_setCandidate_success() (gas: 588379)
RMNHome_setDynamicConfig:test_setDynamicConfig_DigestNotFound_reverts() (gas: 30159)
RMNHome_setDynamicConfig:test_setDynamicConfig_MinObserversTooHigh_reverts() (gas: 18848)
RMNHome_setDynamicConfig:test_setDynamicConfig_OnlyOwner_reverts() (gas: 14115)
RMNHome_setDynamicConfig:test_setDynamicConfig_success() (gas: 103992)
RMNHome_setDynamicConfig:test_setDynamicConfig_success() (gas: 103911)
RMNRemote_constructor:test_constructor_success() (gas: 8334)
RMNRemote_constructor:test_constructor_zeroChainSelector_reverts() (gas: 59165)
RMNRemote_curse:test_curse_AlreadyCursed_duplicateSubject_reverts() (gas: 154457)
Expand Down
44 changes: 35 additions & 9 deletions contracts/src/v0.8/ccip/rmn/RMNHome.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ import {OwnerIsCreator} from "../../shared/access/OwnerIsCreator.sol";
/// Note that we explicitly do allow promoteCandidateAndRevokeActive() to be called when there is an active config but
/// no candidate config. This is the only way to remove the active config. The alternative would be to set some unusable
/// config as candidate and promote that, but fully clearing it is cleaner.
///
/// ┌─────────────┐ setCandidate ┌─────────────┐
/// │ ├───────────────────►│ │ setCandidate
/// │ Init │ revokeCandidate │ Candidate │◄───────────┐
/// │ [0,0] │◄───────────────────┤ [0,1] │────────────┘
/// │ │ ┌─────────────────┤ │
/// └─────────────┘ │ promote- └─────────────┘
/// ▲ │ Candidate
/// promote- │ │
/// Candidate │ │
/// │ │
/// ┌──────────┴──┐ │ promote- ┌─────────────┐
/// │ │◄─┘ Candidate OR │ Active & │ setCandidate
/// │ Active │ revokeCandidate │ Candidate │◄───────────┐
/// │ [1,0] │◄───────────────────┤ [1,1] │────────────┘
/// │ ├───────────────────►│ │
/// └─────────────┘ setSecondary └─────────────┘
///
contract RMNHome is OwnerIsCreator, ITypeAndVersion {
event ConfigSet(bytes32 indexed configDigest, uint32 version, StaticConfig staticConfig, DynamicConfig dynamicConfig);
event ActiveConfigRevoked(bytes32 indexed configDigest);
Expand Down Expand Up @@ -125,17 +143,17 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
/// @return activeConfigDigest The digest of the active config.
/// @return candidateConfigDigest The digest of the candidate config.
function getConfigDigests() external view returns (bytes32 activeConfigDigest, bytes32 candidateConfigDigest) {
return (s_configs[s_activeConfigIndex].configDigest, s_configs[s_activeConfigIndex ^ 1].configDigest);
return (s_configs[getActiveIndex()].configDigest, s_configs[getCandidateIndex()].configDigest);
}

/// @notice Returns the active config digest
function getActiveDigest() external view returns (bytes32) {
return s_configs[s_activeConfigIndex].configDigest;
return s_configs[getActiveIndex()].configDigest;
}

/// @notice Returns the candidate config digest
function getCandidateDigest() public view returns (bytes32) {
return s_configs[s_activeConfigIndex ^ 1].configDigest;
return s_configs[getCandidateIndex()].configDigest;
}

/// @notice The offchain code can use this to fetch an old config which might still be in use by some remotes. Use
Expand All @@ -159,12 +177,12 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
view
returns (VersionedConfig memory activeConfig, VersionedConfig memory candidateConfig)
{
VersionedConfig memory storedActiveConfig = s_configs[s_activeConfigIndex];
VersionedConfig memory storedActiveConfig = s_configs[getActiveIndex()];
if (storedActiveConfig.configDigest != ZERO_DIGEST) {
activeConfig = storedActiveConfig;
}

VersionedConfig memory storedCandidateConfig = s_configs[s_activeConfigIndex ^ 1];
VersionedConfig memory storedCandidateConfig = s_configs[getCandidateIndex()];
if (storedCandidateConfig.configDigest != ZERO_DIGEST) {
candidateConfig = storedCandidateConfig;
}
Expand Down Expand Up @@ -203,7 +221,7 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
uint32 newVersion = ++s_currentVersion;
newConfigDigest = _calculateConfigDigest(abi.encode(staticConfig), newVersion);

VersionedConfig storage existingConfig = s_configs[s_activeConfigIndex ^ 1];
VersionedConfig storage existingConfig = s_configs[getCandidateIndex()];
existingConfig.configDigest = newConfigDigest;
existingConfig.version = newVersion;
existingConfig.staticConfig = staticConfig;
Expand All @@ -223,7 +241,7 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
revert RevokingZeroDigestNotAllowed();
}

uint256 candidateConfigIndex = s_activeConfigIndex ^ 1;
uint256 candidateConfigIndex = getCandidateIndex();
if (s_configs[candidateConfigIndex].configDigest != configDigest) {
revert ConfigDigestMismatch(s_configs[candidateConfigIndex].configDigest, configDigest);
}
Expand All @@ -246,12 +264,12 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
revert NoOpStateTransitionNotAllowed();
}

uint256 candidateConfigIndex = s_activeConfigIndex ^ 1;
uint256 candidateConfigIndex = getCandidateIndex();
if (s_configs[candidateConfigIndex].configDigest != digestToPromote) {
revert ConfigDigestMismatch(s_configs[candidateConfigIndex].configDigest, digestToPromote);
}

VersionedConfig storage activeConfig = s_configs[s_activeConfigIndex];
VersionedConfig storage activeConfig = s_configs[getActiveIndex()];
if (activeConfig.configDigest != digestToRevoke) {
revert ConfigDigestMismatch(activeConfig.configDigest, digestToRevoke);
}
Expand Down Expand Up @@ -299,6 +317,14 @@ contract RMNHome is OwnerIsCreator, ITypeAndVersion {
);
}

function getActiveIndex() private view returns (uint32) {
return s_activeConfigIndex;
}

function getCandidateIndex() private view returns (uint32) {
return s_activeConfigIndex ^ 1;
}

// ================================================================
// │ Validation │
// ================================================================
Expand Down

0 comments on commit c12b5ad

Please sign in to comment.