Skip to content

Commit

Permalink
Fix/tx set (paritytech#126)
Browse files Browse the repository at this point in the history
* change node struct data sequence

* Add BlockNumber in TxSet

* provide financialrecords record txid, rename token

* add txid for Record in financialrecords
* rename token
* format code

* Fix totalBalance

* change verify btc address to base58

* /TxSet:

Change cert handle

* Temporarily comment to issue certificate

* Add cert issue

* format
  • Loading branch information
eee-byte authored and Aton committed Nov 28, 2018
1 parent 215611a commit bb86a1c
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 136 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 @@ -24,6 +24,7 @@ cxrml-associations = { path = "../../associations", default-features = false }
cxrml-support = { path = "../../support", default-features = false }
cxrml-tokenbalances = { path = "../../tokenbalances", default-features = false }
cxrml-funds-financialrecords = { path = "../../funds/financialrecords", default-features = false }
cxrml-mining-staking = { path = "../../mining/staking", default-features = false }

# bitcoin-rust
chain = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
Expand Down Expand Up @@ -61,6 +62,7 @@ std=[
"cxrml-support/std",
"cxrml-tokenbalances/std",
"cxrml-funds-financialrecords/std",
"cxrml-mining-staking/std",

# bitcoin-rust
"chain/std",
Expand Down
29 changes: 18 additions & 11 deletions cxrml/bridge/btc/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use financial_records;
use financial_records::Symbol;
use primitives::hash::H256;
use script::Script;
use staking;

use {
AccountMap, BestIndex, BlockHeaderFor, CertCache, DepositCache, HashsForNumber, Module,
Expand Down Expand Up @@ -169,8 +170,8 @@ impl<T: Trait> Chain<T> {
// Deposit
if let Some(vec) = <DepositCache<T>>::take() {
runtime_io::print("-----------DepositCache take");
let mut uncomplete_cache: Vec<(T::AccountId, u64, H256)> = Vec::new();
for (account_id, amount, block_hash) in vec {
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 {
Expand All @@ -179,13 +180,14 @@ impl<T: Trait> Chain<T> {
&account_id,
&symbol,
As::sa(amount),
Some(tx_hash.as_ref().to_vec()),
);
} else {
uncomplete_cache.push((account_id, amount, block_hash));
uncomplete_cache.push((account_id, amount, tx_hash, block_hash));
}
}
None => {
uncomplete_cache.push((account_id, amount, block_hash));
uncomplete_cache.push((account_id, amount, tx_hash, block_hash));
} // Optmise
}
}
Expand All @@ -200,6 +202,7 @@ impl<T: Trait> Chain<T> {
Some(height) => {
if new_best_header.number > height + irr_block {
runtime_io::print("----new_best_header.number-----");
let txid = tx.tx.hash();
for output in tx.tx.outputs.iter() {
let script: Script = output.clone().script_pubkey.into();
let script_address =
Expand All @@ -217,17 +220,15 @@ impl<T: Trait> Chain<T> {
};
let account_id = <AddressMap<T>>::get(address);
if account_id.is_some() {
runtime_io::print("----account_id.is_some()-----");
<financial_records::Module<T>>::withdrawal_finish(
&account_id.unwrap(),
&symbol,
true,
Some(txid.as_ref().to_vec()),
);
}
}
let vec = <financial_records::Module<T>>::get_withdraw_cache(&symbol);
if vec.is_some() {
runtime_io::print("----account_id.is_some()-----");
let mut address_vec = Vec::new();
for (account_id, balance) in vec.unwrap() {
let address = <AccountMap<T>>::get(account_id);
Expand All @@ -236,7 +237,9 @@ impl<T: Trait> Chain<T> {
}
}
let btc_fee = <BtcFee<T>>::get();
let _ = <Proposal<T>>::create_proposal(address_vec, btc_fee);
if let Err(e) = <Proposal<T>>::create_proposal(address_vec, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
} else {
<TxProposal<T>>::kill();
}
Expand All @@ -256,13 +259,17 @@ impl<T: Trait> Chain<T> {
}
}
let btc_fee = <BtcFee<T>>::get();
let _ = <Proposal<T>>::create_proposal(address_vec, btc_fee);
if let Err(e) = <Proposal<T>>::create_proposal(address_vec, btc_fee) {
return Err(ChainErr::OtherErr(e));
}
}
}
// SendCert
if let Some(_cert_info) = <CertCache<T>>::take() {
if let Some(cert_info) = <CertCache<T>>::take() {
runtime_io::print("------CertCache take");
//TO DO
if let Err(e) = <staking::Module<T>>::issue(cert_info.0, cert_info.1, cert_info.2) {
return Err(ChainErr::OtherErr(e));
}
}

<NumberForHash<T>>::insert(new_best_header.hash.clone(), new_best_header.number);
Expand Down
29 changes: 18 additions & 11 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern crate srml_timestamp as timestamp;
#[cfg(test)]
extern crate cxrml_associations as associations;
extern crate cxrml_funds_financialrecords as financial_records;
extern crate cxrml_mining_staking as staking;
extern crate cxrml_support as cxsupport;
#[cfg(test)]
extern crate cxrml_system as cxsystem;
Expand Down Expand Up @@ -85,7 +86,7 @@ pub use tx::RelayTx;
use tx::{handle_cert, handle_input, handle_output, handle_proposal, validate_transaction, UTXO};

pub trait Trait:
system::Trait + balances::Trait + timestamp::Trait + financial_records::Trait
system::Trait + balances::Trait + timestamp::Trait + financial_records::Trait + staking::Trait
{
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
Expand All @@ -108,7 +109,7 @@ decl_module! {
}

impl<T: Trait> tokenbalances::TokenT for Module<T> {
const SYMBOL: &'static [u8] = b"btc";
const SYMBOL: &'static [u8] = b"BTC";
fn check_addr(addr: &[u8], _: &[u8]) -> Result {
Self::verify_btc_address(addr).map_err(|_| "verify btc addr err")?;
Ok(())
Expand Down Expand Up @@ -195,16 +196,19 @@ pub struct CandidateTx<AccountId: Parameter + Ord + Default> {
}

#[derive(PartialEq, Clone, Encode, Decode)]
pub struct BTCTxLog<AccountId> {
pub struct BTCTxLog<AccountId, BlockNumber> {
pub who: AccountId,
pub addr: keys::Address,
pub tx_type: TxType,
pub balance: u64,
pub block_hash: H256,
pub time: BlockNumber,
pub tx: BTCTransaction,
}

impl<AccountId: Parameter + Ord + Default> NodeT for BTCTxLog<AccountId> {
impl<AccountId: Parameter + Ord + Default, BlockNumber: Parameter + Copy + Default> NodeT
for BTCTxLog<AccountId, BlockNumber>
{
type Index = H256;
fn index(&self) -> H256 {
self.tx.hash()
Expand Down Expand Up @@ -250,19 +254,21 @@ decl_storage! {
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>>>;
TxSetTail get(tx_list_tail): Option<NodeIndex<BTCTxLog<T::AccountId>>>;
TxSet get(tx_set): map H256 => Option<Node<BTCTxLog<T::AccountId>>>;
TxSetHeader get(tx_list_header): Option<NodeIndex<BTCTxLog<T::AccountId, T::BlockNumber>>>;
TxSetTail get(tx_list_tail): Option<NodeIndex<BTCTxLog<T::AccountId, T::BlockNumber>>>;
TxSet get(tx_set): map H256 => Option<Node<BTCTxLog<T::AccountId, T::BlockNumber>>>;

pub BlockTxids get(block_txids): map H256 => Vec<H256>;
pub AddressMap get(address_map): map Address => Option<T::AccountId>;
pub AccountMap get(account_map): map T::AccountId => Option<keys::Address>;
pub TxProposal get(tx_proposal): Option<CandidateTx<T::AccountId>>;
pub DepositCache get(deposit_cache): Option<Vec<(T::AccountId, u64, H256)>>; // account_id, amount, H256
/// account, btc value, txhash, blockhash
pub DepositCache get(deposit_cache): Option<Vec<(T::AccountId, u64, H256, H256)>>;
/// tx_hash, utxo index, btc value, blockhash
pub DepositRecords get(deposit_records): map Address => Option<Vec<(H256, u32, u64, H256)>>;
pub RegInfoMaxIndex get(accounts_max_index) config(): u64;
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>, T::AccountId)>;
pub CertCache get(cert_cache): Option<(Vec<u8>, u32, T::AccountId)>;

// =====
// others
Expand Down Expand Up @@ -336,7 +342,8 @@ impl<T: Trait> Module<T> {

impl<T: Trait> Module<T> {
pub fn verify_btc_address(data: &[u8]) -> StdResult<Address, AddressError> {
Address::from_layout(data)
let r = b58::from(data.to_vec()).map_err(|_| AddressError::InvalidAddress)?;
Address::from_layout(&r)
}

pub fn process_header(header: BlockHeader, who: &T::AccountId) -> Result {
Expand Down Expand Up @@ -387,7 +394,7 @@ impl<T: Trait> Module<T> {
handle_input::<T>(&tx.raw, &tx.block_hash, &who, &receive_address);
}
TxType::SendCert => {
handle_cert::<T>(&tx.raw, &tx.block_hash, &who, &cert_address);
handle_cert::<T>(&tx.raw);
}
_ => {
handle_output::<T>(
Expand Down
Loading

0 comments on commit bb86a1c

Please sign in to comment.