Skip to content

Commit

Permalink
Fix/test fmt (paritytech#145)
Browse files Browse the repository at this point in the history
* Stage/poc (paritytech#144)

fmt & cargo test

* Fix build error

* Fix chain sync

* Fix warning

* Make format
  • Loading branch information
eee-byte authored and gguoss committed Dec 4, 2018
1 parent 6ac8fb3 commit 8951bdd
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 298 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.

2 changes: 2 additions & 0 deletions cxrml/bridge/btc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ srml-system = { git = "https://github.com/paritytech/substrate", default-feature
srml-balances = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-consensus = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false }
srml-session = { git = "https://github.com/paritytech/substrate", default_features = false }
cxrml-system = { path = "../../system", default-features = false }
cxrml-associations = { path = "../../associations", default-features = false }
cxrml-support = { path = "../../support", default-features = false }
Expand Down Expand Up @@ -57,6 +58,7 @@ std=[
"srml-balances/std",
"srml-consensus/std",
"srml-timestamp/std",
"srml-session/std",
"cxrml-system/std",
"cxrml-associations/std",
"cxrml-support/std",
Expand Down
122 changes: 46 additions & 76 deletions cxrml/bridge/btc/src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use rstd::marker::PhantomData;
use rstd::prelude::*;
use rstd::result::Result;
use runtime_io;
use runtime_primitives::traits::As;
use runtime_support::{StorageMap, StorageValue};
use {AddressMap, BtcFee, IrrBlock, NetworkId};
use {BtcFee, IrrBlock};

use chain::BlockHeader;
use financial_records;
use financial_records::Symbol;
use primitives::hash::H256;
use script::Script;
use staking;

use {
Expand Down Expand Up @@ -167,8 +165,6 @@ impl<T: Trait> Chain<T> {
};

<NumberForHash<T>>::insert(new_best_header.hash.clone(), new_best_header.number);
runtime_io::print("------------");
runtime_io::print(new_best_header.hash.to_vec().as_slice());
<HashsForNumber<T>>::mutate(new_best_header.number, |v| {
let h = new_best_header.hash.clone();
if v.contains(&h) == false {
Expand All @@ -183,13 +179,11 @@ impl<T: Trait> Chain<T> {
let irr_block = <IrrBlock<T>>::get();
// Deposit
if let Some(vec) = <DepositCache<T>>::take() {
runtime_io::print("-----------DepositCache take");
let mut uncomplete_cache: Vec<(T::AccountId, u64, H256, H256)> = Vec::new();
for (account_id, amount, tx_hash, block_hash) in vec {
match <NumberForHash<T>>::get(block_hash.clone()) {
Some(height) => {
if new_best_header.number >= height + irr_block {
runtime_io::print("-----------financial_records deposit");
// TODO handle err
let _ = <financial_records::Module<T>>::deposit(
&account_id,
Expand All @@ -214,85 +208,61 @@ impl<T: Trait> Chain<T> {
// get last proposal
if len > 0 {
let mut candidate = Module::<T>::tx_proposal(len - 1).unwrap();
// candidate: CandidateTx
if candidate.confirmed == false {
match <NumberForHash<T>>::get(&candidate.block_hash) {
Some(height) => {
if new_best_header.number >= height + irr_block {
runtime_io::print("----new_best_header.number-----");
let txid = candidate.tx.hash();
/*for output in candidate.tx.outputs.iter() {
let script: Script = output.clone().script_pubkey.into();
let script_address =
script.extract_destinations().unwrap_or(Vec::new());
let network_id = <NetworkId<T>>::get();
let network = if network_id == 1 {
keys::Network::Testnet
} else {
keys::Network::Mainnet
};
let address = keys::Address {
kind: script_address[0].kind,
network,
hash: script_address[0].hash.clone(),
};*/
//let account_id = <AddressMap<T>>::get(address);
//if account_id.is_some() {
for (account_id, _) in candidate.outs.clone() {
// TODO handle err
let _ = <financial_records::Module<T>>::withdrawal_finish(
&account_id,
&symbol,
Some(txid.as_ref().to_vec()),
);
}
//}
candidate.confirmed = true;
// mark this tx withdraw finish!
TxProposal::<T>::insert(len - 1, candidate);
// candidate: CandidateTx
if candidate.confirmed == false {
match <NumberForHash<T>>::get(&candidate.block_hash) {
Some(height) => {
if new_best_header.number >= height + irr_block {
let txid = candidate.tx.hash();
for (account_id, _) in candidate.outs.clone() {
// TODO handle err
let _ = <financial_records::Module<T>>::withdrawal_finish(
&account_id,
&symbol,
Some(txid.as_ref().to_vec()),
);
}
candidate.confirmed = true;
// mark this tx withdraw finish!
TxProposal::<T>::insert(len - 1, candidate);
}
None => {}
}
None => {}
}
}
}

// case 0: 当刚启动时Candidate lenth = 0 时
// case 1: 所有提现交易都是正常逻辑执行,会confirmed.
// case 2: 非正常逻辑提现,candidate.unexpect 会在handle_input时设置,
// 标记该链上这笔proposal由于BTC 托管人没有按着正常逻辑签名广播, 该proposal可能永远不会confirmed.
// 所以开始重新创建proposal.
if len == 0 {
// no withdraw cache would return None
if let Some(indexs) = financial_records::Module::<T>::withdrawal_cache_indexs(&symbol) {
let btc_fee = <BtcFee<T>>::get();
if let Err(e) = <Proposal<T>>::create_proposal(indexs, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
}

// case 0: 当刚启动时Candidate lenth = 0 时
// case 1: 所有提现交易都是正常逻辑执行,会confirmed.
// case 2: 非正常逻辑提现,candidate.unexpect 会在handle_input时设置,
// 标记该链上这笔proposal由于BTC 托管人没有按着正常逻辑签名广播, 该proposal可能永远不会confirmed.
// 所以开始重新创建proposal.
if len == 0 {
// no withdraw cache would return None
if let Some(indexs) = financial_records::Module::<T>::withdrawal_cache_indexs(&symbol) {
let btc_fee = <BtcFee<T>>::get();
if let Err(e) = <Proposal<T>>::create_proposal(indexs, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
}
if len > 0 {
let candidate = Module::<T>::tx_proposal(len - 1).unwrap();
if candidate.confirmed || candidate.unexpect {
// no withdraw cache would return None
if let Some(indexs) = financial_records::Module::<T>::withdrawal_cache_indexs(&symbol) {
let btc_fee = <BtcFee<T>>::get();
if let Err(e) = <Proposal<T>>::create_proposal(indexs, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
}
}
}
if len > 0 {
let candidate = Module::<T>::tx_proposal(len - 1).unwrap();
if candidate.confirmed || candidate.unexpect {
// no withdraw cache would return None
if let Some(indexs) =
financial_records::Module::<T>::withdrawal_cache_indexs(&symbol)
{
let btc_fee = <BtcFee<T>>::get();
if let Err(e) = <Proposal<T>>::create_proposal(indexs, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
}
}
// let candidate = <TxProposal<T>>::get();
// if candidate.is_some() {
// let tx = candidate.unwrap();
// } else {
//
// }
}

// SendCert
if let Some(cert_info) = <CertCache<T>>::take() {
runtime_io::print("------CertCache take");
if let Err(e) = <staking::Module<T>>::issue(cert_info.0, cert_info.1, cert_info.2) {
return Err(ChainErr::OtherErr(e));
}
Expand Down
11 changes: 3 additions & 8 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ mod tests;
mod b58;
mod blockchain;
mod tx;
mod verify_header;
mod utils;
mod verify_header;

use chain::{BlockHeader, Transaction as BTCTransaction};
use codec::Decode;
Expand Down Expand Up @@ -241,7 +241,6 @@ impl<T: Trait> LinkedNodeCollection for LinkedNodes<T> {

decl_storage! {
trait Store for Module<T: Trait> as BridgeOfBTC {
// =====
// header
pub BestIndex get(best_index): BestHeader;

Expand All @@ -257,7 +256,6 @@ decl_storage! {
pub ParamsInfo get(params_info) config(): Params;
pub NetworkId get(network_id) config(): u32;

// =====
// tx
pub ReceiveAddress get(receive_address) config(): Option<keys::Address>;
pub RedeemScript get(redeem_script) config(): Option<Vec<u8>>;
Expand All @@ -268,7 +266,7 @@ decl_storage! {
pub UTXOMaxIndex get(utxo_max_index) config(): u64;
pub IrrBlock get(irr_block) config(): u32;
pub BtcFee get(btc_fee) config(): u64;
// pub TxSet get(tx_set): map H256 => Option<(T::AccountId, keys::Address, TxType, u64, H256, BTCTransaction)>; // Address, type, balance

/// btc all related transactions set, use TxSetTail or TxSetHeader could iter them
TxSetHeader get(tx_list_header): Option<NodeIndex<BTCTxLog<T::AccountId, T::BlockNumber>>>;
TxSetTail get(tx_list_tail): Option<NodeIndex<BTCTxLog<T::AccountId, T::BlockNumber>>>;
Expand All @@ -278,7 +276,6 @@ decl_storage! {
pub AddressMap get(address_map): map Address => Option<T::AccountId>;
pub AccountMap get(account_map): map T::AccountId => Option<keys::Address>;
/// withdrawal tx outs for account, tx_hash => outs ( out index => withdrawal account )
// pub WithdrawalOutsAccount get(withdrawal_outs_account): map H256 => Vec<(u32, T::AccountId)>

pub TxProposalLen get(tx_proposal_len): u32;
pub TxProposal get(tx_proposal): map u32 => Option<CandidateTx<T::AccountId>>;
Expand All @@ -291,7 +288,6 @@ decl_storage! {
pub RegInfoSet get(accounts_set): map u64 => Option<(H256, keys::Address, T::AccountId, T::BlockNumber, Vec<u8>, TxType)>;
pub CertCache get(cert_cache): Option<(Vec<u8>, u32, T::AccountId)>;

// =====
// others
pub Fee get(fee) config(): T::Balance;
}
Expand Down Expand Up @@ -327,6 +323,7 @@ decl_storage! {
impl<T: Trait> Module<T> {
// event
/// Deposit one of this module's events.
#[allow(unused)]
fn deposit_event(event: Event<T>) {
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
}
Expand All @@ -345,7 +342,6 @@ impl<T: Trait> Module<T> {

pub fn push_transaction(origin: T::Origin, tx: Vec<u8>) -> Result {
let from = ensure_signed(origin)?;
runtime_io::print("---------push_transaction--------");
let tx: RelayTx = Decode::decode(&mut tx.as_slice()).ok_or("parse RelayTx err")?;
Self::process_tx(tx, &from)?;
Ok(())
Expand Down Expand Up @@ -398,7 +394,6 @@ impl<T: Trait> Module<T> {
}

pub fn process_tx(tx: RelayTx, who: &T::AccountId) -> Result {
runtime_io::print("---------process_tx--------");
let receive_address: Address = if let Some(h) = <ReceiveAddress<T>>::get() {
h
} else {
Expand Down
Loading

0 comments on commit 8951bdd

Please sign in to comment.