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

Rewards refund for relaying BridgeHubRococo/BridgeHubWococo #1894

Merged
merged 10 commits into from
Feb 21, 2023
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 primitives/chain-bridge-hub-cumulus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

bp-polkadot-core = { path = "../../primitives/polkadot-core", default-features = false }
bp-messages = { path = "../../primitives/messages", default-features = false }
bp-runtime = { path = "../../primitives/runtime", default-features = false }

# Substrate Based Dependencies

Expand All @@ -27,6 +28,7 @@ default = ["std"]
std = [
"bp-polkadot-core/std",
"bp-messages/std",
"bp-runtime/std",
"frame-system/std",
"frame-support/std",
"sp-api/std",
Expand Down
76 changes: 74 additions & 2 deletions primitives/chain-bridge-hub-cumulus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use bp_messages::*;
pub use bp_polkadot_core::{
AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, Balance, BlockNumber, Hash, Hasher,
Hashing, Header, Index, Nonce, Perbill, PolkadotSignedExtension, Signature, SignedBlock,
UncheckedExtrinsic, EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
Hashing, Header, Index, Nonce, Perbill, Signature, SignedBlock, UncheckedExtrinsic,
EXTRA_STORAGE_PROOF_SIZE, TX_EXTRA_BYTES,
};
use frame_support::{
dispatch::DispatchClass,
Expand Down Expand Up @@ -124,3 +124,75 @@ pub const MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX: MessageNonce = 1024;

/// Maximal number of unconfirmed messages at inbound lane for Cumulus-based parachains.
pub const MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX: MessageNonce = 4096;

/// Module with rewarding bridge signed extension support
pub mod rewarding_bridge_signed_extension {
bkontur marked this conversation as resolved.
Show resolved Hide resolved
use super::*;
use bp_polkadot_core::PolkadotLike;
use bp_runtime::extensions::*;

/// The `SignedExtensionSchema` for `RefundRelayerForMessagesFromParachain`.
type RefundRelayerForMessagesFromParachain = GenericSignedExtensionSchema<(), ()>;
bkontur marked this conversation as resolved.
Show resolved Hide resolved

type RewardingBridgeSignedExtra = (
CheckNonZeroSender,
CheckSpecVersion,
CheckTxVersion,
CheckGenesis<PolkadotLike>,
CheckEra<PolkadotLike>,
CheckNonce<Nonce>,
CheckWeight,
ChargeTransactionPayment<PolkadotLike>,
BridgeRejectObsoleteHeadersAndMessages,
RefundRelayerForMessagesFromParachain,
);

/// The signed extension used by Cumulus and Cumulus-like parachain with bridging and rewarding.
pub type RewardingBridgeSignedExtension = GenericSignedExtension<RewardingBridgeSignedExtra>;

pub fn from_params(
spec_version: u32,
transaction_version: u32,
era: bp_runtime::TransactionEraOf<PolkadotLike>,
genesis_hash: Hash,
nonce: Nonce,
tip: Balance,
) -> RewardingBridgeSignedExtension {
GenericSignedExtension::<RewardingBridgeSignedExtra>::new(
(
(), // non-zero sender
(), // spec version
(), // tx version
(), // genesis
era.frame_era(), // era
nonce.into(), // nonce (compact encoding)
(), // Check weight
tip.into(), // transaction payment / tip (compact encoding)
(), // bridge reject obsolete headers and msgs
(), // bridge register reward to relayer for message passing
),
Some((
(),
spec_version,
transaction_version,
genesis_hash,
era.signed_payload(genesis_hash),
(),
(),
(),
(),
(),
)),
)
}

/// Return signer nonce, used to craft transaction.
pub fn nonce(sign_ext: &RewardingBridgeSignedExtension) -> Nonce {
sign_ext.payload.5.into()
}

/// Return transaction tip.
pub fn tip(sign_ext: &RewardingBridgeSignedExtension) -> Balance {
sign_ext.payload.7.into()
}
}
13 changes: 9 additions & 4 deletions relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//! Types used to connect to the BridgeHub-Rococo-Substrate parachain.

use bp_bridge_hub_rococo::AVERAGE_BLOCK_INTERVAL;
use bp_bridge_hub_wococo::PolkadotSignedExtension;
use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
Expand Down Expand Up @@ -72,7 +71,7 @@ impl ChainWithTransactions for BridgeHubRococo {
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::new(
unsigned.call,
bp_bridge_hub_rococo::SignedExtension::from_params(
bp_bridge_hub_rococo::rewarding_bridge_signed_extension::from_params(
param.spec_version,
param.transaction_version,
unsigned.era,
Expand All @@ -86,7 +85,7 @@ impl ChainWithTransactions for BridgeHubRococo {
let signer: sp_runtime::MultiSigner = param.signer.public().into();
let (call, extra, _) = raw_payload.deconstruct();

Ok(bp_bridge_hub_rococo::UncheckedExtrinsic::new_signed(
Ok(runtime::UncheckedExtrinsic::new_signed(
call,
signer.into_account().into(),
signature.into(),
Expand All @@ -109,7 +108,13 @@ impl ChainWithTransactions for BridgeHubRococo {

fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
Some(
UnsignedTransaction::new(
tx.function,
bp_bridge_hub_rococo::rewarding_bridge_signed_extension::nonce(extra),
)
.tip(bp_bridge_hub_rococo::rewarding_bridge_signed_extension::tip(extra)),
)
}
}

Expand Down
8 changes: 4 additions & 4 deletions relays/client-bridge-hub-rococo/src/runtime_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
// 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/>.

// TODO: join with primitives do we need this here or move to the primitives?

//! Types that are specific to the BridgeHubRococo runtime.

use codec::{Decode, Encode};
use scale_info::TypeInfo;

use bp_bridge_hub_rococo::SignedExtension;
pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::{SystemCall, UtilityCall};

/// Unchecked BridgeHubRococo extrinsic.
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<Call, SignedExtension>;
pub type UncheckedExtrinsic = bp_bridge_hub_rococo::UncheckedExtrinsic<
Call,
bp_bridge_hub_rococo::rewarding_bridge_signed_extension::RewardingBridgeSignedExtension,
>;

// The indirect pallet call used to sync `Wococo` GRANDPA finality to `BHRococo`.
pub type BridgeWococoGrandpaCall = BridgeGrandpaCallOf<bp_wococo::Wococo>;
Expand Down
14 changes: 10 additions & 4 deletions relays/client-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Types used to connect to the BridgeHub-Wococo-Substrate parachain.

use bp_bridge_hub_wococo::{PolkadotSignedExtension, AVERAGE_BLOCK_INTERVAL};
use bp_bridge_hub_wococo::AVERAGE_BLOCK_INTERVAL;
use bp_messages::MessageNonce;
use codec::Encode;
use relay_substrate_client::{
Expand Down Expand Up @@ -71,7 +71,7 @@ impl ChainWithTransactions for BridgeHubWococo {
) -> Result<Self::SignedTransaction, SubstrateError> {
let raw_payload = SignedPayload::new(
unsigned.call,
bp_bridge_hub_wococo::SignedExtension::from_params(
bp_bridge_hub_wococo::rewarding_bridge_signed_extension::from_params(
param.spec_version,
param.transaction_version,
unsigned.era,
Expand All @@ -85,7 +85,7 @@ impl ChainWithTransactions for BridgeHubWococo {
let signer: sp_runtime::MultiSigner = param.signer.public().into();
let (call, extra, _) = raw_payload.deconstruct();

Ok(bp_bridge_hub_wococo::UncheckedExtrinsic::new_signed(
Ok(runtime::UncheckedExtrinsic::new_signed(
call,
signer.into_account().into(),
signature.into(),
Expand All @@ -108,7 +108,13 @@ impl ChainWithTransactions for BridgeHubWococo {

fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>> {
let extra = &tx.signature.as_ref()?.2;
Some(UnsignedTransaction::new(tx.function, extra.nonce()).tip(extra.tip()))
Some(
UnsignedTransaction::new(
tx.function,
bp_bridge_hub_wococo::rewarding_bridge_signed_extension::nonce(extra),
)
.tip(bp_bridge_hub_wococo::rewarding_bridge_signed_extension::tip(extra)),
)
}
}

Expand Down
6 changes: 4 additions & 2 deletions relays/client-bridge-hub-wococo/src/runtime_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
use codec::{Decode, Encode};
use scale_info::TypeInfo;

use bp_bridge_hub_wococo::SignedExtension;
pub use bp_header_chain::BridgeGrandpaCallOf;
pub use bp_parachains::BridgeParachainCall;
pub use bridge_runtime_common::messages::BridgeMessagesCallOf;
pub use relay_substrate_client::calls::{SystemCall, UtilityCall};

/// Unchecked BridgeHubWococo extrinsic.
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<Call, SignedExtension>;
pub type UncheckedExtrinsic = bp_bridge_hub_wococo::UncheckedExtrinsic<
Call,
bp_bridge_hub_wococo::rewarding_bridge_signed_extension::RewardingBridgeSignedExtension,
>;

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