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 Nov 6, 2024
2 parents c87192e + 4c3b2af commit 6a79b20
Show file tree
Hide file tree
Showing 23 changed files with 535 additions and 462 deletions.
15 changes: 8 additions & 7 deletions contracts/scripts/DeployLocal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
import {IGatewayV2} from "../src/v2/IGateway.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
import {Gateway} from "../src/Gateway.sol";
import {MockGatewayV2} from "../test/mocks/MockGatewayV2.sol";
Expand Down Expand Up @@ -75,6 +75,9 @@ contract DeployLocal is Script {
defaultOperatingMode = OperatingMode.Normal;
}

// Deploy WETH for testing
address weth = address(new WETH9());

Initializer.Config memory config = Initializer.Config({
mode: defaultOperatingMode,
deliveryCost: uint128(vm.envUint("DELIVERY_COST")),
Expand All @@ -85,23 +88,21 @@ contract DeployLocal is Script {
multiplier: ud60x18(vm.envUint("FEE_MULTIPLIER")),
rescueOperator: address(0),
foreignTokenDecimals: foreignTokenDecimals,
maxDestinationFee: maxDestinationFee
maxDestinationFee: maxDestinationFee,
weth: weth
});

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

// Deploy WETH for testing
new WETH9();

// 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 =
IGateway(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
IGatewayV2(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
address assetHubAgent =
IGateway(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);
IGatewayV2(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
7 changes: 3 additions & 4 deletions contracts/scripts/FundAgent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ pragma solidity 0.8.25;
import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
import {IGatewayV2} from "../src/v2/IGateway.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
import {Gateway} from "../src/Gateway.sol";
import {Agent} from "../src/Agent.sol";
Expand All @@ -32,8 +31,8 @@ contract FundAgent is Script {
bytes32 bridgeHubAgentID = vm.envBytes32("BRIDGE_HUB_AGENT_ID");
bytes32 assetHubAgentID = vm.envBytes32("ASSET_HUB_AGENT_ID");

address bridgeHubAgent = IGateway(gatewayAddress).agentOf(bridgeHubAgentID);
address assetHubAgent = IGateway(gatewayAddress).agentOf(assetHubAgentID);
address bridgeHubAgent = IGatewayV2(gatewayAddress).agentOf(bridgeHubAgentID);
address assetHubAgent = IGatewayV2(gatewayAddress).agentOf(assetHubAgentID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/UpgradeShell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
import {IShell} from "../src/interfaces/IShell.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
import {Gateway} from "../src/Gateway.sol";
Expand Down Expand Up @@ -56,7 +55,8 @@ contract UpgradeShell is Script {
multiplier: ud60x18(1.33e18),
rescueOperator: 0x4B8a782D4F03ffcB7CE1e95C5cfe5BFCb2C8e967,
foreignTokenDecimals: 10,
maxDestinationFee: dot(2)
maxDestinationFee: dot(2),
weth: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
})
});
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/westend/UpgradeShell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {stdJson} from "forge-std/StdJson.sol";
import {UD60x18, ud60x18} from "prb/math/src/UD60x18.sol";

import {BeefyClient} from "../../src/BeefyClient.sol";
import {IGateway} from "../../src/interfaces/IGateway.sol";
import {IShell} from "../../src/interfaces/IShell.sol";
import {GatewayProxy} from "../../src/GatewayProxy.sol";
import {Gateway} from "../../src/Gateway.sol";
Expand Down Expand Up @@ -43,7 +42,8 @@ contract UpgradeShell is Script {
multiplier: ud60x18(1_330_000_000_000_000_000),
rescueOperator: 0x302F0B71B8aD3CF6dD90aDb668E49b2168d652fd,
foreignTokenDecimals: 12,
maxDestinationFee: 2_000_000_000_000
maxDestinationFee: 2_000_000_000_000,
weth: 0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9
})
});
}
Expand Down
25 changes: 16 additions & 9 deletions contracts/src/Functions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pragma solidity 0.8.25;

import {IERC20} from "./interfaces/IERC20.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {SafeTokenTransferFrom} from "./utils/SafeTransfer.sol";
import {Agent} from "./Agent.sol";
import {Call} from "./utils/Call.sol";
Expand All @@ -14,6 +13,9 @@ import {AssetsStorage} from "./storage/AssetsStorage.sol";
import {Token} from "./Token.sol";
import {TokenInfo} from "./types/Common.sol";
import {ChannelID, Channel} from "./v1/Types.sol";
import {IGatewayBase} from "./interfaces/IGatewayBase.sol";
import {IGatewayV1} from "./v1/IGateway.sol";
import {IGatewayV2} from "./v2/IGateway.sol";

