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

Transact from eth to sub #1141

Open
wants to merge 45 commits into
base: bridge-next-gen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
55cc688
Change for contracts
yrong Feb 7, 2024
07395ce
Convert logic for transact command
yrong Feb 8, 2024
d41bd08
Reuse msg.sender for both originKinds
yrong Feb 13, 2024
16e7cbe
Revert to burn&teleport
yrong Feb 13, 2024
a01aaab
Update contract bindings
yrong Feb 13, 2024
b660ea8
Update sdk
yrong Feb 13, 2024
89feaf3
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Feb 14, 2024
a94183a
Remove unused
yrong Feb 14, 2024
7a110d3
Add smoke test
yrong Feb 15, 2024
b57f0c1
Improve smoke test
yrong Feb 15, 2024
3ad5b7d
Add polkadot-sdk as soft link
yrong Feb 15, 2024
e93c725
Revert typos config
yrong Feb 15, 2024
65714ef
Add valid check
yrong Feb 16, 2024
4719a42
Remove TransactMessage and inline the params
yrong Feb 19, 2024
a78b1f9
Remove from .gitignore
yrong Feb 19, 2024
c192361
Add quoteTransactFee
yrong Feb 19, 2024
acf2362
Rename to destinationFee
yrong Feb 20, 2024
207460d
Test quoteTransactFee
yrong Feb 20, 2024
f8f0f57
Check call length
yrong Feb 22, 2024
00abc13
Initialize ticket with explicit cost
yrong Feb 22, 2024
5084432
Rename to sendCall
yrong Feb 22, 2024
d7cc7d8
Reuse weight type with compact u64 encode support
yrong Feb 23, 2024
2f3560c
Fix smoke test with sovereign account of the sender
yrong Feb 23, 2024
e99ce53
Fix generate inbound fixture
yrong Feb 26, 2024
2e4140c
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Feb 29, 2024
dd3378e
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Mar 6, 2024
3d91da4
Support TransactFeeMode
yrong Mar 6, 2024
ac7e1f5
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Mar 7, 2024
72f6d3e
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Mar 19, 2024
68a567f
Remove prefunding mode
yrong Mar 19, 2024
923187e
Merge branch 'main' into ron/transact-from-eth-to-sub
yrong Apr 11, 2024
3dd1adc
Add param checks back
yrong Apr 11, 2024
f07507f
Remove script unused
yrong Apr 11, 2024
70413d8
Remove the fund script not in use
yrong Apr 11, 2024
a364298
Update relayer
yrong Apr 11, 2024
bdf4c71
Merge branch 'bridge-next-gen' into ron/transact-from-eth-to-sub
yrong Apr 16, 2024
748eea4
Add rfc doc
yrong Apr 17, 2024
e27c0eb
Ignore destination fee for the transact
yrong Apr 17, 2024
e5c1a70
Update rfc
yrong Apr 22, 2024
53791d2
Update rfc with fee section
yrong Apr 22, 2024
61d5ea5
Cleanup
yrong Apr 24, 2024
dfa331d
Merge branch 'bridge-next-gen' into ron/transact-from-eth-to-sub
yrong Apr 24, 2024
b935ca8
Update rfc doc
yrong Apr 29, 2024
2847751
Update rfc
yrong May 5, 2024
bfdff26
Remove unused destinationFee
yrong May 5, 2024
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
33 changes: 32 additions & 1 deletion contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
Command,
MultiAddress,
Ticket,
Costs
Costs,
TransactMessage,
OriginKind
} from "./Types.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";
Expand All @@ -27,6 +29,7 @@ import {SafeNativeTransfer} from "./utils/SafeTransfer.sol";
import {Call} from "./utils/Call.sol";
import {Math} from "./utils/Math.sol";
import {ScaleCodec} from "./utils/ScaleCodec.sol";
import {SubstrateTypes} from "./SubstrateTypes.sol";

