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

Signed extension to refund relayer at the target chain #1657

Merged
merged 25 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8999d63
add utlity pallet to the Millau runtime
svyatonik Nov 22, 2022
89a9268
RefundRelayerForMessagesDeliveryFromParachain prototype
svyatonik Nov 22, 2022
20f90aa
Merge branch 'master' into signed-extension-to-refund-relayer-at-targ…
svyatonik Nov 25, 2022
dc35853
done with RefundRelayerForMessagesDeliveryFromParachain::post_dispatch
svyatonik Nov 25, 2022
25e4c09
parse calls
svyatonik Nov 25, 2022
e72c723
check batch for obsolete headers/messages
svyatonik Nov 28, 2022
4bb3766
fmt
svyatonik Nov 28, 2022
54e350b
shorten generic arg names + add parachain id generic arg
svyatonik Nov 28, 2022
92cc156
check lane_id
svyatonik Nov 28, 2022
5dc098a
impl all state read functions
svyatonik Nov 28, 2022
29dc969
Merge branch 'master' into signed-extension-to-refund-relayer-at-targ…
svyatonik Nov 28, 2022
e581c4d
fix typos from review
svyatonik Nov 29, 2022
d067da3
renamed extension + reference issue from TODO
svyatonik Nov 29, 2022
8ab7c9f
tests for pre-dispaytch
svyatonik Nov 29, 2022
da8699e
renamed extension source file
svyatonik Nov 29, 2022
d2596ab
tests for post-dispatch
svyatonik Nov 29, 2022
3f5e08d
abstract fee calculation
svyatonik Nov 30, 2022
7d31015
clippy
svyatonik Nov 30, 2022
3258a8e
actually fix clippy
svyatonik Nov 30, 2022
ecf5f7c
Merge branch 'master' into signed-extension-to-refund-relayer-at-targ…
svyatonik Nov 30, 2022
9b3b5f3
Update bin/runtime-common/src/refund_relayer_extension.rs
svyatonik Dec 9, 2022
557fce8
Update bin/runtime-common/src/refund_relayer_extension.rs
svyatonik Dec 9, 2022
16686ee
Update bin/runtime-common/src/refund_relayer_extension.rs
svyatonik Dec 9, 2022
054d4e8
Update bin/runtime-common/src/refund_relayer_extension.rs
svyatonik Dec 9, 2022
948db62
Merge branch 'master' into signed-extension-to-refund-relayer-at-targ…
svyatonik Dec 9, 2022
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ frame-support = { git = "https://github.com/paritytech/substrate", branch = "mas
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-utility = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down Expand Up @@ -65,6 +66,7 @@ std = [
"pallet-bridge-parachains/std",
"pallet-bridge-relayers/std",
"pallet-transaction-payment/std",
"pallet-utility/std",
"pallet-xcm/std",
"scale-info/std",
"sp-api/std",
Expand Down
112 changes: 100 additions & 12 deletions bin/runtime-common/src/messages_batch_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@

use bp_messages::{LaneId, MessageNonce};
use bp_polkadot_core::parachains::ParaId;
use bp_runtime::Chain;
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, Dispatchable, PostDispatchInfo},
dispatch::{CallableCallFor, DispatchInfo, Dispatchable, PostDispatchInfo},
traits::IsSubType,
RuntimeDebugNoBound,
};
use pallet_bridge_parachains::RelayBlockNumber;
use pallet_bridge_grandpa::{Call as GrandpaCall, Config as GrandpaConfig, Pallet as GrandpaPallet};
use pallet_bridge_messages::{Call as MessagesCall, Config as MessagesConfig, Pallet as MessagesPallet};
use pallet_bridge_parachains::{Call as ParachainsCall, Config as ParachainsConfig, Pallet as ParachainsPallet, RelayBlockHash, RelayBlockHasher, RelayBlockNumber};
use pallet_transaction_payment::OnChargeTransaction;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, SignedExtension, Zero},
traits::{DispatchInfoOf, Header as HeaderT, PostDispatchInfoOf, SignedExtension, Zero},
transaction_validity::{TransactionValidity, TransactionValidityError, ValidTransaction},
DispatchResult, FixedPointOperand,
};
Expand Down Expand Up @@ -150,6 +154,7 @@ type BalanceOf<Runtime> =
<<Runtime as pallet_transaction_payment::Config>::OnChargeTransaction as OnChargeTransaction<
Runtime,
>>::Balance;
type CallOf<Runtime> = <Runtime as frame_system::Config>::RuntimeCall;

impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExtension
for RefundRelayerForMessagesDeliveryFromParachain<
Expand All @@ -163,8 +168,9 @@ impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExten
+ Sync
+ frame_system::Config
+ pallet_transaction_payment::Config
+ pallet_bridge_grandpa::Config<GrandpaInstance>
+ pallet_bridge_parachains::Config<ParachainsInstance>
+ pallet_utility::Config<RuntimeCall = CallOf<Runtime>>
+ GrandpaConfig<GrandpaInstance>
+ pallet_bridge_parachains::Config<ParachainsInstance, BridgesGrandpaPalletInstance = GrandpaInstance>
+ pallet_bridge_messages::Config<MessagesInstance>
+ pallet_bridge_relayers::Config<Reward = BalanceOf<Runtime>>,
GrandpaInstance: 'static + Send + Sync,
Expand All @@ -173,12 +179,17 @@ impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExten
<Runtime as frame_system::Config>::RuntimeCall:
Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
BalanceOf<Runtime>: FixedPointOperand,
CallOf<Runtime>: IsSubType<CallableCallFor<pallet_utility::Pallet<Runtime>, Runtime>>
+ IsSubType<CallableCallFor<GrandpaPallet<Runtime, GrandpaInstance>, Runtime>>
+ IsSubType<CallableCallFor<pallet_bridge_parachains::Pallet<Runtime, ParachainsInstance>, Runtime>>
+ IsSubType<CallableCallFor<pallet_bridge_messages::Pallet<Runtime, MessagesInstance>, Runtime>>,
<Runtime as GrandpaConfig<GrandpaInstance>>::BridgedChain: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
{
const IDENTIFIER: &'static str = "RefundRelayerForMessagesDeliveryFromParachain";
type AccountId = Runtime::AccountId;
type Call = Runtime::RuntimeCall;
type Call = CallOf<Runtime>;
type AdditionalSigned = ();
type Pre = PreDispatchData<Runtime::AccountId>;
type Pre = Option<PreDispatchData<Runtime::AccountId>>;

fn additional_signed(&self) -> Result<(), TransactionValidityError> {
Ok(())
Expand All @@ -196,14 +207,36 @@ impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExten

fn pre_dispatch(
self,
_who: &Self::AccountId,
_call: &Self::Call,
who: &Self::AccountId,
call: &Self::Call,
_post_info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
// TODO: for every call from the batch - call the `BridgesExtension` to ensure that every
// call transaction brings something new and reject obsolete transactions
unimplemented!("TODO") // TODO: return actual call type

let parse_call_type = || {
if let Some(pallet_utility::Call::<Runtime>::batch_all { ref calls }) = call.is_sub_type() {
if calls.len() == 3 {
return Some(CallType::AllFinalityAndDelivery(
extract_expected_relay_chain_state::<Runtime, GrandpaInstance>(&calls[0])?,
extract_expected_parachain_state::<Runtime, GrandpaInstance, ParachainsInstance>(&calls[1])?,
extract_messages_state::<Runtime, MessagesInstance>(&calls[2])?,
));
}
if calls.len() == 2 {
return Some(CallType::ParachainFinalityAndDelivery(
extract_expected_parachain_state::<Runtime, GrandpaInstance, ParachainsInstance>(&calls[0])?,
extract_messages_state::<Runtime, MessagesInstance>(&calls[1])?,
));
}
return None
}

Some(CallType::Delivery(extract_messages_state::<Runtime, MessagesInstance>(call)?))
};

Ok(parse_call_type().map(|call_type| PreDispatchData { relayer: who.clone(), call_type }))
}

fn post_dispatch(
Expand All @@ -216,8 +249,8 @@ impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExten
// we never refund anything if that is not bridge transaction or if it is a bridge
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
// transaction that we do not support here
let (relayer, call_type) = match pre {
Some(pre) => (pre.relayer, pre.call_type),
None => return Ok(()),
Some(Some(pre)) => (pre.relayer, pre.call_type),
_ => return Ok(()),
};

// we never refund anything if transaction has failed
Expand Down Expand Up @@ -281,6 +314,61 @@ impl<Runtime, GrandpaInstance, ParachainsInstance, MessagesInstance> SignedExten
}
}

/// Extracts expected relay chain state from the call.
fn extract_expected_relay_chain_state<Runtime, GrandpaInstance>(call: &CallOf<Runtime>) -> Option<ExpectedRelayChainState> where
Runtime: GrandpaConfig<GrandpaInstance>,
GrandpaInstance: 'static,
<Runtime as GrandpaConfig<GrandpaInstance>>::BridgedChain: Chain<BlockNumber = RelayBlockNumber>,
CallOf<Runtime>: IsSubType<CallableCallFor<GrandpaPallet<Runtime, GrandpaInstance>, Runtime>>,
{
if let Some(GrandpaCall::<Runtime, GrandpaInstance>::submit_finality_proof {
ref finality_target,
..
}) = call.is_sub_type() {
return Some(ExpectedRelayChainState { best_block_number: *finality_target.number() });
}
None
}

/// Extracts expected parachain state from the call.
fn extract_expected_parachain_state<Runtime, GrandpaInstance, ParachainsInstance>(call: &CallOf<Runtime>) -> Option<ExpectedParachainState> where
Runtime: GrandpaConfig<GrandpaInstance> + ParachainsConfig<ParachainsInstance, BridgesGrandpaPalletInstance = GrandpaInstance>,
GrandpaInstance: 'static,
ParachainsInstance: 'static,
<Runtime as GrandpaConfig<GrandpaInstance>>::BridgedChain: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>,
CallOf<Runtime>: IsSubType<CallableCallFor<ParachainsPallet<Runtime, ParachainsInstance>, Runtime>>,
{
// TODO: check para id (we'll need to bound instance of messages pallet with some ParaId)
svyatonik marked this conversation as resolved.
Show resolved Hide resolved
if let Some(ParachainsCall::<Runtime, ParachainsInstance>::submit_parachain_heads {
ref at_relay_block,
..
}) = call.is_sub_type() {
return Some(ExpectedParachainState {
para: { unimplemented!("TODO") }, // TODO: fill parachain id
at_relay_block_number: at_relay_block.0,
});
}
None
}

/// Extracts messages state from the call.
fn extract_messages_state<Runtime, MessagesInstance>(call: &CallOf<Runtime>) -> Option<MessagesState> where
Runtime: MessagesConfig<MessagesInstance>,
MessagesInstance: 'static,
CallOf<Runtime>: IsSubType<CallableCallFor<MessagesPallet<Runtime, MessagesInstance>, Runtime>>,
{
if let Some(MessagesCall::<Runtime, MessagesInstance>::receive_messages_proof {
ref proof,
..
}) = call.is_sub_type() {
return Some(MessagesState {
lane: { unimplemented!("TODO") },
last_delivered_nonce: { unimplemented!("TODO") },
});
}
None
}

/// Returns relay chain state that we are interested in.
fn relay_chain_state<Runtime, GrandpaInstance>() -> ExpectedRelayChainState {
unimplemented!("TODO")
Expand Down