Skip to content

Commit

Permalink
Rialto to Millau Transfer Generator (paritytech#568)
Browse files Browse the repository at this point in the history
* Add option to relay for sending transfers to Millau chain

* Endow derived accounts on Millau chain

* Update message generator entrypoint script to send transfers

* Use correct command when sending messages

* Send Root messages from Root origin on source chain

* Wrap calls from Root Origin in Sudo Call

* Allow Root to send messages without paying fees

* Use correct variable when sending messages to Rialto

* Print warning if no message type is provided to script.

* Add note mentioning that certain source origins aren't supported yet

* Use correct runtime when initializing header sync

* Remove option to send messages as Root

* Remove endowment of derived Root accounts

* Fix indentation.

Co-authored-by: Tomasz Drwięga <[email protected]>
  • Loading branch information
2 people authored and serban300 committed Apr 8, 2024
1 parent 29a2737 commit 4c5914d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bridges/bin/millau/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ structopt = "0.3.21"
# Bridge dependencies

bp-message-lane = { path = "../../../primitives/message-lane" }
bp-rialto = { path = "../../../primitives/rialto" }
bp-millau= { path = "../../../primitives/millau" }
bp-runtime = { path = "../../../primitives/runtime" }
millau-runtime = { path = "../runtime" }
pallet-message-lane-rpc = { path = "../../../modules/message-lane/rpc" }
Expand Down
14 changes: 14 additions & 0 deletions bridges/bin/millau/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use bp_millau::derive_account_from_rialto_id;
use millau_runtime::{
AccountId, AuraConfig, BalancesConfig, BridgeRialtoConfig, GenesisConfig, GrandpaConfig, SessionConfig,
SessionKeys, Signature, SudoConfig, SystemConfig, WASM_BINARY,
Expand Down Expand Up @@ -121,6 +122,9 @@ impl Alternative {
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
get_account_id_from_seed::<sr25519::Public>("George//stash"),
get_account_id_from_seed::<sr25519::Public>("Harry//stash"),
derive_account_from_rialto_id(bp_runtime::SourceAccount::Account(
get_account_id_from_seed::<sr25519::Public>("Dave"),
)),
],
true,
)
Expand Down Expand Up @@ -173,3 +177,13 @@ fn testnet_genesis(
}),
}
}

#[test]
fn derived_dave_account_is_as_expected() {
let dave = get_account_id_from_seed::<sr25519::Public>("Dave");
let derived: AccountId = derive_account_from_rialto_id(bp_runtime::SourceAccount::Account(dave));
assert_eq!(
derived.to_string(),
"5G81vRqUUysQGtN5aEThD5UsLdt4rZWSbVLkjuZzLHadp8ZD".to_string()
);
}
10 changes: 0 additions & 10 deletions bridges/bin/rialto/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl Alternative {
get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
get_account_id_from_seed::<sr25519::Public>("George//stash"),
get_account_id_from_seed::<sr25519::Public>("Harry//stash"),
derive_account_from_millau_id(bp_runtime::SourceAccount::Root),
derive_account_from_millau_id(bp_runtime::SourceAccount::Account(
get_account_id_from_seed::<sr25519::Public>("Dave"),
)),
Expand Down Expand Up @@ -206,12 +205,3 @@ fn derived_dave_account_is_as_expected() {
"5Hg7WQyk8C1FmPzxY3xSjR7S6zZZC5sAL35vMr6NpW17jBhQ".to_string()
);
}

#[test]
fn derived_root_account_is_as_expected() {
let root: AccountId = derive_account_from_millau_id(bp_runtime::SourceAccount::Root);
assert_eq!(
root.to_string(),
"5HYYwXQvxhgdcBYs6kzqfK1HW6M3UF3Kh4YM7j288yiqbhnt".to_string()
);
}
8 changes: 5 additions & 3 deletions bridges/modules/message-lane/src/instant_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ where
Currency::transfer(submitter, relayer_fund_account, *fee, ExistenceRequirement::AllowDeath)
.map_err(Into::into)
}
Sender::Root | Sender::None => {
// fixme: we might want to add root account id to this struct.
Err("Root and None account is not allowed to send regular messages.")
Sender::Root => {
Err("Sending messages from Root account is not supported yet. See GitHub issue #559 for more.")
}
Sender::None => {
Err("Sending messages from None account is not supported yet. See GitHub issue #559 for more.")
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions bridges/primitives/millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use frame_support::{
RuntimeDebug,
};
use sp_core::Hasher as HasherT;
use sp_runtime::traits::Convert;
use sp_runtime::{
traits::{IdentifyAccount, Verify},
MultiSignature, MultiSigner, Perbill,
Expand Down Expand Up @@ -161,6 +162,19 @@ impl sp_runtime::traits::Convert<sp_core::H256, AccountId> for AccountIdConverte
}
}

/// We use this to get the account on Millau (target) which is derived from Rialto's (source)
/// account. We do this so we can fund the derived account on Millau at Genesis to it can pay
/// transaction fees.
///
/// The reason we can use the same `AccountId` type for both chains is because they share the same
/// development seed phrase.
///
/// Note that this should only be used for testing.
pub fn derive_account_from_rialto_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
let encoded_id = bp_runtime::derive_account_id(bp_runtime::RIALTO_BRIDGE_INSTANCE, id);
AccountIdConverter::convert(encoded_id)
}

