Skip to content

Commit

Permalink
Refactor/bitcoin 0.9.10 (paritytech#584)
Browse files Browse the repository at this point in the history
reject more than 1/3, related to https://github.com/chainpool/ChainX/issues/571
all tx inputs sig count should same
apply_create_withdraw support check sign same time
fix https://github.com/chainpool/ChainX/issues/570
fix https://github.com/chainpool/ChainX/issues/546
fix https://github.com/chainpool/ChainX/issues/578
fix std print addr in runtime
fix withdraw list tx state
fix https://github.com/chainpool/ChainX/issues/575
  • Loading branch information
atenjin authored and toxotguo committed May 10, 2019
1 parent e150acc commit 23224b9
Show file tree
Hide file tree
Showing 22 changed files with 353 additions and 161 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions rpc/src/chainx/impl_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use support::storage::{StorageMap, StorageValue};
// chainx
use chainx_primitives::{AccountId, AccountIdForRpc, AuthorityId, Balance, BlockNumber, Timestamp};
use chainx_runtime::{Call, Runtime};
use xr_primitives::Name;
use xr_primitives::{AddrStr, Name};

use xaccounts::IntentionProps;
use xassets::{Asset, AssetType, Chain, ChainT, Token};
Expand Down Expand Up @@ -140,7 +140,7 @@ where

fn verify_addr(&self, token: String, addr: String, memo: String) -> Result<Option<bool>> {
let token: xassets::Token = token.as_bytes().to_vec();
let addr: xrecords::AddrStr = addr.as_bytes().to_vec();
let addr: AddrStr = addr.as_bytes().to_vec();
let memo: xassets::Memo = memo.as_bytes().to_vec();

// test valid before call runtime api
Expand Down
15 changes: 13 additions & 2 deletions rpc/src/chainx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ pub struct WithdrawInfo {
/// btc-address
pub address: String,
/// withdraw status
pub status: TxState,
pub status: Value,
/// OP_RETURN
pub memo: String,
}
Expand All @@ -403,6 +403,17 @@ impl From<RecordInfo<AccountId, Balance, BlockNumber, Timestamp>> for WithdrawIn
panic!("deposit record should be timestamp, not height");
};

let status = match record.state {
TxState::Confirming(a, b) => json!({
"value": "Confirming",
"confirm": a,
"total_confirm": b,
}),
_ => json!({
"value": record.state
}),
};

WithdrawInfo {
height,
id: record.withdrawal_id,
Expand All @@ -411,7 +422,7 @@ impl From<RecordInfo<AccountId, Balance, BlockNumber, Timestamp>> for WithdrawIn
token: String::from_utf8_lossy(&record.token).into_owned(),
accountid: record.who.into(),
address: String::from_utf8_lossy(&record.addr).into_owned(),
status: record.state,
status,
memo: String::from_utf8_lossy(&record.ext).into_owned(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod xassets_api {
use super::*;
use rstd::collections::btree_map::BTreeMap;
use xassets::{Asset, AssetType, Memo, Token};
use xrecords::AddrStr;
use xr_primitives::AddrStr;

decl_runtime_apis! {
pub trait XAssetsApi {
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use version::RuntimeVersion;
use chainx_primitives;
use runtime_api;
use xgrandpa::fg_primitives::{self, ScheduledChange};
use xr_primitives;
use xr_primitives::AddrStr;

// chainx
use chainx_primitives::{
Expand Down Expand Up @@ -399,7 +399,7 @@ impl_runtime_apis! {
}
}

fn verify_address(token: xassets::Token, addr: xrecords::AddrStr, ext: xassets::Memo) -> Result<(), Vec<u8>> {
fn verify_address(token: xassets::Token, addr: AddrStr, ext: xassets::Memo) -> Result<(), Vec<u8>> {
XAssetsProcess::verify_address(token, addr, ext).map_err(|e| e.as_bytes().to_vec())
}

Expand Down
1 change: 1 addition & 0 deletions runtime/wasm/Cargo.lock

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

Binary file not shown.
3 changes: 3 additions & 0 deletions xr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ pub type XString = Vec<u8>;
pub type Name = XString;
pub type URL = XString;
pub type AddrStr = XString;
pub type Memo = XString;
pub type Token = XString;
pub type Desc = XString;
5 changes: 1 addition & 4 deletions xrml/xassets/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use support::StorageMap;

use primitives::traits::{Saturating, Zero};
// ChainX
use xr_primitives::XString;
pub use xr_primitives::{Desc, Memo, Token};

use super::traits::ChainT;
use super::{Module, Trait};
Expand All @@ -24,10 +24,7 @@ const MAX_DESC_LEN: usize = 128;

pub type TokenString = &'static [u8];
pub type DescString = TokenString;
pub type Token = XString;
pub type Desc = XString;
pub type Precision = u16;
pub type Memo = XString;

pub type SignedImbalanceT<T> = SignedImbalance<<T as Trait>::Balance, PositiveImbalance<T>>;

Expand Down
2 changes: 2 additions & 0 deletions xrml/xassets/process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ primitives = { package = "sr-primitives", git = "https://github.com/chainpool/su
support = { package = "srml-support", git = "https://github.com/chainpool/substrate", default-features = false }
system = { package = "srml-system", git = "https://github.com/chainpool/substrate", default-features = false }
# ChainX
xr-primitives = { path = "../../../xr-primitives", default-features = false }
xsupport = { package = "xrml-xsupport", path = "../../xsupport", default-features = false }
xassets = { package = "xrml-xassets-assets", path = "../assets", default-features = false }
xrecords = { package = "xrml-xassets-records", path = "../records", default-features = false }
Expand Down Expand Up @@ -43,6 +44,7 @@ std = [
"support/std",
"system/std",
# ChainX
"xr-primitives/std",
"xsupport/std",
"xassets/std",
"xrecords/std",
Expand Down
13 changes: 9 additions & 4 deletions xrml/xassets/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ use system::ensure_signed;

// ChainX
use xassets::{Chain, ChainT, Memo, Token};
use xrecords::AddrStr;
use xr_primitives::AddrStr;
#[cfg(feature = "std")]
use xsupport::token;
use xsupport::warn;
use xsupport::{debug, warn};

pub trait Trait: xassets::Trait + xrecords::Trait + xbitcoin::Trait {}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn withdraw(origin, token: Token, value: T::Balance, addr: AddrStr, ext: Memo) -> Result {
runtime_io::print("[xassets process withdrawal] withdraw");
let who = ensure_signed(origin)?;

Self::check_black_list(&token)?;

debug!("[withdraw]withdraw|who:{:?}|token:{:}|value:{:}", who, token!(token), value);

let asset = xassets::Module::<T>::get_asset(&token)?;
if asset.chain() == Chain::ChainX {
return Err("Can't withdraw the asset on ChainX")
Expand All @@ -45,6 +45,11 @@ decl_module! {
xrecords::Module::<T>::withdrawal(&who, &token, value, addr, ext)?;
Ok(())
}

fn revoke_withdraw(origin, id: u32) -> Result {
let from = ensure_signed(origin)?;
xrecords::Module::<T>::withdrawal_revoke(&from, id)
}
}
}

Expand Down
118 changes: 100 additions & 18 deletions xrml/xassets/records/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ pub mod types;

// Substrate
use rstd::prelude::*;
use support::{decl_event, decl_module, decl_storage, dispatch::Result, StorageValue};
use support::{decl_event, decl_module, decl_storage, dispatch::Result, StorageMap, StorageValue};

// ChainX
use xr_primitives::AddrStr;

use xassets::{AssetType, Chain, ChainT, Memo, Token};
use xsupport::storage::linked_node::{MultiNodeIndex, Node};

use xsupport::{error, info};
use xsupport::{error, info, warn};
#[cfg(feature = "std")]
use xsupport::{token, u8array_to_addr, u8array_to_string};

pub use self::types::{AddrStr, Application, HeightOrTime, LinkedMultiKey, RecordInfo, TxState};
pub use self::types::{
Application, ApplicationState, HeightOrTime, LinkedMultiKey, RecordInfo, TxState,
};

pub trait Trait: system::Trait + xassets::Trait + timestamp::Trait {
/// The overarching event type.
Expand All @@ -38,8 +42,25 @@ decl_module! {
Self::withdrawal(&who, &token, balance, Default::default(), Default::default())
}

pub fn fix_withdrawal_state(withdrawal_id: u32, success: bool) -> Result {
match Self::withdrawal_finish(withdrawal_id, success) {
pub fn fix_withdrawal_state_by_trustees(withdrawal_id: u32, state: ApplicationState) -> Result {
if let Some(node) = Self::application_map(withdrawal_id) {
if node.data.state != ApplicationState::Processing {
error!("[fix_withdrawal_state_by_trustees]only allow `Processing` for this application|id:{:}|state:{:?}", withdrawal_id, node.data.state);
return Err("only allow `Processing` for this application")
}
}
match state {
ApplicationState::RootFinish | ApplicationState::RootCancel => { /*do nothing*/ },
_ => {
error!("[fix_withdrawal_state_by_trustees]|state only allow `RootFinish` and `RootCancel`|state:{:?}", state);
return Err("state only allow `RootFinish` and `RootCancel`")
}
}
Self::fix_withdrawal_state(withdrawal_id, state)
}

pub fn fix_withdrawal_state(withdrawal_id: u32, state: ApplicationState) -> Result {
match Self::withdrawal_finish_impl(withdrawal_id, state) {
Ok(_) => {
info!("[withdraw]|ID of withdrawal completion: {:}", withdrawal_id);
Ok(())
Expand All @@ -51,9 +72,9 @@ decl_module! {
}
}

pub fn fix_withdrawal_state_list(item: Vec<(u32, bool)>) -> Result {
for (withdrawal_id, success) in item {
let _ = Self::fix_withdrawal_state(withdrawal_id, success);
pub fn fix_withdrawal_state_list(item: Vec<(u32, ApplicationState)>) -> Result {
for (withdrawal_id, state) in item {
let _ = Self::fix_withdrawal_state(withdrawal_id, state);
}
Ok(())
}
Expand All @@ -65,8 +86,8 @@ decl_event!(
<T as system::Trait>::AccountId,
<T as xassets::Trait>::Balance {
Deposit(AccountId, Token, Balance),
WithdrawalApply(u32, AccountId, Chain, Token, Balance, Memo, AddrStr, TxState),
WithdrawalFinish(u32, bool),
WithdrawalApply(u32, AccountId, Chain, Token, Balance, Memo, AddrStr),
WithdrawalFinish(u32, ApplicationState),
}
);

Expand Down Expand Up @@ -146,6 +167,7 @@ impl<T: Trait> Module<T> {
u8array_to_string(&ext)
);

// state is Applying
let appl = Application::<T::AccountId, T::Balance, T::BlockNumber>::new(
id,
who.clone(),
Expand Down Expand Up @@ -182,17 +204,71 @@ impl<T: Trait> Module<T> {
appl.balance,
appl.ext,
appl.addr, // if btc, the addr is base58 addr
TxState::Applying,
));
Ok(())
}

/// change Applying to Processing
pub fn withdrawal_processing(serial_number: &[u32]) -> Result {
let mut v = Vec::new();
for id in serial_number.iter() {
if let Some(node) = Self::application_map(id) {
if node.data.state() != ApplicationState::Applying {
error!("[withdrawal_processing]|application state not `Applying`|id:{:}|state:{:?}", id, node.data.state());
return Err("application state not `Applying`");
}
v.push((*id, node));
} else {
error!(
"[withdrawal_processing]|id not in application records|id:{:}",
id
);
return Err("id not in application records");
}
}
// mark all records is `Processing`
for (id, node) in v.iter_mut() {
node.data.state = ApplicationState::Processing;
ApplicationMap::<T>::insert(id, node);
}
Ok(())
}

/// withdrawal finish, let the locking token destroy
pub fn withdrawal_finish(serial_number: u32, success: bool) -> Result {
/// Change Processing to final state
pub fn withdrawal_finish(serial_number: u32) -> Result {
if let Some(node) = Self::application_map(serial_number) {
if node.data.state != ApplicationState::Processing {
error!("[withdrawal_finish]only allow `Processing` for this application|id:{:}|state:{:?}", serial_number, node.data.state);
return Err("only allow `Processing` for this application");
}
}
Self::withdrawal_finish_impl(serial_number, ApplicationState::NormalFinish)
}

pub fn withdrawal_revoke(who: &T::AccountId, serial_number: u32) -> Result {
if let Some(node) = Self::application_map(serial_number) {
if node.data.applicant != *who {
error!(
"[withdrawal_revoke]|the applicant is not this account|applicant:{:?}|who:{:?}",
node.data.applicant, who
);
return Err("the applicant is not this account");
}

if node.data.state != ApplicationState::Applying {
error!("[withdrawal_finish]only allow `Processing` for this application|id:{:}|state:{:?}", serial_number, node.data.state);
return Err("only allow `Applying` state for applicant revoke");
}
}
Self::withdrawal_finish_impl(serial_number, ApplicationState::NormalCancel)
}

fn withdrawal_finish_impl(serial_number: u32, state: ApplicationState) -> Result {
let mut node = if let Some(node) = Self::application_map(serial_number) {
node
} else {
error!("[withdrawal_finish]|withdrawal application record not exist|withdrawal id:{:}|success:{:}", serial_number, success);
error!("[withdrawal_finish]|withdrawal application record not exist|withdrawal id:{:}|state:{:?}", serial_number, state);
return Err("withdrawal application record not exist");
};

Expand All @@ -213,13 +289,19 @@ impl<T: Trait> Module<T> {
balance
);
// destroy reserved token
if success {
Self::destroy(&who, &token, balance)?;
} else {
Self::unlock(&who, &token, balance)?;
match state {
ApplicationState::NormalFinish | ApplicationState::RootFinish => {
Self::destroy(&who, &token, balance)?;
}
ApplicationState::NormalCancel | ApplicationState::RootCancel => {
Self::unlock(&who, &token, balance)?;
}
_ => {
warn!("[withdrawal_finish_impl]|should not meet this branch in normally, except in root|state:{:?}", state);
}
}

Self::deposit_event(RawEvent::WithdrawalFinish(serial_number, success));
Self::deposit_event(RawEvent::WithdrawalFinish(serial_number, state));
Ok(())
}

Expand Down
Loading

0 comments on commit 23224b9

Please sign in to comment.