Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

add issuing pallet #85

Merged
merged 25 commits into from
Jun 6, 2022
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
701 changes: 399 additions & 302 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ serde = { version = "1.0", features = ["derive"] }
tempfile = { version = "3.2" }

# parachain
crab-parachain-runtime = { path = "runtime/crab-parachain" }
darwinia-parachain-runtime = { path = "runtime/darwinia-parachain" }
dc-primitives = { path = "primitives" }
dc-rpc = { path = "rpc" }
pangolin-parachain-runtime = { path = "runtime/pangolin-parachain" }
crab-parachain-runtime = { path = "runtime/crab-parachain" }
darwinia-parachain-runtime = { path = "runtime/darwinia-parachain" }
dc-primitives = { path = "primitives" }
dc-rpc = { path = "rpc" }
pangolin-parachain-runtime = { path = "runtime/pangolin-parachain" }

# substrate
frame-benchmarking = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
Expand Down
22 changes: 22 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,64 @@ version = "5.2.1"

[dependencies]
# crates.io
array-bytes = { version = "1.5" }
codec = { package = "parity-scale-codec", version = "3.1", default-features = false, features = ["derive"] }
ethereum-types = { version = "0.12", default-features = false }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }
smallvec = { version = "1.8" }
# cumulus
pallet-collator-selection = { default-features = false, git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.18" }
# darwinia-bridges-substrate
bp-messages = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
bp-message-dispatch = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
bp-runtime = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
bridge-runtime-common = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
pallet-bridge-dispatch = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
pallet-fee-market = { default-features = false, git = "https://github.com/darwinia-network/darwinia-bridges-substrate", branch = "polkadot-v0.9.18" }
# darwinia-collator
dc-primitives = { default-features = false, path = "../../primitives" }
# substrate
frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
pallet-transaction-payment = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }

[dev-dependencies]
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.18" }

[features]
default = ["std"]

std = [
# crates.io
"codec/std",
"ethereum-types/std",
"scale-info/std",
# cumulus
"pallet-collator-selection/std",
# darwinia-bridges-substrate
"bp-messages/std",
"bp-message-dispatch/std",
"bp-runtime/std",
"bridge-runtime-common/std",
"pallet-bridge-dispatch/std",
"pallet-fee-market/std",
# darwinia-collator
"dc-primitives/std",
# substrate
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-transaction-payment/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]

runtime-benchmarks = ["frame-benchmarking"]
69 changes: 69 additions & 0 deletions runtime/common/src/helixbridge/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

// --- crates.io ---
use array_bytes::hex2bytes_unchecked;
// --- paritytech ---
use frame_benchmarking::benchmarks;
use frame_support::assert_ok;
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sp_runtime::traits::Zero;
// --- darwinia-network ---
use crate::*;

use crate::Pallet as ParaIssuing;

benchmarks! {
issue_from_remote {
let caller_bytes = hex2bytes_unchecked("0x28f900e9928c356287bb8806c9044168560dee80b91d2015653c9639a4998d8d");
let caller: T::AccountId = T::AccountId::decode(&mut &caller_bytes[..]).unwrap_or_default();
let addr_bytes = hex2bytes_unchecked("0x6d6f646c64612f73327362610000000000000000000000000000000000000000");
let pallet_account_id: T::AccountId = T::AccountId::decode(&mut &addr_bytes[..]).unwrap_or_default();
<T as Config>::RingCurrency::deposit_creating(&pallet_account_id, U256::from(5000).low_u128().saturated_into());
let recipient_bytes = hex2bytes_unchecked("0x8e13b96a9c9e3b1832f07935be76c2b331251e26445f520ad1c56b24477ed8d6");
let token_address = <T as Config>::RingAddress::get();
assert_ok!(<ParaIssuing<T>>::set_remote_backing_account(
RawOrigin::Root.into(),
pallet_account_id
));
}:_(RawOrigin::Signed(caller), token_address, recipient_bytes, 1000.into())

burn_and_remote_unlock {
let caller_bytes = hex2bytes_unchecked("0x28f900e9928c356287bb8806c9044168560dee80b91d2015653c9639a4998d8d");
let caller: T::AccountId = T::AccountId::decode(&mut &caller_bytes[..]).unwrap_or_default();
<T as Config>::RingCurrency::deposit_creating(&caller, U256::from(5000).low_u128().saturated_into());
let recipient = caller.clone();
}:_(RawOrigin::Signed(caller), 1, 1,
100u128.saturated_into(),
10u128.saturated_into(),
recipient)

set_remote_backing_account {
let backing: T::AccountId = Default::default();
}:_(RawOrigin::Root, backing)

set_secure_limited_period {
let period: BlockNumberFor<T> = Zero::zero();
}:_(RawOrigin::Root, period)

set_security_limitation_ring_amount {
let limitation: RingBalance<T> = Zero::zero();
}:_(RawOrigin::Root, limitation)
}
195 changes: 195 additions & 0 deletions runtime/common/src/helixbridge/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.
//

