Skip to content

Commit

Permalink
Fix/gateway test (paritytech#207)
Browse files Browse the repository at this point in the history
* fix tests for records

1. fix all tests for records,
2. add `with_transaction_result` to roll back state
3. add an option chain for finish_withdrawal

* refactor trait for GatewayCommon GatewayBitcoin

* fix genesis in bitcoin test

* fix ancientfork bug for bitcoin header

1. fix ancientfork check bug for bitcoin header
2. should check main-chain for tx verify
3. add forked header test

* finish header test

* fix compile error

* add btc tx tests

* add tx call test

* fix opreturn test

* fix tests for trustees

* add more trustee test for signature
  • Loading branch information
atenjin authored Aug 26, 2020
1 parent 893e953 commit 7ae2b1e
Show file tree
Hide file tree
Showing 28 changed files with 10,514 additions and 883 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ xpallet-assets-registrar = { path = "../xpallets/assets-registrar", default-feat
xpallet-assets-rpc-runtime-api = { path = "../xpallets/assets/rpc/runtime-api", default-features = false }
xpallet-gateway-records = { path = "../xpallets/gateway/records", default-features = false }
xpallet-gateway-records-rpc-runtime-api = { path = "../xpallets/gateway/records/rpc/runtime-api", default-features = false }
xpallet-gateway-common = { path = "../xpallets/gateway/common", default-features = false }
# we use feature "ss48check" for using local runtime-interface to check address, if in parachain, do not use this feature
xpallet-gateway-common = { path = "../xpallets/gateway/common", default-features = false, features = ["ss58check"] }
xpallet-gateway-common-rpc-runtime-api = { path = "../xpallets/gateway/common/rpc/runtime-api", default-features = false }
xpallet-gateway-bitcoin = { path = "../xpallets/gateway/bitcoin", default-features = false }
xpallet-contracts = { path = "../xpallets/contracts", default-features = false }
Expand Down
11 changes: 10 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use xpallet_contracts_rpc_runtime_api::ContractExecResult;
use xpallet_dex_spot::{Depth, FullPairInfo, RpcOrder, TradingPairId};
use xpallet_mining_asset::{MiningAssetInfo, RpcMinerLedger};
use xpallet_mining_staking::{NominatorInfo, RpcNominatorLedger, ValidatorInfo};
use xpallet_support::{RpcBalance, RpcPrice};
use xpallet_support::{traits::MultisigAddressFor, RpcBalance, RpcPrice};

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand Down Expand Up @@ -831,15 +831,24 @@ impl xpallet_gateway_records::Trait for Runtime {
type Event = Event;
}

pub struct MultisigProvider;
impl MultisigAddressFor<AccountId> for MultisigProvider {
fn calc_multisig(who: &[AccountId], threshold: u16) -> AccountId {
Multisig::multi_account_id(who, threshold)
}
}

impl xpallet_gateway_common::Trait for Runtime {
type Event = Event;
type Validator = XStaking;
type DetermineMultisigAddress = MultisigProvider;
type Bitcoin = XGatewayBitcoin;
type BitcoinTrustee = XGatewayBitcoin;
}

impl xpallet_gateway_bitcoin::Trait for Runtime {
type Event = Event;
type UnixTime = Timestamp;
type AccountExtractor = xpallet_gateway_common::extractor::Extractor;
type TrusteeSessionProvider = trustees::bitcoin::BtcTrusteeSessionManager<Runtime>;
type TrusteeOrigin = EnsureSignedBy<trustees::bitcoin::BtcTrusteeMultisig<Runtime>, AccountId>;
Expand Down
62 changes: 35 additions & 27 deletions xpallets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]
#![allow(clippy::transmute_ptr_to_ptr)]

#[cfg(test)]
mod mock;
Expand Down Expand Up @@ -384,7 +386,7 @@ impl<T: Trait> Module<T> {
xpallet_assets_registrar::Module::<T>::ensure_asset_is_valid(id)?;
Self::can_destroy_withdrawal(id)?;

let _imbalance = Self::inner_destroy(id, who, AssetType::ReservedWithdrawal, value)?;
Self::inner_destroy(id, who, AssetType::ReservedWithdrawal, value)?;
Ok(())
}

Expand All @@ -393,7 +395,7 @@ impl<T: Trait> Module<T> {
xpallet_assets_registrar::Module::<T>::ensure_asset_is_valid(id)?;
Self::can_destroy_usable(id)?;

let _imbalance = Self::inner_destroy(id, who, AssetType::Usable, value)?;
Self::inner_destroy(id, who, AssetType::Usable, value)?;
Ok(())
}

Expand Down Expand Up @@ -599,36 +601,42 @@ impl<T: Trait> Module<T> {
fn update_locks(currency_id: AssetId, who: &T::AccountId, locks: &[BalanceLock<BalanceOf<T>>]) {
// update locked balance
if let Some(max_locked) = locks.iter().map(|lock| lock.amount).max() {
let locked = Self::asset_balance_of(who, &currency_id, AssetType::Locked);
let result = if max_locked > locked {
// new lock more than current locked, move usable to locked
Self::move_balance(
&currency_id,
who,
AssetType::Usable,
who,
AssetType::Locked,
max_locked - locked,
)
} else if max_locked < locked {
// new lock less then current locked, release locked to usable
Self::move_balance(
&currency_id,
who,
AssetType::Locked,
who,
AssetType::Usable,
locked - max_locked,
)
} else {
// if max_locked == locked, need do nothing
Ok(())
use sp_std::cmp::Ordering;
let current_locked = Self::asset_balance_of(who, &currency_id, AssetType::Locked);

let result = match max_locked.cmp(&current_locked) {
Ordering::Greater => {
// new lock more than current locked, move usable to locked
Self::move_balance(
&currency_id,
who,
AssetType::Usable,
who,
AssetType::Locked,
max_locked - current_locked,
)
}
Ordering::Less => {
// new lock less then current locked, release locked to usable
Self::move_balance(
&currency_id,
who,
AssetType::Locked,
who,
AssetType::Usable,
current_locked - max_locked,
)
}
Ordering::Equal => {
// if max_locked == locked, need do nothing
Ok(())
}
};
if let Err(e) = result {
// should not fail, for set lock need to check free_balance, free_balance = usable + free
error!(
"[update_locks]|move between usable and locked should not fail|asset_id:{:}|who:{:?}|max_locked:{:?}|current locked:{:?}|err:{:?}",
currency_id, who, max_locked, locked, e
currency_id, who, max_locked, current_locked, e
);
}
}
Expand Down
14 changes: 14 additions & 0 deletions xpallets/gateway/bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ frame-support = { version = "2.0.0-rc5", default-features = false }
frame-system = { version = "2.0.0-rc5", default-features = false }
pallet-timestamp = { version = "2.0.0-rc5", default-features = false }

# orml
orml-utilities = { version = "0.1.3-dev", git = "https://github.com/open-web3-stack/open-runtime-module-library.git", rev = "d3630e35ce737aef3f9c7857e9b4bd540f37530c", default-features = false }

# ChainX primitives
chainx-primitives = { path = "../../../primitives", default-features = false }
xp-io = { path = "../../../primitives/io", default-features = false }
Expand All @@ -32,6 +35,15 @@ xpallet-gateway-records = { path = "../records", default-features = false }
# light-bitcoin
light-bitcoin = { git = "https://github.com/chainx-org/light-bitcoin", branch = "v2", default-features = false }

[dev-dependencies]
lazy_static = "1.4"
hex = "0.4"
serde_json = "1.0"
pallet-balances = { version = "2.0.0-rc5" }
pallet-multisig = { version = "2.0.0-rc5" }

xpallet-assets-registrar = { path = "../../assets-registrar" }

[features]
default = ["std"]
std = [
Expand All @@ -47,6 +59,8 @@ std = [
"frame-system/std",
"pallet-timestamp/std",

"orml-utilities/std",

"chainx-primitives/std",
"xp-io/std",

Expand Down
24 changes: 6 additions & 18 deletions xpallets/gateway/bitcoin/src/header/header_proof.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright 2018-2019 Chainpool.

// Substrate
use frame_support::dispatch::DispatchResult;
use sp_runtime::traits::SaturatedConversion;
use sp_std::{cmp, result};
use frame_support::{dispatch::DispatchResult, traits::UnixTime};
use sp_std::{cmp, convert::TryFrom, result};

// ChainX
use xpallet_support::{debug, ensure_with_errorlog, error, info, warn};
Expand All @@ -27,21 +26,10 @@ pub struct HeaderVerifier<'a> {

impl<'a> HeaderVerifier<'a> {
pub fn new<T: Trait>(header_info: &'a BtcHeaderInfo) -> result::Result<Self, ChainErr> {
let now: T::Moment = pallet_timestamp::Module::<T>::now();
// in substrate, timestamp would use u64 with milliseconds
let now: u64 = now.saturated_into::<u64>();
// transfer milliseconds to seconds
let current_time: Option<u32> = now.checked_div(1000_u64).and_then(|now| {
// bitcoin use u32 to log time, we think the timestamp would not more then u32
let now = now.saturated_into::<u32>();
if now == u32::max_value() {
// convert from u64 to u32 failed, ignore timestamp check
// timestamp check are not important
None
} else {
Some(now)
}
});
let current = T::UnixTime::now();
// if convert from u64 to u32 failed, ignore timestamp check
// timestamp check are not important
let current_time = u32::try_from(current.as_secs()).ok();

Ok(HeaderVerifier {
work: HeaderWork::new(header_info),
Expand Down
Loading

0 comments on commit 7ae2b1e

Please sign in to comment.