Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden authored and claravanstaden committed Nov 9, 2023
1 parent a41122c commit 6fbaf31
Show file tree
Hide file tree
Showing 34 changed files with 447 additions and 448 deletions.
1 change: 1 addition & 0 deletions contracts/src/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ library Assets {

payload = SubstrateTypes.SendToken(address(this), token, destinationChain, destinationAddress, amount);
extraFee = $.sendTokenFee;

emit IGateway.TokenSent(sender, token, destinationChain, abi.encodePacked(destinationAddress), amount);
}

Expand Down
15 changes: 13 additions & 2 deletions contracts/src/DeployScript.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Gateway} from "./Gateway.sol";
import {GatewayUpgradeMock} from "../test/mocks/GatewayUpgradeMock.sol";
import {Agent} from "./Agent.sol";
import {AgentExecutor} from "./AgentExecutor.sol";
import {ParaID, Config} from "./Types.sol";
import {ParaID, Config, OperatingMode} from "./Types.sol";
import {SafeNativeTransfer} from "./utils/SafeTransfer.sol";
import {stdJson} from "forge-std/StdJson.sol";

Expand Down Expand Up @@ -66,8 +66,19 @@ contract DeployScript is Script {
assetHubAgentID
);

bool rejectOutboundMessages = vm.envBool("REJECT_OUTBOUND_MESSAGES");
OperatingMode defaultOperatingMode;
if (rejectOutboundMessages) {
defaultOperatingMode = OperatingMode.RejectingOutboundMessages;
} else {
defaultOperatingMode = OperatingMode.Normal;
}

bytes memory initParams = abi.encode(
vm.envUint("DEFAULT_FEE"), vm.envUint("REGISTER_NATIVE_TOKEN_FEE"), vm.envUint("SEND_NATIVE_TOKEN_FEE")
defaultOperatingMode,
vm.envUint("DEFAULT_FEE"),
vm.envUint("REGISTER_NATIVE_TOKEN_FEE"),
vm.envUint("SEND_NATIVE_TOKEN_FEE")
);

GatewayProxy gateway = new GatewayProxy(address(gatewayLogic), initParams);
Expand Down
13 changes: 8 additions & 5 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ contract Gateway is IGateway, IInitializable {
_transferNativeFromAgent(channel.agent, payable(msg.sender), amount);
}

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

/**
Expand Down Expand Up @@ -512,7 +512,10 @@ contract Gateway is IGateway, IInitializable {
payable(msg.sender).safeNativeTransfer(msg.value - channel.fee - extraFee);
}

emit IGateway.OutboundMessageAccepted(dest, channel.outboundNonce, payload);
// Generate a unique ID for this message
bytes32 messageID = keccak256(abi.encodePacked(dest, channel.outboundNonce));

emit IGateway.OutboundMessageAccepted(dest, channel.outboundNonce, messageID, payload);
}

/// @dev Outbound message can be disabled globally or on a per-channel basis.
Expand Down Expand Up @@ -569,12 +572,12 @@ contract Gateway is IGateway, IInitializable {
revert Unauthorized();
}

(uint256 defaultFee, uint256 registerTokenFee, uint256 sendTokenFee) =
abi.decode(data, (uint256, uint256, uint256));
(OperatingMode defaultMode, uint256 defaultFee, uint256 registerTokenFee, uint256 sendTokenFee) =
abi.decode(data, (OperatingMode, uint256, uint256, uint256));

CoreStorage.Layout storage $ = CoreStorage.layout();

$.mode = OperatingMode.RejectingOutboundMessages;
$.mode = defaultMode;
$.defaultFee = defaultFee;

// Initialize an agent & channel for BridgeHub
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct InboundMessage {
uint256 maxRefund;
/// @dev The reward for message submission
uint256 reward;
/// @dev ID for this message
bytes32 id;
}

enum OperatingMode {
Expand Down
25 changes: 15 additions & 10 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ interface IGateway {
*/

// Emitted when inbound message has been dispatched
event InboundMessageDispatched(ParaID indexed origin, uint64 nonce, bool success);
event InboundMessageDispatched(ParaID indexed origin, uint64 nonce, bytes32 indexed messageID, bool success);

// Emitted when an outbound message has been accepted for delivery to a Polkadot parachain
event OutboundMessageAccepted(ParaID indexed destination, uint64 nonce, bytes payload);
event OutboundMessageAccepted(ParaID indexed destination, uint64 nonce, bytes32 indexed messageID, bytes payload);

