Skip to content

Commit 7026aa2

Browse files
committed
add custom consistency level
1 parent 75feab1 commit 7026aa2

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache 2
2+
pragma solidity ^0.8.0;
3+
4+
// from https://github.com/wormhole-foundation/wormhole/blob/39081fa2936badf178f8b7e5eb63074d3308bf7d/ethereum/contracts/custom_consistency_level/interfaces/ICustomConsistencyLevel.sol
5+
interface ICustomConsistencyLevel {
6+
/// @notice The configuration for an emitter has been set.
7+
/// @dev Topic0
8+
/// 0xa37f0112e03d41de27266c1680238ff1548c0441ad1e73c82917c000eefdd5ea.
9+
/// @param emitterAddress The emitter address for which the config has been set.
10+
/// @param config The config data that was set.
11+
event ConfigSet(address emitterAddress, bytes32 config);
12+
13+
/// @notice Sets / updates the configuration for a given emitter address (msg.sender).
14+
/// @param config The config used to determine the custom consistency level handling for an emitter.
15+
function configure(bytes32 config) external;
16+
17+
/// @notice Returns the configuration for a given emitter address.
18+
/// @param emitterAddress The emitter address for which the config has been set.
19+
/// @return The configuration.
20+
function getConfiguration(address emitterAddress) external returns (bytes32);
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
// from https://github.com/wormhole-foundation/wormhole/blob/39081fa2936badf178f8b7e5eb63074d3308bf7d/ethereum/contracts/custom_consistency_level/libraries/ConfigMakers.sol
5+
library ConsistencyConfigMakers {
6+
uint8 public constant TYPE_ADDITIONAL_BLOCKS = 1;
7+
8+
// from https://github.com/wormhole-foundation/wormhole/blob/39081fa2936badf178f8b7e5eb63074d3308bf7d/ethereum/contracts/libraries/external/BytesLib.sol#L385-L394
9+
function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) {
10+
require(_bytes.length >= _start + 32, "toBytes32_outOfBounds");
11+
bytes32 tempBytes32;
12+
13+
assembly {
14+
tempBytes32 := mload(add(add(_bytes, 0x20), _start))
15+
}
16+
17+
return tempBytes32;
18+
}
19+
20+
/// @notice Encodes an additional blocks custom consistency level configuration.
21+
/// @param consistencyLevel The consistency level to wait for.
22+
/// @param blocksToWait The number of additional blocks to wait after the consistency level is reached.
23+
/// @return bytes The encoded config.
24+
function makeAdditionalBlocksConfig(uint8 consistencyLevel, uint16 blocksToWait) internal pure returns (bytes32) {
25+
bytes28 padding;
26+
return toBytes32(abi.encodePacked(TYPE_ADDITIONAL_BLOCKS, consistencyLevel, blocksToWait, padding), 0);
27+
}
28+
}

test/CustomConsistencyLevel.t.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {ConsistencyConfigMakers} from "../src/libraries/ConsistencyConfigMakers.sol";
6+
7+
// from https://github.com/wormhole-foundation/wormhole/blob/39081fa2936badf178f8b7e5eb63074d3308bf7d/ethereum/forge-test/CustomConsistencyLevel.t.sol
8+
contract CustomConsistencyLevelTest is Test {
9+
function test_makeAdditionalBlocksConfig() public {
10+
bytes32 expected = 0x01c9002a00000000000000000000000000000000000000000000000000000000;
11+
bytes32 result = ConsistencyConfigMakers.makeAdditionalBlocksConfig(201, 42);
12+
assertEq(expected, result);
13+
}
14+
}

0 commit comments

Comments
 (0)