Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub type AccountIndex = u32;
pub type Balance = bp_millau::Balance;

/// Index of a transaction in the chain.
pub type Index = u32;
pub type Index = bp_millau::Index;

/// A hash of some data used by the chain.
pub type Hash = bp_millau::Hash;
Expand Down
2 changes: 1 addition & 1 deletion bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub type AccountIndex = u32;
pub type Balance = bp_rialto::Balance;

/// Index of a transaction in the chain.
pub type Index = u32;
pub type Index = bp_rialto::Index;

/// A hash of some data used by the chain.
pub type Hash = bp_rialto::Hash;
Expand Down
7 changes: 6 additions & 1 deletion modules/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use frame_support::{construct_runtime, parameter_types, weights::Weight};
use sp_runtime::{
testing::{Header, H256},
traits::{BlakeTwo256, IdentityLookup},
Perbill,
AnySignature, Perbill,
};

pub type AccountId = u64;
Expand Down Expand Up @@ -101,6 +101,11 @@ impl Chain for TestBridgedChain {
type Hash = <TestRuntime as frame_system::Config>::Hash;
type Hasher = <TestRuntime as frame_system::Config>::Hashing;
type Header = <TestRuntime as frame_system::Config>::Header;

type AccountId = AccountId;
type Balance = u64;
type Index = u64;
type Signature = AnySignature;
}

pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
Expand Down
88 changes: 51 additions & 37 deletions modules/token-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,39 @@ pub mod pallet {
/// Converter from raw hash (derived from swap) to This chain account.
type FromSwapToThisAccountIdConverter: Convert<H256, Self::AccountId>;

/// Tokens balance type at the Bridged chain.
type BridgedBalance: Parameter;
/// Account identifier type at the Bridged chain.
type BridgedAccountId: Parameter;
/// Account public key type at the Bridged chain.
type BridgedAccountPublic: Parameter;
/// Account signature type at the Bridged chain.
type BridgedAccountSignature: Parameter;
/// The chain we're bridged to.
type BridgedChain: bp_runtime::Chain;
/// Converter from raw hash (derived from Bridged chain account) to This chain account.
type FromBridgedToThisAccountIdConverter: Convert<H256, Self::AccountId>;
}

/// Type of the Bridged chain.
pub type BridgedChainOf<T, I> = <T as Config<I>>::BridgedChain;
/// Tokens balance type at the Bridged chain.
pub type BridgedBalanceOf<T, I> = bp_runtime::BalanceOf<BridgedChainOf<T, I>>;
/// Account identifier type at the Bridged chain.
pub type BridgedAccountIdOf<T, I> = bp_runtime::AccountIdOf<BridgedChainOf<T, I>>;
/// Account public key type at the Bridged chain.
pub type BridgedAccountPublicOf<T, I> = bp_runtime::AccountPublicOf<BridgedChainOf<T, I>>;
/// Account signature type at the Bridged chain.
pub type BridgedAccountSignatureOf<T, I> = bp_runtime::SignatureOf<BridgedChainOf<T, I>>;

/// SCALE-encoded `Currency::transfer` call on the bridged chain.
pub type RawBridgedTransferCall = Vec<u8>;
/// Bridge message payload used by the pallet.
pub type MessagePayloadOf<T, I> = bp_message_dispatch::MessagePayload<
<T as frame_system::Config>::AccountId,
<T as Config<I>>::BridgedAccountPublic,
<T as Config<I>>::BridgedAccountSignature,
BridgedAccountPublicOf<T, I>,
BridgedAccountSignatureOf<T, I>,
RawBridgedTransferCall,
>;
/// Type of `TokenSwap` used by the pallet.
pub type TokenSwapOf<T, I> = TokenSwap<
BlockNumberFor<T>,
<<T as Config<I>>::ThisCurrency as Currency<<T as frame_system::Config>::AccountId>>::Balance,
<T as frame_system::Config>::AccountId,
<T as Config<I>>::BridgedBalance,
<T as Config<I>>::BridgedAccountId,
BridgedBalanceOf<T, I>,
BridgedAccountIdOf<T, I>,
>;

