Skip to content

Commit

Permalink
Feature/btc interface (paritytech#107)
Browse files Browse the repository at this point in the history
* Fix receive_address bug

* Add register channels

* Change channel type from String to Vec<u8>

* Add blockhash in TxSet
  • Loading branch information
eee-byte authored and gguoss committed Nov 16, 2018
1 parent 8da5194 commit 6154848
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 39 deletions.
35 changes: 26 additions & 9 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions cxrml/bridge/btc/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use primitives::hash::H256;
use script::Script;

use {
AccountMap, BestIndex, BlockHeaderFor, DepositCache, HashsForNumber, NumberForHash, Params,
ParamsInfo, Trait, TxProposal,
AccountMap, BestIndex, BlockHeaderFor, CertCache, DepositCache, HashsForNumber, NumberForHash,
Params, ParamsInfo, Trait, TxProposal,
};

use tx::{Proposal, RollBack, TxStorage};
Expand Down Expand Up @@ -242,6 +242,12 @@ impl<T: Trait> Chain<T> {
}
}

// SendCert
if let Some(certInfo) = <CertCache<T>>::take() {
runtime_io::print("------CertCache take");
//TO DO
}

<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());
Expand Down
24 changes: 18 additions & 6 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use blockchain::Chain;
use keys::DisplayLayout;
pub use keys::{Address, Error as AddressError};
pub use tx::RelayTx;
use tx::{handle_input, handle_output, handle_proposal, validate_transaction, UTXO};
use tx::{handle_input, handle_output, handle_proposal, handle_cert, validate_transaction, UTXO};

pub trait Trait:
system::Trait + balances::Trait + timestamp::Trait + financial_records::Trait
Expand Down Expand Up @@ -175,6 +175,7 @@ pub enum TxType {
Deposit,
Register,
RegisterDeposit,
SendCert,
}

#[derive(PartialEq, Clone, Encode, Decode)]
Expand Down Expand Up @@ -213,20 +214,23 @@ decl_storage! {
// tx
pub ReceiveAddress get(receive_address) config(): Option<Address>;
pub RedeemScript get(redeem_script) config(): Option<Vec<u8>>;
pub CertAddress get(cert_address) config(): Option<keys::Address>;
pub CertRedeemScript get(cert_redeem_script) config(): Option<Vec<u8>>;

pub UTXOSet get(utxo_set): map u64 => UTXO;
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, Address, TxType, u64, BTCTransaction)>; // Address, type, balance
pub TxSet get(tx_set): map H256 => Option<(T::AccountId, keys::Address, TxType, u64, BTCTransaction, H256)>; // Address, type, balance
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<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

pub AccountsMaxIndex get(accounts_max_index) config(): u64;
pub AccountsSet get(accounts_set): map u64 => Option<(H256, Address, T::AccountId, T::BlockNumber, TxType)>;
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)>;

// =====
// others
Expand Down Expand Up @@ -339,12 +343,20 @@ impl<T: Trait> Module<T> {
} else {
return Err("should set RECEIVE_address first");
};

