Skip to content

Commit

Permalink
Merge branch 'vincent/v2' into ron/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Dec 8, 2024
2 parents 1e168c3 + 4e17456 commit 0d0046b
Show file tree
Hide file tree
Showing 79 changed files with 1,001 additions and 865 deletions.
7 changes: 5 additions & 2 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[profile.default]
solc_version = "0.8.25"
solc_version = "0.8.28"
optimizer = true
optimizer_runs = 20000
via_ir = false
Expand All @@ -13,7 +13,10 @@ fs_permissions = [
ignored_error_codes = [
# DeployLocal.sol is never deployed
5574,
# tstore
2394,
]
evm_version = 'Cancun'

[profile.production]
via_ir = true
Expand All @@ -23,4 +26,4 @@ mainnet = { key = "${ETHERSCAN_API_KEY}" }

[fmt]
number_underscore = "thousands"
line_length = 89
line_length = 99
2 changes: 1 addition & 1 deletion contracts/scripts/Deploy.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {Script} from "forge-std/Script.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/DeployBeefyClient.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";
Expand All @@ -18,7 +18,7 @@ contract DeployBeefyClient is Script {
function readConfig() internal pure returns (Config memory config) {
// Checkpoint generated using the script `./beefy-checkpoint.js` script in Polkadot-JS.
config = Config({
startBlock: 21087413,
startBlock: 21_087_413,
current: BeefyClient.ValidatorSet({
id: 644,
length: 297,
Expand Down
18 changes: 5 additions & 13 deletions contracts/scripts/DeployLocal.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
Expand Down Expand Up @@ -52,17 +52,11 @@ contract DeployLocal is Script {
uint256 randaoCommitExpiration = vm.envUint("RANDAO_COMMIT_EXP");
uint256 minimumSignatures = vm.envUint("MINIMUM_REQUIRED_SIGNATURES");
BeefyClient beefyClient = new BeefyClient(
randaoCommitDelay,
randaoCommitExpiration,
minimumSignatures,
startBlock,
current,
next
randaoCommitDelay, randaoCommitExpiration, minimumSignatures, startBlock, current, next
);

uint8 foreignTokenDecimals = uint8(vm.envUint("FOREIGN_TOKEN_DECIMALS"));
uint128 maxDestinationFee =
uint128(vm.envUint("RESERVE_TRANSFER_MAX_DESTINATION_FEE"));
uint128 maxDestinationFee = uint128(vm.envUint("RESERVE_TRANSFER_MAX_DESTINATION_FEE"));

AgentExecutor executor = new AgentExecutor();
Gateway gatewayLogic = new Gateway(address(beefyClient), address(executor));
Expand Down Expand Up @@ -92,17 +86,15 @@ contract DeployLocal is Script {
weth: weth
});

GatewayProxy gateway =
new GatewayProxy(address(gatewayLogic), abi.encode(config));
GatewayProxy gateway = new GatewayProxy(address(gatewayLogic), abi.encode(config));

// Fund the sovereign account for the BridgeHub parachain. Used to reward relayers
// of messages originating from BridgeHub
uint256 initialDeposit = vm.envUint("BRIDGE_HUB_INITIAL_DEPOSIT");

address bridgeHubAgent =
IGatewayV2(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
address assetHubAgent =
IGatewayV2(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);
address assetHubAgent = IGatewayV2(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/DeployLocalGatewayLogic.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {AgentExecutor} from "../src/AgentExecutor.sol";
import {Gateway} from "../src//Gateway.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/FundAgent.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/UpgradeShell.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/westend/UpgradeShell.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/Agent.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

/// @title An agent contract that acts on behalf of a consensus system on Polkadot
/// @dev Instances of this contract act as an agents for arbitrary consensus systems on Polkadot. These consensus systems
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/AgentExecutor.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {ParaID} from "./Types.sol";
import {SubstrateTypes} from "./SubstrateTypes.sol";
Expand Down
82 changes: 57 additions & 25 deletions contracts/src/BeefyClient.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {ECDSA} from "openzeppelin/utils/cryptography/ECDSA.sol";
import {SubstrateMerkleProof} from "./utils/SubstrateMerkleProof.sol";
Expand Down Expand Up @@ -247,9 +247,11 @@ contract BeefyClient {
* @param bitfield a bitfield claiming which validators have signed the commitment
* @param proof a proof that a single validator from currentValidatorSet has signed the commitment
*/
function submitInitial(Commitment calldata commitment, uint256[] calldata bitfield, ValidatorProof calldata proof)
external
{
function submitInitial(
Commitment calldata commitment,
uint256[] calldata bitfield,
ValidatorProof calldata proof
) external {
if (commitment.blockNumber <= latestBeefyBlock) {
revert StaleCommitment();
}
Expand All @@ -258,7 +260,9 @@ contract BeefyClient {
uint16 signatureUsageCount;
if (commitment.validatorSetID == currentValidatorSet.id) {
signatureUsageCount = currentValidatorSet.usageCounters.get(proof.index);
currentValidatorSet.usageCounters.set(proof.index, signatureUsageCount.saturatingAdd(1));
currentValidatorSet.usageCounters.set(
proof.index, signatureUsageCount.saturatingAdd(1)
);
vset = currentValidatorSet;
} else if (commitment.validatorSetID == nextValidatorSet.id) {
signatureUsageCount = nextValidatorSet.usageCounters.get(proof.index);
Expand All @@ -269,8 +273,10 @@ contract BeefyClient {
}

// Check if merkle proof is valid based on the validatorSetRoot and if proof is included in bitfield
if (!isValidatorInSet(vset, proof.account, proof.index, proof.proof) || !Bitfield.isSet(bitfield, proof.index))
{
if (
!isValidatorInSet(vset, proof.account, proof.index, proof.proof)
|| !Bitfield.isSet(bitfield, proof.index)
) {
revert InvalidValidatorProof();
}

Expand All @@ -291,7 +297,9 @@ contract BeefyClient {
blockNumber: uint64(block.number),
validatorSetLen: uint32(vset.length),
numRequiredSignatures: uint32(
computeNumRequiredSignatures(vset.length, signatureUsageCount, minNumRequiredSignatures)
computeNumRequiredSignatures(
vset.length, signatureUsageCount, minNumRequiredSignatures
)
),
prevRandao: 0,
bitfieldHash: keccak256(abi.encodePacked(bitfield))
Expand Down Expand Up @@ -371,8 +379,9 @@ contract BeefyClient {
if (leaf.nextAuthoritySetID != nextValidatorSet.id + 1) {
revert InvalidMMRLeaf();
}
bool leafIsValid =
MMRProof.verifyLeafProof(newMMRRoot, keccak256(encodeMMRLeaf(leaf)), leafProof, leafProofOrder);
bool leafIsValid = MMRProof.verifyLeafProof(
newMMRRoot, keccak256(encodeMMRLeaf(leaf)), leafProof, leafProofOrder
);
if (!leafIsValid) {
revert InvalidMMRLeafProof();
}
Expand Down Expand Up @@ -434,13 +443,19 @@ contract BeefyClient {
if (ticket.bitfieldHash != keccak256(abi.encodePacked(bitfield))) {
revert InvalidBitfield();
}
return Bitfield.subsample(ticket.prevRandao, bitfield, ticket.numRequiredSignatures, ticket.validatorSetLen);
return Bitfield.subsample(
ticket.prevRandao, bitfield, ticket.numRequiredSignatures, ticket.validatorSetLen
);
}

/* Internal Functions */

// Creates a unique ticket ID for a new interactive prover-verifier session
function createTicketID(address account, bytes32 commitmentHash) internal pure returns (bytes32 value) {
function createTicketID(address account, bytes32 commitmentHash)
internal
pure
returns (bytes32 value)
{
assembly {
mstore(0x00, account)
mstore(0x20, commitmentHash)
Expand Down Expand Up @@ -525,7 +540,11 @@ contract BeefyClient {
}

// Ensure that the commitment provides a new MMR root
function ensureProvidesMMRRoot(Commitment calldata commitment) internal pure returns (bytes32) {
function ensureProvidesMMRRoot(Commitment calldata commitment)
internal
pure
returns (bytes32)
{
for (uint256 i = 0; i < commitment.payload.length; i++) {
if (commitment.payload[i].payloadID == MMR_ROOT_ID) {
if (commitment.payload[i].data.length != 32) {
Expand All @@ -538,19 +557,30 @@ contract BeefyClient {
revert CommitmentNotRelevant();
}

function encodeCommitment(Commitment calldata commitment) internal pure returns (bytes memory) {
function encodeCommitment(Commitment calldata commitment)
internal
pure
returns (bytes memory)
{
return bytes.concat(
encodeCommitmentPayload(commitment.payload),
ScaleCodec.encodeU32(commitment.blockNumber),
ScaleCodec.encodeU64(commitment.validatorSetID)
);
}

function encodeCommitmentPayload(PayloadItem[] calldata items) internal pure returns (bytes memory) {
function encodeCommitmentPayload(PayloadItem[] calldata items)
internal
pure
returns (bytes memory)
{
bytes memory payload = ScaleCodec.checkedEncodeCompactU32(items.length);
for (uint256 i = 0; i < items.length; i++) {
payload = bytes.concat(
payload, items[i].payloadID, ScaleCodec.checkedEncodeCompactU32(items[i].data.length), items[i].data
payload,
items[i].payloadID,
ScaleCodec.checkedEncodeCompactU32(items[i].data.length),
items[i].data
);
}

Expand All @@ -577,22 +607,24 @@ contract BeefyClient {
* @param proof Merkle proof required for validation of the address
* @return true if the validator is in the set
*/
function isValidatorInSet(ValidatorSetState storage vset, address account, uint256 index, bytes32[] calldata proof)
internal
view
returns (bool)
{
function isValidatorInSet(
ValidatorSetState storage vset,
address account,
uint256 index,
bytes32[] calldata proof
) internal view returns (bool) {
bytes32 hashedLeaf = keccak256(abi.encodePacked(account));
return SubstrateMerkleProof.verify(vset.root, hashedLeaf, index, vset.length, proof);
}

/**
* @dev Basic validation of a ticket for submitFinal
*/
function validateTicket(bytes32 ticketID, Commitment calldata commitment, uint256[] calldata bitfield)
internal
view
{
function validateTicket(
bytes32 ticketID,
Commitment calldata commitment,
uint256[] calldata bitfield
) internal view {
Ticket storage ticket = tickets[ticketID];

if (ticket.blockNumber == 0) {
Expand Down
5 changes: 2 additions & 3 deletions contracts/src/Constants.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {ParaID, ChannelID} from "./v1/Types.sol";

Expand All @@ -14,8 +14,7 @@ library Constants {
0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314;

// ChannelIDs
ChannelID internal constant PRIMARY_GOVERNANCE_CHANNEL_ID =
ChannelID.wrap(bytes32(uint256(1)));
ChannelID internal constant PRIMARY_GOVERNANCE_CHANNEL_ID = ChannelID.wrap(bytes32(uint256(1)));
ChannelID internal constant SECONDARY_GOVERNANCE_CHANNEL_ID =
ChannelID.wrap(bytes32(uint256(2)));
}
Loading

0 comments on commit 0d0046b

Please sign in to comment.