Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XCM Topics as message IDs for improved message tracing #997

Merged
merged 16 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
claravanstaden marked this conversation as resolved.
Show resolved Hide resolved

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
Loading