library Functions {
using Address for address;
Expand All @@ -24,10 +26,15 @@ library Functions {
error InvalidAmount();
error ChannelDoesNotExist();

function weth() internal view returns (address) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
return $.weth;
}

function ensureAgent(bytes32 agentID) internal view returns (address agent) {
agent = CoreStorage.layout().agents[agentID];
if (agent == address(0)) {
revert AgentDoesNotExist();
revert IGatewayBase.AgentDoesNotExist();
}
}

Expand All @@ -40,7 +47,7 @@ library Functions {
ch = CoreStorage.layout().channels[channelID];
// A channel always has an agent specified.
if (ch.agent == address(0)) {
revert ChannelDoesNotExist();
revert IGatewayV1.ChannelDoesNotExist();
}
}

Expand Down Expand Up @@ -78,9 +85,9 @@ library Functions {
if (agent == address(0)) {
agent = address(new Agent(origin));
core.agents[origin] = agent;
emit IGateway.AgentCreated(origin, agent);
emit IGatewayBase.AgentCreated(origin, agent);
} else {
revert IGateway.AgentAlreadyCreated();
revert IGatewayBase.AgentAlreadyCreated();
}
}

Expand Down Expand Up @@ -108,7 +115,7 @@ library Functions {
abi.encodeCall(AgentExecutor.transferToken, (token, recipient, amount));
(bool success,) = Agent(payable(agent)).invoke(executor, call);
if (!success) {
revert IGateway.TokenTransferFailed();
revert IGatewayBase.TokenTransferFailed();
}
}

Expand All @@ -120,7 +127,7 @@ library Functions {
) external {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
if ($.tokenAddressOf[foreignTokenID] != address(0)) {
revert IGateway.TokenAlreadyRegistered();
revert IGatewayBase.TokenAlreadyRegistered();
}
Token token = new Token(name, symbol, decimals);
TokenInfo memory info =
Expand All @@ -129,7 +136,7 @@ library Functions {
$.tokenAddressOf[foreignTokenID] = address(token);
$.tokenRegistry[address(token)] = info;

emit IGateway.ForeignTokenRegistered(foreignTokenID, address(token));
emit IGatewayBase.ForeignTokenRegistered(foreignTokenID, address(token));
}

function mintForeignToken(bytes32 foreignTokenID, address recipient, uint128 amount)
Expand All @@ -143,7 +150,7 @@ library Functions {
function _ensureTokenAddressOf(bytes32 tokenID) internal view returns (address) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
if ($.tokenAddressOf[tokenID] == address(0)) {
revert IGateway.TokenNotRegistered();
revert IGatewayBase.TokenNotRegistered();
}
return $.tokenAddressOf[tokenID];
}
Expand Down
45 changes: 31 additions & 14 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Initializer} from "./Initializer.sol";
import {AgentExecutor} from "./AgentExecutor.sol";
import {Agent} from "./Agent.sol";
import {MultiAddress} from "./MultiAddress.sol";
import {IGatewayBase} from "./interfaces/IGatewayBase.sol";
import {
OperatingMode,
ParaID,
Expand All @@ -22,10 +23,11 @@ import {
CallsV1,
HandlersV1,
CallsV2,
HandlersV2
HandlersV2,
IGatewayV1,
IGatewayV2
} from "./Types.sol";
import {Upgrade} from "./Upgrade.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";
import {IUpgradable} from "./interfaces/IUpgradable.sol";
import {ERC1967} from "./utils/ERC1967.sol";
Expand All @@ -44,7 +46,7 @@ import {OperatorStorage} from "./storage/OperatorStorage.sol";

import {UD60x18, ud60x18, convert} from "prb/math/src/UD60x18.sol";

contract Gateway is IGateway, IInitializable, IUpgradable {
contract Gateway is IGatewayBase, IGatewayV1, IGatewayV2, IInitializable, IUpgradable {
using Address for address;
using SafeNativeTransfer for address payable;

Expand All @@ -61,7 +63,7 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
// Message handlers can only be dispatched by the gateway itself
modifier onlySelf() {
if (msg.sender != address(this)) {
revert IGateway.Unauthorized();
revert IGatewayBase.Unauthorized();
}
_;
}
Expand Down Expand Up @@ -132,7 +134,7 @@ contract Gateway is IGateway, IInitializable, IUpgradable {

// Ensure this message is not being replayed
if (message.nonce != channel.inboundNonce + 1) {
revert IGateway.InvalidNonce();
revert IGatewayBase.InvalidNonce();
}

// Increment nonce for origin.
Expand All @@ -146,14 +148,14 @@ contract Gateway is IGateway, IInitializable, IUpgradable {

// Verify that the commitment is included in a parachain header finalized by BEEFY.
if (!_verifyCommitment(commitment, headerProof)) {
revert IGateway.InvalidProof();
revert IGatewayBase.InvalidProof();
}

// Make sure relayers provide enough gas so that inner message dispatch
// does not run out of gas.
uint256 maxDispatchGas = message.maxDispatchGas;
if (gasleft() < maxDispatchGas + DISPATCH_OVERHEAD_GAS_V1) {
revert IGateway.NotEnoughGas();
revert IGatewayBase.NotEnoughGas();
}

bool success = true;
Expand Down Expand Up @@ -239,12 +241,17 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
);
}

emit IGateway.InboundMessageDispatched(
emit IGatewayV1.InboundMessageDispatched(
message.channelID, message.nonce, message.id, success
);
}

function operatingMode() external view returns (OperatingMode) {
function operatingMode()
external
view
override(IGatewayV1, IGatewayV2)
returns (OperatingMode)
{
return CoreStorage.layout().mode;
}

Expand All @@ -264,7 +271,12 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
return CallsV1.channelNoncesOf(channelID);
}

function agentOf(bytes32 agentID) external view returns (address) {
function agentOf(bytes32 agentID)
external
view
override(IGatewayV1, IGatewayV2)
returns (address)
{
return Functions.ensureAgent(agentID);
}

Expand All @@ -276,7 +288,12 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
return ERC1967.load();
}

function isTokenRegistered(address token) external view returns (bool) {
function isTokenRegistered(address token)
external
view
override(IGatewayV1, IGatewayV2)
returns (bool)
{
return CallsV1.isTokenRegistered(token);
}

Expand Down Expand Up @@ -427,7 +444,7 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
bytes32 leafHash = keccak256(abi.encode(message));

if ($.inboundNonce.get(message.nonce)) {
revert IGateway.InvalidNonce();
revert IGatewayBase.InvalidNonce();
}

$.inboundNonce.set(message.nonce);
Expand All @@ -437,12 +454,12 @@ contract Gateway is IGateway, IInitializable, IUpgradable {

// Verify that the commitment is included in a parachain header finalized by BEEFY.
if (!_verifyCommitment(commitment, headerProof)) {
revert IGateway.InvalidProof();
revert IGatewayBase.InvalidProof();
}

bool success = v2_dispatch(message);

emit IGateway.InboundMessageDispatched(message.nonce, success, rewardAddress);
emit IGatewayV2.InboundMessageDispatched(message.nonce, success, rewardAddress);
}

function v2_dispatch(InboundMessageV2 calldata message) internal returns (bool) {
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/Initializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ChannelID
} from "./Types.sol";
import {Upgrade} from "./Upgrade.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";
import {IUpgradable} from "./interfaces/IUpgradable.sol";
import {ERC1967} from "./utils/ERC1967.sol";
Expand Down Expand Up @@ -57,6 +56,7 @@ library Initializer {
address rescueOperator;
uint8 foreignTokenDecimals;
uint128 maxDestinationFee;
address weth;
}

function initialize(bytes calldata data) external {
Expand Down Expand Up @@ -119,6 +119,7 @@ library Initializer {
assets.assetHubReserveTransferFee = config.assetHubReserveTransferFee;
assets.foreignTokenDecimals = config.foreignTokenDecimals;
assets.maxDestinationFee = config.maxDestinationFee;
assets.weth = config.weth;

// Initialize operator storage
OperatorStorage.Layout storage operatorStorage = OperatorStorage.layout();
Expand Down
10 changes: 9 additions & 1 deletion contracts/src/Shell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ contract Shell is IShell, IUpgradable, IInitializable {
OPERATOR = _operator;
}

function upgrade(address impl, bytes32 implCodeHash, bytes calldata initializerParams) external {
function upgrade(
address impl,
bytes32 implCodeHash,
bytes calldata initializerParams
) external {
if (msg.sender != OPERATOR) {
revert Unauthorized();
}
Expand All @@ -32,4 +36,8 @@ contract Shell is IShell, IUpgradable, IInitializable {
function operator() external view returns (address) {
return OPERATOR;
}

function implementation() public view returns (address) {
return ERC1967.load();
}
}
2 changes: 2 additions & 0 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
} from "./v1/Types.sol";
import {CallsV1} from "./v1/Calls.sol";
import {HandlersV1} from "./v1/Handlers.sol";
import {IGatewayV1} from "./v1/IGateway.sol";

import {InboundMessageV2, CommandV2, CommandKind} from "./v2/Types.sol";
import {CallsV2} from "./v2/Calls.sol";
import {HandlersV2} from "./v2/Handlers.sol";
import {IGatewayV2} from "./v2/IGateway.sol";
Loading

0 comments on commit 6a79b20

Please sign in to comment.