Skip to content

Commit

Permalink
ccip - fix ocr3 keyring adapter, transmitter, evm keyring lib (#15500)
Browse files Browse the repository at this point in the history
* fix ocr3 keyring adapter

* update transmitter

* export func

* fix onchain reportContext length

* changeset onchain

* fix liq man

* use cw compatible types

* fix RawReportContext3

* gen snap

---------

Co-authored-by: Rens Rooimans <github@rensrooimans.nl>
  • Loading branch information
dimkouv and RensR authored Dec 5, 2024
1 parent 5ef07c8 commit a0d9eb5
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 251 deletions.
5 changes: 5 additions & 0 deletions contracts/.changeset/rude-badgers-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/contracts': patch
---

reduce length of reportContext in OCR3
280 changes: 140 additions & 140 deletions contracts/gas-snapshots/ccip.gas-snapshot

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions contracts/src/v0.8/ccip/ocr/MultiOCR3Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ abstract contract MultiOCR3Base is ITypeAndVersion, Ownable2StepMsgSender {
/// @notice Constant length component for transmit functions with no signatures.
/// The signatures are expected to match transmitPlugin(reportContext, report).
uint16 private constant TRANSMIT_MSGDATA_CONSTANT_LENGTH_COMPONENT_NO_SIGNATURES = 4 // function selector.
+ 3 * 32 // 3 words containing reportContext.
+ 2 * 32 // 2 words containing reportContext.
+ 32 // word containing start location of abiencoded report value.
+ 32; // word containing length of report.

Expand Down Expand Up @@ -230,7 +230,7 @@ abstract contract MultiOCR3Base is ITypeAndVersion, Ownable2StepMsgSender {
uint8 ocrPluginType,
// NOTE: If these parameters are changed, expectedMsgDataLength and/or TRANSMIT_MSGDATA_CONSTANT_LENGTH_COMPONENT
// need to be changed accordingly.
bytes32[3] calldata reportContext,
bytes32[2] calldata reportContext,
bytes calldata report,
bytes32[] memory rs,
bytes32[] memory ss,
Expand All @@ -239,7 +239,6 @@ abstract contract MultiOCR3Base is ITypeAndVersion, Ownable2StepMsgSender {
// reportContext consists of:
// reportContext[0]: ConfigDigest.
// reportContext[1]: 24 byte padding, 8 byte sequence number.
// reportContext[2]: ExtraHash.
ConfigInfo memory configInfo = s_ocrConfigs[ocrPluginType].configInfo;
bytes32 configDigest = reportContext[0];

Expand Down
4 changes: 2 additions & 2 deletions contracts/src/v0.8/ccip/offRamp/OffRamp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ contract OffRamp is ITypeAndVersion, MultiOCR3Base {
/// @notice Transmit function for execution reports. The function takes no signatures, and expects the exec plugin
/// type to be configured with no signatures.
/// @param report serialized execution report.
function execute(bytes32[3] calldata reportContext, bytes calldata report) external {
function execute(bytes32[2] calldata reportContext, bytes calldata report) external {
_batchExecute(abi.decode(report, (Internal.ExecutionReport[])), new GasLimitOverride[][](0));

bytes32[] memory emptySigs = new bytes32[](0);
Expand Down Expand Up @@ -773,7 +773,7 @@ contract OffRamp is ITypeAndVersion, MultiOCR3Base {
/// price updates is submitted, we are OK to revert to preserve the invariant that we always revert on invalid
/// sequence number ranges. If that happens, prices will be updated in later rounds.
function commit(
bytes32[3] calldata reportContext,
bytes32[2] calldata reportContext,
bytes calldata report,
bytes32[] calldata rs,
bytes32[] calldata ss,
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/v0.8/ccip/test/helpers/MultiOCR3Helper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contract MultiOCR3Helper is MultiOCR3Base {
event AfterConfigSet(uint8 ocrPluginType);

/// @dev OCR plugin type used for transmit.
/// Defined in storage since it cannot be passed as calldata due to strict transmit checks
/// Defined in storage since it cannot be passed as calldata due to strict transmit checks
uint8 internal s_transmitOcrPluginType;

function setTransmitOcrPluginType(
Expand All @@ -18,7 +18,7 @@ contract MultiOCR3Helper is MultiOCR3Base {

/// @dev transmit function with signatures
function transmitWithSignatures(
bytes32[3] calldata reportContext,
bytes32[2] calldata reportContext,
bytes calldata report,
bytes32[] calldata rs,
bytes32[] calldata ss,
Expand All @@ -28,7 +28,7 @@ contract MultiOCR3Helper is MultiOCR3Base {
}

/// @dev transmit function with no signatures
function transmitWithoutSignatures(bytes32[3] calldata reportContext, bytes calldata report) external {
function transmitWithoutSignatures(bytes32[2] calldata reportContext, bytes calldata report) external {
bytes32[] memory emptySigs = new bytes32[](0);
_transmit(s_transmitOcrPluginType, reportContext, report, emptySigs, emptySigs, bytes32(""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

function test_TransmitSigners_gas_Success() public {
vm.pauseGasMetering();
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

// F = 2, need 2 signatures
(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
Expand All @@ -65,7 +65,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

function test_TransmitWithoutSignatureVerification_gas_Success() public {
vm.pauseGasMetering();
bytes32[3] memory reportContext = [s_configDigest3, s_configDigest3, s_configDigest3];
bytes32[2] memory reportContext = [s_configDigest3, s_configDigest3];

s_multiOCR3.setTransmitOcrPluginType(2);

Expand Down Expand Up @@ -115,7 +115,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
// Randomise picked transmitter with random offset
vm.startPrank(transmitters[randomAddressOffset % signersLength]);

bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

// condition: matches signature expectation for transmit
uint8 numSignatures = F + 1;
Expand All @@ -138,7 +138,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

// Reverts
function test_ForkedChain_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, REPORT, reportContext, 2);
Expand All @@ -155,7 +155,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_ZeroSignatures_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

s_multiOCR3.setTransmitOcrPluginType(0);

Expand All @@ -165,7 +165,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_TooManySignatures_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

// 1 signature too many
(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
Expand All @@ -179,7 +179,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_InsufficientSignatures_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

// Missing 1 signature for unique report
(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
Expand All @@ -194,7 +194,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

function test_ConfigDigestMismatch_Revert() public {
bytes32 configDigest;
bytes32[3] memory reportContext = [configDigest, configDigest, configDigest];
bytes32[2] memory reportContext = [configDigest, configDigest];

(,,, bytes32 rawVs) = _getSignaturesForDigest(s_validSignerKeys, REPORT, reportContext, 2);

Expand All @@ -205,7 +205,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_SignatureOutOfRegistration_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

bytes32[] memory rs = new bytes32[](2);
bytes32[] memory ss = new bytes32[](1);
Expand All @@ -218,7 +218,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_UnAuthorizedTransmitter_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];
bytes32[] memory rs = new bytes32[](2);
bytes32[] memory ss = new bytes32[](2);

Expand All @@ -229,7 +229,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_NonUniqueSignature_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

(bytes32[] memory rs, bytes32[] memory ss, uint8[] memory vs, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, REPORT, reportContext, 2);
Expand All @@ -247,7 +247,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_UnauthorizedSigner_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, REPORT, reportContext, 2);
Expand All @@ -264,7 +264,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

function test_UnconfiguredPlugin_Revert() public {
bytes32 configDigest;
bytes32[3] memory reportContext = [configDigest, configDigest, configDigest];
bytes32[2] memory reportContext = [configDigest, configDigest];

s_multiOCR3.setTransmitOcrPluginType(42);

Expand All @@ -273,15 +273,15 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_TransmitWithLessCalldataArgs_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];

s_multiOCR3.setTransmitOcrPluginType(0);

// The transmit should fail, since we are trying to transmit without signatures when signatures are enabled
vm.startPrank(s_validTransmitters[1]);

// report length + function selector + report length + abiencoded location of report value + report context words
uint256 receivedLength = REPORT.length + 4 + 5 * 32;
uint256 receivedLength = REPORT.length + 4 + 4 * 32;
vm.expectRevert(
abi.encodeWithSelector(
MultiOCR3Base.WrongMessageLength.selector,
Expand All @@ -294,7 +294,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {
}

function test_TransmitWithExtraCalldataArgs_Revert() public {
bytes32[3] memory reportContext = [s_configDigest1, s_configDigest1, s_configDigest1];
bytes32[2] memory reportContext = [s_configDigest1, s_configDigest1];
bytes32[] memory rs = new bytes32[](2);
bytes32[] memory ss = new bytes32[](2);

Expand All @@ -305,7 +305,7 @@ contract MultiOCR3Base_transmit is MultiOCR3BaseSetup {

// dynamic length + function selector + report length + abiencoded location of report value + report context words
// rawVs value, lengths of rs, ss, and start locations of rs & ss -> 5 words
uint256 receivedLength = REPORT.length + 4 + (5 * 32) + (5 * 32) + (2 * 32) + (2 * 32);
uint256 receivedLength = REPORT.length + 4 + (4 * 32) + (5 * 32) + (2 * 32) + (2 * 32);
vm.expectRevert(
abi.encodeWithSelector(
MultiOCR3Base.WrongMessageLength.selector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ contract MultiOCR3BaseSetup is BaseTest {
function _getSignaturesForDigest(
uint256[] memory signerPrivateKeys,
bytes memory report,
bytes32[3] memory reportContext,
bytes32[2] memory reportContext,
uint8 signatureCount
) internal pure returns (bytes32[] memory rs, bytes32[] memory ss, uint8[] memory vs, bytes32 rawVs) {
rs = new bytes32[](signatureCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ contract OffRamp_commit is OffRampSetup {
function test_UnauthorizedTransmitter_Revert() public {
OffRamp.CommitReport memory commitReport = _constructCommitReport();

bytes32[3] memory reportContext =
[s_configDigestCommit, bytes32(uint256(s_latestSequenceNumber)), s_configDigestCommit];
bytes32[2] memory reportContext = [s_configDigestCommit, bytes32(uint256(s_latestSequenceNumber))];

(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, abi.encode(commitReport), reportContext, F + 1);
Expand All @@ -300,7 +299,7 @@ contract OffRamp_commit is OffRampSetup {

OffRamp.CommitReport memory commitReport = _constructCommitReport();

bytes32[3] memory reportContext = [bytes32(""), s_configDigestCommit, s_configDigestCommit];
bytes32[2] memory reportContext = [bytes32(""), s_configDigestCommit];
(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, abi.encode(commitReport), reportContext, F + 1);

Expand All @@ -325,7 +324,7 @@ contract OffRamp_commit is OffRampSetup {

OffRamp.CommitReport memory commitReport = _constructCommitReport();

bytes32[3] memory reportContext = [bytes32(""), s_configDigestCommit, s_configDigestCommit];
bytes32[2] memory reportContext = [bytes32(""), s_configDigestCommit];
(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, abi.encode(commitReport), reportContext, F + 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ contract OffRamp_execute is OffRampSetup {
// Reverts

function test_UnauthorizedTransmitter_Revert() public {
bytes32[3] memory reportContext = [s_configDigestExec, s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [s_configDigestExec, s_configDigestExec];

Internal.Any2EVMRampMessage[] memory messages =
_generateSingleBasicMessage(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1);
Expand All @@ -216,7 +216,7 @@ contract OffRamp_execute is OffRampSetup {
_generateSingleBasicMessage(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1);
Internal.ExecutionReport[] memory reports = _generateBatchReportFromMessages(SOURCE_CHAIN_SELECTOR_1, messages);

bytes32[3] memory reportContext = [bytes32(""), s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [bytes32(""), s_configDigestExec];

vm.startPrank(s_validTransmitters[0]);
vm.expectRevert(MultiOCR3Base.UnauthorizedTransmitter.selector);
Expand All @@ -242,7 +242,7 @@ contract OffRamp_execute is OffRampSetup {
_generateSingleBasicMessage(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1);
Internal.ExecutionReport[] memory reports = _generateBatchReportFromMessages(SOURCE_CHAIN_SELECTOR_1, messages);

bytes32[3] memory reportContext = [bytes32(""), s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [bytes32(""), s_configDigestExec];

vm.startPrank(s_validTransmitters[0]);
vm.expectRevert(MultiOCR3Base.UnauthorizedTransmitter.selector);
Expand Down Expand Up @@ -277,7 +277,7 @@ contract OffRamp_execute is OffRampSetup {
}

function test_IncorrectArrayType_Revert() public {
bytes32[3] memory reportContext = [s_configDigestExec, s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [s_configDigestExec, s_configDigestExec];

uint256[] memory wrongData = new uint256[](2);
wrongData[0] = 1;
Expand All @@ -288,7 +288,7 @@ contract OffRamp_execute is OffRampSetup {
}

function test_NonArray_Revert() public {
bytes32[3] memory reportContext = [s_configDigestExec, s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [s_configDigestExec, s_configDigestExec];

Internal.Any2EVMRampMessage[] memory messages =
_generateSingleBasicMessage(SOURCE_CHAIN_SELECTOR_1, ON_RAMP_ADDRESS_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ contract OffRampSetup is FeeQuoterSetup, MultiOCR3BaseSetup {
}

function _commit(OffRamp.CommitReport memory commitReport, uint64 sequenceNumber) internal {
bytes32[3] memory reportContext = [s_configDigestCommit, bytes32(uint256(sequenceNumber)), s_configDigestCommit];
bytes32[2] memory reportContext = [s_configDigestCommit, bytes32(uint256(sequenceNumber))];

(bytes32[] memory rs, bytes32[] memory ss,, bytes32 rawVs) =
_getSignaturesForDigest(s_validSignerKeys, abi.encode(commitReport), reportContext, F + 1);
Expand All @@ -383,7 +383,7 @@ contract OffRampSetup is FeeQuoterSetup, MultiOCR3BaseSetup {
function _execute(
Internal.ExecutionReport[] memory reports
) internal {
bytes32[3] memory reportContext = [s_configDigestExec, s_configDigestExec, s_configDigestExec];
bytes32[2] memory reportContext = [s_configDigestExec, s_configDigestExec];

vm.startPrank(s_validTransmitters[0]);
s_offRamp.execute(reportContext, abi.encode(reports));
Expand Down
Loading

0 comments on commit a0d9eb5

Please sign in to comment.