// Emitted when an agent has been created for a consensus system on Polkadot
event AgentCreated(bytes32 agentID, address agent);
Expand All @@ -34,14 +34,6 @@ interface IGateway {
// Emitted when funds are withdrawn from an agent
event AgentFundsWithdrawn(bytes32 indexed agentID, address indexed recipient, uint256 amount);

// Emitted when the fees updated
event TokenTransferFeesChanged(uint256 register, uint256 send);
/// @dev Emitted once the funds are locked and a message is successfully queued.
event TokenSent(
address indexed token, address indexed sender, ParaID destinationChain, bytes destinationAddress, uint128 amount
);
event TokenRegistrationSent(address token);

/**
* Getters
*/
Expand All @@ -67,6 +59,19 @@ interface IGateway {
/**
* Token Transfers
*/

// @dev Emitted when the fees updated
event TokenTransferFeesChanged(uint256 register, uint256 send);

/// @dev Emitted once the funds are locked and an outbound message is successfully queued.
event TokenSent(
address indexed token, address indexed sender, ParaID destinationChain, bytes destinationAddress, uint128 amount
);

/// @dev Emitted when a command is sent to register a new wrapped token on AssetHub
event TokenRegistrationSent(address token);

// @dev Fees in Ether for registering and sending tokens respectively
function tokenTransferFees() external view returns (uint256, uint256);

/// @dev Send a message to the AssetHub parachain to register a new fungible asset
Expand Down
54 changes: 20 additions & 34 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ import {WETH9} from "canonical-weth/WETH9.sol";
import "./mocks/GatewayUpgradeMock.sol";

contract GatewayTest is Test {
event InboundMessageDispatched(ParaID indexed origin, uint64 nonce, bool result);
event OutboundMessageAccepted(ParaID indexed dest, uint64 nonce, bytes payload);
event NativeTokensUnlocked(address token, address recipient, uint256 amount);
event TokenRegistrationSent(address token);
event TokenSent(
address indexed sender, address indexed token, ParaID destinationChain, bytes destinationAddress, uint128 amount
);
event AgentCreated(bytes32 agentID, address agent);
event ChannelCreated(ParaID indexed paraID);
event ChannelUpdated(ParaID indexed paraID);

event Upgraded(address indexed implementation);
event Initialized(uint256 d0, uint256 d1);

ParaID public bridgeHubParaID = ParaID.wrap(1001);
bytes32 public bridgeHubAgentID = keccak256("1001");
address public bridgeHubAgent;
Expand All @@ -66,6 +52,7 @@ contract GatewayTest is Test {
uint256 public maxDispatchGas = 500_000;
uint256 public maxRefund = 1 ether;
uint256 public reward = 1 ether;
bytes32 public messageID = keccak256("cabbage");

uint256 public baseFee = 1 ether;
uint256 public registerNativeTokenFee = 1 ether;
Expand All @@ -84,6 +71,7 @@ contract GatewayTest is Test {
gateway = new GatewayProxy(
address(gatewayLogic),
abi.encode(
OperatingMode.Normal,
baseFee,
registerNativeTokenFee,
sendNativeTokenFee
Expand Down Expand Up @@ -156,11 +144,11 @@ contract GatewayTest is Test {

// Expect the gateway to emit `InboundMessageDispatched`
vm.expectEmit(true, false, false, false);
emit IGateway.InboundMessageDispatched(bridgeHubParaID, 1, true);
emit IGateway.InboundMessageDispatched(bridgeHubParaID, 1, messageID, true);

hoax(relayer, 1 ether);
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand All @@ -173,7 +161,7 @@ contract GatewayTest is Test {

hoax(relayer, 1 ether);
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand All @@ -182,7 +170,7 @@ contract GatewayTest is Test {
vm.expectRevert(Gateway.InvalidNonce.selector);
hoax(relayer, 1 ether);
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand All @@ -194,7 +182,9 @@ contract GatewayTest is Test {
vm.expectRevert(Gateway.ChannelDoesNotExist.selector);
hoax(relayer);
IGateway(address(gateway)).submitInbound(
InboundMessage(ParaID.wrap(42), 1, command, "", maxDispatchGas, maxRefund, reward), proof, makeMockProof()
InboundMessage(ParaID.wrap(42), 1, command, "", maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
}

Expand All @@ -208,7 +198,7 @@ contract GatewayTest is Test {

hoax(relayer, 1 ether);
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand All @@ -231,7 +221,7 @@ contract GatewayTest is Test {

uint256 startGas = gasleft();
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand All @@ -257,7 +247,7 @@ contract GatewayTest is Test {

hoax(relayer, 1 ether);
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward),
InboundMessage(bridgeHubParaID, 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);
Expand Down Expand Up @@ -532,10 +522,10 @@ contract GatewayTest is Test {

function testRegisterToken() public {
vm.expectEmit(false, false, false, true);
emit TokenRegistrationSent(address(token));
emit IGateway.TokenRegistrationSent(address(token));

vm.expectEmit(true, false, false, false);
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(token)));
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, messageID, bytes(""));

IGateway(address(gateway)).registerToken{value: 2 ether}(address(token));
}
Expand All @@ -545,7 +535,7 @@ contract GatewayTest is Test {
emit IGateway.TokenRegistrationSent(address(token));

vm.expectEmit(true, false, false, false);
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(token)));
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, messageID, bytes(""));

uint256 totalFee = baseFee + registerNativeTokenFee;
uint256 balanceBefore = address(this).balance;
Expand All @@ -571,9 +561,7 @@ contract GatewayTest is Test {

// Expect the gateway to emit `OutboundMessageAccepted`
vm.expectEmit(true, false, false, false);
emit IGateway.OutboundMessageAccepted(
assetHubParaID, 1, SubstrateTypes.SendToken(address(token), destPara, destAddress, 1)
);
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, messageID, bytes(""));

IGateway(address(gateway)).sendToken{value: 2 ether}(address(token), destPara, destAddress, 1);
}
Expand All @@ -591,9 +579,7 @@ contract GatewayTest is Test {

// Expect the gateway to emit `OutboundMessageAccepted`
vm.expectEmit(true, false, false, false);
emit IGateway.OutboundMessageAccepted(
assetHubParaID, 1, SubstrateTypes.SendToken(address(token), destAddress, 1)
);
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, messageID, bytes(""));

IGateway(address(gateway)).sendToken{value: 2 ether}(address(token), destPara, destAddress, 1);
}
Expand All @@ -611,7 +597,7 @@ contract GatewayTest is Test {

// Expect the gateway to emit `OutboundMessageAccepted`
vm.expectEmit(true, false, false, false);
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, hex"");
emit IGateway.OutboundMessageAccepted(assetHubParaID, 1, messageID, bytes(""));

IGateway(address(gateway)).sendToken{value: 2 ether}(address(token), destPara, destAddress, 1);
}
Expand Down Expand Up @@ -743,10 +729,10 @@ contract GatewayTest is Test {

vm.expectEmit(true, false, false, true);
// Expect dispatch result as false for `OutOfGas`
emit IGateway.InboundMessageDispatched(bridgeHubParaID, 1, false);
emit IGateway.InboundMessageDispatched(bridgeHubParaID, 1, messageID, false);
// maxDispatchGas as 1 for `create_agent` is definitely not enough
IGateway(address(gateway)).submitInbound(
InboundMessage(bridgeHubParaID, 1, command, params, 1, maxRefund, reward), proof, makeMockProof()
InboundMessage(bridgeHubParaID, 1, command, params, 1, maxRefund, reward, messageID), proof, makeMockProof()
);
}

Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// Send `command` to the Gateway on the channel identified by `origin`.
fn send(origin: ParaId, command: Command, pays_fee: PaysFee<T>) -> DispatchResult {
let message = Message { origin, command };
let message = Message { id: None, origin, command };
let (ticket, fee) =
T::OutboundQueue::validate(&message).map_err(|err| Error::<T>::Send(err))?;

Expand Down
8 changes: 6 additions & 2 deletions parachain/pallets/control/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,11 @@ fn charge_fee_for_create_agent() {

assert_ok!(EthereumControl::create_agent(origin.clone()));
// assert sovereign_balance decreased by (fee.base_fee + fee.delivery_fee)
let message =
Message { origin: para_id.into(), command: Command::CreateAgent { agent_id } };
let message = Message {
id: None,
origin: para_id.into(),
command: Command::CreateAgent { agent_id },
};
let (_, fee) = OutboundQueue::validate(&message).unwrap();
let sovereign_balance = Balances::balance(&sovereign_account);
assert_eq!(sovereign_balance + fee.local + fee.remote, InitialFunding::get());
Expand Down Expand Up @@ -555,6 +558,7 @@ fn charge_fee_for_transfer_native_from_agent() {
let sovereign_balance_before = Balances::balance(&sovereign_account);
assert_ok!(EthereumControl::transfer_native_from_agent(origin.clone(), recipient, amount));
let message = Message {
id: None,
origin: para_id.into(),
command: Command::TransferNativeFromAgent { agent_id, recipient, amount },
};
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ scale-info = { version = "2.9.0", default-features = false, features = [ "derive
ssz_rs = { version="0.9.0", default-features = false }
ssz_rs_derive = { version="0.9.0", default-features = false }
byte-slice-cast = { version = "1.2.1", default-features = false }
rlp = { version = "0.5", default-features = false }
rlp = { version = "0.5.2", default-features = false }
hex-literal = { version = "0.4.1", optional = true }
log = { version = "0.4.20", default-features = false }

Expand Down
Loading

0 comments on commit 6fbaf31

Please sign in to comment.