// --- paritytech ---
use bp_messages::source_chain::SendMessageArtifacts;
use frame_support::{
traits::{Everything, GenesisBuild},
PalletId,
};
use frame_system::mocking::*;
use pallet_balances::AccountData;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
AccountId32,
};
// --- darwinia-network ---
use crate::helixbridge::{
*, {self as s2s_issuing},
};

type Block = MockBlock<Test>;
//type SignedExtra = (frame_system::CheckSpecVersion<Test>,);
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test, (), ()>;
type Balance = u64;

pub fn build_account(x: u8) -> (AccountId32, Vec<u8>) {
let origin = [x; 32];
(AccountId32::decode(&mut &origin.clone()[..]).unwrap(), origin.to_vec())
}

frame_support::parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}

impl pallet_balances::Config for Test {
type AccountStore = System;
type Balance = Balance;
type DustRemoval = ();
type Event = ();
type ExistentialDeposit = ExistentialDeposit;
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
}

frame_support::parameter_types! {
pub const MinimumPeriod: u64 = 6000 / 2;
}
impl pallet_timestamp::Config for Test {
type MinimumPeriod = MinimumPeriod;
type Moment = u64;
type OnTimestampSet = ();
type WeightInfo = ();
}

impl frame_system::Config for Test {
type AccountData = AccountData<Balance>;
type AccountId = AccountId32;
type BaseCallFilter = Everything;
type BlockHashCount = ();
type BlockLength = ();
type BlockNumber = u64;
type BlockWeights = ();
type Call = Call;
type DbWeight = ();
type Event = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type Header = Header;
type Index = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type MaxConsumers = ConstU32<16>;
type OnKilledAccount = ();
type OnNewAccount = ();
type OnSetCode = ();
type Origin = Origin;
type PalletInfo = PalletInfo;
type SS58Prefix = ();
type SystemWeightInfo = ();
type Version = ();
}

frame_support::parameter_types! {
pub const S2sRelayPalletId: PalletId = PalletId(*b"da/s2sre");
pub const PangolinChainId: bp_runtime::ChainId = *b"pagl";
pub PangolinName: Vec<u8> = (b"Pangolin").to_vec();
pub MessageLaneId: [u8; 4] = *b"ptol";
pub DecimalsDifference: Balance = 1_000_000_000;
}

pub struct AccountIdConverter;
impl Convert<H256, AccountId32> for AccountIdConverter {
fn convert(hash: H256) -> AccountId32 {
hash.to_fixed_bytes().into()
}
}
pub struct MockS2sMessageSender;
impl LatestMessageNoncer for MockS2sMessageSender {
fn outbound_latest_generated_nonce(_lane_id: [u8; 4]) -> u64 {
0
}
}

pub struct MockMessagesBridge;
impl MessagesBridge<AccountId<Test>, Balance, ()> for MockMessagesBridge {
type Error = DispatchErrorWithPostInfo<PostDispatchInfo>;

fn send_message(
submitter: RawOrigin<AccountId<Test>>,
_laneid: [u8; 4],
_payload: (),
fee: Balance,
) -> Result<SendMessageArtifacts, Self::Error> {
// send fee to fund account [2;32]
Balances::transfer(submitter.into(), build_account(2).0, fee)?;
Ok(SendMessageArtifacts { nonce: 0, weight: 0 })
}
}

impl<AccountId, Signer, Signature> CreatePayload<AccountId, Signer, Signature, Test> for () {
type Payload = ();

fn create(
_: CallOrigin<AccountId, Signer, Signature>,
_: u32,
_: u64,
_: CallParams<Test>,
_: DispatchFeePayment,
) -> Result<Self::Payload, &'static str> {
Ok(())
}
}

impl Config for Test {
type BackingChainName = PangolinName;
type BridgedAccountIdConverter = AccountIdConverter;
type BridgedChainId = PangolinChainId;
type DecimalsDifference = DecimalsDifference;
type Event = ();
type MessageLaneId = MessageLaneId;
type MessageNoncer = MockS2sMessageSender;
type MessagesBridge = MockMessagesBridge;
type OutboundPayloadCreator = ();
type PalletId = S2sRelayPalletId;
type RingCurrency = Balances;
type WeightInfo = ();
}

frame_support::construct_runtime! {
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>} = 0,
Timestamp: pallet_timestamp::{Pallet, Call, Inherent, Storage} = 1,
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>} = 2,
S2sIssuing: s2s_issuing::{Pallet, Call, Storage, Config<T>, Event<T>} = 3,
}
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

s2s_issuing::GenesisConfig::<Test> {
secure_limited_period: 10,
secure_limited_ring_amount: 1_000_000_000_000_000,
}
.assimilate_storage(&mut storage)
.unwrap();

// add some balance to backing account 10 ring
let balances = vec![(build_account(1).0, 100)];
pallet_balances::GenesisConfig::<Test> { balances }.assimilate_storage(&mut storage).unwrap();

storage.into()
}
Loading