Skip to content

Commit a910616

Browse files
authored
Rewards refund for relaying BridgeHubRococo/BridgeHubWococo (paritytech#1894)
* Rewards refund for relaying BridgeHubRococo/BridgeHubWococo * spellcheck + clippy * RefundBridgedParachainMessages move to bp-runtime * Dedicated RewardingBridgeSignedExtra for Rococo/Wococo shared runtime with two instances of `RefundBridgedParachainMessages` * RefundBridgedParachainMessages with Tuple support for multiple * Fix additional_signed * revert fix * Refactor to `RefundBridgedParachainMessagesSchema` * removed unused deps
1 parent 29b7c7e commit a910616

File tree

7 files changed

+111
-16
lines changed

7 files changed

+111
-16
lines changed

bridges/primitives/chain-bridge-hub-cumulus/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
1111

1212
bp-polkadot-core = { path = "../../primitives/polkadot-core", default-features = false }
1313
bp-messages = { path = "../../primitives/messages", default-features = false }
14+
bp-runtime = { path = "../../primitives/runtime", default-features = false }
1415

1516
# Substrate Based Dependencies
1617

@@ -27,6 +28,7 @@ default = ["std"]
2728
std = [
2829
"bp-polkadot-core/std",
2930
"bp-messages/std",
31+
"bp-runtime/std",
3032
"frame-system/std",
3133
"frame-support/std",
3234
"sp-api/std",

bridges/primitives/chain-bridge-hub-cumulus/src/lib.rs

+71-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
use bp_messages::*;
2020
pub use bp_polkadot_core::{
2121
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
22-
Hashing, Header, Index, Nonce, Perbill, PolkadotSignedExtension, Signature, SignedBlock,
23-
UncheckedExtrinsic, EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
22+
Hashing, Header, Index, Nonce, Perbill, Signature, SignedBlock, UncheckedExtrinsic,
23+
EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
2424
};
2525
use frame_support::{
2626
dispatch::DispatchClass,
@@ -124,3 +124,72 @@ pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;
124124

125125
/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
126126
pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;
127+
128+
/// Module with rewarding bridge signed extension support
129+
pub mod rewarding_bridge_signed_extension {
130+
use super::*;
131+
use bp_polkadot_core::PolkadotLike;
132+
use bp_runtime::extensions::*;
133+
134+
type RewardingBridgeSignedExtra = (
135+
CheckNonZeroSender,
136+
CheckSpecVersion,
137+
CheckTxVersion,
138+
CheckGenesis<PolkadotLike>,
139+
CheckEra<PolkadotLike>,
140+
CheckNonce<Nonce>,
141+
CheckWeight,
142+
ChargeTransactionPayment<PolkadotLike>,
143+
BridgeRejectObsoleteHeadersAndMessages,
144+
RefundBridgedParachainMessagesSchema,
145+
);
146+
147+
/// The signed extension used by Cumulus and Cumulus-like parachain with bridging and rewarding.
148+
pub type RewardingBridgeSignedExtension = GenericSignedExtension<RewardingBridgeSignedExtra>;
149+
150+
pub fn from_params(
151+
spec_version: u32,
152+
transaction_version: u32,
153+
era: bp_runtime::TransactionEraOf<PolkadotLike>,
154+
genesis_hash: Hash,
155+
nonce: Nonce,
156+
tip: Balance,
157+
) -> RewardingBridgeSignedExtension {
158+
GenericSignedExtension::<RewardingBridgeSignedExtra>::new(
159+
(
160+
(), // non-zero sender
161+
(), // spec version
162+
(), // tx version
163+
(), // genesis
164+
era.frame_era(), // era
165+
nonce.into(), // nonce (compact encoding)
166+
(), // Check weight
167+
tip.into(), // transaction payment / tip (compact encoding)
168+
(), // bridge reject obsolete headers and msgs
169+
(), // bridge register reward to relayer for message passing
170+
),
171+
Some((
172+
(),
173+
spec_version,
174+
transaction_version,
175+
genesis_hash,
176+
era.signed_payload(genesis_hash),
177+
(),
178+
(),
179+
(),
180+
(),
181+
(),
182+
)),
183+
)
184+
}
185+
186+
/// Return signer nonce, used to craft transaction.
187+
pub fn nonce(sign_ext: &RewardingBridgeSignedExtension) -> Nonce {
188+
sign_ext.payload.5.into()
189+
}
190+
191+
/// Return transaction tip.
192+
pub fn tip(sign_ext: &RewardingBridgeSignedExtension) -> Balance {
193+
sign_ext.payload.7.into()
194+
}
195+
}

bridges/primitives/runtime/src/extensions.rs

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ pub type ChargeTransactionPayment<C> = GenericSignedExtensionSchema<Compact<Bala
7676
/// The `SignedExtensionSchema` for `BridgeRejectObsoleteHeadersAndMessages`.
7777
pub type BridgeRejectObsoleteHeadersAndMessages = GenericSignedExtensionSchema<(), ()>;
7878

79+
/// The `SignedExtensionSchema` for `RefundBridgedParachainMessages`.
80+
/// This schema is dedicated for `RefundBridgedParachainMessages` signed extension as
81+
/// wildcard/placeholder, which relies on the scale encoding for `()` or `((), ())`, or `((), (),
82+
/// ())` is the same. So runtime can contains any kind of tuple:
83+
/// `(BridgeRefundBridgeHubRococoMessages)`
84+
/// `(BridgeRefundBridgeHubRococoMessages, BridgeRefundBridgeHubWococoMessages)`
85+
/// `(BridgeRefundParachainMessages1, ..., BridgeRefundParachainMessagesN)`
86+
pub type RefundBridgedParachainMessagesSchema = GenericSignedExtensionSchema<(), ()>;
87+
7988
#[impl_for_tuples(1, 12)]
8089
impl SignedExtensionSchema for Tuple {
8190
for_tuples!( type Payload = ( #( Tuple::Payload ),* ); );

bridges/relays/client-bridge-hub-rococo/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! Types used to connect to the BridgeHub-Rococo-Substrate parachain.
1818
1919
use bp_bridge_hub_rococo::AVERAGE_BLOCK_INTERVAL;
20-
use bp_bridge_hub_wococo::PolkadotSignedExtension;
2120
use bp_messages::MessageNonce;
2221
use codec::Encode;
2322
use relay_substrate_client::{
@@ -72,7 +71,7 @@ impl ChainWithTransactions for BridgeHubRococo {
7271
) -> Result<Self::SignedTransaction, SubstrateError> {
7372
let raw_payload = SignedPayload::new(
7473
unsigned.call,
75-
bp_bridge_hub_rococo::SignedExtension::from_params(
74+
runtime::rewarding_bridge_signed_extension::from_params(
7675
param.spec_version,
7776
param.transaction_version,
7877
unsigned.era,
@@ -86,7 +85,7 @@ impl ChainWithTransactions for BridgeHubRococo {
8685
let signer: sp_runtime::MultiSigner = param.signer.public().into();
8786
let (call, extra, _) = raw_payload.deconstruct();
8887

89-
Ok(bp_bridge_hub_rococo::UncheckedExtrinsic::new_signed(
88+
Ok(runtime::UncheckedExtrinsic::new_signed(
9089
call,
9190
signer.into_account().into(),
9291
signature.into(),
@@ -109,7 +108,13 @@ impl ChainWithTransactions for BridgeHubRococo {
109108

110109
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
111110
let extra = &tx.signature.as_ref()?.2;
112-
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
111+
Some(
112+
UnsignedTransaction::new(
113+
tx.function,
114+
runtime::rewarding_bridge_signed_extension::nonce(extra),
115+
)
116+
.tip(runtime::rewarding_bridge_signed_extension::tip(extra)),
117+
)
113118
}
114119
}
115120

bridges/relays/client-bridge-hub-rococo/src/runtime_wrapper.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// TODO: join with primitives do we need this here or move to the primitives?
18-
1917
//! Types that are specific to the BridgeHubRococo runtime.
2018
2119
use codec::{Decode, Encode};
2220
use scale_info::TypeInfo;
2321

24-
use bp_bridge_hub_rococo::SignedExtension;
22+
pub use bp_bridge_hub_rococo::rewarding_bridge_signed_extension;
2523
pub use bp_header_chain::BridgeGrandpaCallOf;
2624
pub use bp_parachains::BridgeParachainCall;
2725
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
2826
pub use relay_substrate_client::calls::{SystemCall, UtilityCall};
2927

3028
/// Unchecked BridgeHubRococo extrinsic.
31-
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<Call, SignedExtension>;
29+
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<
30+
Call,
31+
rewarding_bridge_signed_extension::RewardingBridgeSignedExtension,
32+
>;
3233

3334
// The indirect pallet call used to sync `Wococo` GRANDPA finality to `BHRococo`.
3435
pub type BridgeWococoGrandpaCall = BridgeGrandpaCallOf<bp_wococo::Wococo>;

bridges/relays/client-bridge-hub-wococo/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
//! Types used to connect to the BridgeHub-Wococo-Substrate parachain.
1818
19-
use bp_bridge_hub_wococo::{PolkadotSignedExtension, AVERAGE_BLOCK_INTERVAL};
19+
use bp_bridge_hub_wococo::AVERAGE_BLOCK_INTERVAL;
2020
use bp_messages::MessageNonce;
2121
use codec::Encode;
2222
use relay_substrate_client::{
@@ -71,7 +71,7 @@ impl ChainWithTransactions for BridgeHubWococo {
7171
) -> Result<Self::SignedTransaction, SubstrateError> {
7272
let raw_payload = SignedPayload::new(
7373
unsigned.call,
74-
bp_bridge_hub_wococo::SignedExtension::from_params(
74+
runtime::rewarding_bridge_signed_extension::from_params(
7575
param.spec_version,
7676
param.transaction_version,
7777
unsigned.era,
@@ -85,7 +85,7 @@ impl ChainWithTransactions for BridgeHubWococo {
8585
let signer: sp_runtime::MultiSigner = param.signer.public().into();
8686
let (call, extra, _) = raw_payload.deconstruct();
8787

88-
Ok(bp_bridge_hub_wococo::UncheckedExtrinsic::new_signed(
88+
Ok(runtime::UncheckedExtrinsic::new_signed(
8989
call,
9090
signer.into_account().into(),
9191
signature.into(),
@@ -108,7 +108,13 @@ impl ChainWithTransactions for BridgeHubWococo {
108108

109109
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
110110
let extra = &tx.signature.as_ref()?.2;
111-
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
111+
Some(
112+
UnsignedTransaction::new(
113+
tx.function,
114+
runtime::rewarding_bridge_signed_extension::nonce(extra),
115+
)
116+
.tip(runtime::rewarding_bridge_signed_extension::tip(extra)),
117+
)
112118
}
113119
}
114120

bridges/relays/client-bridge-hub-wococo/src/runtime_wrapper.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
use codec::{Decode, Encode};
2020
use scale_info::TypeInfo;
2121

22-
use bp_bridge_hub_wococo::SignedExtension;
22+
pub use bp_bridge_hub_wococo::rewarding_bridge_signed_extension;
2323
pub use bp_header_chain::BridgeGrandpaCallOf;
2424
pub use bp_parachains::BridgeParachainCall;
2525
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
2626
pub use relay_substrate_client::calls::{SystemCall, UtilityCall};
2727

2828
/// Unchecked BridgeHubWococo extrinsic.
29-
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<Call, SignedExtension>;
29+
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<
30+
Call,
31+
rewarding_bridge_signed_extension::RewardingBridgeSignedExtension,
32+
>;
3033

3134
// The indirect pallet call used to sync `Rococo` GRANDPA finality to `BHWococo`.
3235
pub type BridgeRococoGrandpaCall = BridgeGrandpaCallOf<bp_rococo::Rococo>;

0 commit comments

Comments
 (0)