#[pallet::pallet]
Expand All @@ -160,7 +165,10 @@ pub mod pallet {
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
impl<T: Config<I>, I: 'static> Pallet<T, I>
where
BridgedAccountPublicOf<T, I>: Parameter,
{
/// Start token swap procedure.
///
/// The dispatch origin for this call must be exactly the `swap.source_account_at_this_chain` account.
Expand Down Expand Up @@ -194,11 +202,11 @@ pub mod pallet {
pub fn create_swap(
origin: OriginFor<T>,
swap: TokenSwapOf<T, I>,
target_public_at_bridged_chain: T::BridgedAccountPublic,
target_public_at_bridged_chain: BridgedAccountPublicOf<T, I>,
bridged_chain_spec_version: u32,
bridged_currency_transfer: RawBridgedTransferCall,
bridged_currency_transfer_weight: Weight,
bridged_currency_transfer_signature: T::BridgedAccountSignature,
bridged_currency_transfer_signature: BridgedAccountSignatureOf<T, I>,
) -> DispatchResultWithPostInfo {
// ensure that the `origin` is the same account that is mentioned in the `swap` intention
let origin_account = ensure_signed(origin)?;
Expand Down Expand Up @@ -541,12 +549,18 @@ mod tests {
const CAN_START_BLOCK_NUMBER: u64 = 10;
const CAN_CLAIM_BLOCK_NUMBER: u64 = CAN_START_BLOCK_NUMBER + 1;

const BRIDGED_CHAIN_ACCOUNT_PUBLIC: BridgedAccountPublic = 1;
const BRIDGED_CHAIN_ACCOUNT_SIGNATURE: BridgedAccountSignature = 2;
const BRIDGED_CHAIN_ACCOUNT: BridgedAccountId = 3;
const BRIDGED_CHAIN_SPEC_VERSION: u32 = 4;
const BRIDGED_CHAIN_CALL_WEIGHT: Balance = 5;

fn bridged_chain_account_public() -> BridgedAccountPublic {
1.into()
}

fn bridged_chain_account_signature() -> BridgedAccountSignature {
sp_runtime::testing::TestSignature(2, Vec::new())
}

fn test_swap() -> TokenSwapOf<TestRuntime, ()> {
bp_token_swap::TokenSwap {
swap_type: TokenSwapType::LockClaimUntilBlock(CAN_START_BLOCK_NUMBER, 0.into()),
Expand All @@ -569,11 +583,11 @@ mod tests {
assert_ok!(Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
));
}

Expand All @@ -591,11 +605,11 @@ mod tests {
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT + 1),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::MismatchedSwapSourceOrigin
);
Expand All @@ -611,11 +625,11 @@ mod tests {
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
swap,
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::TooLowBalanceOnThisChain
);
Expand All @@ -631,11 +645,11 @@ mod tests {
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
swap,
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::FailedToTransferToSwapAccount
);
Expand All @@ -651,11 +665,11 @@ mod tests {
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
transfer,
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::FailedToSendTransferMessage
);
Expand All @@ -668,22 +682,22 @@ mod tests {
assert_ok!(Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
));

assert_noop!(
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::SwapAlreadyStarted
);
Expand All @@ -698,11 +712,11 @@ mod tests {
Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
),
Error::<TestRuntime, ()>::SwapPeriodIsFinished
);
Expand All @@ -716,11 +730,11 @@ mod tests {
assert_ok!(Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
));
});
}
Expand All @@ -734,11 +748,11 @@ mod tests {
assert_ok!(Pallet::<TestRuntime>::create_swap(
Origin::signed(THIS_CHAIN_ACCOUNT),
test_swap(),
BRIDGED_CHAIN_ACCOUNT_PUBLIC,
bridged_chain_account_public(),
BRIDGED_CHAIN_SPEC_VERSION,
test_transfer(),
BRIDGED_CHAIN_CALL_WEIGHT,
BRIDGED_CHAIN_ACCOUNT_SIGNATURE,
bridged_chain_account_signature(),
));

let swap_hash = test_swap_hash();
Expand Down
23 changes: 17 additions & 6 deletions modules/token-swap/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub type AccountId = u64;
pub type Balance = u64;
pub type Block = frame_system::mocking::MockBlock<TestRuntime>;
pub type BridgedAccountId = u64;
pub type BridgedAccountPublic = u64;
pub type BridgedAccountSignature = u64;
pub type BridgedAccountPublic = sp_runtime::testing::UintAuthorityId;
pub type BridgedAccountSignature = sp_runtime::testing::TestSignature;
pub type BridgedBalance = u64;
pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;

Expand Down Expand Up @@ -122,13 +122,24 @@ impl pallet_bridge_token_swap::Config for TestRuntime {
type ThisCurrency = pallet_balances::Pallet<TestRuntime>;
type FromSwapToThisAccountIdConverter = TestAccountConverter;

type BridgedBalance = BridgedBalance;
type BridgedAccountId = BridgedAccountId;
type BridgedAccountPublic = BridgedAccountPublic;
type BridgedAccountSignature = BridgedAccountSignature;
type BridgedChain = BridgedChain;
type FromBridgedToThisAccountIdConverter = TestAccountConverter;
}

pub struct BridgedChain;

impl bp_runtime::Chain for BridgedChain {
type BlockNumber = u64;
type Hash = H256;
type Hasher = BlakeTwo256;
type Header = sp_runtime::generic::Header<u64, BlakeTwo256>;

type AccountId = BridgedAccountId;
type Balance = BridgedBalance;
type Index = u64;
type Signature = BridgedAccountSignature;
}

pub struct TestMessagesBridge;

impl MessagesBridge<AccountId, Balance, MessagePayloadOf<TestRuntime, ()>> for TestMessagesBridge {
Expand Down
8 changes: 8 additions & 0 deletions primitives/chain-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub type AccountSigner = MultiSigner;
/// Balance of an account.
pub type Balance = u64;

/// Index of a transaction in the chain.
pub type Index = u32;

/// Weight-to-Fee type used by Millau.
pub type WeightToFee = IdentityFee<Balance>;

Expand All @@ -161,6 +164,11 @@ impl Chain for Millau {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;

type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}

/// Millau Hasher (Blake2-256 ++ Keccak-256) implementation.
Expand Down
8 changes: 8 additions & 0 deletions primitives/chain-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub type Balance = u128;
/// An instant or duration in time.
pub type Moment = u64;

/// Index of a transaction in the chain.
pub type Index = u32;

/// Weight-to-Fee type used by Rialto.
pub type WeightToFee = IdentityFee<Balance>;

Expand All @@ -160,6 +163,11 @@ impl Chain for Rialto {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;

type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}

/// Convert a 256-bit hash into an AccountId.
Expand Down
5 changes: 5 additions & 0 deletions primitives/polkadot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ impl Chain for PolkadotLike {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;

type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}

/// Convert a 256-bit hash into an AccountId.
Expand Down
Loading