mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 14:01:02 +00:00
Remove messed up bridges subtree
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
[package]
|
||||
name = "bp-currency-exchange"
|
||||
description = "Primitives of currency exchange module."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-support = { 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-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"sp-api/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,150 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Generated by `DecodeLimit::decode_with_depth_limit`
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use codec::{Decode, Encode, EncodeLike};
|
||||
use frame_support::{Parameter, RuntimeDebug};
|
||||
use sp_api::decl_runtime_apis;
|
||||
use sp_std::marker::PhantomData;
|
||||
|
||||
/// All errors that may happen during exchange.
|
||||
#[derive(RuntimeDebug, PartialEq)]
|
||||
pub enum Error {
|
||||
/// Invalid peer blockchain transaction provided.
|
||||
InvalidTransaction,
|
||||
/// Peer transaction has invalid amount.
|
||||
InvalidAmount,
|
||||
/// Peer transaction has invalid recipient.
|
||||
InvalidRecipient,
|
||||
/// Cannot map from peer recipient to this blockchain recipient.
|
||||
FailedToMapRecipients,
|
||||
/// Failed to convert from peer blockchain currency to this blockhain currency.
|
||||
FailedToConvertCurrency,
|
||||
/// Deposit has failed.
|
||||
DepositFailed,
|
||||
/// Deposit has partially failed (changes to recipient account were made).
|
||||
DepositPartiallyFailed,
|
||||
}
|
||||
|
||||
/// Result of all exchange operations.
|
||||
pub type Result<T> = sp_std::result::Result<T, Error>;
|
||||
|
||||
/// Peer blockchain lock funds transaction.
|
||||
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
|
||||
pub struct LockFundsTransaction<TransferId, Recipient, Amount> {
|
||||
/// Something that uniquely identifies this transfer.
|
||||
pub id: TransferId,
|
||||
/// Funds recipient on the peer chain.
|
||||
pub recipient: Recipient,
|
||||
/// Amount of the locked funds.
|
||||
pub amount: Amount,
|
||||
}
|
||||
|
||||
/// Peer blockchain transaction that may represent lock funds transaction.
|
||||
pub trait MaybeLockFundsTransaction {
|
||||
/// Transaction type.
|
||||
type Transaction;
|
||||
/// Identifier that uniquely identifies this transfer.
|
||||
type Id: Decode + Encode + EncodeLike + sp_std::fmt::Debug;
|
||||
/// Peer recipient type.
|
||||
type Recipient;
|
||||
/// Peer currency amount type.
|
||||
type Amount;
|
||||
|
||||
/// Parse lock funds transaction of the peer blockchain. Returns None if
|
||||
/// transaction format is unknown, or it isn't a lock funds transaction.
|
||||
fn parse(tx: &Self::Transaction) -> Result<LockFundsTransaction<Self::Id, Self::Recipient, Self::Amount>>;
|
||||
}
|
||||
|
||||
/// Map that maps recipients from peer blockchain to this blockchain recipients.
|
||||
pub trait RecipientsMap {
|
||||
/// Peer blockchain recipient type.
|
||||
type PeerRecipient;
|
||||
/// Current blockchain recipient type.
|
||||
type Recipient;
|
||||
|
||||
/// Lookup current blockchain recipient by peer blockchain recipient.
|
||||
fn map(peer_recipient: Self::PeerRecipient) -> Result<Self::Recipient>;
|
||||
}
|
||||
|
||||
/// Conversion between two currencies.
|
||||
pub trait CurrencyConverter {
|
||||
/// Type of the source currency amount.
|
||||
type SourceAmount;
|
||||
/// Type of the target currency amount.
|
||||
type TargetAmount;
|
||||
|
||||
/// Covert from source to target currency.
|
||||
fn convert(amount: Self::SourceAmount) -> Result<Self::TargetAmount>;
|
||||
}
|
||||
|
||||
/// Currency deposit.
|
||||
pub trait DepositInto {
|
||||
/// Recipient type.
|
||||
type Recipient;
|
||||
/// Currency amount type.
|
||||
type Amount;
|
||||
|
||||
/// Grant some money to given account.
|
||||
fn deposit_into(recipient: Self::Recipient, amount: Self::Amount) -> Result<()>;
|
||||
}
|
||||
|
||||
/// Recipients map which is used when accounts ids are the same on both chains.
|
||||
#[derive(Debug)]
|
||||
pub struct IdentityRecipients<AccountId>(PhantomData<AccountId>);
|
||||
|
||||
impl<AccountId> RecipientsMap for IdentityRecipients<AccountId> {
|
||||
type PeerRecipient = AccountId;
|
||||
type Recipient = AccountId;
|
||||
|
||||
fn map(peer_recipient: Self::PeerRecipient) -> Result<Self::Recipient> {
|
||||
Ok(peer_recipient)
|
||||
}
|
||||
}
|
||||
|
||||
/// Currency converter which is used when currency is the same on both chains.
|
||||
#[derive(Debug)]
|
||||
pub struct IdentityCurrencyConverter<Amount>(PhantomData<Amount>);
|
||||
|
||||
impl<Amount> CurrencyConverter for IdentityCurrencyConverter<Amount> {
|
||||
type SourceAmount = Amount;
|
||||
type TargetAmount = Amount;
|
||||
|
||||
fn convert(currency: Self::SourceAmount) -> Result<Self::TargetAmount> {
|
||||
Ok(currency)
|
||||
}
|
||||
}
|
||||
|
||||
decl_runtime_apis! {
|
||||
/// API for Rialto exchange transactions submitters.
|
||||
pub trait RialtoCurrencyExchangeApi<Proof: Parameter> {
|
||||
/// Returns true if currency exchange module is able to import transaction proof in
|
||||
/// its current state.
|
||||
fn filter_transaction_proof(proof: Proof) -> bool;
|
||||
}
|
||||
|
||||
/// API for Kovan exchange transactions submitters.
|
||||
pub trait KovanCurrencyExchangeApi<Proof: Parameter> {
|
||||
/// Returns true if currency exchange module is able to import transaction proof in
|
||||
/// its current state.
|
||||
fn filter_transaction_proof(proof: Proof) -> bool;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
[package]
|
||||
name = "bp-eth-poa"
|
||||
description = "Primitives of Ethereum PoA Bridge module."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
ethbloom = { version = "0.10.0", default-features = false, features = ["rlp"] }
|
||||
fixed-hash = { version = "0.7", default-features = false }
|
||||
hash-db = { version = "0.15.2", default-features = false }
|
||||
impl-rlp = { version = "0.3", default-features = false }
|
||||
impl-serde = { version = "0.3.1", optional = true }
|
||||
libsecp256k1 = { version = "0.3.4", default-features = false, features = ["hmac"] }
|
||||
parity-bytes = { version = "0.1", default-features = false }
|
||||
plain_hasher = { version = "0.2.2", default-features = false }
|
||||
primitive-types = { version = "0.9", default-features = false, features = ["codec", "rlp"] }
|
||||
rlp = { version = "0.5", default-features = false }
|
||||
serde = { version = "1.0", optional = true }
|
||||
serde-big-array = { version = "0.2", optional = true }
|
||||
triehash = { version = "0.8.2", default-features = false }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
hex-literal = "0.2"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"ethbloom/std",
|
||||
"fixed-hash/std",
|
||||
"hash-db/std",
|
||||
"impl-rlp/std",
|
||||
"impl-serde",
|
||||
"libsecp256k1/std",
|
||||
"parity-bytes/std",
|
||||
"plain_hasher/std",
|
||||
"primitive-types/std",
|
||||
"primitive-types/serde",
|
||||
"rlp/std",
|
||||
"serde/std",
|
||||
"serde-big-array",
|
||||
"sp-api/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"triehash/std",
|
||||
]
|
||||
@@ -1,734 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Generated by `DecodeLimit::decode_with_depth_limit`
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
pub use parity_bytes::Bytes;
|
||||
pub use primitive_types::{H160, H256, H512, U128, U256};
|
||||
pub use rlp::encode as rlp_encode;
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use ethbloom::{Bloom as EthBloom, Input as BloomInput};
|
||||
use fixed_hash::construct_fixed_hash;
|
||||
use rlp::{Decodable, DecoderError, Rlp, RlpStream};
|
||||
use sp_io::hashing::keccak_256;
|
||||
use sp_runtime::RuntimeDebug;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
use impl_rlp::impl_fixed_hash_rlp;
|
||||
#[cfg(feature = "std")]
|
||||
use impl_serde::impl_fixed_hash_serde;
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "std")]
|
||||
use serde_big_array::big_array;
|
||||
|
||||
construct_fixed_hash! { pub struct H520(65); }
|
||||
impl_fixed_hash_rlp!(H520, 65);
|
||||
#[cfg(feature = "std")]
|
||||
impl_fixed_hash_serde!(H520, 65);
|
||||
|
||||
/// Raw (RLP-encoded) ethereum transaction.
|
||||
pub type RawTransaction = Vec<u8>;
|
||||
|
||||
/// Raw (RLP-encoded) ethereum transaction receipt.
|
||||
pub type RawTransactionReceipt = Vec<u8>;
|
||||
|
||||
/// An ethereum address.
|
||||
pub type Address = H160;
|
||||
|
||||
pub mod signatures;
|
||||
|
||||
/// Complete header id.
|
||||
#[derive(Encode, Decode, Default, RuntimeDebug, PartialEq, Clone, Copy)]
|
||||
pub struct HeaderId {
|
||||
/// Header number.
|
||||
pub number: u64,
|
||||
/// Header hash.
|
||||
pub hash: H256,
|
||||
}
|
||||
|
||||
/// An Aura header.
|
||||
#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct AuraHeader {
|
||||
/// Parent block hash.
|
||||
pub parent_hash: H256,
|
||||
/// Block timestamp.
|
||||
pub timestamp: u64,
|
||||
/// Block number.
|
||||
pub number: u64,
|
||||
/// Block author.
|
||||
pub author: Address,
|
||||
|
||||
/// Transactions root.
|
||||
pub transactions_root: H256,
|
||||
/// Block uncles hash.
|
||||
pub uncles_hash: H256,
|
||||
/// Block extra data.
|
||||
pub extra_data: Bytes,
|
||||
|
||||
/// State root.
|
||||
pub state_root: H256,
|
||||
/// Block receipts root.
|
||||
pub receipts_root: H256,
|
||||
/// Block bloom.
|
||||
pub log_bloom: Bloom,
|
||||
/// Gas used for contracts execution.
|
||||
pub gas_used: U256,
|
||||
/// Block gas limit.
|
||||
pub gas_limit: U256,
|
||||
|
||||
/// Block difficulty.
|
||||
pub difficulty: U256,
|
||||
/// Vector of post-RLP-encoded fields.
|
||||
pub seal: Vec<Bytes>,
|
||||
}
|
||||
|
||||
/// Parsed ethereum transaction.
|
||||
#[derive(PartialEq, RuntimeDebug)]
|
||||
pub struct Transaction {
|
||||
/// Sender address.
|
||||
pub sender: Address,
|
||||
/// Unsigned portion of ethereum transaction.
|
||||
pub unsigned: UnsignedTransaction,
|
||||
}
|
||||
|
||||
/// Unsigned portion of ethereum transaction.
|
||||
#[derive(Clone, PartialEq, RuntimeDebug)]
|
||||
pub struct UnsignedTransaction {
|
||||
/// Sender nonce.
|
||||
pub nonce: U256,
|
||||
/// Gas price.
|
||||
pub gas_price: U256,
|
||||
/// Gas limit.
|
||||
pub gas: U256,
|
||||
/// Transaction destination address. None if it is contract creation transaction.
|
||||
pub to: Option<Address>,
|
||||
/// Value.
|
||||
pub value: U256,
|
||||
/// Associated data.
|
||||
pub payload: Bytes,
|
||||
}
|
||||
|
||||
/// Information describing execution of a transaction.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
pub struct Receipt {
|
||||
/// The total gas used in the block following execution of the transaction.
|
||||
pub gas_used: U256,
|
||||
/// The OR-wide combination of all logs' blooms for this transaction.
|
||||
pub log_bloom: Bloom,
|
||||
/// The logs stemming from this transaction.
|
||||
pub logs: Vec<LogEntry>,
|
||||
/// Transaction outcome.
|
||||
pub outcome: TransactionOutcome,
|
||||
}
|
||||
|
||||
/// Transaction outcome store in the receipt.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
pub enum TransactionOutcome {
|
||||
/// Status and state root are unknown under EIP-98 rules.
|
||||
Unknown,
|
||||
/// State root is known. Pre EIP-98 and EIP-658 rules.
|
||||
StateRoot(H256),
|
||||
/// Status code is known. EIP-658 rules.
|
||||
StatusCode(u8),
|
||||
}
|
||||
|
||||
/// A record of execution for a `LOG` operation.
|
||||
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
|
||||
pub struct LogEntry {
|
||||
/// The address of the contract executing at the point of the `LOG` operation.
|
||||
pub address: Address,
|
||||
/// The topics associated with the `LOG` operation.
|
||||
pub topics: Vec<H256>,
|
||||
/// The data associated with the `LOG` operation.
|
||||
pub data: Bytes,
|
||||
}
|
||||
|
||||
/// Logs bloom.
|
||||
#[derive(Clone, Encode, Decode)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct Bloom(#[cfg_attr(feature = "std", serde(with = "BigArray"))] [u8; 256]);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
big_array! { BigArray; }
|
||||
|
||||
/// An empty step message that is included in a seal, the only difference is that it doesn't include
|
||||
/// the `parent_hash` in order to save space. The included signature is of the original empty step
|
||||
/// message, which can be reconstructed by using the parent hash of the block in which this sealed
|
||||
/// empty message is included.
|
||||
pub struct SealedEmptyStep {
|
||||
/// Signature of the original message author.
|
||||
pub signature: H520,
|
||||
/// The step this message is generated for.
|
||||
pub step: u64,
|
||||
}
|
||||
|
||||
impl AuraHeader {
|
||||
/// Compute id of this header.
|
||||
pub fn compute_id(&self) -> HeaderId {
|
||||
HeaderId {
|
||||
number: self.number,
|
||||
hash: self.compute_hash(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute hash of this header (keccak of the RLP with seal).
|
||||
pub fn compute_hash(&self) -> H256 {
|
||||
keccak_256(&self.rlp(true)).into()
|
||||
}
|
||||
|
||||
/// Get id of this header' parent. Returns None if this is genesis header.
|
||||
pub fn parent_id(&self) -> Option<HeaderId> {
|
||||
self.number.checked_sub(1).map(|parent_number| HeaderId {
|
||||
number: parent_number,
|
||||
hash: self.parent_hash,
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if passed transactions receipts are matching receipts root in this header.
|
||||
/// Returns Ok(computed-root) if check succeeds.
|
||||
/// Returns Err(computed-root) if check fails.
|
||||
pub fn check_receipts_root(&self, receipts: &[Receipt]) -> Result<H256, H256> {
|
||||
check_merkle_proof(self.receipts_root, receipts.iter().map(|r| r.rlp()))
|
||||
}
|
||||
|
||||
/// Check if passed raw transactions receipts are matching receipts root in this header.
|
||||
/// Returns Ok(computed-root) if check succeeds.
|
||||
/// Returns Err(computed-root) if check fails.
|
||||
pub fn check_raw_receipts_root<'a>(
|
||||
&self,
|
||||
receipts: impl IntoIterator<Item = &'a RawTransactionReceipt>,
|
||||
) -> Result<H256, H256> {
|
||||
check_merkle_proof(self.receipts_root, receipts.into_iter())
|
||||
}
|
||||
|
||||
/// Check if passed transactions are matching transactions root in this header.
|
||||
/// Returns Ok(computed-root) if check succeeds.
|
||||
/// Returns Err(computed-root) if check fails.
|
||||
pub fn check_transactions_root<'a>(
|
||||
&self,
|
||||
transactions: impl IntoIterator<Item = &'a RawTransaction>,
|
||||
) -> Result<H256, H256> {
|
||||
check_merkle_proof(self.transactions_root, transactions.into_iter())
|
||||
}
|
||||
|
||||
/// Gets the seal hash of this header.
|
||||
pub fn seal_hash(&self, include_empty_steps: bool) -> Option<H256> {
|
||||
Some(match include_empty_steps {
|
||||
true => {
|
||||
let mut message = self.compute_hash().as_bytes().to_vec();
|
||||
message.extend_from_slice(self.seal.get(2)?);
|
||||
keccak_256(&message).into()
|
||||
}
|
||||
false => keccak_256(&self.rlp(false)).into(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get step this header is generated for.
|
||||
pub fn step(&self) -> Option<u64> {
|
||||
self.seal.get(0).map(|x| Rlp::new(&x)).and_then(|x| x.as_val().ok())
|
||||
}
|
||||
|
||||
/// Get header author' signature.
|
||||
pub fn signature(&self) -> Option<H520> {
|
||||
self.seal.get(1).and_then(|x| Rlp::new(x).as_val().ok())
|
||||
}
|
||||
|
||||
/// Extracts the empty steps from the header seal.
|
||||
pub fn empty_steps(&self) -> Option<Vec<SealedEmptyStep>> {
|
||||
self.seal
|
||||
.get(2)
|
||||
.and_then(|x| Rlp::new(x).as_list::<SealedEmptyStep>().ok())
|
||||
}
|
||||
|
||||
/// Returns header RLP with or without seals.
|
||||
fn rlp(&self, with_seal: bool) -> Bytes {
|
||||
let mut s = RlpStream::new();
|
||||
if with_seal {
|
||||
s.begin_list(13 + self.seal.len());
|
||||
} else {
|
||||
s.begin_list(13);
|
||||
}
|
||||
|
||||
s.append(&self.parent_hash);
|
||||
s.append(&self.uncles_hash);
|
||||
s.append(&self.author);
|
||||
s.append(&self.state_root);
|
||||
s.append(&self.transactions_root);
|
||||
s.append(&self.receipts_root);
|
||||
s.append(&EthBloom::from(self.log_bloom.0));
|
||||
s.append(&self.difficulty);
|
||||
s.append(&self.number);
|
||||
s.append(&self.gas_limit);
|
||||
s.append(&self.gas_used);
|
||||
s.append(&self.timestamp);
|
||||
s.append(&self.extra_data);
|
||||
|
||||
if with_seal {
|
||||
for b in &self.seal {
|
||||
s.append_raw(b, 1);
|
||||
}
|
||||
}
|
||||
|
||||
s.out().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl UnsignedTransaction {
|
||||
/// Decode unsigned portion of raw transaction RLP.
|
||||
pub fn decode_rlp(raw_tx: &[u8]) -> Result<Self, DecoderError> {
|
||||
let tx_rlp = Rlp::new(raw_tx);
|
||||
let to = tx_rlp.at(3)?;
|
||||
Ok(UnsignedTransaction {
|
||||
nonce: tx_rlp.val_at(0)?,
|
||||
gas_price: tx_rlp.val_at(1)?,
|
||||
gas: tx_rlp.val_at(2)?,
|
||||
to: match to.is_empty() {
|
||||
false => Some(to.as_val()?),
|
||||
true => None,
|
||||
},
|
||||
value: tx_rlp.val_at(4)?,
|
||||
payload: tx_rlp.val_at(5)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns message that has to be signed to sign this transaction.
|
||||
pub fn message(&self, chain_id: Option<u64>) -> H256 {
|
||||
keccak_256(&self.rlp(chain_id)).into()
|
||||
}
|
||||
|
||||
/// Returns unsigned transaction RLP.
|
||||
pub fn rlp(&self, chain_id: Option<u64>) -> Bytes {
|
||||
let mut stream = RlpStream::new_list(if chain_id.is_some() { 9 } else { 6 });
|
||||
self.rlp_to(chain_id, &mut stream);
|
||||
stream.out().to_vec()
|
||||
}
|
||||
|
||||
/// Encode to given rlp stream.
|
||||
pub fn rlp_to(&self, chain_id: Option<u64>, stream: &mut RlpStream) {
|
||||
stream.append(&self.nonce);
|
||||
stream.append(&self.gas_price);
|
||||
stream.append(&self.gas);
|
||||
match self.to {
|
||||
Some(to) => stream.append(&to),
|
||||
None => stream.append(&""),
|
||||
};
|
||||
stream.append(&self.value);
|
||||
stream.append(&self.payload);
|
||||
if let Some(chain_id) = chain_id {
|
||||
stream.append(&chain_id);
|
||||
stream.append(&0u8);
|
||||
stream.append(&0u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Receipt {
|
||||
/// Decode status from raw transaction receipt RLP.
|
||||
pub fn is_successful_raw_receipt(raw_receipt: &[u8]) -> Result<bool, DecoderError> {
|
||||
let rlp = Rlp::new(raw_receipt);
|
||||
if rlp.item_count()? == 3 {
|
||||
// no outcome - invalid tx?
|
||||
Ok(false)
|
||||
} else {
|
||||
let first = rlp.at(0)?;
|
||||
if first.is_data() && first.data()?.len() <= 1 {
|
||||
// EIP-658 transaction - status of successful transaction is 1
|
||||
let status: u8 = first.as_val()?;
|
||||
Ok(status == 1)
|
||||
} else {
|
||||
// pre-EIP-658 transaction - we do not support this kind of transactions
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns receipt RLP.
|
||||
pub fn rlp(&self) -> Bytes {
|
||||
let mut s = RlpStream::new();
|
||||
match self.outcome {
|
||||
TransactionOutcome::Unknown => {
|
||||
s.begin_list(3);
|
||||
}
|
||||
TransactionOutcome::StateRoot(ref root) => {
|
||||
s.begin_list(4);
|
||||
s.append(root);
|
||||
}
|
||||
TransactionOutcome::StatusCode(ref status_code) => {
|
||||
s.begin_list(4);
|
||||
s.append(status_code);
|
||||
}
|
||||
}
|
||||
s.append(&self.gas_used);
|
||||
s.append(&EthBloom::from(self.log_bloom.0));
|
||||
|
||||
s.begin_list(self.logs.len());
|
||||
for log in &self.logs {
|
||||
s.begin_list(3);
|
||||
s.append(&log.address);
|
||||
s.begin_list(log.topics.len());
|
||||
for topic in &log.topics {
|
||||
s.append(topic);
|
||||
}
|
||||
s.append(&log.data);
|
||||
}
|
||||
|
||||
s.out().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl SealedEmptyStep {
|
||||
/// Returns message that has to be signed by the validator.
|
||||
pub fn message(&self, parent_hash: &H256) -> H256 {
|
||||
let mut message = RlpStream::new_list(2);
|
||||
message.append(&self.step);
|
||||
message.append(parent_hash);
|
||||
keccak_256(&message.out()).into()
|
||||
}
|
||||
|
||||
/// Returns rlp for the vector of empty steps (we only do encoding in tests).
|
||||
pub fn rlp_of(empty_steps: &[SealedEmptyStep]) -> Bytes {
|
||||
let mut s = RlpStream::new();
|
||||
s.begin_list(empty_steps.len());
|
||||
for empty_step in empty_steps {
|
||||
s.begin_list(2).append(&empty_step.signature).append(&empty_step.step);
|
||||
}
|
||||
s.out().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for SealedEmptyStep {
|
||||
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
|
||||
let signature: H520 = rlp.val_at(0)?;
|
||||
let step = rlp.val_at(1)?;
|
||||
|
||||
Ok(SealedEmptyStep { signature, step })
|
||||
}
|
||||
}
|
||||
|
||||
impl LogEntry {
|
||||
/// Calculates the bloom of this log entry.
|
||||
pub fn bloom(&self) -> Bloom {
|
||||
let eth_bloom =
|
||||
self.topics
|
||||
.iter()
|
||||
.fold(EthBloom::from(BloomInput::Raw(self.address.as_bytes())), |mut b, t| {
|
||||
b.accrue(BloomInput::Raw(t.as_bytes()));
|
||||
b
|
||||
});
|
||||
Bloom(*eth_bloom.data())
|
||||
}
|
||||
}
|
||||
|
||||
impl Bloom {
|
||||
/// Returns true if this bloom has all bits from the other set.
|
||||
pub fn contains(&self, other: &Bloom) -> bool {
|
||||
self.0.iter().zip(other.0.iter()).all(|(l, r)| (l & r) == *r)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u8; 256]> for Bloom {
|
||||
fn from(buffer: &'a [u8; 256]) -> Bloom {
|
||||
Bloom(*buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Bloom> for Bloom {
|
||||
fn eq(&self, other: &Bloom) -> bool {
|
||||
self.0.iter().zip(other.0.iter()).all(|(l, r)| l == r)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Bloom {
|
||||
fn default() -> Self {
|
||||
Bloom([0; 256])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::fmt::Debug for Bloom {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fmt.debug_struct("Bloom").finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode Ethereum transaction.
|
||||
pub fn transaction_decode_rlp(raw_tx: &[u8]) -> Result<Transaction, DecoderError> {
|
||||
// parse transaction fields
|
||||
let unsigned = UnsignedTransaction::decode_rlp(raw_tx)?;
|
||||
let tx_rlp = Rlp::new(raw_tx);
|
||||
let v: u64 = tx_rlp.val_at(6)?;
|
||||
let r: U256 = tx_rlp.val_at(7)?;
|
||||
let s: U256 = tx_rlp.val_at(8)?;
|
||||
|
||||
// reconstruct signature
|
||||
let mut signature = [0u8; 65];
|
||||
let (chain_id, v) = match v {
|
||||
v if v == 27u64 => (None, 0),
|
||||
v if v == 28u64 => (None, 1),
|
||||
v if v >= 35u64 => (Some((v - 35) / 2), ((v - 1) % 2) as u8),
|
||||
_ => (None, 4),
|
||||
};
|
||||
r.to_big_endian(&mut signature[0..32]);
|
||||
s.to_big_endian(&mut signature[32..64]);
|
||||
signature[64] = v;
|
||||
|
||||
// reconstruct message that has been signed
|
||||
let message = unsigned.message(chain_id);
|
||||
|
||||
// recover tx sender
|
||||
let sender_public = sp_io::crypto::secp256k1_ecdsa_recover(&signature, &message.as_fixed_bytes())
|
||||
.map_err(|_| rlp::DecoderError::Custom("Failed to recover transaction sender"))?;
|
||||
let sender_address = public_to_address(&sender_public);
|
||||
|
||||
Ok(Transaction {
|
||||
sender: sender_address,
|
||||
unsigned,
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert public key into corresponding ethereum address.
|
||||
pub fn public_to_address(public: &[u8; 64]) -> Address {
|
||||
let hash = keccak_256(public);
|
||||
let mut result = Address::zero();
|
||||
result.as_bytes_mut().copy_from_slice(&hash[12..]);
|
||||
result
|
||||
}
|
||||
|
||||
/// Check ethereum merkle proof.
|
||||
/// Returns Ok(computed-root) if check succeeds.
|
||||
/// Returns Err(computed-root) if check fails.
|
||||
fn check_merkle_proof<T: AsRef<[u8]>>(expected_root: H256, items: impl Iterator<Item = T>) -> Result<H256, H256> {
|
||||
let computed_root = compute_merkle_root(items);
|
||||
if computed_root == expected_root {
|
||||
Ok(computed_root)
|
||||
} else {
|
||||
Err(computed_root)
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute ethereum merkle root.
|
||||
pub fn compute_merkle_root<T: AsRef<[u8]>>(items: impl Iterator<Item = T>) -> H256 {
|
||||
struct Keccak256Hasher;
|
||||
|
||||
impl hash_db::Hasher for Keccak256Hasher {
|
||||
type Out = H256;
|
||||
type StdHasher = plain_hasher::PlainHasher;
|
||||
const LENGTH: usize = 32;
|
||||
fn hash(x: &[u8]) -> Self::Out {
|
||||
keccak_256(x).into()
|
||||
}
|
||||
}
|
||||
|
||||
triehash::ordered_trie_root::<Keccak256Hasher, _>(items)
|
||||
}
|
||||
|
||||
/// Get validator that should author the block at given step.
|
||||
pub fn step_validator<T>(header_validators: &[T], header_step: u64) -> &T {
|
||||
&header_validators[(header_step % header_validators.len() as u64) as usize]
|
||||
}
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for querying information about headers from the Rialto Bridge Pallet
|
||||
pub trait RialtoPoAHeaderApi {
|
||||
/// Returns number and hash of the best block known to the bridge module.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_block() -> (u64, H256);
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (u64, H256);
|
||||
/// Returns true if the import of given block requires transactions receipts.
|
||||
fn is_import_requires_receipts(header: AuraHeader) -> bool;
|
||||
/// Returns true if header is known to the runtime.
|
||||
fn is_known_block(hash: H256) -> bool;
|
||||
}
|
||||
|
||||
/// API for querying information about headers from the Kovan Bridge Pallet
|
||||
pub trait KovanHeaderApi {
|
||||
/// Returns number and hash of the best block known to the bridge module.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_block() -> (u64, H256);
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (u64, H256);
|
||||
/// Returns true if the import of given block requires transactions receipts.
|
||||
fn is_import_requires_receipts(header: AuraHeader) -> bool;
|
||||
/// Returns true if header is known to the runtime.
|
||||
fn is_known_block(hash: H256) -> bool;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use hex_literal::hex;
|
||||
|
||||
#[test]
|
||||
fn transfer_transaction_decode_works() {
|
||||
// value transfer transaction
|
||||
// https://etherscan.io/tx/0xb9d4ad5408f53eac8627f9ccd840ba8fb3469d55cd9cc2a11c6e049f1eef4edd
|
||||
// https://etherscan.io/getRawTx?tx=0xb9d4ad5408f53eac8627f9ccd840ba8fb3469d55cd9cc2a11c6e049f1eef4edd
|
||||
let raw_tx = hex!("f86c0a85046c7cfe0083016dea94d1310c1e038bc12865d3d3997275b3e4737c6302880b503be34d9fe80080269fc7eaaa9c21f59adf8ad43ed66cf5ef9ee1c317bd4d32cd65401e7aaca47cfaa0387d79c65b90be6260d09dcfb780f29dd8133b9b1ceb20b83b7e442b4bfc30cb");
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: hex!("67835910d32600471f388a137bbff3eb07993c04").into(),
|
||||
unsigned: UnsignedTransaction {
|
||||
nonce: 10.into(),
|
||||
gas_price: 19000000000u64.into(),
|
||||
gas: 93674.into(),
|
||||
to: Some(hex!("d1310c1e038bc12865d3d3997275b3e4737c6302").into()),
|
||||
value: 815217380000000000_u64.into(),
|
||||
payload: Default::default(),
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Kovan value transfer transaction
|
||||
// https://kovan.etherscan.io/tx/0x3b4b7bd41c1178045ccb4753aa84c1ef9864b4d712fa308b228917cd837915da
|
||||
// https://kovan.etherscan.io/getRawTx?tx=0x3b4b7bd41c1178045ccb4753aa84c1ef9864b4d712fa308b228917cd837915da
|
||||
let raw_tx = hex!("f86a822816808252089470c1ccde719d6f477084f07e4137ab0e55f8369f8930cf46e92063afd8008078a00e4d1f4d8aa992bda3c105ff3d6e9b9acbfd99facea00985e2131029290adbdca028ea29a46a4b66ec65b454f0706228e3768cb0ecf755f67c50ddd472f11d5994");
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: hex!("faadface3fbd81ce37b0e19c0b65ff4234148132").into(),
|
||||
unsigned: UnsignedTransaction {
|
||||
nonce: 10262.into(),
|
||||
gas_price: 0.into(),
|
||||
gas: 21000.into(),
|
||||
to: Some(hex!("70c1ccde719d6f477084f07e4137ab0e55f8369f").into()),
|
||||
value: 900379597077600000000_u128.into(),
|
||||
payload: Default::default(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_transaction_decode_works() {
|
||||
// contract call transaction
|
||||
// https://etherscan.io/tx/0xdc2b996b4d1d6922bf6dba063bfd70913279cb6170967c9bb80252aeb061cf65
|
||||
// https://etherscan.io/getRawTx?tx=0xdc2b996b4d1d6922bf6dba063bfd70913279cb6170967c9bb80252aeb061cf65
|
||||
let raw_tx = hex!("f8aa76850430e234008301500094dac17f958d2ee523a2206206994597c13d831ec780b844a9059cbb000000000000000000000000e08f35f66867a454835b25118f1e490e7f9e9a7400000000000000000000000000000000000000000000000000000000004c4b4025a0964e023999621dc3d4d831c43c71f7555beb6d1192dee81a3674b3f57e310f21a00f229edd86f841d1ee4dc48cc16667e2283817b1d39bae16ced10cd206ae4fd4");
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: hex!("2b9a4d37bdeecdf994c4c9ad7f3cf8dc632f7d70").into(),
|
||||
unsigned: UnsignedTransaction {
|
||||
nonce: 118.into(),
|
||||
gas_price: 18000000000u64.into(),
|
||||
gas: 86016.into(),
|
||||
to: Some(hex!("dac17f958d2ee523a2206206994597c13d831ec7").into()),
|
||||
value: 0.into(),
|
||||
payload: hex!("a9059cbb000000000000000000000000e08f35f66867a454835b25118f1e490e7f9e9a7400000000000000000000000000000000000000000000000000000000004c4b40").to_vec(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// Kovan contract call transaction
|
||||
// https://kovan.etherscan.io/tx/0x2904b4451d23665492239016b78da052d40d55fdebc7304b38e53cf6a37322cf
|
||||
// https://kovan.etherscan.io/getRawTx?tx=0x2904b4451d23665492239016b78da052d40d55fdebc7304b38e53cf6a37322cf
|
||||
let raw_tx = hex!("f8ac8302200b843b9aca00830271009484dd11eb2a29615303d18149c0dbfa24167f896680b844a9059cbb00000000000000000000000001503dfc5ad81bf630d83697e98601871bb211b600000000000000000000000000000000000000000000000000000000000027101ba0ce126d2cca81f5e245f292ff84a0d915c0a4ac52af5c51219db1e5d36aa8da35a0045298b79dac631907403888f9b04c2ab5509fe0cc31785276d30a40b915fcf9");
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: hex!("617da121abf03d4c1af572f5a4e313e26bef7bdc").into(),
|
||||
unsigned: UnsignedTransaction {
|
||||
nonce: 139275.into(),
|
||||
gas_price: 1000000000.into(),
|
||||
gas: 160000.into(),
|
||||
to: Some(hex!("84dd11eb2a29615303d18149c0dbfa24167f8966").into()),
|
||||
value: 0.into(),
|
||||
payload: hex!("a9059cbb00000000000000000000000001503dfc5ad81bf630d83697e98601871bb211b60000000000000000000000000000000000000000000000000000000000002710").to_vec(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_successful_raw_receipt_works() {
|
||||
assert!(Receipt::is_successful_raw_receipt(&[]).is_err());
|
||||
|
||||
assert_eq!(
|
||||
Receipt::is_successful_raw_receipt(
|
||||
&Receipt {
|
||||
outcome: TransactionOutcome::Unknown,
|
||||
gas_used: Default::default(),
|
||||
log_bloom: Default::default(),
|
||||
logs: Vec::new(),
|
||||
}
|
||||
.rlp()
|
||||
),
|
||||
Ok(false),
|
||||
);
|
||||
assert_eq!(
|
||||
Receipt::is_successful_raw_receipt(
|
||||
&Receipt {
|
||||
outcome: TransactionOutcome::StateRoot(Default::default()),
|
||||
gas_used: Default::default(),
|
||||
log_bloom: Default::default(),
|
||||
logs: Vec::new(),
|
||||
}
|
||||
.rlp()
|
||||
),
|
||||
Ok(false),
|
||||
);
|
||||
assert_eq!(
|
||||
Receipt::is_successful_raw_receipt(
|
||||
&Receipt {
|
||||
outcome: TransactionOutcome::StatusCode(0),
|
||||
gas_used: Default::default(),
|
||||
log_bloom: Default::default(),
|
||||
logs: Vec::new(),
|
||||
}
|
||||
.rlp()
|
||||
),
|
||||
Ok(false),
|
||||
);
|
||||
assert_eq!(
|
||||
Receipt::is_successful_raw_receipt(
|
||||
&Receipt {
|
||||
outcome: TransactionOutcome::StatusCode(1),
|
||||
gas_used: Default::default(),
|
||||
log_bloom: Default::default(),
|
||||
logs: Vec::new(),
|
||||
}
|
||||
.rlp()
|
||||
),
|
||||
Ok(true),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_successful_raw_receipt_with_empty_data() {
|
||||
let mut stream = RlpStream::new();
|
||||
stream.begin_list(4);
|
||||
stream.append_empty_data();
|
||||
stream.append(&1u64);
|
||||
stream.append(&2u64);
|
||||
stream.append(&3u64);
|
||||
|
||||
assert_eq!(Receipt::is_successful_raw_receipt(&stream.out()), Ok(false),);
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
//
|
||||
|
||||
//! Helpers related to signatures.
|
||||
//!
|
||||
//! Used for testing and benchmarking.
|
||||
|
||||
// reexport to avoid direct secp256k1 deps by other crates
|
||||
pub use secp256k1::SecretKey;
|
||||
|
||||
use crate::{
|
||||
public_to_address, rlp_encode, step_validator, Address, AuraHeader, RawTransaction, UnsignedTransaction, H256,
|
||||
H520, U256,
|
||||
};
|
||||
|
||||
use secp256k1::{Message, PublicKey};
|
||||
|
||||
/// Utilities for signing headers.
|
||||
pub trait SignHeader {
|
||||
/// Signs header by given author.
|
||||
fn sign_by(self, author: &SecretKey) -> AuraHeader;
|
||||
/// Signs header by given authors set.
|
||||
fn sign_by_set(self, authors: &[SecretKey]) -> AuraHeader;
|
||||
}
|
||||
|
||||
/// Utilities for signing transactions.
|
||||
pub trait SignTransaction {
|
||||
/// Sign transaction by given author.
|
||||
fn sign_by(self, author: &SecretKey, chain_id: Option<u64>) -> RawTransaction;
|
||||
}
|
||||
|
||||
impl SignHeader for AuraHeader {
|
||||
fn sign_by(mut self, author: &SecretKey) -> Self {
|
||||
self.author = secret_to_address(author);
|
||||
|
||||
let message = self.seal_hash(false).unwrap();
|
||||
let signature = sign(author, message);
|
||||
self.seal[1] = rlp_encode(&signature).to_vec();
|
||||
self
|
||||
}
|
||||
|
||||
fn sign_by_set(self, authors: &[SecretKey]) -> Self {
|
||||
let step = self.step().unwrap();
|
||||
let author = step_validator(authors, step);
|
||||
self.sign_by(author)
|
||||
}
|
||||
}
|
||||
|
||||
impl SignTransaction for UnsignedTransaction {
|
||||
fn sign_by(self, author: &SecretKey, chain_id: Option<u64>) -> RawTransaction {
|
||||
let message = self.message(chain_id);
|
||||
let signature = sign(author, message);
|
||||
let signature_r = U256::from_big_endian(&signature.as_fixed_bytes()[..32][..]);
|
||||
let signature_s = U256::from_big_endian(&signature.as_fixed_bytes()[32..64][..]);
|
||||
let signature_v = signature.as_fixed_bytes()[64] as u64;
|
||||
let signature_v = signature_v + if let Some(n) = chain_id { 35 + n * 2 } else { 27 };
|
||||
|
||||
let mut stream = rlp::RlpStream::new_list(9);
|
||||
self.rlp_to(None, &mut stream);
|
||||
stream.append(&signature_v);
|
||||
stream.append(&signature_r);
|
||||
stream.append(&signature_s);
|
||||
stream.out().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
/// Return author's signature over given message.
|
||||
pub fn sign(author: &SecretKey, message: H256) -> H520 {
|
||||
let (signature, recovery_id) = secp256k1::sign(&Message::parse(message.as_fixed_bytes()), author);
|
||||
let mut raw_signature = [0u8; 65];
|
||||
raw_signature[..64].copy_from_slice(&signature.serialize());
|
||||
raw_signature[64] = recovery_id.serialize();
|
||||
raw_signature.into()
|
||||
}
|
||||
|
||||
/// Returns address corresponding to given secret key.
|
||||
pub fn secret_to_address(secret: &SecretKey) -> Address {
|
||||
let public = PublicKey::from_secret_key(secret);
|
||||
let mut raw_public = [0u8; 64];
|
||||
raw_public.copy_from_slice(&public.serialize()[1..]);
|
||||
public_to_address(&raw_public)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{transaction_decode_rlp, Transaction};
|
||||
|
||||
#[test]
|
||||
fn transaction_signed_properly() {
|
||||
// case1: with chain_id replay protection + to
|
||||
let signer = SecretKey::parse(&[1u8; 32]).unwrap();
|
||||
let signer_address = secret_to_address(&signer);
|
||||
let unsigned = UnsignedTransaction {
|
||||
nonce: 100.into(),
|
||||
gas_price: 200.into(),
|
||||
gas: 300.into(),
|
||||
to: Some([42u8; 20].into()),
|
||||
value: 400.into(),
|
||||
payload: vec![1, 2, 3],
|
||||
};
|
||||
let raw_tx = unsigned.clone().sign_by(&signer, Some(42));
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: signer_address,
|
||||
unsigned,
|
||||
}),
|
||||
);
|
||||
|
||||
// case2: without chain_id replay protection + contract creation
|
||||
let unsigned = UnsignedTransaction {
|
||||
nonce: 100.into(),
|
||||
gas_price: 200.into(),
|
||||
gas: 300.into(),
|
||||
to: None,
|
||||
value: 400.into(),
|
||||
payload: vec![1, 2, 3],
|
||||
};
|
||||
let raw_tx = unsigned.clone().sign_by(&signer, None);
|
||||
assert_eq!(
|
||||
transaction_decode_rlp(&raw_tx),
|
||||
Ok(Transaction {
|
||||
sender: signer_address,
|
||||
unsigned,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
[package]
|
||||
name = "bp-header-chain"
|
||||
description = "A common interface for describing what a bridge pallet should be able to do."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
finality-grandpa = { version = "0.14.0", default-features = false }
|
||||
serde = { version = "1.0", optional = true }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-support = { 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-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
bp-test-utils = { path = "../test-utils" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"finality-grandpa/std",
|
||||
"serde/std",
|
||||
"frame-support/std",
|
||||
"sp-core/std",
|
||||
"sp-finality-grandpa/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,183 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Module for checking GRANDPA Finality Proofs.
|
||||
//!
|
||||
//! Adapted copy of substrate/client/finality-grandpa/src/justification.rs. If origin
|
||||
//! will ever be moved to the sp_finality_grandpa, we should reuse that implementation.
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use finality_grandpa::{voter_set::VoterSet, Chain, Error as GrandpaError};
|
||||
use frame_support::RuntimeDebug;
|
||||
use sp_finality_grandpa::{AuthorityId, AuthoritySignature, SetId};
|
||||
use sp_runtime::traits::Header as HeaderT;
|
||||
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
|
||||
use sp_std::prelude::Vec;
|
||||
|
||||
/// Justification verification error.
|
||||
#[derive(RuntimeDebug, PartialEq)]
|
||||
pub enum Error {
|
||||
/// Failed to decode justification.
|
||||
JustificationDecode,
|
||||
/// Justification is finalizing unexpected header.
|
||||
InvalidJustificationTarget,
|
||||
/// Invalid commit in justification.
|
||||
InvalidJustificationCommit,
|
||||
/// Justification has invalid authority singature.
|
||||
InvalidAuthoritySignature,
|
||||
/// The justification has precommit for the header that has no route from the target header.
|
||||
InvalidPrecommitAncestryProof,
|
||||
/// The justification has 'unused' headers in its precommit ancestries.
|
||||
InvalidPrecommitAncestries,
|
||||
}
|
||||
|
||||
/// Decode justification target.
|
||||
pub fn decode_justification_target<Header: HeaderT>(
|
||||
raw_justification: &[u8],
|
||||
) -> Result<(Header::Hash, Header::Number), Error> {
|
||||
GrandpaJustification::<Header>::decode(&mut &*raw_justification)
|
||||
.map(|justification| (justification.commit.target_hash, justification.commit.target_number))
|
||||
.map_err(|_| Error::JustificationDecode)
|
||||
}
|
||||
|
||||
/// Verify that justification, that is generated by given authority set, finalizes given header.
|
||||
pub fn verify_justification<Header: HeaderT>(
|
||||
finalized_target: (Header::Hash, Header::Number),
|
||||
authorities_set_id: SetId,
|
||||
authorities_set: VoterSet<AuthorityId>,
|
||||
raw_justification: &[u8],
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
Header::Number: finality_grandpa::BlockNumberOps,
|
||||
{
|
||||
// Decode justification first
|
||||
let justification =
|
||||
GrandpaJustification::<Header>::decode(&mut &*raw_justification).map_err(|_| Error::JustificationDecode)?;
|
||||
|
||||
// Ensure that it is justification for the expected header
|
||||
if (justification.commit.target_hash, justification.commit.target_number) != finalized_target {
|
||||
return Err(Error::InvalidJustificationTarget);
|
||||
}
|
||||
|
||||
// Validate commit of the justification. Note that `validate_commit()` assumes that all
|
||||
// signatures are valid. We'll check the validity of the signatures later since they're more
|
||||
// resource intensive to verify.
|
||||
let ancestry_chain = AncestryChain::new(&justification.votes_ancestries);
|
||||
match finality_grandpa::validate_commit(&justification.commit, &authorities_set, &ancestry_chain) {
|
||||
Ok(ref result) if result.ghost().is_some() => {}
|
||||
_ => return Err(Error::InvalidJustificationCommit),
|
||||
}
|
||||
|
||||
// Now that we know that the commit is correct, check authorities signatures
|
||||
let mut buf = Vec::new();
|
||||
let mut visited_hashes = BTreeSet::new();
|
||||
for signed in &justification.commit.precommits {
|
||||
if !sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
&finality_grandpa::Message::Precommit(signed.precommit.clone()),
|
||||
&signed.id,
|
||||
&signed.signature,
|
||||
justification.round,
|
||||
authorities_set_id,
|
||||
&mut buf,
|
||||
) {
|
||||
return Err(Error::InvalidAuthoritySignature);
|
||||
}
|
||||
|
||||
if justification.commit.target_hash == signed.precommit.target_hash {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ancestry_chain.ancestry(justification.commit.target_hash, signed.precommit.target_hash) {
|
||||
Ok(route) => {
|
||||
// ancestry starts from parent hash but the precommit target hash has been visited
|
||||
visited_hashes.insert(signed.precommit.target_hash);
|
||||
visited_hashes.extend(route);
|
||||
}
|
||||
_ => {
|
||||
// could this happen in practice? I don't think so, but original code has this check
|
||||
return Err(Error::InvalidPrecommitAncestryProof);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let ancestry_hashes = justification
|
||||
.votes_ancestries
|
||||
.iter()
|
||||
.map(|h: &Header| h.hash())
|
||||
.collect();
|
||||
if visited_hashes != ancestry_hashes {
|
||||
return Err(Error::InvalidPrecommitAncestries);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A GRANDPA Justification is a proof that a given header was finalized
|
||||
/// at a certain height and with a certain set of authorities.
|
||||
///
|
||||
/// This particular proof is used to prove that headers on a bridged chain
|
||||
/// (so not our chain) have been finalized correctly.
|
||||
#[derive(Encode, Decode, RuntimeDebug)]
|
||||
pub struct GrandpaJustification<Header: HeaderT> {
|
||||
/// The round (voting period) this justification is valid for.
|
||||
pub round: u64,
|
||||
/// The set of votes for the chain which is to be finalized.
|
||||
pub commit: finality_grandpa::Commit<Header::Hash, Header::Number, AuthoritySignature, AuthorityId>,
|
||||
/// A proof that the chain of blocks in the commit are related to each other.
|
||||
pub votes_ancestries: Vec<Header>,
|
||||
}
|
||||
|
||||
/// A utility trait implementing `finality_grandpa::Chain` using a given set of headers.
|
||||
#[derive(RuntimeDebug)]
|
||||
struct AncestryChain<Header: HeaderT> {
|
||||
ancestry: BTreeMap<Header::Hash, Header::Hash>,
|
||||
}
|
||||
|
||||
impl<Header: HeaderT> AncestryChain<Header> {
|
||||
fn new(ancestry: &[Header]) -> AncestryChain<Header> {
|
||||
AncestryChain {
|
||||
ancestry: ancestry
|
||||
.iter()
|
||||
.map(|header| (header.hash(), *header.parent_hash()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Header: HeaderT> finality_grandpa::Chain<Header::Hash, Header::Number> for AncestryChain<Header>
|
||||
where
|
||||
Header::Number: finality_grandpa::BlockNumberOps,
|
||||
{
|
||||
fn ancestry(&self, base: Header::Hash, block: Header::Hash) -> Result<Vec<Header::Hash>, GrandpaError> {
|
||||
let mut route = Vec::new();
|
||||
let mut current_hash = block;
|
||||
loop {
|
||||
if current_hash == base {
|
||||
break;
|
||||
}
|
||||
match self.ancestry.get(¤t_hash).cloned() {
|
||||
Some(parent_hash) => {
|
||||
current_hash = parent_hash;
|
||||
route.push(current_hash);
|
||||
}
|
||||
_ => return Err(GrandpaError::NotDescendent),
|
||||
}
|
||||
}
|
||||
route.pop(); // remove the base
|
||||
|
||||
Ok(route)
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Defines traits which represent a common interface for Substrate pallets which want to
|
||||
//! incorporate bridge functionality.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::{Codec, Decode, Encode, EncodeLike};
|
||||
use core::clone::Clone;
|
||||
use core::cmp::Eq;
|
||||
use core::default::Default;
|
||||
use core::fmt::Debug;
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sp_finality_grandpa::{AuthorityList, SetId};
|
||||
use sp_runtime::traits::Header as HeaderT;
|
||||
use sp_runtime::RuntimeDebug;
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
pub mod justification;
|
||||
|
||||
/// A type that can be used as a parameter in a dispatchable function.
|
||||
///
|
||||
/// When using `decl_module` all arguments for call functions must implement this trait.
|
||||
pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug {}
|
||||
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug {}
|
||||
|
||||
/// A GRANDPA Authority List and ID.
|
||||
#[derive(Default, Encode, Decode, RuntimeDebug, PartialEq, Clone)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct AuthoritySet {
|
||||
/// List of GRANDPA authorities for the current round.
|
||||
pub authorities: AuthorityList,
|
||||
/// Monotonic identifier of the current GRANDPA authority set.
|
||||
pub set_id: SetId,
|
||||
}
|
||||
|
||||
impl AuthoritySet {
|
||||
/// Create a new GRANDPA Authority Set.
|
||||
pub fn new(authorities: AuthorityList, set_id: SetId) -> Self {
|
||||
Self { authorities, set_id }
|
||||
}
|
||||
}
|
||||
|
||||
/// base trait for verifying transaction inclusion proofs.
|
||||
pub trait InclusionProofVerifier {
|
||||
/// Transaction type.
|
||||
type Transaction: Parameter;
|
||||
/// Transaction inclusion proof type.
|
||||
type TransactionInclusionProof: Parameter;
|
||||
|
||||
/// Verify that transaction is a part of given block.
|
||||
///
|
||||
/// Returns Some(transaction) if proof is valid and None otherwise.
|
||||
fn verify_transaction_inclusion_proof(proof: &Self::TransactionInclusionProof) -> Option<Self::Transaction>;
|
||||
}
|
||||
|
||||
/// A trait for pallets which want to keep track of finalized headers from a bridged chain.
|
||||
pub trait HeaderChain<H, E> {
|
||||
/// Get the best finalized header known to the header chain.
|
||||
fn best_finalized() -> H;
|
||||
|
||||
/// Get the best authority set known to the header chain.
|
||||
fn authority_set() -> AuthoritySet;
|
||||
|
||||
/// Write a header finalized by GRANDPA to the underlying pallet storage.
|
||||
fn append_header(header: H);
|
||||
}
|
||||
|
||||
impl<H: Default, E> HeaderChain<H, E> for () {
|
||||
fn best_finalized() -> H {
|
||||
H::default()
|
||||
}
|
||||
|
||||
fn authority_set() -> AuthoritySet {
|
||||
AuthoritySet::default()
|
||||
}
|
||||
|
||||
fn append_header(_header: H) {}
|
||||
}
|
||||
|
||||
/// A trait for checking if a given child header is a direct descendant of an ancestor.
|
||||
pub trait AncestryChecker<H, P> {
|
||||
/// Is the child header a descendant of the ancestor header?
|
||||
fn are_ancestors(ancestor: &H, child: &H, proof: &P) -> bool;
|
||||
}
|
||||
|
||||
impl<H, P> AncestryChecker<H, P> for () {
|
||||
fn are_ancestors(_ancestor: &H, _child: &H, _proof: &P) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple ancestry checker which verifies ancestry by walking every header between `child` and
|
||||
/// `ancestor`.
|
||||
pub struct LinearAncestryChecker;
|
||||
|
||||
impl<H: HeaderT> AncestryChecker<H, Vec<H>> for LinearAncestryChecker {
|
||||
fn are_ancestors(ancestor: &H, child: &H, proof: &Vec<H>) -> bool {
|
||||
// You can't be your own parent
|
||||
if proof.len() < 2 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Let's make sure that the given headers are actually in the proof
|
||||
match proof.first() {
|
||||
Some(first) if first == ancestor => {}
|
||||
_ => return false,
|
||||
}
|
||||
|
||||
match proof.last() {
|
||||
Some(last) if last == child => {}
|
||||
_ => return false,
|
||||
}
|
||||
|
||||
// Now we actually check the proof
|
||||
for i in 1..proof.len() {
|
||||
if &proof[i - 1].hash() != proof[i].parent_hash() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bp_test_utils::test_header;
|
||||
use sp_runtime::testing::Header;
|
||||
|
||||
#[test]
|
||||
fn can_verify_ancestry_correctly() {
|
||||
let ancestor: Header = test_header(1);
|
||||
let header2: Header = test_header(2);
|
||||
let header3: Header = test_header(3);
|
||||
let child: Header = test_header(4);
|
||||
|
||||
let ancestry_proof = vec![ancestor.clone(), header2, header3, child.clone()];
|
||||
|
||||
assert!(LinearAncestryChecker::are_ancestors(&ancestor, &child, &ancestry_proof));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn does_not_verify_invalid_proof() {
|
||||
let ancestor: Header = test_header(1);
|
||||
let header2: Header = test_header(2);
|
||||
let header3: Header = test_header(3);
|
||||
let child: Header = test_header(4);
|
||||
|
||||
let ancestry_proof = vec![ancestor.clone(), header3, header2, child.clone()];
|
||||
|
||||
let invalid = !LinearAncestryChecker::are_ancestors(&ancestor, &child, &ancestry_proof);
|
||||
assert!(invalid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_is_not_allowed_to_be_its_own_ancestor() {
|
||||
let ancestor: Header = test_header(1);
|
||||
let child: Header = ancestor.clone();
|
||||
let ancestry_proof = vec![ancestor.clone()];
|
||||
|
||||
let invalid = !LinearAncestryChecker::are_ancestors(&ancestor, &child, &ancestry_proof);
|
||||
assert!(invalid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proof_is_considered_invalid_if_child_and_ancestor_do_not_match() {
|
||||
let ancestor: Header = test_header(1);
|
||||
let header2: Header = test_header(2);
|
||||
let header3: Header = test_header(3);
|
||||
let child: Header = test_header(4);
|
||||
|
||||
let ancestry_proof = vec![ancestor, header3.clone(), header2.clone(), child];
|
||||
|
||||
let invalid = !LinearAncestryChecker::are_ancestors(&header2, &header3, &ancestry_proof);
|
||||
assert!(invalid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_proof_is_invalid() {
|
||||
let ancestor: Header = test_header(1);
|
||||
let child: Header = ancestor.clone();
|
||||
let ancestry_proof = vec![];
|
||||
|
||||
let invalid = !LinearAncestryChecker::are_ancestors(&ancestor, &child, &ancestry_proof);
|
||||
assert!(invalid);
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Tests for Grandpa Justification code.
|
||||
|
||||
use bp_header_chain::justification::{verify_justification, Error, GrandpaJustification};
|
||||
use bp_test_utils::*;
|
||||
use codec::Encode;
|
||||
|
||||
type TestHeader = sp_runtime::testing::Header;
|
||||
|
||||
fn make_justification_for_header_1() -> GrandpaJustification<TestHeader> {
|
||||
make_justification_for_header(
|
||||
&test_header(1),
|
||||
TEST_GRANDPA_ROUND,
|
||||
TEST_GRANDPA_SET_ID,
|
||||
&authority_list(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_with_invalid_encoding_rejected() {
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(header_id::<TestHeader>(1), TEST_GRANDPA_SET_ID, voter_set(), &[],),
|
||||
Err(Error::JustificationDecode),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_with_invalid_target_rejected() {
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(
|
||||
header_id::<TestHeader>(2),
|
||||
TEST_GRANDPA_SET_ID,
|
||||
voter_set(),
|
||||
&make_justification_for_header_1().encode(),
|
||||
),
|
||||
Err(Error::InvalidJustificationTarget),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_with_invalid_commit_rejected() {
|
||||
let mut justification = make_justification_for_header_1();
|
||||
justification.commit.precommits.clear();
|
||||
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(
|
||||
header_id::<TestHeader>(1),
|
||||
TEST_GRANDPA_SET_ID,
|
||||
voter_set(),
|
||||
&justification.encode(),
|
||||
),
|
||||
Err(Error::InvalidJustificationCommit),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_with_invalid_authority_signature_rejected() {
|
||||
let mut justification = make_justification_for_header_1();
|
||||
justification.commit.precommits[0].signature = Default::default();
|
||||
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(
|
||||
header_id::<TestHeader>(1),
|
||||
TEST_GRANDPA_SET_ID,
|
||||
voter_set(),
|
||||
&justification.encode(),
|
||||
),
|
||||
Err(Error::InvalidAuthoritySignature),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn justification_with_invalid_precommit_ancestry() {
|
||||
let mut justification = make_justification_for_header_1();
|
||||
justification.votes_ancestries.push(test_header(10));
|
||||
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(
|
||||
header_id::<TestHeader>(1),
|
||||
TEST_GRANDPA_SET_ID,
|
||||
voter_set(),
|
||||
&justification.encode(),
|
||||
),
|
||||
Err(Error::InvalidPrecommitAncestries),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_justification_accepted() {
|
||||
assert_eq!(
|
||||
verify_justification::<TestHeader>(
|
||||
header_id::<TestHeader>(1),
|
||||
TEST_GRANDPA_SET_ID,
|
||||
voter_set(),
|
||||
&make_justification_for_header_1().encode(),
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
[package]
|
||||
name = "bp-kusama"
|
||||
description = "Primitives of Kusama runtime."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Bridge Dependencies
|
||||
|
||||
bp-message-lane = { path = "../message-lane", default-features = false }
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
|
||||
# Substrate Based Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
frame-system = { 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-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-message-lane/std",
|
||||
"bp-runtime/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"sp-api/std",
|
||||
"sp-core/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,164 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Runtime-generated DecodeLimit::decode_all_with_depth_limit
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use bp_message_lane::{LaneId, MessageNonce};
|
||||
use bp_runtime::Chain;
|
||||
use frame_support::{weights::Weight, RuntimeDebug};
|
||||
use sp_core::Hasher as HasherT;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, IdentifyAccount, Verify},
|
||||
MultiSignature, OpaqueExtrinsic as UncheckedExtrinsic,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
// TODO: may need to be updated after https://github.com/paritytech/parity-bridges-common/issues/78
|
||||
/// Maximal number of messages in single delivery transaction.
|
||||
pub const MAX_MESSAGES_IN_DELIVERY_TRANSACTION: MessageNonce = 128;
|
||||
|
||||
/// Maximal number of unrewarded relayer entries at inbound lane.
|
||||
pub const MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE: MessageNonce = 128;
|
||||
|
||||
// TODO: should be selected keeping in mind:
|
||||
// finality delay on both chains + reward payout cost + messages throughput.
|
||||
/// Maximal number of unconfirmed messages at inbound lane.
|
||||
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 8192;
|
||||
|
||||
/// Block number type used in Kusama.
|
||||
pub type BlockNumber = u32;
|
||||
|
||||
/// Hash type used in Kusama.
|
||||
pub type Hash = <BlakeTwo256 as HasherT>::Out;
|
||||
|
||||
/// The type of an object that can produce hashes on Kusama.
|
||||
pub type Hasher = BlakeTwo256;
|
||||
|
||||
/// The header type used by Kusama.
|
||||
pub type Header = generic::Header<BlockNumber, Hasher>;
|
||||
|
||||
/// Signature type used by Kusama.
|
||||
pub type Signature = MultiSignature;
|
||||
|
||||
/// Public key of account on Kusama chain.
|
||||
pub type AccountPublic = <Signature as Verify>::Signer;
|
||||
|
||||
/// Id of account on Kusama chain.
|
||||
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
|
||||
|
||||
/// Index of a transaction on the Kusama chain.
|
||||
pub type Nonce = u32;
|
||||
|
||||
/// Block type of Kusama.
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
/// Kusama block signed with a Justification.
|
||||
pub type SignedBlock = generic::SignedBlock<Block>;
|
||||
|
||||
/// The balance of an account on Polkadot.
|
||||
pub type Balance = u128;
|
||||
|
||||
/// Kusama chain.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct Kusama;
|
||||
|
||||
impl Chain for Kusama {
|
||||
type BlockNumber = BlockNumber;
|
||||
type Hash = Hash;
|
||||
type Hasher = Hasher;
|
||||
type Header = Header;
|
||||
}
|
||||
|
||||
/// Convert a 256-bit hash into an AccountId.
|
||||
pub struct AccountIdConverter;
|
||||
|
||||
impl sp_runtime::traits::Convert<sp_core::H256, AccountId> for AccountIdConverter {
|
||||
fn convert(hash: sp_core::H256) -> AccountId {
|
||||
hash.to_fixed_bytes().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of the `KusamaHeaderApi::best_blocks` runtime method.
|
||||
pub const BEST_KUSAMA_BLOCKS_METHOD: &str = "KusamaHeaderApi_best_blocks";
|
||||
/// Name of the `KusamaHeaderApi::finalized_block` runtime method.
|
||||
pub const FINALIZED_KUSAMA_BLOCK_METHOD: &str = "KusamaHeaderApi_finalized_block";
|
||||
/// Name of the `KusamaHeaderApi::is_known_block` runtime method.
|
||||
pub const IS_KNOWN_KUSAMA_BLOCK_METHOD: &str = "KusamaHeaderApi_is_known_block";
|
||||
/// Name of the `KusamaHeaderApi::incomplete_headers` runtime method.
|
||||
pub const INCOMPLETE_KUSAMA_HEADERS_METHOD: &str = "KusamaHeaderApi_incomplete_headers";
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for querying information about Kusama headers from the Bridge Pallet instance.
|
||||
///
|
||||
/// This API is implemented by runtimes that are bridging with Kusama chain, not the
|
||||
/// Kusama runtime itself.
|
||||
pub trait KusamaHeaderApi {
|
||||
/// Returns number and hash of the best blocks known to the bridge module.
|
||||
///
|
||||
/// Will return multiple headers if there are many headers at the same "best" height.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (BlockNumber, Hash);
|
||||
/// Returns numbers and hashes of headers that require finality proofs.
|
||||
///
|
||||
/// An empty response means that there are no headers which currently require a
|
||||
/// finality proof.
|
||||
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns true if the header is known to the runtime.
|
||||
fn is_known_block(hash: Hash) -> bool;
|
||||
/// Returns true if the header is considered finalized by the runtime.
|
||||
fn is_finalized_block(hash: Hash) -> bool;
|
||||
}
|
||||
|
||||
/// Outbound message lane API for messages that are sent to Kusama chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are sending messages to Kusama chain, not the
|
||||
/// Kusama runtime itself.
|
||||
pub trait ToKusamaOutboundLaneApi {
|
||||
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
|
||||
///
|
||||
/// If some (or all) messages are missing from the storage, they'll also will
|
||||
/// be missing from the resulting vector. The vector is ordered by the nonce.
|
||||
fn messages_dispatch_weight(
|
||||
lane: LaneId,
|
||||
begin: MessageNonce,
|
||||
end: MessageNonce,
|
||||
) -> Vec<(MessageNonce, Weight, u32)>;
|
||||
/// Returns nonce of the latest message, received by bridged chain.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Returns nonce of the latest message, generated by given lane.
|
||||
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
|
||||
/// Inbound message lane API for messages sent by Kusama chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are receiving messages from Kusama chain, not the
|
||||
/// Kusama runtime itself.
|
||||
pub trait FromKusamaInboundLaneApi {
|
||||
/// Returns nonce of the latest message, received by given lane.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Nonce of latest message that has been confirmed to the bridged chain.
|
||||
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
[package]
|
||||
name = "bp-message-dispatch"
|
||||
description = "Primitives of bridge messages dispatch modules."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-runtime/std",
|
||||
"codec/std",
|
||||
]
|
||||
@@ -1,49 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! A common interface for all Bridge Message Dispatch modules.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use bp_runtime::InstanceId;
|
||||
|
||||
/// Message dispatch weight.
|
||||
pub type Weight = u64;
|
||||
|
||||
/// A generic trait to dispatch arbitrary messages delivered over the bridge.
|
||||
pub trait MessageDispatch<MessageId> {
|
||||
/// A type of the message to be dispatched.
|
||||
type Message: codec::Decode;
|
||||
|
||||
/// Estimate dispatch weight.
|
||||
///
|
||||
/// This function must: (1) be instant and (2) return correct upper bound
|
||||
/// of dispatch weight.
|
||||
fn dispatch_weight(message: &Self::Message) -> Weight;
|
||||
|
||||
/// Dispatches the message internally.
|
||||
///
|
||||
/// `bridge` indicates instance of deployed bridge where the message came from.
|
||||
///
|
||||
/// `id` is a short unique identifier of the message.
|
||||
///
|
||||
/// If message is `Ok`, then it should be dispatched. If it is `Err`, then it's just
|
||||
/// a sign that some other component has rejected the message even before it has
|
||||
/// reached `dispatch` method (right now this may only be caused if we fail to decode
|
||||
/// the whole message).
|
||||
fn dispatch(bridge: InstanceId, id: MessageId, message: Result<Self::Message, ()>);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
[package]
|
||||
name = "bp-message-lane"
|
||||
description = "Primitives of message lane module."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
|
||||
# Bridge dependencies
|
||||
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-runtime/std",
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"sp-std/std"
|
||||
]
|
||||
@@ -1,228 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Primitives of message lane module.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Generated by `DecodeLimit::decode_with_depth_limit`
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::RuntimeDebug;
|
||||
use sp_std::{collections::vec_deque::VecDeque, prelude::*};
|
||||
|
||||
pub mod source_chain;
|
||||
pub mod target_chain;
|
||||
|
||||
// Weight is reexported to avoid additional frame-support dependencies in message-lane related crates.
|
||||
pub use frame_support::weights::Weight;
|
||||
|
||||
/// Message lane pallet parameter.
|
||||
pub trait Parameter: frame_support::Parameter {
|
||||
/// Save parameter value in the runtime storage.
|
||||
fn save(&self);
|
||||
}
|
||||
|
||||
/// Lane identifier.
|
||||
pub type LaneId = [u8; 4];
|
||||
|
||||
/// Message nonce. Valid messages will never have 0 nonce.
|
||||
pub type MessageNonce = u64;
|
||||
|
||||
/// Message id as a tuple.
|
||||
pub type MessageId = (LaneId, MessageNonce);
|
||||
|
||||
/// Opaque message payload. We only decode this payload when it is dispatched.
|
||||
pub type MessagePayload = Vec<u8>;
|
||||
|
||||
/// Message key (unique message identifier) as it is stored in the storage.
|
||||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
|
||||
pub struct MessageKey {
|
||||
/// ID of the message lane.
|
||||
pub lane_id: LaneId,
|
||||
/// Message nonce.
|
||||
pub nonce: MessageNonce,
|
||||
}
|
||||
|
||||
/// Message data as it is stored in the storage.
|
||||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
|
||||
pub struct MessageData<Fee> {
|
||||
/// Message payload.
|
||||
pub payload: MessagePayload,
|
||||
/// Message delivery and dispatch fee, paid by the submitter.
|
||||
pub fee: Fee,
|
||||
}
|
||||
|
||||
/// Message as it is stored in the storage.
|
||||
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
|
||||
pub struct Message<Fee> {
|
||||
/// Message key.
|
||||
pub key: MessageKey,
|
||||
/// Message data.
|
||||
pub data: MessageData<Fee>,
|
||||
}
|
||||
|
||||
/// Inbound lane data.
|
||||
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
|
||||
pub struct InboundLaneData<RelayerId> {
|
||||
/// Identifiers of relayers and messages that they have delivered to this lane (ordered by message nonce).
|
||||
///
|
||||
/// This serves as a helper storage item, to allow the source chain to easily pay rewards
|
||||
/// to the relayers who succesfuly delivered messages to the target chain (inbound lane).
|
||||
///
|
||||
/// It is guaranteed to have at most N entries, where N is configured at the module level.
|
||||
/// If there are N entries in this vec, then:
|
||||
/// 1) all incoming messages are rejected if they're missing corresponding `proof-of(outbound-lane.state)`;
|
||||
/// 2) all incoming messages are rejected if `proof-of(outbound-lane.state).last_delivered_nonce` is
|
||||
/// equal to `self.last_confirmed_nonce`.
|
||||
/// Given what is said above, all nonces in this queue are in range:
|
||||
/// `(self.last_confirmed_nonce; self.last_delivered_nonce()]`.
|
||||
///
|
||||
/// When a relayer sends a single message, both of MessageNonces are the same.
|
||||
/// When relayer sends messages in a batch, the first arg is the lowest nonce, second arg the highest nonce.
|
||||
/// Multiple dispatches from the same relayer are allowed.
|
||||
pub relayers: VecDeque<(MessageNonce, MessageNonce, RelayerId)>,
|
||||
|
||||
/// Nonce of the last message that
|
||||
/// a) has been delivered to the target (this) chain and
|
||||
/// b) the delivery has been confirmed on the source chain
|
||||
///
|
||||
/// that the target chain knows of.
|
||||
///
|
||||
/// This value is updated indirectly when an `OutboundLane` state of the source
|
||||
/// chain is received alongside with new messages delivery.
|
||||
pub last_confirmed_nonce: MessageNonce,
|
||||
}
|
||||
|
||||
impl<RelayerId> Default for InboundLaneData<RelayerId> {
|
||||
fn default() -> Self {
|
||||
InboundLaneData {
|
||||
relayers: VecDeque::new(),
|
||||
last_confirmed_nonce: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<RelayerId> InboundLaneData<RelayerId> {
|
||||
/// Returns approximate size of the struct, given number of entries in the `relayers` set and
|
||||
/// size of each entry.
|
||||
///
|
||||
/// Returns `None` if size overflows `u32` limits.
|
||||
pub fn encoded_size_hint(relayer_id_encoded_size: u32, relayers_entries: u32) -> Option<u32> {
|
||||
let message_nonce_size = 8;
|
||||
let relayers_entry_size = relayer_id_encoded_size.checked_add(2 * message_nonce_size)?;
|
||||
let relayers_size = relayers_entries.checked_mul(relayers_entry_size)?;
|
||||
relayers_size.checked_add(message_nonce_size)
|
||||
}
|
||||
|
||||
/// Nonce of the last message that has been delivered to this (target) chain.
|
||||
pub fn last_delivered_nonce(&self) -> MessageNonce {
|
||||
self.relayers
|
||||
.back()
|
||||
.map(|(_, last_nonce, _)| *last_nonce)
|
||||
.unwrap_or(self.last_confirmed_nonce)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gist of `InboundLaneData::relayers` field used by runtime APIs.
|
||||
#[derive(Clone, Default, Encode, Decode, RuntimeDebug, PartialEq, Eq)]
|
||||
pub struct UnrewardedRelayersState {
|
||||
/// Number of entries in the `InboundLaneData::relayers` set.
|
||||
pub unrewarded_relayer_entries: MessageNonce,
|
||||
/// Number of messages in the oldest entry of `InboundLaneData::relayers`. This is the
|
||||
/// minimal number of reward proofs required to push out this entry from the set.
|
||||
pub messages_in_oldest_entry: MessageNonce,
|
||||
/// Total number of messages in the relayers vector.
|
||||
pub total_messages: MessageNonce,
|
||||
}
|
||||
|
||||
/// Outbound lane data.
|
||||
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
|
||||
pub struct OutboundLaneData {
|
||||
/// Nonce of oldest message that we haven't yet pruned. May point to not-yet-generated message if
|
||||
/// all sent messages are already pruned.
|
||||
pub oldest_unpruned_nonce: MessageNonce,
|
||||
/// Nonce of latest message, received by bridged chain.
|
||||
pub latest_received_nonce: MessageNonce,
|
||||
/// Nonce of latest message, generated by us.
|
||||
pub latest_generated_nonce: MessageNonce,
|
||||
}
|
||||
|
||||
impl Default for OutboundLaneData {
|
||||
fn default() -> Self {
|
||||
OutboundLaneData {
|
||||
// it is 1 because we're pruning everything in [oldest_unpruned_nonce; latest_received_nonce]
|
||||
oldest_unpruned_nonce: 1,
|
||||
latest_received_nonce: 0,
|
||||
latest_generated_nonce: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns total number of messages in the `InboundLaneData::relayers` vector.
|
||||
///
|
||||
/// Returns `None` if there are more messages that `MessageNonce` may fit (i.e. `MessageNonce + 1`).
|
||||
pub fn total_unrewarded_messages<RelayerId>(
|
||||
relayers: &VecDeque<(MessageNonce, MessageNonce, RelayerId)>,
|
||||
) -> Option<MessageNonce> {
|
||||
match (relayers.front(), relayers.back()) {
|
||||
(Some((begin, _, _)), Some((_, end, _))) => {
|
||||
if let Some(difference) = end.checked_sub(*begin) {
|
||||
difference.checked_add(1)
|
||||
} else {
|
||||
Some(0)
|
||||
}
|
||||
}
|
||||
_ => Some(0),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn total_unrewarded_messages_does_not_overflow() {
|
||||
assert_eq!(
|
||||
total_unrewarded_messages(
|
||||
&vec![(0, 0, 1), (MessageNonce::MAX, MessageNonce::MAX, 2)]
|
||||
.into_iter()
|
||||
.collect()
|
||||
),
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inbound_lane_data_returns_correct_hint() {
|
||||
let expected_size = InboundLaneData::<u8>::encoded_size_hint(1, 13);
|
||||
let actual_size = InboundLaneData {
|
||||
relayers: (1u8..=13u8).map(|i| (i as _, i as _, i)).collect(),
|
||||
last_confirmed_nonce: 13,
|
||||
}
|
||||
.encode()
|
||||
.len();
|
||||
let difference = (expected_size.unwrap() as f64 - actual_size as f64).abs();
|
||||
assert!(
|
||||
difference / (std::cmp::min(actual_size, expected_size.unwrap() as usize) as f64) < 0.1,
|
||||
"Too large difference between actual ({}) and expected ({:?}) inbound lane data size",
|
||||
actual_size,
|
||||
expected_size,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Primitives of message lane module, that are used on the source chain.
|
||||
|
||||
use crate::{InboundLaneData, LaneId, MessageNonce, OutboundLaneData};
|
||||
|
||||
use bp_runtime::Size;
|
||||
use frame_support::{Parameter, RuntimeDebug};
|
||||
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug};
|
||||
|
||||
/// The sender of the message on the source chain.
|
||||
pub type Sender<AccountId> = frame_system::RawOrigin<AccountId>;
|
||||
|
||||
/// Relayers rewards, grouped by relayer account id.
|
||||
pub type RelayersRewards<AccountId, Balance> = BTreeMap<AccountId, RelayerRewards<Balance>>;
|
||||
|
||||
/// Single relayer rewards.
|
||||
#[derive(RuntimeDebug, Default)]
|
||||
pub struct RelayerRewards<Balance> {
|
||||
/// Total rewards that are to be paid to the relayer.
|
||||
pub reward: Balance,
|
||||
/// Total number of messages relayed by this relayer.
|
||||
pub messages: MessageNonce,
|
||||
}
|
||||
|
||||
/// Target chain API. Used by source chain to verify target chain proofs.
|
||||
///
|
||||
/// All implementations of this trait should only work with finalized data that
|
||||
/// can't change. Wrong implementation may lead to invalid lane states (i.e. lane
|
||||
/// that's stuck) and/or processing messages without paying fees.
|
||||
pub trait TargetHeaderChain<Payload, AccountId> {
|
||||
/// Error type.
|
||||
type Error: Debug + Into<&'static str>;
|
||||
|
||||
/// Proof that messages have been received by target chain.
|
||||
type MessagesDeliveryProof: Parameter + Size;
|
||||
|
||||
/// Verify message payload before we accept it.
|
||||
///
|
||||
/// **CAUTION**: this is very important function. Incorrect implementation may lead
|
||||
/// to stuck lanes and/or relayers loses.
|
||||
///
|
||||
/// The proper implementation must ensure that the delivery-transaction with this
|
||||
/// payload would (at least) be accepted into target chain transaction pool AND
|
||||
/// eventually will be successfully 'mined'. The most obvious incorrect implementation
|
||||
/// example would be implementation for BTC chain that accepts payloads larger than
|
||||
/// 1MB. BTC nodes aren't accepting transactions that are larger than 1MB, so relayer
|
||||
/// will be unable to craft valid transaction => this (and all subsequent) messages will
|
||||
/// never be delivered.
|
||||
fn verify_message(payload: &Payload) -> Result<(), Self::Error>;
|
||||
|
||||
/// Verify messages delivery proof and return lane && nonce of the latest recevied message.
|
||||
fn verify_messages_delivery_proof(
|
||||
proof: Self::MessagesDeliveryProof,
|
||||
) -> Result<(LaneId, InboundLaneData<AccountId>), Self::Error>;
|
||||
}
|
||||
|
||||
/// Lane message verifier.
|
||||
///
|
||||
/// Runtime developer may implement any additional validation logic over message-lane mechanism.
|
||||
/// E.g. if lanes should have some security (e.g. you can only accept Lane1 messages from
|
||||
/// Submitter1, Lane2 messages for those who has submitted first message to this lane, disable
|
||||
/// Lane3 until some block, ...), then it may be built using this verifier.
|
||||
///
|
||||
/// Any fee requirements should also be enforced here.
|
||||
pub trait LaneMessageVerifier<Submitter, Payload, Fee> {
|
||||
/// Error type.
|
||||
type Error: Debug + Into<&'static str>;
|
||||
|
||||
/// Verify message payload and return Ok(()) if message is valid and allowed to be sent over the lane.
|
||||
fn verify_message(
|
||||
submitter: &Sender<Submitter>,
|
||||
delivery_and_dispatch_fee: &Fee,
|
||||
lane: &LaneId,
|
||||
outbound_data: &OutboundLaneData,
|
||||
payload: &Payload,
|
||||
) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
/// Message delivery payment. It is called as a part of submit-message transaction. Transaction
|
||||
/// submitter is paying (in source chain tokens/assets) for:
|
||||
///
|
||||
/// 1) submit-message-transaction-fee itself. This fee is not included in the
|
||||
/// `delivery_and_dispatch_fee` and is witheld by the regular transaction payment mechanism;
|
||||
/// 2) message-delivery-transaction-fee. It is submitted to the target node by relayer;
|
||||
/// 3) message-dispatch fee. It is paid by relayer for processing message by target chain;
|
||||
/// 4) message-receiving-delivery-transaction-fee. It is submitted to the source node
|
||||
/// by relayer.
|
||||
///
|
||||
/// So to be sure that any non-altruist relayer would agree to deliver message, submitter
|
||||
/// should set `delivery_and_dispatch_fee` to at least (equialent of): sum of fees from (2)
|
||||
/// to (4) above, plus some interest for the relayer.
|
||||
pub trait MessageDeliveryAndDispatchPayment<AccountId, Balance> {
|
||||
/// Error type.
|
||||
type Error: Debug + Into<&'static str>;
|
||||
|
||||
/// Withhold/write-off delivery_and_dispatch_fee from submitter account to
|
||||
/// some relayers-fund account.
|
||||
fn pay_delivery_and_dispatch_fee(
|
||||
submitter: &Sender<AccountId>,
|
||||
fee: &Balance,
|
||||
relayer_fund_account: &AccountId,
|
||||
) -> Result<(), Self::Error>;
|
||||
|
||||
/// Pay rewards for delivering messages to the given relayers.
|
||||
///
|
||||
/// The implementation may also choose to pay reward to the `confirmation_relayer`, which is
|
||||
/// a relayer that has submitted delivery confirmation transaction.
|
||||
fn pay_relayers_rewards(
|
||||
confirmation_relayer: &AccountId,
|
||||
relayers_rewards: RelayersRewards<AccountId, Balance>,
|
||||
relayer_fund_account: &AccountId,
|
||||
);
|
||||
|
||||
/// Perform some initialization in externalities-provided environment.
|
||||
///
|
||||
/// For instance you may ensure that particular required accounts or storage items are present.
|
||||
/// Returns the number of storage reads performed.
|
||||
fn initialize(_relayer_fund_account: &AccountId) -> usize {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Structure that may be used in place of `TargetHeaderChain`, `LaneMessageVerifier` and
|
||||
/// `MessageDeliveryAndDispatchPayment` on chains, where outbound messages are forbidden.
|
||||
pub struct ForbidOutboundMessages;
|
||||
|
||||
/// Error message that is used in `ForbidOutboundMessages` implementation.
|
||||
const ALL_OUTBOUND_MESSAGES_REJECTED: &str = "This chain is configured to reject all outbound messages";
|
||||
|
||||
impl<Payload, AccountId> TargetHeaderChain<Payload, AccountId> for ForbidOutboundMessages {
|
||||
type Error = &'static str;
|
||||
|
||||
type MessagesDeliveryProof = ();
|
||||
|
||||
fn verify_message(_payload: &Payload) -> Result<(), Self::Error> {
|
||||
Err(ALL_OUTBOUND_MESSAGES_REJECTED)
|
||||
}
|
||||
|
||||
fn verify_messages_delivery_proof(
|
||||
_proof: Self::MessagesDeliveryProof,
|
||||
) -> Result<(LaneId, InboundLaneData<AccountId>), Self::Error> {
|
||||
Err(ALL_OUTBOUND_MESSAGES_REJECTED)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Submitter, Payload, Fee> LaneMessageVerifier<Submitter, Payload, Fee> for ForbidOutboundMessages {
|
||||
type Error = &'static str;
|
||||
|
||||
fn verify_message(
|
||||
_submitter: &Sender<Submitter>,
|
||||
_delivery_and_dispatch_fee: &Fee,
|
||||
_lane: &LaneId,
|
||||
_outbound_data: &OutboundLaneData,
|
||||
_payload: &Payload,
|
||||
) -> Result<(), Self::Error> {
|
||||
Err(ALL_OUTBOUND_MESSAGES_REJECTED)
|
||||
}
|
||||
}
|
||||
|
||||
impl<AccountId, Balance> MessageDeliveryAndDispatchPayment<AccountId, Balance> for ForbidOutboundMessages {
|
||||
type Error = &'static str;
|
||||
|
||||
fn pay_delivery_and_dispatch_fee(
|
||||
_submitter: &Sender<AccountId>,
|
||||
_fee: &Balance,
|
||||
_relayer_fund_account: &AccountId,
|
||||
) -> Result<(), Self::Error> {
|
||||
Err(ALL_OUTBOUND_MESSAGES_REJECTED)
|
||||
}
|
||||
|
||||
fn pay_relayers_rewards(
|
||||
_confirmation_relayer: &AccountId,
|
||||
_relayers_rewards: RelayersRewards<AccountId, Balance>,
|
||||
_relayer_fund_account: &AccountId,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Primitives of message lane module, that are used on the target chain.
|
||||
|
||||
use crate::{LaneId, Message, MessageData, MessageKey, OutboundLaneData};
|
||||
|
||||
use bp_runtime::Size;
|
||||
use codec::{Decode, Encode, Error as CodecError};
|
||||
use frame_support::{weights::Weight, Parameter, RuntimeDebug};
|
||||
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug, prelude::*};
|
||||
|
||||
/// Proved messages from the source chain.
|
||||
pub type ProvedMessages<Message> = BTreeMap<LaneId, ProvedLaneMessages<Message>>;
|
||||
|
||||
/// Proved messages from single lane of the source chain.
|
||||
#[derive(RuntimeDebug, Encode, Decode, Clone, PartialEq, Eq)]
|
||||
pub struct ProvedLaneMessages<Message> {
|
||||
/// Optional outbound lane state.
|
||||
pub lane_state: Option<OutboundLaneData>,
|
||||
/// Messages sent through this lane.
|
||||
pub messages: Vec<Message>,
|
||||
}
|
||||
|
||||
/// Message data with decoded dispatch payload.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct DispatchMessageData<DispatchPayload, Fee> {
|
||||
/// Result of dispatch payload decoding.
|
||||
pub payload: Result<DispatchPayload, CodecError>,
|
||||
/// Message delivery and dispatch fee, paid by the submitter.
|
||||
pub fee: Fee,
|
||||
}
|
||||
|
||||
/// Message with decoded dispatch payload.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct DispatchMessage<DispatchPayload, Fee> {
|
||||
/// Message key.
|
||||
pub key: MessageKey,
|
||||
/// Message data with decoded dispatch payload.
|
||||
pub data: DispatchMessageData<DispatchPayload, Fee>,
|
||||
}
|
||||
|
||||
/// Source chain API. Used by target chain, to verify source chain proofs.
|
||||
///
|
||||
/// All implementations of this trait should only work with finalized data that
|
||||
/// can't change. Wrong implementation may lead to invalid lane states (i.e. lane
|
||||
/// that's stuck) and/or processing messages without paying fees.
|
||||
pub trait SourceHeaderChain<Fee> {
|
||||
/// Error type.
|
||||
type Error: Debug + Into<&'static str>;
|
||||
|
||||
/// Proof that messages are sent from source chain. This may also include proof
|
||||
/// of corresponding outbound lane states.
|
||||
type MessagesProof: Parameter + Size;
|
||||
|
||||
/// Verify messages proof and return proved messages.
|
||||
///
|
||||
/// Returns error if either proof is incorrect, or the number of messages in the proof
|
||||
/// is not matching the `messages_count`.
|
||||
///
|
||||
/// Messages vector is required to be sorted by nonce within each lane. Out-of-order
|
||||
/// messages will be rejected.
|
||||
///
|
||||
/// The `messages_count` argument verification (sane limits) is supposed to be made
|
||||
/// outside of this function. This function only verifies that the proof declares exactly
|
||||
/// `messages_count` messages.
|
||||
fn verify_messages_proof(
|
||||
proof: Self::MessagesProof,
|
||||
messages_count: u32,
|
||||
) -> Result<ProvedMessages<Message<Fee>>, Self::Error>;
|
||||
}
|
||||
|
||||
/// Called when inbound message is received.
|
||||
pub trait MessageDispatch<Fee> {
|
||||
/// Decoded message payload type. Valid message may contain invalid payload. In this case
|
||||
/// message is delivered, but dispatch fails. Therefore, two separate types of payload
|
||||
/// (opaque `MessagePayload` used in delivery and this `DispatchPayload` used in dispatch).
|
||||
type DispatchPayload: Decode;
|
||||
|
||||
/// Estimate dispatch weight.
|
||||
///
|
||||
/// This function must: (1) be instant and (2) return correct upper bound
|
||||
/// of dispatch weight.
|
||||
fn dispatch_weight(message: &DispatchMessage<Self::DispatchPayload, Fee>) -> Weight;
|
||||
|
||||
/// Called when inbound message is received.
|
||||
///
|
||||
/// It is up to the implementers of this trait to determine whether the message
|
||||
/// is invalid (i.e. improperly encoded, has too large weight, ...) or not.
|
||||
fn dispatch(message: DispatchMessage<Self::DispatchPayload, Fee>);
|
||||
}
|
||||
|
||||
impl<Message> Default for ProvedLaneMessages<Message> {
|
||||
fn default() -> Self {
|
||||
ProvedLaneMessages {
|
||||
lane_state: None,
|
||||
messages: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<DispatchPayload: Decode, Fee> From<Message<Fee>> for DispatchMessage<DispatchPayload, Fee> {
|
||||
fn from(message: Message<Fee>) -> Self {
|
||||
DispatchMessage {
|
||||
key: message.key,
|
||||
data: message.data.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<DispatchPayload: Decode, Fee> From<MessageData<Fee>> for DispatchMessageData<DispatchPayload, Fee> {
|
||||
fn from(data: MessageData<Fee>) -> Self {
|
||||
DispatchMessageData {
|
||||
payload: DispatchPayload::decode(&mut &data.payload[..]),
|
||||
fee: data.fee,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Structure that may be used in place of `SourceHeaderChain` and `MessageDispatch` on chains,
|
||||
/// where inbound messages are forbidden.
|
||||
pub struct ForbidInboundMessages;
|
||||
|
||||
/// Error message that is used in `ForbidOutboundMessages` implementation.
|
||||
const ALL_INBOUND_MESSAGES_REJECTED: &str = "This chain is configured to reject all inbound messages";
|
||||
|
||||
impl<Fee> SourceHeaderChain<Fee> for ForbidInboundMessages {
|
||||
type Error = &'static str;
|
||||
type MessagesProof = ();
|
||||
|
||||
fn verify_messages_proof(
|
||||
_proof: Self::MessagesProof,
|
||||
_messages_count: u32,
|
||||
) -> Result<ProvedMessages<Message<Fee>>, Self::Error> {
|
||||
Err(ALL_INBOUND_MESSAGES_REJECTED)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fee> MessageDispatch<Fee> for ForbidInboundMessages {
|
||||
type DispatchPayload = ();
|
||||
|
||||
fn dispatch_weight(_message: &DispatchMessage<Self::DispatchPayload, Fee>) -> Weight {
|
||||
Weight::MAX
|
||||
}
|
||||
|
||||
fn dispatch(_message: DispatchMessage<Self::DispatchPayload, Fee>) {}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
[package]
|
||||
name = "bp-millau"
|
||||
description = "Primitives of Millau runtime."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Bridge Dependencies
|
||||
|
||||
bp-message-lane = { path = "../message-lane", default-features = false }
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
fixed-hash = { version = "0.7.0", default-features = false }
|
||||
hash256-std-hasher = { version = "0.15.2", default-features = false }
|
||||
impl-codec = { version = "0.5.0", default-features = false }
|
||||
impl-serde = { version = "0.3.1", optional = true }
|
||||
parity-util-mem = { version = "0.9.0", default-features = false, features = ["primitive-types"] }
|
||||
serde = { version = "1.0.101", optional = true, features = ["derive"] }
|
||||
|
||||
# Substrate Based Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
frame-system = { 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 }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-message-lane/std",
|
||||
"bp-runtime/std",
|
||||
"fixed-hash/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"hash256-std-hasher/std",
|
||||
"impl-codec/std",
|
||||
"impl-serde",
|
||||
"parity-util-mem/std",
|
||||
"serde",
|
||||
"sp-api/std",
|
||||
"sp-core/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
"sp-trie/std",
|
||||
]
|
||||
@@ -1,345 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
mod millau_hash;
|
||||
|
||||
use bp_message_lane::{LaneId, MessageNonce, UnrewardedRelayersState};
|
||||
use bp_runtime::Chain;
|
||||
use frame_support::{
|
||||
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
|
||||
Parameter, RuntimeDebug,
|
||||
};
|
||||
use frame_system::limits;
|
||||
use sp_core::Hasher as HasherT;
|
||||
use sp_runtime::traits::Convert;
|
||||
use sp_runtime::{
|
||||
traits::{IdentifyAccount, Verify},
|
||||
MultiSignature, MultiSigner, Perbill,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
use sp_trie::{trie_types::Layout, TrieConfiguration};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub use millau_hash::MillauHash;
|
||||
|
||||
/// Number of extra bytes (excluding size of storage value itself) of storage proof, built at
|
||||
/// Millau chain. This mostly depends on number of entries (and their density) in the storage trie.
|
||||
/// Some reserve is reserved to account future chain growth.
|
||||
pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
|
||||
|
||||
/// Maximal size (in bytes) of encoded (using `Encode::encode()`) account id.
|
||||
pub const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 = 32;
|
||||
|
||||
/// Maximum weight of single Millau block.
|
||||
///
|
||||
/// This represents 0.5 seconds of compute assuming a target block time of six seconds.
|
||||
pub const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND / 2;
|
||||
|
||||
/// Represents the average portion of a block's weight that will be used by an
|
||||
/// `on_initialize()` runtime call.
|
||||
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
|
||||
|
||||
/// Represents the portion of a block that will be used by Normal extrinsics.
|
||||
pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
|
||||
|
||||
/// Maximal number of unrewarded relayer entries at inbound lane.
|
||||
pub const MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE: MessageNonce = 1024;
|
||||
|
||||
/// Maximal number of unconfirmed messages at inbound lane.
|
||||
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 1024;
|
||||
|
||||
/// Weight of single regular message delivery transaction on Millau chain.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::Module::receive_messages_proof_weight()` call
|
||||
/// for the case when single message of `pallet_message_lane::EXPECTED_DEFAULT_MESSAGE_LENGTH` bytes is delivered.
|
||||
/// The message must have dispatch weight set to zero. The result then must be rounded up to account
|
||||
/// possible future runtime upgrades.
|
||||
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_000_000_000;
|
||||
|
||||
/// Increase of delivery transaction weight on Millau chain with every additional message byte.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::WeightInfoExt::storage_proof_size_overhead(1)` call. The
|
||||
/// result then must be rounded up to account possible future runtime upgrades.
|
||||
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
|
||||
|
||||
/// Maximal weight of single message delivery confirmation transaction on Millau chain.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::Module::receive_messages_delivery_proof` weight formula computation
|
||||
/// for the case when single message is confirmed. The result then must be rounded up to account possible future
|
||||
/// runtime upgrades.
|
||||
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
|
||||
|
||||
/// The length of a session (how often authorities change) on Millau measured in of number of blocks.
|
||||
pub const SESSION_LENGTH: BlockNumber = 5 * time_units::MINUTES;
|
||||
|
||||
/// Re-export `time_units` to make usage easier.
|
||||
pub use time_units::*;
|
||||
|
||||
/// Human readable time units defined in terms of number of blocks.
|
||||
pub mod time_units {
|
||||
use super::BlockNumber;
|
||||
|
||||
pub const MILLISECS_PER_BLOCK: u64 = 6000;
|
||||
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
|
||||
|
||||
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
|
||||
pub const HOURS: BlockNumber = MINUTES * 60;
|
||||
pub const DAYS: BlockNumber = HOURS * 24;
|
||||
}
|
||||
|
||||
/// Block number type used in Millau.
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
/// Hash type used in Millau.
|
||||
pub type Hash = <BlakeTwoAndKeccak256 as HasherT>::Out;
|
||||
|
||||
/// The type of an object that can produce hashes on Millau.
|
||||
pub type Hasher = BlakeTwoAndKeccak256;
|
||||
|
||||
/// The header type used by Millau.
|
||||
pub type Header = sp_runtime::generic::Header<BlockNumber, Hasher>;
|
||||
|
||||
/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
|
||||
pub type Signature = MultiSignature;
|
||||
|
||||
/// Some way of identifying an account on the chain. We intentionally make it equivalent
|
||||
/// to the public key of our transaction signing scheme.
|
||||
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
|
||||
|
||||
/// Public key of the chain account that may be used to verify signatures.
|
||||
pub type AccountSigner = MultiSigner;
|
||||
|
||||
/// Balance of an account.
|
||||
pub type Balance = u64;
|
||||
|
||||
/// Millau chain.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct Millau;
|
||||
|
||||
impl Chain for Millau {
|
||||
type BlockNumber = BlockNumber;
|
||||
type Hash = Hash;
|
||||
type Hasher = Hasher;
|
||||
type Header = Header;
|
||||
}
|
||||
|
||||
/// Millau Hasher (Blake2-256 ++ Keccak-256) implementation.
|
||||
#[derive(PartialEq, Eq, Clone, Copy, RuntimeDebug)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
pub struct BlakeTwoAndKeccak256;
|
||||
|
||||
impl sp_core::Hasher for BlakeTwoAndKeccak256 {
|
||||
type Out = MillauHash;
|
||||
type StdHasher = hash256_std_hasher::Hash256StdHasher;
|
||||
const LENGTH: usize = 64;
|
||||
|
||||
fn hash(s: &[u8]) -> Self::Out {
|
||||
let mut combined_hash = MillauHash::default();
|
||||
combined_hash.as_mut()[..32].copy_from_slice(&sp_io::hashing::blake2_256(s));
|
||||
combined_hash.as_mut()[32..].copy_from_slice(&sp_io::hashing::keccak_256(s));
|
||||
combined_hash
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_runtime::traits::Hash for BlakeTwoAndKeccak256 {
|
||||
type Output = MillauHash;
|
||||
|
||||
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> Self::Output {
|
||||
Layout::<BlakeTwoAndKeccak256>::trie_root(input)
|
||||
}
|
||||
|
||||
fn ordered_trie_root(input: Vec<Vec<u8>>) -> Self::Output {
|
||||
Layout::<BlakeTwoAndKeccak256>::ordered_trie_root(input)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a 256-bit hash into an AccountId.
|
||||
pub struct AccountIdConverter;
|
||||
|
||||
impl sp_runtime::traits::Convert<sp_core::H256, AccountId> for AccountIdConverter {
|
||||
fn convert(hash: sp_core::H256) -> AccountId {
|
||||
hash.to_fixed_bytes().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
frame_support::parameter_types! {
|
||||
pub BlockLength: limits::BlockLength =
|
||||
limits::BlockLength::max_with_normal_ratio(2 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
|
||||
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
|
||||
// Allowance for Normal class
|
||||
.for_class(DispatchClass::Normal, |weights| {
|
||||
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
|
||||
})
|
||||
// Allowance for Operational class
|
||||
.for_class(DispatchClass::Operational, |weights| {
|
||||
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
|
||||
// Extra reserved space for Operational class
|
||||
weights.reserved = Some(MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
|
||||
})
|
||||
// By default Mandatory class is not limited at all.
|
||||
// This parameter is used to derive maximal size of a single extrinsic.
|
||||
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
|
||||
.build_or_panic();
|
||||
}
|
||||
|
||||
/// Get the maximum weight (compute time) that a Normal extrinsic on the Millau chain can use.
|
||||
pub fn max_extrinsic_weight() -> Weight {
|
||||
BlockWeights::get()
|
||||
.get(DispatchClass::Normal)
|
||||
.max_extrinsic
|
||||
.unwrap_or(Weight::MAX)
|
||||
}
|
||||
|
||||
/// Get the maximum length in bytes that a Normal extrinsic on the Millau chain requires.
|
||||
pub fn max_extrinsic_size() -> u32 {
|
||||
*BlockLength::get().max.get(DispatchClass::Normal)
|
||||
}
|
||||
|
||||
/// Name of the `MillauHeaderApi::best_block` runtime method.
|
||||
pub const BEST_MILLAU_BLOCKS_METHOD: &str = "MillauHeaderApi_best_blocks";
|
||||
/// Name of the `MillauHeaderApi::finalized_block` runtime method.
|
||||
pub const FINALIZED_MILLAU_BLOCK_METHOD: &str = "MillauHeaderApi_finalized_block";
|
||||
/// Name of the `MillauHeaderApi::is_known_block` runtime method.
|
||||
pub const IS_KNOWN_MILLAU_BLOCK_METHOD: &str = "MillauHeaderApi_is_known_block";
|
||||
/// Name of the `MillauHeaderApi::incomplete_headers` runtime method.
|
||||
pub const INCOMPLETE_MILLAU_HEADERS_METHOD: &str = "MillauHeaderApi_incomplete_headers";
|
||||
|
||||
/// Name of the `ToMillauOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
|
||||
pub const TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD: &str =
|
||||
"ToMillauOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
|
||||
/// Name of the `ToMillauOutboundLaneApi::messages_dispatch_weight` runtime method.
|
||||
pub const TO_MILLAU_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToMillauOutboundLaneApi_messages_dispatch_weight";
|
||||
/// Name of the `ToMillauOutboundLaneApi::latest_received_nonce` runtime method.
|
||||
pub const TO_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_received_nonce";
|
||||
/// Name of the `ToMillauOutboundLaneApi::latest_generated_nonce` runtime method.
|
||||
pub const TO_MILLAU_LATEST_GENERATED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_generated_nonce";
|
||||
|
||||
/// Name of the `FromMillauInboundLaneApi::latest_received_nonce` runtime method.
|
||||
pub const FROM_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_received_nonce";
|
||||
/// Name of the `FromMillauInboundLaneApi::latest_onfirmed_nonce` runtime method.
|
||||
pub const FROM_MILLAU_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_confirmed_nonce";
|
||||
/// Name of the `FromMillauInboundLaneApi::unrewarded_relayers_state` runtime method.
|
||||
pub const FROM_MILLAU_UNREWARDED_RELAYERS_STATE: &str = "FromMillauInboundLaneApi_unrewarded_relayers_state";
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for querying information about Millau headers from the Bridge Pallet instance.
|
||||
///
|
||||
/// This API is implemented by runtimes that are bridging with Millau chain, not the
|
||||
/// Millau runtime itself.
|
||||
pub trait MillauHeaderApi {
|
||||
/// Returns number and hash of the best blocks known to the bridge module.
|
||||
///
|
||||
/// Will return multiple headers if there are many headers at the same "best" height.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (BlockNumber, Hash);
|
||||
/// Returns numbers and hashes of headers that require finality proofs.
|
||||
///
|
||||
/// An empty response means that there are no headers which currently require a
|
||||
/// finality proof.
|
||||
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns true if the header is known to the runtime.
|
||||
fn is_known_block(hash: Hash) -> bool;
|
||||
/// Returns true if the header is considered finalized by the runtime.
|
||||
fn is_finalized_block(hash: Hash) -> bool;
|
||||
}
|
||||
|
||||
/// Outbound message lane API for messages that are sent to Millau chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are sending messages to Millau chain, not the
|
||||
/// Millau runtime itself.
|
||||
pub trait ToMillauOutboundLaneApi<OutboundMessageFee: Parameter, OutboundPayload: Parameter> {
|
||||
/// Estimate message delivery and dispatch fee that needs to be paid by the sender on
|
||||
/// this chain.
|
||||
///
|
||||
/// Returns `None` if message is too expensive to be sent to Millau from this chain.
|
||||
///
|
||||
/// Please keep in mind that this method returns lowest message fee required for message
|
||||
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
|
||||
/// future exchange rate changes and guarantee that relayer would deliver your message
|
||||
/// to the target chain.
|
||||
fn estimate_message_delivery_and_dispatch_fee(
|
||||
lane_id: LaneId,
|
||||
payload: OutboundPayload,
|
||||
) -> Option<OutboundMessageFee>;
|
||||
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
|
||||
///
|
||||
/// If some (or all) messages are missing from the storage, they'll also will
|
||||
/// be missing from the resulting vector. The vector is ordered by the nonce.
|
||||
fn messages_dispatch_weight(
|
||||
lane: LaneId,
|
||||
begin: MessageNonce,
|
||||
end: MessageNonce,
|
||||
) -> Vec<(MessageNonce, Weight, u32)>;
|
||||
/// Returns nonce of the latest message, received by bridged chain.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Returns nonce of the latest message, generated by given lane.
|
||||
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
|
||||
/// Inbound message lane API for messages sent by Millau chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are receiving messages from Millau chain, not the
|
||||
/// Millau runtime itself.
|
||||
pub trait FromMillauInboundLaneApi {
|
||||
/// Returns nonce of the latest message, received by given lane.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Nonce of latest message that has been confirmed to the bridged chain.
|
||||
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// State of the unrewarded relayers set at given lane.
|
||||
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_runtime::codec::Encode;
|
||||
|
||||
#[test]
|
||||
fn maximal_account_size_does_not_overflow_constant() {
|
||||
assert!(
|
||||
MAXIMAL_ENCODED_ACCOUNT_ID_SIZE as usize >= AccountId::default().encode().len(),
|
||||
"Actual maximal size of encoded AccountId ({}) overflows expected ({})",
|
||||
AccountId::default().encode().len(),
|
||||
MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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 parity_util_mem::MallocSizeOf;
|
||||
use sp_runtime::traits::CheckEqual;
|
||||
|
||||
// `sp_core::H512` can't be used, because it doesn't implement `CheckEqual`, which is required
|
||||
// by `frame_system::Config::Hash`.
|
||||
|
||||
fixed_hash::construct_fixed_hash! {
|
||||
/// Hash type used in Millau chain.
|
||||
#[derive(MallocSizeOf)]
|
||||
pub struct MillauHash(64);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl_serde::impl_fixed_hash_serde!(MillauHash, 64);
|
||||
|
||||
impl_codec::impl_fixed_hash_codec!(MillauHash, 64);
|
||||
|
||||
impl CheckEqual for MillauHash {
|
||||
#[cfg(feature = "std")]
|
||||
fn check_equal(&self, other: &Self) {
|
||||
use sp_core::hexdisplay::HexDisplay;
|
||||
if self != other {
|
||||
println!(
|
||||
"Hash: given={}, expected={}",
|
||||
HexDisplay::from(self.as_fixed_bytes()),
|
||||
HexDisplay::from(other.as_fixed_bytes()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn check_equal(&self, other: &Self) {
|
||||
use frame_support::Printable;
|
||||
|
||||
if self != other {
|
||||
"Hash not equal".print();
|
||||
self.as_bytes().print();
|
||||
other.as_bytes().print();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
[package]
|
||||
name = "bp-polkadot"
|
||||
description = "Primitives of Polkadot runtime."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Bridge Dependencies
|
||||
|
||||
bp-message-lane = { path = "../message-lane", default-features = false }
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
|
||||
# Substrate Based Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
frame-system = { 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-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-message-lane/std",
|
||||
"bp-runtime/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"sp-api/std",
|
||||
"sp-core/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,164 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Runtime-generated DecodeLimit::decode_all_with_depth_limit
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use bp_message_lane::{LaneId, MessageNonce};
|
||||
use bp_runtime::Chain;
|
||||
use frame_support::{weights::Weight, RuntimeDebug};
|
||||
use sp_core::Hasher as HasherT;
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, IdentifyAccount, Verify},
|
||||
MultiSignature, OpaqueExtrinsic as UncheckedExtrinsic,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
// TODO: may need to be updated after https://github.com/paritytech/parity-bridges-common/issues/78
|
||||
/// Maximal number of messages in single delivery transaction.
|
||||
pub const MAX_MESSAGES_IN_DELIVERY_TRANSACTION: MessageNonce = 128;
|
||||
|
||||
/// Maximal number of unrewarded relayer entries at inbound lane.
|
||||
pub const MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE: MessageNonce = 128;
|
||||
|
||||
// TODO: should be selected keeping in mind:
|
||||
// finality delay on both chains + reward payout cost + messages throughput.
|
||||
/// Maximal number of unconfirmed messages at inbound lane.
|
||||
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 8192;
|
||||
|
||||
/// Block number type used in Polkadot.
|
||||
pub type BlockNumber = u32;
|
||||
|
||||
/// Hash type used in Polkadot.
|
||||
pub type Hash = <BlakeTwo256 as HasherT>::Out;
|
||||
|
||||
/// The type of an object that can produce hashes on Polkadot.
|
||||
pub type Hasher = BlakeTwo256;
|
||||
|
||||
/// The header type used by Polkadot.
|
||||
pub type Header = generic::Header<BlockNumber, Hasher>;
|
||||
|
||||
/// Signature type used by Polkadot.
|
||||
pub type Signature = MultiSignature;
|
||||
|
||||
/// Public key of account on Polkadot chain.
|
||||
pub type AccountPublic = <Signature as Verify>::Signer;
|
||||
|
||||
/// Id of account on Polkadot chain.
|
||||
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
|
||||
|
||||
/// Index of a transaction on the Polkadot chain.
|
||||
pub type Nonce = u32;
|
||||
|
||||
/// Block type of Polkadot.
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
/// Polkadot block signed with a Justification.
|
||||
pub type SignedBlock = generic::SignedBlock<Block>;
|
||||
|
||||
/// The balance of an account on Polkadot.
|
||||
pub type Balance = u128;
|
||||
|
||||
/// Polkadot chain.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct Polkadot;
|
||||
|
||||
impl Chain for Polkadot {
|
||||
type BlockNumber = BlockNumber;
|
||||
type Hash = Hash;
|
||||
type Hasher = Hasher;
|
||||
type Header = Header;
|
||||
}
|
||||
|
||||
/// Convert a 256-bit hash into an AccountId.
|
||||
pub struct AccountIdConverter;
|
||||
|
||||
impl sp_runtime::traits::Convert<sp_core::H256, AccountId> for AccountIdConverter {
|
||||
fn convert(hash: sp_core::H256) -> AccountId {
|
||||
hash.to_fixed_bytes().into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Name of the `PolkadotHeaderApi::best_blocks` runtime method.
|
||||
pub const BEST_POLKADOT_BLOCKS_METHOD: &str = "PolkadotHeaderApi_best_blocks";
|
||||
/// Name of the `PolkadotHeaderApi::finalized_block` runtime method.
|
||||
pub const FINALIZED_POLKADOT_BLOCK_METHOD: &str = "PolkadotHeaderApi_finalized_block";
|
||||
/// Name of the `PolkadotHeaderApi::is_known_block` runtime method.
|
||||
pub const IS_KNOWN_POLKADOT_BLOCK_METHOD: &str = "PolkadotHeaderApi_is_known_block";
|
||||
/// Name of the `PolkadotHeaderApi::incomplete_headers` runtime method.
|
||||
pub const INCOMPLETE_POLKADOT_HEADERS_METHOD: &str = "PolkadotHeaderApi_incomplete_headers";
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for querying information about Polkadot headers from the Bridge Pallet instance.
|
||||
///
|
||||
/// This API is implemented by runtimes that are bridging with Polkadot chain, not the
|
||||
/// Polkadot runtime itself.
|
||||
pub trait PolkadotHeaderApi {
|
||||
/// Returns number and hash of the best blocks known to the bridge module.
|
||||
///
|
||||
/// Will return multiple headers if there are many headers at the same "best" height.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (BlockNumber, Hash);
|
||||
/// Returns numbers and hashes of headers that require finality proofs.
|
||||
///
|
||||
/// An empty response means that there are no headers which currently require a
|
||||
/// finality proof.
|
||||
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns true if the header is known to the runtime.
|
||||
fn is_known_block(hash: Hash) -> bool;
|
||||
/// Returns true if the header is considered finalized by the runtime.
|
||||
fn is_finalized_block(hash: Hash) -> bool;
|
||||
}
|
||||
|
||||
/// Outbound message lane API for messages that are sent to Polkadot chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are sending messages to Polkadot chain, not the
|
||||
/// Polkadot runtime itself.
|
||||
pub trait ToPolkadotOutboundLaneApi {
|
||||
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
|
||||
///
|
||||
/// If some (or all) messages are missing from the storage, they'll also will
|
||||
/// be missing from the resulting vector. The vector is ordered by the nonce.
|
||||
fn messages_dispatch_weight(
|
||||
lane: LaneId,
|
||||
begin: MessageNonce,
|
||||
end: MessageNonce,
|
||||
) -> Vec<(MessageNonce, Weight, u32)>;
|
||||
/// Returns nonce of the latest message, received by bridged chain.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Returns nonce of the latest message, generated by given lane.
|
||||
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
|
||||
/// Inbound message lane API for messages sent by Polkadot chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are receiving messages from Polkadot chain, not the
|
||||
/// Polkadot runtime itself.
|
||||
pub trait FromPolkadotInboundLaneApi {
|
||||
/// Returns nonce of the latest message, received by given lane.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Nonce of latest message that has been confirmed to the bridged chain.
|
||||
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
[package]
|
||||
name = "bp-rialto"
|
||||
description = "Primitives of Rialto runtime."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Bridge Dependencies
|
||||
|
||||
bp-message-lane = { path = "../message-lane", default-features = false }
|
||||
bp-runtime = { path = "../runtime", default-features = false }
|
||||
|
||||
# Substrate Based Dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
frame-system = { 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-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-message-lane/std",
|
||||
"bp-runtime/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"sp-api/std",
|
||||
"sp-core/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,306 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
// RuntimeApi generated functions
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
// Runtime-generated DecodeLimit::decode_all_With_depth_limit
|
||||
#![allow(clippy::unnecessary_mut_passed)]
|
||||
|
||||
use bp_message_lane::{LaneId, MessageNonce, UnrewardedRelayersState};
|
||||
use bp_runtime::Chain;
|
||||
use frame_support::{
|
||||
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
|
||||
Parameter, RuntimeDebug,
|
||||
};
|
||||
use frame_system::limits;
|
||||
use sp_core::Hasher as HasherT;
|
||||
use sp_runtime::{
|
||||
traits::{BlakeTwo256, Convert, IdentifyAccount, Verify},
|
||||
MultiSignature, MultiSigner, Perbill,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// Number of extra bytes (excluding size of storage value itself) of storage proof, built at
|
||||
/// Rialto chain. This mostly depends on number of entries (and their density) in the storage trie.
|
||||
/// Some reserve is reserved to account future chain growth.
|
||||
pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
|
||||
|
||||
/// Maximal size (in bytes) of encoded (using `Encode::encode()`) account id.
|
||||
pub const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 = 32;
|
||||
|
||||
/// Maximal weight of single Rialto block.
|
||||
///
|
||||
/// This represents two seconds of compute assuming a target block time of six seconds.
|
||||
pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
|
||||
|
||||
/// Represents the average portion of a block's weight that will be used by an
|
||||
/// `on_initialize()` runtime call.
|
||||
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
|
||||
|
||||
/// Represents the portion of a block that will be used by Normal extrinsics.
|
||||
pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
|
||||
|
||||
/// Maximal number of unrewarded relayer entries at inbound lane.
|
||||
pub const MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE: MessageNonce = 128;
|
||||
|
||||
/// Maximal number of unconfirmed messages at inbound lane.
|
||||
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 128;
|
||||
|
||||
/// Weight of single regular message delivery transaction on Rialto chain.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::Module::receive_messages_proof_weight()` call
|
||||
/// for the case when single message of `pallet_message_lane::EXPECTED_DEFAULT_MESSAGE_LENGTH` bytes is delivered.
|
||||
/// The message must have dispatch weight set to zero. The result then must be rounded up to account
|
||||
/// possible future runtime upgrades.
|
||||
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_000_000_000;
|
||||
|
||||
/// Increase of delivery transaction weight on Rialto chain with every additional message byte.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::WeightInfoExt::storage_proof_size_overhead(1)` call. The
|
||||
/// result then must be rounded up to account possible future runtime upgrades.
|
||||
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
|
||||
|
||||
/// Maximal weight of single message delivery confirmation transaction on Rialto chain.
|
||||
///
|
||||
/// This value is a result of `pallet_message_lane::Module::receive_messages_delivery_proof` weight formula computation
|
||||
/// for the case when single message is confirmed. The result then must be rounded up to account possible future
|
||||
/// runtime upgrades.
|
||||
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
|
||||
|
||||
/// The length of a session (how often authorities change) on Rialto measured in of number of blocks.
|
||||
pub const SESSION_LENGTH: BlockNumber = 4;
|
||||
|
||||
/// Re-export `time_units` to make usage easier.
|
||||
pub use time_units::*;
|
||||
|
||||
/// Human readable time units defined in terms of number of blocks.
|
||||
pub mod time_units {
|
||||
use super::BlockNumber;
|
||||
|
||||
pub const MILLISECS_PER_BLOCK: u64 = 6000;
|
||||
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
|
||||
|
||||
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
|
||||
pub const HOURS: BlockNumber = MINUTES * 60;
|
||||
pub const DAYS: BlockNumber = HOURS * 24;
|
||||
}
|
||||
|
||||
/// Block number type used in Rialto.
|
||||
pub type BlockNumber = u32;
|
||||
|
||||
/// Hash type used in Rialto.
|
||||
pub type Hash = <BlakeTwo256 as HasherT>::Out;
|
||||
|
||||
/// The type of an object that can produce hashes on Rialto.
|
||||
pub type Hasher = BlakeTwo256;
|
||||
|
||||
/// The header type used by Rialto.
|
||||
pub type Header = sp_runtime::generic::Header<BlockNumber, Hasher>;
|
||||
|
||||
/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
|
||||
pub type Signature = MultiSignature;
|
||||
|
||||
/// Some way of identifying an account on the chain. We intentionally make it equivalent
|
||||
/// to the public key of our transaction signing scheme.
|
||||
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
|
||||
|
||||
/// Public key of the chain account that may be used to verify signatures.
|
||||
pub type AccountSigner = MultiSigner;
|
||||
|
||||
/// Balance of an account.
|
||||
pub type Balance = u128;
|
||||
|
||||
/// Rialto chain.
|
||||
#[derive(RuntimeDebug)]
|
||||
pub struct Rialto;
|
||||
|
||||
impl Chain for Rialto {
|
||||
type BlockNumber = BlockNumber;
|
||||
type Hash = Hash;
|
||||
type Hasher = Hasher;
|
||||
type Header = Header;
|
||||
}
|
||||
|
||||
/// Convert a 256-bit hash into an AccountId.
|
||||
pub struct AccountIdConverter;
|
||||
|
||||
impl Convert<sp_core::H256, AccountId> for AccountIdConverter {
|
||||
fn convert(hash: sp_core::H256) -> AccountId {
|
||||
hash.to_fixed_bytes().into()
|
||||
}
|
||||
}
|
||||
|
||||
// We use this to get the account on Rialto (target) which is derived from Millau's (source)
|
||||
// account. We do this so we can fund the derived account on Rialto 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_millau_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
|
||||
let encoded_id = bp_runtime::derive_account_id(bp_runtime::MILLAU_BRIDGE_INSTANCE, id);
|
||||
AccountIdConverter::convert(encoded_id)
|
||||
}
|
||||
|
||||
frame_support::parameter_types! {
|
||||
pub BlockLength: limits::BlockLength =
|
||||
limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
|
||||
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
|
||||
// Allowance for Normal class
|
||||
.for_class(DispatchClass::Normal, |weights| {
|
||||
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
|
||||
})
|
||||
// Allowance for Operational class
|
||||
.for_class(DispatchClass::Operational, |weights| {
|
||||
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
|
||||
// Extra reserved space for Operational class
|
||||
weights.reserved = Some(MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
|
||||
})
|
||||
// By default Mandatory class is not limited at all.
|
||||
// This parameter is used to derive maximal size of a single extrinsic.
|
||||
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
|
||||
.build_or_panic();
|
||||
}
|
||||
|
||||
/// Get the maximum weight (compute time) that a Normal extrinsic on the Millau chain can use.
|
||||
pub fn max_extrinsic_weight() -> Weight {
|
||||
BlockWeights::get()
|
||||
.get(DispatchClass::Normal)
|
||||
.max_extrinsic
|
||||
.unwrap_or(Weight::MAX)
|
||||
}
|
||||
|
||||
/// Get the maximum length in bytes that a Normal extrinsic on the Millau chain requires.
|
||||
pub fn max_extrinsic_size() -> u32 {
|
||||
*BlockLength::get().max.get(DispatchClass::Normal)
|
||||
}
|
||||
|
||||
/// Name of the `RialtoHeaderApi::best_blocks` runtime method.
|
||||
pub const BEST_RIALTO_BLOCKS_METHOD: &str = "RialtoHeaderApi_best_blocks";
|
||||
/// Name of the `RialtoHeaderApi::finalized_block` runtime method.
|
||||
pub const FINALIZED_RIALTO_BLOCK_METHOD: &str = "RialtoHeaderApi_finalized_block";
|
||||
/// Name of the `RialtoHeaderApi::is_known_block` runtime method.
|
||||
pub const IS_KNOWN_RIALTO_BLOCK_METHOD: &str = "RialtoHeaderApi_is_known_block";
|
||||
/// Name of the `RialtoHeaderApi::incomplete_headers` runtime method.
|
||||
pub const INCOMPLETE_RIALTO_HEADERS_METHOD: &str = "RialtoHeaderApi_incomplete_headers";
|
||||
|
||||
/// Name of the `ToRialtoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
|
||||
pub const TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD: &str =
|
||||
"ToRialtoOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
|
||||
/// Name of the `ToRialtoOutboundLaneApi::messages_dispatch_weight` runtime method.
|
||||
pub const TO_RIALTO_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToRialtoOutboundLaneApi_messages_dispatch_weight";
|
||||
/// Name of the `ToRialtoOutboundLaneApi::latest_generated_nonce` runtime method.
|
||||
pub const TO_RIALTO_LATEST_GENERATED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_generated_nonce";
|
||||
/// Name of the `ToRialtoOutboundLaneApi::latest_received_nonce` runtime method.
|
||||
pub const TO_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_received_nonce";
|
||||
|
||||
/// Name of the `FromRialtoInboundLaneApi::latest_received_nonce` runtime method.
|
||||
pub const FROM_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_received_nonce";
|
||||
/// Name of the `FromRialtoInboundLaneApi::latest_onfirmed_nonce` runtime method.
|
||||
pub const FROM_RIALTO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_confirmed_nonce";
|
||||
/// Name of the `FromRialtoInboundLaneApi::unrewarded_relayers_state` runtime method.
|
||||
pub const FROM_RIALTO_UNREWARDED_RELAYERS_STATE: &str = "FromRialtoInboundLaneApi_unrewarded_relayers_state";
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
/// API for querying information about Rialto headers from the Bridge Pallet instance.
|
||||
///
|
||||
/// This API is implemented by runtimes that are bridging with Rialto chain, not the
|
||||
/// Rialto runtime itself.
|
||||
pub trait RialtoHeaderApi {
|
||||
/// Returns number and hash of the best blocks known to the bridge module.
|
||||
///
|
||||
/// Will return multiple headers if there are many headers at the same "best" height.
|
||||
///
|
||||
/// The caller should only submit an `import_header` transaction that makes
|
||||
/// (or leads to making) other header the best one.
|
||||
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns number and hash of the best finalized block known to the bridge module.
|
||||
fn finalized_block() -> (BlockNumber, Hash);
|
||||
/// Returns numbers and hashes of headers that require finality proofs.
|
||||
///
|
||||
/// An empty response means that there are no headers which currently require a
|
||||
/// finality proof.
|
||||
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
|
||||
/// Returns true if the header is known to the runtime.
|
||||
fn is_known_block(hash: Hash) -> bool;
|
||||
/// Returns true if the header is considered finalized by the runtime.
|
||||
fn is_finalized_block(hash: Hash) -> bool;
|
||||
}
|
||||
|
||||
/// Outbound message lane API for messages that are sent to Rialto chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are sending messages to Rialto chain, not the
|
||||
/// Rialto runtime itself.
|
||||
pub trait ToRialtoOutboundLaneApi<OutboundMessageFee: Parameter, OutboundPayload: Parameter> {
|
||||
/// Estimate message delivery and dispatch fee that needs to be paid by the sender on
|
||||
/// this chain.
|
||||
///
|
||||
/// Returns `None` if message is too expensive to be sent to Rialto from this chain.
|
||||
///
|
||||
/// Please keep in mind that this method returns lowest message fee required for message
|
||||
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
|
||||
/// future exchange rate changes and guarantee that relayer would deliver your message
|
||||
/// to the target chain.
|
||||
fn estimate_message_delivery_and_dispatch_fee(
|
||||
lane_id: LaneId,
|
||||
payload: OutboundPayload,
|
||||
) -> Option<OutboundMessageFee>;
|
||||
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
|
||||
///
|
||||
/// If some (or all) messages are missing from the storage, they'll also will
|
||||
/// be missing from the resulting vector. The vector is ordered by the nonce.
|
||||
fn messages_dispatch_weight(
|
||||
lane: LaneId,
|
||||
begin: MessageNonce,
|
||||
end: MessageNonce,
|
||||
) -> Vec<(MessageNonce, Weight, u32)>;
|
||||
/// Returns nonce of the latest message, received by bridged chain.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Returns nonce of the latest message, generated by given lane.
|
||||
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
|
||||
}
|
||||
|
||||
/// Inbound message lane API for messages sent by Rialto chain.
|
||||
///
|
||||
/// This API is implemented by runtimes that are receiving messages from Rialto chain, not the
|
||||
/// Rialto runtime itself.
|
||||
pub trait FromRialtoInboundLaneApi {
|
||||
/// Returns nonce of the latest message, received by given lane.
|
||||
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// Nonce of latest message that has been confirmed to the bridged chain.
|
||||
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
|
||||
/// State of the unrewarded relayers set at given lane.
|
||||
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_runtime::codec::Encode;
|
||||
|
||||
#[test]
|
||||
fn maximal_account_size_does_not_overflow_constant() {
|
||||
assert!(
|
||||
MAXIMAL_ENCODED_ACCOUNT_ID_SIZE as usize >= AccountId::default().encode().len(),
|
||||
"Actual maximal size of encoded AccountId ({}) overflows expected ({})",
|
||||
AccountId::default().encode().len(),
|
||||
MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
[package]
|
||||
name = "bp-runtime"
|
||||
description = "Primitives that may be used at (bridges) runtime level."
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
|
||||
num-traits = { version = "0.2", default-features = false }
|
||||
|
||||
# Substrate Dependencies
|
||||
|
||||
frame-support = { 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 }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"num-traits/std",
|
||||
"sp-core/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
@@ -1,87 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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 frame_support::Parameter;
|
||||
use num_traits::AsPrimitive;
|
||||
use sp_runtime::traits::{
|
||||
AtLeast32BitUnsigned, Hash as HashT, Header as HeaderT, MaybeDisplay, MaybeMallocSizeOf, MaybeSerializeDeserialize,
|
||||
Member, SimpleBitOps,
|
||||
};
|
||||
use sp_std::str::FromStr;
|
||||
|
||||
/// Minimal Substrate-based chain representation that may be used from no_std environment.
|
||||
pub trait Chain: Send + Sync + 'static {
|
||||
/// A type that fulfills the abstract idea of what a Substrate block number is.
|
||||
// Constraits come from the associated Number type of `sp_runtime::traits::Header`
|
||||
// See here for more info:
|
||||
// https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Number
|
||||
//
|
||||
// Note that the `AsPrimitive<usize>` trait is required by the GRANDPA justification
|
||||
// verifier, and is not usually part of a Substrate Header's Number type.
|
||||
type BlockNumber: Parameter
|
||||
+ Member
|
||||
+ MaybeSerializeDeserialize
|
||||
+ sp_std::hash::Hash
|
||||
+ Copy
|
||||
+ Default
|
||||
+ MaybeDisplay
|
||||
+ AtLeast32BitUnsigned
|
||||
+ FromStr
|
||||
+ MaybeMallocSizeOf
|
||||
+ AsPrimitive<usize>
|
||||
+ Default;
|
||||
|
||||
/// A type that fulfills the abstract idea of what a Substrate hash is.
|
||||
// Constraits come from the associated Hash type of `sp_runtime::traits::Header`
|
||||
// See here for more info:
|
||||
// https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hash
|
||||
type Hash: Parameter
|
||||
+ Member
|
||||
+ MaybeSerializeDeserialize
|
||||
+ sp_std::hash::Hash
|
||||
+ Ord
|
||||
+ Copy
|
||||
+ MaybeDisplay
|
||||
+ Default
|
||||
+ SimpleBitOps
|
||||
+ AsRef<[u8]>
|
||||
+ AsMut<[u8]>
|
||||
+ MaybeMallocSizeOf;
|
||||
|
||||
/// A type that fulfills the abstract idea of what a Substrate hasher (a type
|
||||
/// that produces hashes) is.
|
||||
// Constraits come from the associated Hashing type of `sp_runtime::traits::Header`
|
||||
// See here for more info:
|
||||
// https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hashing
|
||||
type Hasher: HashT<Output = Self::Hash>;
|
||||
|
||||
/// A type that fulfills the abstract idea of what a Substrate header is.
|
||||
// See here for more info:
|
||||
// https://crates.parity.io/sp_runtime/traits/trait.Header.html
|
||||
type Header: Parameter + HeaderT<Number = Self::BlockNumber, Hash = Self::Hash> + MaybeSerializeDeserialize;
|
||||
}
|
||||
|
||||
/// Block number used by the chain.
|
||||
pub type BlockNumberOf<C> = <C as Chain>::BlockNumber;
|
||||
|
||||
/// Hash type used by the chain.
|
||||
pub type HashOf<C> = <C as Chain>::Hash;
|
||||
|
||||
/// Hasher type used by the chain.
|
||||
pub type HasherOf<C> = <C as Chain>::Hasher;
|
||||
|
||||
/// Header type used by the chain.
|
||||
pub type HeaderOf<C> = <C as Chain>::Header;
|
||||
@@ -1,128 +0,0 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Primitives that may be used at (bridges) runtime level.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use codec::Encode;
|
||||
use sp_core::hash::H256;
|
||||
use sp_io::hashing::blake2_256;
|
||||
use sp_std::convert::TryFrom;
|
||||
|
||||
pub use chain::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf};
|
||||
|
||||
mod chain;
|
||||
|
||||
/// Use this when something must be shared among all instances.
|
||||
pub const NO_INSTANCE_ID: InstanceId = [0, 0, 0, 0];
|
||||
|
||||
/// Bridge-with-Rialto instance id.
|
||||
pub const RIALTO_BRIDGE_INSTANCE: InstanceId = *b"rlto";
|
||||
|
||||
/// Bridge-with-Millau instance id.
|
||||
pub const MILLAU_BRIDGE_INSTANCE: InstanceId = *b"mlau";
|
||||
|
||||
/// Bridge-with-Polkadot instance id.
|
||||
pub const POLKADOT_BRIDGE_INSTANCE: InstanceId = *b"pdot";
|
||||
|
||||
/// Bridge-with-Kusama instance id.
|
||||
pub const KUSAMA_BRIDGE_INSTANCE: InstanceId = *b"ksma";
|
||||
|
||||
/// Call-dispatch module prefix.
|
||||
pub const CALL_DISPATCH_MODULE_PREFIX: &[u8] = b"pallet-bridge/call-dispatch";
|
||||
|
||||
/// Message-lane module prefix.
|
||||
pub const MESSAGE_LANE_MODULE_PREFIX: &[u8] = b"pallet-bridge/message-lane";
|
||||
|
||||
/// A unique prefix for entropy when generating cross-chain account IDs.
|
||||
pub const ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/account";
|
||||
|
||||
/// A unique prefix for entropy when generating a cross-chain account ID for the Root account.
|
||||
pub const ROOT_ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/root";
|
||||
|
||||
/// Id of deployed module instance. We have a bunch of pallets that may be used in
|
||||
/// different bridges. E.g. message-lane pallet may be deployed twice in the same
|
||||
/// runtime to bridge ThisChain with Chain1 and Chain2. Sometimes we need to be able
|
||||
/// to identify deployed instance dynamically. This type is used for that.
|
||||
pub type InstanceId = [u8; 4];
|
||||
|
||||
/// Type of accounts on the source chain.
|
||||
pub enum SourceAccount<T> {
|
||||
/// An account that belongs to Root (privileged origin).
|
||||
Root,
|
||||
/// A non-privileged account.
|
||||
///
|
||||
/// The embedded account ID may or may not have a private key depending on the "owner" of the
|
||||
/// account (private key, pallet, proxy, etc.).
|
||||
Account(T),
|
||||
}
|
||||
|
||||
/// Derive an account ID from a foreign account ID.
|
||||
///
|
||||
/// This function returns an encoded Blake2 hash. It is the responsibility of the caller to ensure
|
||||
/// this can be succesfully decoded into an AccountId.
|
||||
///
|
||||
/// The `bridge_id` is used to provide extra entropy when producing account IDs. This helps prevent
|
||||
/// AccountId collisions between different bridges on a single target chain.
|
||||
///
|
||||
/// Note: If the same `bridge_id` is used across different chains (for example, if one source chain
|
||||
/// is bridged to multiple target chains), then all the derived accounts would be the same across
|
||||
/// the different chains. This could negatively impact users' privacy across chains.
|
||||
pub fn derive_account_id<AccountId>(bridge_id: InstanceId, id: SourceAccount<AccountId>) -> H256
|
||||
where
|
||||
AccountId: Encode,
|
||||
{
|
||||
match id {
|
||||
SourceAccount::Root => (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256),
|
||||
SourceAccount::Account(id) => (ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Derive the account ID of the shared relayer fund account.
|
||||
///
|
||||
/// This account is used to collect fees for relayers that are passing messages across the bridge.
|
||||
///
|
||||
/// The account ID can be the same across different instances of `message-lane` if the same
|
||||
/// `bridge_id` is used.
|
||||
pub fn derive_relayer_fund_account_id(bridge_id: InstanceId) -> H256 {
|
||||
("relayer-fund-account", bridge_id).using_encoded(blake2_256).into()
|
||||
}
|
||||
|
||||
/// Anything that has size.
|
||||
pub trait Size {
|
||||
/// Return approximate size of this object (in bytes).
|
||||
///
|
||||
/// This function should be lightweight. The result should not necessary be absolutely
|
||||
/// accurate.
|
||||
fn size_hint(&self) -> u32;
|
||||
}
|
||||
|
||||
impl Size for () {
|
||||
fn size_hint(&self) -> u32 {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Pre-computed size.
|
||||
pub struct PreComputedSize(pub usize);
|
||||
|
||||
impl Size for PreComputedSize {
|
||||
fn size_hint(&self) -> u32 {
|
||||
u32::try_from(self.0).unwrap_or(u32::MAX)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
[package]
|
||||
name = "bp-test-utils"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
finality-grandpa = { version = "0.14.0" }
|
||||
bp-header-chain = { path = "../header-chain" }
|
||||
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
|
||||
@@ -1,151 +0,0 @@
|
||||
// Copyright 2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity Bridges Common.
|
||||
|
||||
// Parity Bridges Common is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity Bridges Common is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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/>.
|
||||
|
||||
//! Utilities for testing runtime code.
|
||||
//!
|
||||
//! Unlike other crates in the `primitives` folder, this crate does *not* need to compile in a
|
||||
//! `no_std` environment. This is fine because this code should only be used, as the name implies,
|
||||
//! in tests.
|
||||
|
||||
use bp_header_chain::justification::GrandpaJustification;
|
||||
use finality_grandpa::voter_set::VoterSet;
|
||||
use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight};
|
||||
use sp_finality_grandpa::{AuthoritySignature, SetId};
|
||||
use sp_keyring::Ed25519Keyring;
|
||||
use sp_runtime::traits::Header as HeaderT;
|
||||
use sp_runtime::traits::{One, Zero};
|
||||
|
||||
pub const TEST_GRANDPA_ROUND: u64 = 1;
|
||||
pub const TEST_GRANDPA_SET_ID: SetId = 1;
|
||||
|
||||
/// Get a valid Grandpa justification for a header given a Grandpa round, authority set ID, and
|
||||
/// authority list.
|
||||
pub fn make_justification_for_header<H: HeaderT>(
|
||||
header: &H,
|
||||
round: u64,
|
||||
set_id: SetId,
|
||||
authorities: &[(AuthorityId, AuthorityWeight)],
|
||||
) -> GrandpaJustification<H> {
|
||||
let (target_hash, target_number) = (header.hash(), *header.number());
|
||||
let mut precommits = vec![];
|
||||
let mut votes_ancestries = vec![];
|
||||
|
||||
// We want to make sure that the header included in the vote ancestries
|
||||
// is actually related to our target header
|
||||
let mut precommit_header = test_header::<H>(target_number + One::one());
|
||||
precommit_header.set_parent_hash(target_hash);
|
||||
|
||||
// I'm using the same header for all the voters since it doesn't matter as long
|
||||
// as they all vote on blocks _ahead_ of the one we're interested in finalizing
|
||||
for (id, _weight) in authorities.iter() {
|
||||
let signer = extract_keyring(&id);
|
||||
let precommit = signed_precommit::<H>(
|
||||
signer,
|
||||
(precommit_header.hash(), *precommit_header.number()),
|
||||
round,
|
||||
set_id,
|
||||
);
|
||||
precommits.push(precommit);
|
||||
votes_ancestries.push(precommit_header.clone());
|
||||
}
|
||||
|
||||
GrandpaJustification {
|
||||
round,
|
||||
commit: finality_grandpa::Commit {
|
||||
target_hash,
|
||||
target_number,
|
||||
precommits,
|
||||
},
|
||||
votes_ancestries,
|
||||
}
|
||||
}
|
||||
|
||||
fn signed_precommit<H: HeaderT>(
|
||||
signer: Ed25519Keyring,
|
||||
target: (H::Hash, H::Number),
|
||||
round: u64,
|
||||
set_id: SetId,
|
||||
) -> finality_grandpa::SignedPrecommit<H::Hash, H::Number, AuthoritySignature, AuthorityId> {
|
||||
let precommit = finality_grandpa::Precommit {
|
||||
target_hash: target.0,
|
||||
target_number: target.1,
|
||||
};
|
||||
let encoded =
|
||||
sp_finality_grandpa::localized_payload(round, set_id, &finality_grandpa::Message::Precommit(precommit.clone()));
|
||||
let signature = signer.sign(&encoded[..]).into();
|
||||
finality_grandpa::SignedPrecommit {
|
||||
precommit,
|
||||
signature,
|
||||
id: signer.public().into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a header for testing.
|
||||
///
|
||||
/// The correct parent hash will be used if given a non-zero header.
|
||||
pub fn test_header<H: HeaderT>(number: H::Number) -> H {
|
||||
let mut header = H::new(
|
||||
number,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
if number != Zero::zero() {
|
||||
let parent_hash = test_header::<H>(number - One::one()).hash();
|
||||
header.set_parent_hash(parent_hash);
|
||||
}
|
||||
|
||||
header
|
||||
}
|
||||
|
||||
/// Convenience function for generating a Header ID at a given block number.
|
||||
pub fn header_id<H: HeaderT>(index: u8) -> (H::Hash, H::Number) {
|
||||
(test_header::<H>(index.into()).hash(), index.into())
|
||||
}
|
||||
|
||||
/// Get the identity of a test account given an ED25519 Public key.
|
||||
pub fn extract_keyring(id: &AuthorityId) -> Ed25519Keyring {
|
||||
let mut raw_public = [0; 32];
|
||||
raw_public.copy_from_slice(id.as_ref());
|
||||
Ed25519Keyring::from_raw_public(raw_public).unwrap()
|
||||
}
|
||||
|
||||
/// Get a valid set of voters for a Grandpa round.
|
||||
pub fn voter_set() -> VoterSet<AuthorityId> {
|
||||
VoterSet::new(authority_list()).unwrap()
|
||||
}
|
||||
|
||||
/// Convenience function to get a list of Grandpa authorities.
|
||||
pub fn authority_list() -> AuthorityList {
|
||||
vec![(alice(), 1), (bob(), 1), (charlie(), 1)]
|
||||
}
|
||||
|
||||
/// Get the Public key of the Alice test account.
|
||||
pub fn alice() -> AuthorityId {
|
||||
Ed25519Keyring::Alice.public().into()
|
||||
}
|
||||
|
||||
/// Get the Public key of the Bob test account.
|
||||
pub fn bob() -> AuthorityId {
|
||||
Ed25519Keyring::Bob.public().into()
|
||||
}
|
||||
|
||||
/// Get the Public key of the Charlie test account.
|
||||
pub fn charlie() -> AuthorityId {
|
||||
Ed25519Keyring::Charlie.public().into()
|
||||
}
|
||||
Reference in New Issue
Block a user