/// Get a struct which defines the weight limits and values used during extrinsic execution.
pub fn runtime_block_weights() -> frame_system::limits::BlockWeights {
frame_system::limits::BlockWeights::builder()
Expand Down
2 changes: 1 addition & 1 deletion bridges/primitives/rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Convert<sp_core::H256, AccountId> for AccountIdConverter {
//
// Note that this should only be used for testing.
pub fn derive_account_from_millau_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
let encoded_id = bp_runtime::derive_account_id(*b"mlau", id);
let encoded_id = bp_runtime::derive_account_id(bp_runtime::MILLAU_BRIDGE_INSTANCE, id);
AccountIdConverter::convert(encoded_id)
}

Expand Down
28 changes: 18 additions & 10 deletions bridges/relays/substrate/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ pub enum Command {
/// Hex-encoded lane id.
#[structopt(long)]
lane: HexLaneId,
/// Message type.
#[structopt(long, possible_values = &ToMillauMessage::variants())]
message: ToMillauMessage,
/// Delivery and dispatch fee.
#[structopt(long)]
fee: bp_rialto::Balance,
/// Message type.
#[structopt(subcommand)]
message: ToMillauMessage,
/// The origin to use when dispatching the message on the target chain.
#[structopt(long, possible_values = &Origins::variants())]
origin: Origins,
},
}

Expand All @@ -161,19 +164,24 @@ pub enum ToRialtoMessage {
},
}

arg_enum! {
#[derive(Debug)]
/// All possible messages that may be delivered to the Millau chain.
pub enum ToMillauMessage {
Remark,
}
/// All possible messages that may be delivered to the Millau chain.
#[derive(StructOpt, Debug)]
pub enum ToMillauMessage {
/// Make an on-chain remark (comment).
Remark,
/// Transfer the specified `amount` of native tokens to a particular `recipient`.
Transfer {
#[structopt(long)]
recipient: bp_millau::AccountId,
#[structopt(long)]
amount: bp_millau::Balance,
},
}

arg_enum! {
#[derive(Debug)]
/// The origin to use when dispatching the message on the target chain.
pub enum Origins {
Root,
Target,
Source,
}
Expand Down
46 changes: 27 additions & 19 deletions bridges/relays/substrate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
&rialto_client,
&rialto_sign.signer,
rialto_signer_next_index,
millau_runtime::SudoCall::sudo(Box::new(
rialto_runtime::SudoCall::sudo(Box::new(
rialto_runtime::BridgeMillauCall::initialize(initialization_data).into(),
))
.into(),
Expand Down Expand Up @@ -290,12 +290,6 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
let rialto_origin_public = rialto_sign.signer.public();

let payload = match origin {
cli::Origins::Root => MessagePayload {
spec_version: rialto_runtime::VERSION.spec_version,
weight: rialto_call_weight,
origin: CallOrigin::SourceRoot,
call: rialto_call.encode(),
},
cli::Origins::Source => MessagePayload {
spec_version: rialto_runtime::VERSION.spec_version,
weight: rialto_call_weight,
Expand All @@ -312,7 +306,7 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
spec_version: rialto_runtime::VERSION.spec_version,
weight: rialto_call_weight,
origin: CallOrigin::TargetAccount(
millau_account_id.clone(),
millau_account_id,
rialto_origin_public.into(),
rialto_origin_signature.into(),
),
Expand Down Expand Up @@ -383,6 +377,8 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
lane,
message,
fee,
origin,
..
} => {
let rialto_client = RialtoClient::new(ConnectionParams {
host: rialto.rialto_host,
Expand Down Expand Up @@ -412,21 +408,29 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
.as_bytes()
.to_vec(),
)),
cli::ToMillauMessage::Transfer { recipient, amount } => {
millau_runtime::Call::Balances(millau_runtime::BalancesCall::transfer(recipient, amount))
}
};
let millau_call_weight = millau_call.get_dispatch_info().weight;

let millau_call_weight = millau_call.get_dispatch_info().weight;
let rialto_sender_public: bp_rialto::AccountSigner = rialto_sign.signer.public().clone().into();
let rialto_account_id: bp_rialto::AccountId = rialto_sender_public.into_account();
let millau_origin_public = millau_sign.signer.public();

let mut millau_origin_signature_message = Vec::new();
millau_call.encode_to(&mut millau_origin_signature_message);
rialto_account_id.encode_to(&mut millau_origin_signature_message);
let millau_origin_signature = millau_sign.signer.sign(&millau_origin_signature_message);
let payload = match origin {
cli::Origins::Source => MessagePayload {
spec_version: millau_runtime::VERSION.spec_version,
weight: millau_call_weight,
origin: CallOrigin::SourceAccount(rialto_account_id),
call: millau_call.encode(),
},
cli::Origins::Target => {
let mut millau_origin_signature_message = Vec::new();
millau_call.encode_to(&mut millau_origin_signature_message);
rialto_account_id.encode_to(&mut millau_origin_signature_message);
let millau_origin_signature = millau_sign.signer.sign(&millau_origin_signature_message);

let rialto_call =
rialto_runtime::Call::BridgeMillauMessageLane(rialto_runtime::MessageLaneCall::send_message(
lane.into(),
MessagePayload {
spec_version: millau_runtime::VERSION.spec_version,
weight: millau_call_weight,
Expand All @@ -436,9 +440,13 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
millau_origin_signature.into(),
),
call: millau_call.encode(),
},
fee,
));
}
}
};

let rialto_call = rialto_runtime::Call::BridgeMillauMessageLane(
rialto_runtime::MessageLaneCall::send_message(lane.into(), payload, fee),
);

let signed_rialto_call = Rialto::sign_transaction(
&rialto_client,
Expand Down

0 comments on commit 4c5914d

Please sign in to comment.