diff --git a/.maintain/ci/travis.local.sh b/.maintain/ci/travis.local.sh index 5ee3911fd..ac2d94ce5 100644 --- a/.maintain/ci/travis.local.sh +++ b/.maintain/ci/travis.local.sh @@ -3,6 +3,8 @@ readonly TEST_CRATES=( 'ring' 'staking' 'treasury' + 'eth-relay' + 'eth-backing' ); function main() { diff --git a/.travis.yml b/.travis.yml index aaf3886d2..d35549d22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,6 +45,14 @@ jobs: env: RUST_TOOLCHAIN=nightly-2020-02-07 TARGET=native TREASURY script: .maintain/ci/darwinia_test_script.sh treasury + - stage: Darwinia Test + env: RUST_TOOLCHAIN=nightly-2020-02-07 TARGET=native TREASURY + script: .maintain/ci/darwinia_test_script.sh eth-relay + + - stage: Darwinia Test + env: RUST_TOOLCHAIN=nightly-2020-02-07 TARGET=native TREASURY + script: .maintain/ci/darwinia_test_script.sh eth-backing + # TODO: remove this when overall test case ready allow_failures: - stage: Overall Test diff --git a/Cargo.lock b/Cargo.lock index 676483762..f78c6c4ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1048,6 +1048,7 @@ version = "0.5.0" dependencies = [ "darwinia-eth-relay", "darwinia-kton", + "darwinia-phragmen", "darwinia-ring", "darwinia-staking", "darwinia-support", @@ -1057,13 +1058,13 @@ dependencies = [ "frame-system", "hex-literal", "pallet-session", + "pallet-timestamp", "parity-scale-codec", "rlp 0.4.4 (git+https://github.com/darwinia-network/parity-common.git)", "rustc-hex", "serde", "sp-core", "sp-io", - "sp-phragmen", "sp-runtime", "sp-staking", "sp-std 2.0.0-alpha.3 (git+https://github.com/darwinia-network/substrate.git?tag=v2.0.0-alpha.3)", diff --git a/bin/node/executor/tests/submit_transaction.rs b/bin/node/executor/tests/submit_transaction.rs index df534d0fd..a86d59a27 100644 --- a/bin/node/executor/tests/submit_transaction.rs +++ b/bin/node/executor/tests/submit_transaction.rs @@ -154,7 +154,7 @@ fn submitted_transaction_should_be_valid() { // add balance to the account let author = extrinsic.signature.clone().unwrap().0; let address = Indices::lookup(author).unwrap(); - let data = pallet_balances::AccountData { + let data = pallet_balances::balance::AccountData { free: 5_000_000_000_000, ..Default::default() }; diff --git a/frame/chainrelay/eth/backing/Cargo.toml b/frame/chainrelay/eth/backing/Cargo.toml index cb46d287a..a9ef38239 100644 --- a/frame/chainrelay/eth/backing/Cargo.toml +++ b/frame/chainrelay/eth/backing/Cargo.toml @@ -36,14 +36,15 @@ rustc-hex = "2.0" rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git" } pallet-session = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3"} +pallet-timestamp = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" } sp-io = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" } -sp-phragmen = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3"} -sp-staking = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3"} +sp-staking = { git = "https://github.com/darwinia-network/substrate.git", tag = "v2.0.0-alpha.3" } -kton = { package = "darwinia-kton", path = "../../../balances/kton" } -ring = { package = "darwinia-ring", path = '../../../balances/ring' } -staking = { package = "darwinia-staking", path = "../../../staking" } +pallet-ring = { package = "darwinia-ring", path = "../../../../frame/balances/ring" } +pallet-kton = { package = "darwinia-kton", path = "../../../../frame/balances/kton" } +pallet-staking = { package = "darwinia-staking", path = "../../../../frame/staking" } +darwinia-phragmen = { path = "../../../../primitives/phragmen" } [features] default = ["std"] diff --git a/frame/chainrelay/eth/backing/src/mock.rs b/frame/chainrelay/eth/backing/src/mock.rs index 5b85af4f2..85f923b59 100644 --- a/frame/chainrelay/eth/backing/src/mock.rs +++ b/frame/chainrelay/eth/backing/src/mock.rs @@ -1 +1,228 @@ //! Mock file for eth-backing. + +use hex_literal::hex; +use std::cell::RefCell; + +use frame_support::{impl_outer_origin, parameter_types, weights::Weight}; +use sp_core::{crypto::key_types, H256}; +use sp_io; +use sp_runtime::{ + testing::{Header, UintAuthorityId}, + traits::{IdentifyAccount, IdentityLookup, OpaqueKeys, Verify}, + {KeyTypeId, MultiSignature, Perbill}, +}; +use sp_staking::SessionIndex; + +use pallet_staking::{EraIndex, Exposure, ExposureOf}; + +use darwinia_phragmen::Power; + +use crate::*; + +/// 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 = <::Signer as IdentifyAccount>::AccountId; +pub type BlockNumber = u64; +pub type Balance = u128; + +pub type System = frame_system::Module; +//pub type Session = pallet_session::Module; +pub type Timestamp = pallet_timestamp::Module; + +pub type EthBacking = Module; +pub type Ring = pallet_ring::Module; +pub type Kton = pallet_kton::Module; +pub type Staking = pallet_staking::Module; +pub type EthRelay = darwinia_eth_relay::Module; + +pub const NANO: Balance = 1; +pub const MICRO: Balance = 1_000 * NANO; +pub const MILLI: Balance = 1_000 * MICRO; +pub const COIN: Balance = 1_000 * MILLI; + +pub const CAP: Balance = 10_000_000_000 * COIN; +pub const GENESIS_TIME: Moment = 0; +pub const TOTAL_POWER: Power = 1_000_000_000; + +thread_local! { + static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); + static SLASH_DEFER_DURATION: RefCell = RefCell::new(0); +} + +pub struct TestSessionHandler; +impl pallet_session::SessionHandler for TestSessionHandler { + const KEY_TYPE_IDS: &'static [KeyTypeId] = &[key_types::DUMMY]; + + fn on_genesis_session(_validators: &[(AccountId, Ks)]) {} + + fn on_new_session( + _changed: bool, + _validators: &[(AccountId, Ks)], + _queued_validators: &[(AccountId, Ks)], + ) { + } + + fn on_disabled(_validator_index: usize) {} +} + +impl_outer_origin! { + pub enum Origin for Test where system = system {} +} + +// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Test; +parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: Weight = 1024; + pub const MaximumBlockLength: u32 = 2 * 1024; + pub const AvailableBlockRatio: Perbill = Perbill::one(); +} +impl frame_system::Trait for Test { + type Origin = Origin; + type Call = (); + type Index = u64; + type BlockNumber = BlockNumber; + type Hash = H256; + type Hashing = ::sp_runtime::traits::BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type Event = (); + type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; + type MaximumBlockLength = MaximumBlockLength; + type AvailableBlockRatio = AvailableBlockRatio; + type Version = (); + type ModuleToIndex = (); + type AccountData = darwinia_support::balance::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); +} +impl pallet_kton::Trait for Test { + type Balance = Balance; + type DustRemoval = (); + type Event = (); + type ExistentialDeposit = (); + type AccountStore = System; + type TryDropRing = (); +} +impl pallet_ring::Trait for Test { + type Balance = Balance; + type DustRemoval = (); + type Event = (); + type ExistentialDeposit = (); + type AccountStore = System; + type TryDropKton = (); +} + +parameter_types! { + pub const MinimumPeriod: u64 = 5; +} +impl pallet_timestamp::Trait for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; +} + +parameter_types! { + pub const EthRopsten: u64 = 1; +} + +impl darwinia_eth_relay::Trait for Test { + type Event = (); + type EthNetwork = EthRopsten; +} + +parameter_types! { + pub const SessionsPerEra: SessionIndex = 3; + pub const BondingDurationInEra: EraIndex = 3; + // assume 60 blocks per session + pub const BondingDurationInBlockNumber: BlockNumber = 3 * 3 * 60; + + pub const Cap: Balance = CAP; + pub const TotalPower: Power = TOTAL_POWER; + pub const GenesisTime: Moment = GENESIS_TIME; +} + +impl pallet_staking::Trait for Test { + type Time = Timestamp; + type Event = (); + type SessionsPerEra = SessionsPerEra; + type BondingDurationInEra = (); + type BondingDurationInBlockNumber = (); + type SlashDeferDuration = (); + type SlashCancelOrigin = system::EnsureRoot; + type SessionInterface = Self; + type RingCurrency = Ring; + type RingRewardRemainder = (); + type RingSlash = (); + type RingReward = (); + type KtonCurrency = Kton; + type KtonSlash = (); + type KtonReward = (); + type Cap = Cap; + type TotalPower = TotalPower; + type GenesisTime = GenesisTime; +} + +parameter_types! { + pub const Period: BlockNumber = 1; + pub const Offset: BlockNumber = 0; + pub const UncleGenerations: u64 = 0; + pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(25); +} + +impl pallet_session::Trait for Test { + type Event = (); + type ValidatorId = AccountId; + type ValidatorIdOf = (); + type ShouldEndSession = pallet_session::PeriodicSessions; + type SessionManager = pallet_session::historical::NoteHistoricalRoot; + type SessionHandler = TestSessionHandler; + type Keys = UintAuthorityId; + type DisabledValidatorsThreshold = (); +} +impl pallet_session::historical::Trait for Test { + type FullIdentification = Exposure; + type FullIdentificationOf = ExposureOf; +} + +impl Trait for Test { + type Event = (); + type Time = Timestamp; + type EthRelay = EthRelay; + type Ring = Ring; + type Kton = Kton; + type OnDepositRedeem = Staking; + type DetermineAccountId = AccountIdDeterminator; + type RingReward = (); + type KtonReward = (); +} + +pub struct ExtBuilder; +impl Default for ExtBuilder { + fn default() -> Self { + Self + } +} + +impl ExtBuilder { + pub fn build(self) -> sp_io::TestExternalities { + let mut t = system::GenesisConfig::default().build_storage::().unwrap(); + + let _ = GenesisConfig:: { + ring_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(), + kton_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(), + deposit_redeem_address: hex!["6ef538314829efa8386fc43386cb13b4e0a67d1e"].into(), + ring_locked: 20000000000000, + kton_locked: 5000000000000, + } + .assimilate_storage(&mut t) + .unwrap(); + + t.into() + } +} diff --git a/frame/chainrelay/eth/backing/src/tests.rs b/frame/chainrelay/eth/backing/src/tests.rs index e24ddfc2a..92c8d19c2 100644 --- a/frame/chainrelay/eth/backing/src/tests.rs +++ b/frame/chainrelay/eth/backing/src/tests.rs @@ -2,15 +2,16 @@ use std::str::FromStr; +use frame_support::{assert_err, assert_ok}; +use frame_system::RawOrigin; use hex_literal::hex; use rustc_hex::FromHex; -use sr_primitives::{traits::Dispatchable, AccountId32}; -use support::{assert_err, assert_ok}; +use sp_runtime::{traits::Dispatchable, AccountId32}; use crate::{mock::*, *}; -use darwinia_support::StakingLock; -use sp_eth_primitives::{header::EthHeader, Bloom, EthAddress, H64}; -use staking::{RewardDestination, StakingBalances, StakingLedger, TimeDepositItem}; +use darwinia_support::balance::lock::StakingLock; +use eth_primitives::{header::EthHeader, Bloom, EthAddress, H64}; +use pallet_staking::{RewardDestination, StakingBalance, StakingLedger, TimeDepositItem}; #[test] fn verify_parse_token_redeem_proof() { @@ -18,6 +19,7 @@ fn verify_parse_token_redeem_proof() { .build() .execute_with(|| { // System::inc_account_nonce(&2); + assert_ok!(EthRelay::set_number_of_blocks_safe(RawOrigin::Root.into(), 0)); // https://ropsten.etherscan.io/tx/0x59c6758bd2b93b2f060e471df8d6f4d901c453d2c2c012ba28088acfb94f8216 let proof_record = EthReceiptProof { @@ -62,6 +64,7 @@ fn verify_redeem_ring() { .build() .execute_with(|| { // System::inc_account_nonce(&2); + assert_ok!(EthRelay::set_number_of_blocks_safe(RawOrigin::Root.into(), 0)); // https://ropsten.etherscan.io/tx/0x59c6758bd2b93b2f060e471df8d6f4d901c453d2c2c012ba28088acfb94f8216 let proof_record = EthReceiptProof { @@ -97,8 +100,8 @@ fn verify_redeem_ring() { let expect_account_id = ::DetermineAccountId::account_id_for(&hex!("2a92ae5b41feba5ee68a61449c557efa9e3b894a6461c058ec2de45429adb44546")).unwrap(); let id1 = AccountId32::from([0; 32]); - // If expect_account_id doesn't exist, redeem should fail - assert_err!(EthBacking::redeem_ring(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist"); + // If expect_account_id doesn't exist, redeem should fail, "beneficiary account must pre-exist" + assert_err!(EthBacking::redeem_ring(Origin::signed(id1.clone()), proof_record.clone()), pallet_ring::Error::::DeadAccount); let ring_locked_before = EthBacking::ring_locked(); @@ -122,6 +125,7 @@ fn verify_redeem_kton() { .build() .execute_with(|| { // System::inc_account_nonce(&2); + assert_ok!(EthRelay::set_number_of_blocks_safe(RawOrigin::Root.into(), 0)); // https://ropsten.etherscan.io/tx/0xc878562085dd8b68ad81adf0820aa0380f1f81b0ea7c012be122937b74020f96 // darwinia: 5FP2eFNSVxJzSrE3N2NEVFPhUU34VzYFD6DDtRXbYzTdwPn8 @@ -166,7 +170,7 @@ fn verify_redeem_kton() { let id1 = AccountId32::from([0; 32]); // If expect_account_id doesn't exist, redeem should fail - assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist"); + assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), pallet_kton::Error::::DeadAccount); let kton_locked_before = EthBacking::kton_locked(); @@ -189,6 +193,7 @@ fn verify_redeem_deposit() { .build() .execute_with(|| { // System::inc_account_nonce(&2); + assert_ok!(EthRelay::set_number_of_blocks_safe(RawOrigin::Root.into(), 0)); // 1234ring -> 0.1234kton @@ -242,9 +247,9 @@ fn verify_redeem_deposit() { let controller = AccountId32::from([1; 32]); let _ = Ring::deposit_creating(&expect_account_id, 1); - assert_ok!(staking::Call::::bond( + assert_ok!(pallet_staking::Call::::bond( controller.clone(), - StakingBalances::RingBalance(1), + StakingBalance::RingBalance(1), RewardDestination::Controller, 0, ).dispatch(Origin::signed(expect_account_id.clone()))); diff --git a/frame/chainrelay/eth/relay/src/lib.rs b/frame/chainrelay/eth/relay/src/lib.rs index e004f2b50..749996921 100644 --- a/frame/chainrelay/eth/relay/src/lib.rs +++ b/frame/chainrelay/eth/relay/src/lib.rs @@ -9,7 +9,7 @@ mod mock; mod tests; use codec::{Decode, Encode}; -use frame_support::{decl_event, decl_module, decl_storage, ensure, traits::Get}; +use frame_support::{decl_event, decl_module, decl_storage, ensure, traits::Get, weights::SimpleDispatchInfo}; use frame_system::{self as system, ensure_root, ensure_signed}; use sp_runtime::RuntimeDebug; use sp_std::prelude::*; @@ -89,6 +89,18 @@ decl_module! { { fn deposit_event() = default; + #[weight = SimpleDispatchInfo::FixedNormal(5_000)] + pub fn set_number_of_blocks_finality(origin, #[compact] new: u64) { + ensure_root(origin)?; + NumberOfBlocksFinality::put(new); + } + + #[weight = SimpleDispatchInfo::FixedNormal(5_000)] + pub fn set_number_of_blocks_safe(origin, #[compact] new: u64) { + ensure_root(origin)?; + NumberOfBlocksSafe::put(new); + } + // TODO: Just for easy testing. pub fn reset_genesis_header(origin, header: EthHeader, genesis_difficulty: u64) { let relayer = ensure_signed(origin)?; diff --git a/frame/chainrelay/eth/relay/src/tests.rs b/frame/chainrelay/eth/relay/src/tests.rs index b097ade07..ab6618500 100644 --- a/frame/chainrelay/eth/relay/src/tests.rs +++ b/frame/chainrelay/eth/relay/src/tests.rs @@ -7,6 +7,7 @@ use eth_primitives::{ Bloom, EthAddress, H64, U128, }; use frame_support::assert_ok; +use frame_system::RawOrigin; use hex_literal::hex; use rustc_hex::FromHex; @@ -19,6 +20,8 @@ fn verify_receipt_proof() { .execute_with(|| { System::inc_account_nonce(&2); + assert_ok!(EthRelay::set_number_of_blocks_safe(RawOrigin::Root.into(), 0)); + // https://ropsten.etherscan.io/tx/0xce62c3d1d2a43cfcc39707b98de53e61a7ef7b7f8853e943d85e511b3451aa7e#eventlog let log_entries = vec![LogEntry { address: EthAddress::from_str("ad52e0f67b6f44cd5b9a6f4fbc7c0f78f37e094b").unwrap(), diff --git a/frame/vesting/src/lib.rs b/frame/vesting/src/lib.rs index f31ee250e..f0221c097 100644 --- a/frame/vesting/src/lib.rs +++ b/frame/vesting/src/lib.rs @@ -319,7 +319,7 @@ mod tests { type AvailableBlockRatio = AvailableBlockRatio; type Version = (); type ModuleToIndex = (); - type AccountData = pallet_balances::AccountData; + type AccountData = pallet_balances::balance::AccountData; type OnNewAccount = (); type OnKilledAccount = (); }