import {
UpgradeParams,
Expand Down Expand Up @@ -86,6 +89,7 @@ contract Gateway is IGateway, IInitializable {
error InvalidAgentExecutionPayload();
error InvalidCodeHash();
error InvalidConstructorParams();
error InvalidTransact();

// handler functions are privileged
modifier onlySelf() {
Expand Down Expand Up @@ -617,4 +621,31 @@ contract Gateway is IGateway, IInitializable {
assets.assetHubCreateAssetFee = config.assetHubCreateAssetFee;
assets.assetHubReserveTransferFee = config.assetHubReserveTransferFee;
}

/// @inheritdoc IGateway
function transact(ParaID destinationChain, TransactMessage calldata message) external payable {
vgeddes marked this conversation as resolved.
Show resolved Hide resolved
Ticket memory ticket;
Costs memory costs;
address sender;
bytes1 originKind;
// Mapping originKind to https://github.com/Snowfork/polkadot-sdk/blob/348a1a010481002e41594ed75e5d78b7c2dbed92/polkadot/xcm/src/v2/mod.rs#L86
// only support originKind as SovereignAccount or Xcm for now
// for Xcm the sender will be the agent of the channel which to construct `DescendOrigin` on BH
if (message.originKind == OriginKind.SovereignAccount) {
sender = msg.sender;
originKind = 0x01;
} else if (message.originKind == OriginKind.Xcm) {
Channel storage channel = _ensureChannel(destinationChain.into());
sender = channel.agent;
originKind = 0x03;
costs.foreign = message.fee;
} else {
revert InvalidTransact();
}
yrong marked this conversation as resolved.
Show resolved Hide resolved
bytes memory payload = SubstrateTypes.Transact(sender, originKind, message);
ticket.dest = destinationChain;
ticket.costs = costs;
ticket.payload = payload;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather initialise the ticket like this to make clear that we aren't mistakenly leaving costs uninitialized

Suggested change
ticket.dest = destinationChain;
ticket.costs = costs;
ticket.payload = payload;
Ticket memory ticket = Ticket({
dest: destinationChain,
costs: Costs({
native: 0,
foreign: 0,
}),
payload: payload
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_submitOutbound(ticket);
}
}
22 changes: 21 additions & 1 deletion contracts/src/SubstrateTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity 0.8.23;

import {ScaleCodec} from "./utils/ScaleCodec.sol";
import {ParaID} from "./Types.sol";
import {ParaID, TransactMessage, OriginKind} from "./Types.sol";

/**
* @title SCALE encoders for common Substrate types
Expand Down Expand Up @@ -133,4 +133,24 @@ library SubstrateTypes {
ScaleCodec.encodeU128(xcmFee)
);
}

// Arbitrary transact
function Transact(address sender, bytes1 originKind, TransactMessage memory message)
internal
view
returns (bytes memory)
{
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x02),
SubstrateTypes.H160(sender),
originKind,
ScaleCodec.encodeU128(message.fee),
ScaleCodec.encodeU64(message.weightAtMost.refTime),
ScaleCodec.encodeU64(message.weightAtMost.proofSize),
ScaleCodec.checkedEncodeCompactU32(message.call.length),
message.call
);
}
}
17 changes: 17 additions & 0 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,20 @@ struct TokenInfo {
bool isRegistered;
bytes31 __padding;
}

struct Weight {
uint64 refTime;
uint64 proofSize;
}

enum OriginKind {
SovereignAccount,
Xcm
}

struct TransactMessage {
vgeddes marked this conversation as resolved.
Show resolved Hide resolved
OriginKind originKind;
uint128 fee;
Weight weightAtMost;
bytes call;
}
5 changes: 4 additions & 1 deletion contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.23;

import {OperatingMode, InboundMessage, ParaID, ChannelID, MultiAddress} from "../Types.sol";
import {OperatingMode, InboundMessage, ParaID, ChannelID, MultiAddress, TransactMessage} from "../Types.sol";
import {Verification} from "../Verification.sol";
import {UD60x18} from "prb/math/src/UD60x18.sol";

Expand Down Expand Up @@ -105,4 +105,7 @@ interface IGateway {
uint128 destinationFee,
uint128 amount
) external payable;

vgeddes marked this conversation as resolved.
Show resolved Hide resolved
/// @dev Call transact in destinationChain
function transact(ParaID destinationChain, TransactMessage calldata message) external payable;
}
49 changes: 44 additions & 5 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,28 @@ import {
ParaID,
Command,
multiAddressFromBytes32,
multiAddressFromBytes20
multiAddressFromBytes20,
TransactMessage,
Weight,
OriginKind
} from "../src/Types.sol";

import {WETH9} from "canonical-weth/WETH9.sol";
import "./mocks/GatewayUpgradeMock.sol";
import {UD60x18, ud60x18, convert} from "prb/math/src/UD60x18.sol";

contract GatewayTest is Test {
ParaID public bridgeHubParaID = ParaID.wrap(1001);
bytes32 public bridgeHubAgentID = keccak256("1001");
ParaID public bridgeHubParaID = ParaID.wrap(1013);
bytes32 public bridgeHubAgentID = keccak256("1013");
address public bridgeHubAgent;

ParaID public assetHubParaID = ParaID.wrap(1002);
bytes32 public assetHubAgentID = keccak256("1002");
ParaID public assetHubParaID = ParaID.wrap(1000);
bytes32 public assetHubAgentID = keccak256("1000");
address public assetHubAgent;

ParaID public penpalParaID = ParaID.wrap(2000);
bytes32 public penpalAgentID = keccak256("2000");

address public relayer;

bytes32[] public proof = [bytes32(0x2f9ee6cfdf244060dc28aa46347c5219e303fc95062dd672b4e406ca5c29764b)];
Expand Down Expand Up @@ -114,6 +120,14 @@ contract GatewayTest is Test {
bridgeHubAgent = IGateway(address(gateway)).agentOf(bridgeHubAgentID);
assetHubAgent = IGateway(address(gateway)).agentOf(assetHubAgentID);

// Initialize agent/channel for Penpal
GatewayMock(address(gateway)).createAgentPublic(abi.encode(CreateAgentParams({agentID: penpalAgentID})));
GatewayMock(address(gateway)).createChannelPublic(
abi.encode(
CreateChannelParams({channelID: penpalParaID.into(), agentID: penpalAgentID, mode: OperatingMode.Normal})
)
);

// fund the message relayer account
relayer = makeAddr("relayer");

Expand Down Expand Up @@ -825,4 +839,29 @@ contract GatewayTest is Test {
fee = IGateway(address(gateway)).quoteRegisterTokenFee();
assertEq(fee, 10000000000000000);
}

/**
* Transact
*/
function testTransactFromSovereignOrigin() public {
TransactMessage memory message = TransactMessage(OriginKind.Xcm, 1, Weight(1, 1), bytes("0x1"));
address agentAddress = IGateway(address(gateway)).agentOf(penpalAgentID);
bytes memory payload = SubstrateTypes.Transact(agentAddress, 0x03, message);
console.log(agentAddress);
console.logBytes(payload);
vm.expectEmit(true, false, false, true);
emit IGateway.OutboundMessageAccepted(penpalParaID.into(), 1, messageID, payload);
IGateway(address(gateway)).transact{value: 1 ether}(penpalParaID, message);
}

function testTransactFromSignedOrigin() public {
TransactMessage memory message = TransactMessage(OriginKind.SovereignAccount, 1, Weight(1, 1), bytes("0x1"));
bytes memory payload = SubstrateTypes.Transact(account1, 0x01, message);
console.log(account1);
console.logBytes(payload);
vm.expectEmit(true, false, false, true);
emit IGateway.OutboundMessageAccepted(penpalParaID.into(), 1, messageID, payload);
hoax(address(account1));
IGateway(address(gateway)).transact{value: 1 ether}(penpalParaID, message);
}
}
13 changes: 12 additions & 1 deletion contracts/test/mocks/GatewayUpgradeMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.23;

import {Channel, InboundMessage, OperatingMode, ParaID, Command, ChannelID, MultiAddress} from "../../src/Types.sol";
import {
Channel,
InboundMessage,
OperatingMode,
ParaID,
Command,
ChannelID,
MultiAddress,
TransactMessage
} from "../../src/Types.sol";
import {IGateway} from "../../src/interfaces/IGateway.sol";
import {IInitializable} from "../../src/interfaces/IInitializable.sol";
import {Verification} from "../../src/Verification.sol";
Expand Down Expand Up @@ -61,4 +70,6 @@ contract GatewayUpgradeMock is IGateway, IInitializable {
function pricingParameters() external pure returns (UD60x18, uint128) {
return (convert(0), uint128(0));
}

function transact(ParaID destinationChain, TransactMessage calldata message) external payable {}
}
Loading