let tx_type = validate_transaction::<T>(&tx, &receive_address).unwrap();
let cert_address: keys::Address = if let Some(h) = <CertAddress<T>>::get() {
h
} else {
return Err("should set CERT_address first");
};
let tx_type = validate_transaction::<T>(&tx, (&receive_address, &cert_address)).unwrap();
match tx_type {
TxType::Withdraw => {
handle_input::<T>(&tx.raw, &tx.block_hash, &who, &receive_address);
}
TxType::SendCert => {
handle_cert::<T>(&tx.raw, &tx.block_hash, &who, &cert_address);

}
_ => {
let _utxos = handle_output::<T>(
&tx.raw,
Expand Down
11 changes: 6 additions & 5 deletions cxrml/bridge/btc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use self::base58::FromBase58;
use super::*;
use runtime_io;
use runtime_io::with_externalities;
use self::keys::DisplayLayout;
use runtime_primitives::testing::{Digest, DigestItem, Header};
use runtime_primitives::traits::BlakeTwo256;
use runtime_primitives::BuildStorage;
Expand Down Expand Up @@ -112,7 +113,7 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
irr_block: 6,
btc_fee: 10,
accounts_max_index: 0,
receive_address: keys::Address::from_layout(&"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap()).unwrap(),
receive_address: keys::Address::from_layout(&"2N4C127fBSmqBsNuHeLmAbZEVSPfV6GB2j2".from_base58().unwrap()).unwrap(),
redeem_script: b"52210257aff1270e3163aaae9d972b3d09a2385e0d4877501dbeca3ee045f8de00d21c2103fd58c689594b87bbe20a9a00091d074dc0d9f49a988a7ad4c2575adeda1b507c2102bb2a5aa53ba7c0d77bdd86bb9553f77dd0971d3a6bb6ad609787aa76eb17b6b653ae".to_vec(),
fee: 0,
}.build_storage()
Expand Down Expand Up @@ -156,7 +157,7 @@ pub fn new_test_ext_err_genesisblock() -> runtime_io::TestExternalities<Blake2Ha
irr_block: 6,
btc_fee: 10,
accounts_max_index: 0,
receive_address: keys::Address::from_layout(&"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap()).unwrap(),
receive_address: keys::Address::from_layout(&"2N4C127fBSmqBsNuHeLmAbZEVSPfV6GB2j2".from_base58().unwrap()).unwrap(),
redeem_script: b"52210257aff1270e3163aaae9d972b3d09a2385e0d4877501dbeca3ee045f8de00d21c2103fd58c689594b87bbe20a9a00091d074dc0d9f49a988a7ad4c2575adeda1b507c2102bb2a5aa53ba7c0d77bdd86bb9553f77dd0971d3a6bb6ad609787aa76eb17b6b653ae".to_vec(),
fee: 0,
}.build_storage()
Expand Down Expand Up @@ -273,7 +274,7 @@ pub fn new_test_mock_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
irr_block: 6,
btc_fee: 10,
accounts_max_index: 0,
receive_address: keys::Address::from_layout(&"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap()).unwrap(),
receive_address: keys::Address::from_layout(&"2N4C127fBSmqBsNuHeLmAbZEVSPfV6GB2j2".from_base58().unwrap()).unwrap(),
redeem_script: b"52210257aff1270e3163aaae9d972b3d09a2385e0d4877501dbeca3ee045f8de00d21c2103fd58c689594b87bbe20a9a00091d074dc0d9f49a988a7ad4c2575adeda1b507c2102bb2a5aa53ba7c0d77bdd86bb9553f77dd0971d3a6bb6ad609787aa76eb17b6b653ae".to_vec(),
fee: 0,
}.build_storage()
Expand Down Expand Up @@ -650,7 +651,7 @@ pub fn new_test_ext2() -> runtime_io::TestExternalities<Blake2Hasher> {
irr_block: 6,
btc_fee: 10,
accounts_max_index: 0,
receive_address: keys::Address::from_layout(&"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap()).unwrap(),
receive_address: keys::Address::from_layout(&"2N4C127fBSmqBsNuHeLmAbZEVSPfV6GB2j2".from_base58().unwrap()).unwrap(),
redeem_script: b"52210257aff1270e3163aaae9d972b3d09a2385e0d4877501dbeca3ee045f8de00d21c2103fd58c689594b87bbe20a9a00091d074dc0d9f49a988a7ad4c2575adeda1b507c2102bb2a5aa53ba7c0d77bdd86bb9553f77dd0971d3a6bb6ad609787aa76eb17b6b653ae".to_vec(),
fee: 0,
}.build_storage()
Expand Down Expand Up @@ -723,7 +724,7 @@ pub fn new_test_ext3() -> runtime_io::TestExternalities<Blake2Hasher> {
irr_block: 6,
btc_fee: 10,
accounts_max_index: 0,
receive_address: keys::Address::from_layout(&"mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b".from_base58().unwrap()).unwrap(),
receive_address: keys::Address::from_layout(&"2N4C127fBSmqBsNuHeLmAbZEVSPfV6GB2j2".from_base58().unwrap()).unwrap(),
redeem_script: b"52210257aff1270e3163aaae9d972b3d09a2385e0d4877501dbeca3ee045f8de00d21c2103fd58c689594b87bbe20a9a00091d074dc0d9f49a988a7ad4c2575adeda1b507c2102bb2a5aa53ba7c0d77bdd86bb9553f77dd0971d3a6bb6ad609787aa76eb17b6b653ae".to_vec(),
fee: 0,
}.build_storage()
Expand Down
Loading

0 comments on commit 6154848

Please sign in to comment.