Skip to content

Commit

Permalink
Fix/xbridge btc (paritytech#275)
Browse files Browse the repository at this point in the history
* Fix signature number

* Fix genesis configure error
Organize log information

* Fix veto signature

* Update wasm
Update blockheader info

* Make format

* Fix typo
  • Loading branch information
eee-byte authored and liuchengxu committed Feb 18, 2019
1 parent 286c401 commit 3e715dc
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 88 deletions.
48 changes: 32 additions & 16 deletions cli/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
let auth4 = satoshi.into();
let initial_authorities = match genesis_spec {
GenesisSpec::Dev => vec![auth1],
GenesisSpec::Local => vec![auth1, auth2, auth3],
GenesisSpec::Local => vec![auth1, auth2, auth3, auth4],
GenesisSpec::Multi => vec![auth1, auth2, auth3, auth4, gavin.into(), dave.into()],
};

Expand Down Expand Up @@ -102,36 +102,52 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {

let mut full_endowed = vec![
(
auth1, // auth
auth1, // auth
apply_prec(12.5), // balance
b"Alice".to_vec(), // name
b"Alice.com".to_vec(), // url
b"03f72c448a0e59f48d4adef86cba7b278214cece8e56ef32ba1d179e0a8129bdba".to_vec(), // hot_entity
b"02a79800dfed17ad4c78c52797aa3449925692bc8c83de469421080f42d27790ee".to_vec(), // cold_entity
"03f72c448a0e59f48d4adef86cba7b278214cece8e56ef32ba1d179e0a8129bdba"
.from_hex()
.unwrap(), // hot_entity
"02a79800dfed17ad4c78c52797aa3449925692bc8c83de469421080f42d27790ee"
.from_hex()
.unwrap(), // cold_entity
),
(
auth2,
apply_prec(12.5),
b"Bob".to_vec(),
b"Bob.com".to_vec(),
b"0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd".to_vec(),
b"03ece1a20b5468b12fd7beda3e62ef6b2f6ad9774489e9aff1c8bc684d87d70780".to_vec(),
"0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd"
.from_hex()
.unwrap(),
"03ece1a20b5468b12fd7beda3e62ef6b2f6ad9774489e9aff1c8bc684d87d70780"
.from_hex()
.unwrap(),
),
(
auth3,
apply_prec(12.5),
b"Charlie".to_vec(),
b"Charlie.com".to_vec(),
b"0311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae40".to_vec(),
b"02e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2".to_vec(),
"0311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae40"
.from_hex()
.unwrap(),
"02e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2"
.from_hex()
.unwrap(),
),
(
auth4,
apply_prec(12.5),
b"Satoshi".to_vec(),
b"Satoshi.com".to_vec(),
b"0227e54b65612152485a812b8856e92f41f64788858466cc4d8df674939a5538c3".to_vec(),
b"020699bf931859cafdacd8ac4d3e055eae7551427487e281e3efba618bdd395f2f".to_vec(),
"0227e54b65612152485a812b8856e92f41f64788858466cc4d8df674939a5538c3"
.from_hex()
.unwrap(),
"020699bf931859cafdacd8ac4d3e055eae7551427487e281e3efba618bdd395f2f"
.from_hex()
.unwrap(),
),
];

Expand Down Expand Up @@ -231,13 +247,13 @@ pub fn testnet_genesis(genesis_spec: GenesisSpec) -> GenesisConfig {
bitcoin: Some(XBridgeOfBTCConfig {
// start genesis block: (genesis, blocknumber)
genesis: (BlockHeader {
version: 536870912,
previous_header_hash: H256::from_reversed_str("000000000009a5e5b8b7154bcac5f28f43e22bc3b61883ce65c8caf09f9fa03b"),
merkle_root_hash: H256::from_reversed_str("89323c349e2ec768dcb5b0740eb221103a4490a00e1d137e5c9a4082521bff5e"),
time: 1549942457,
version: 980090880,
previous_header_hash: H256::from_reversed_str("00000000000000ab706b663326210d03780fea6ecfe0cc59c78f0c7dddba9cc2"),
merkle_root_hash: H256::from_reversed_str("91ee572484dabc6edf5a8da44a4fb55b5040facf66624b2a37c4f633070c60c8"),
time: 1550454022,
bits: Compact::new(436283074),
nonce: 2885019376,
}, 1456835),
nonce: 47463732,
}, 1457525),
params_info: Params::new(520159231, // max_bits
2 * 60 * 60, // block_max_future
3, // max_fork_route_preset
Expand Down
Binary file not shown.
10 changes: 5 additions & 5 deletions xrml/xbridge/bitcoin/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ impl<T: Trait> Chain<T> {
fn canonize(hash: &H256) -> Result<(), ChainErr> {
let confirmed_header: BlockHeaderInfo = match <BlockHeaderFor<T>>::get(hash) {
Some(header) => header,
None => return Err(ChainErr::OtherErr("not found blockheader for this hash")),
None => return Err(ChainErr::OtherErr("Not found block header for this hash")),
};

runtime_io::print("[bridge-btc] confirmed header height:");
runtime_io::print("[bridge-btc] Confirmed header height:");
runtime_io::print(confirmed_header.height as u64);

let tx_list = confirmed_header.txid;
for txid in tx_list {
runtime_io::print("[bridge-btc] handle confirmed_header's tx list");
// deposit & bind & withdraw & cert
runtime_io::print("[bridge-btc] Handle confirmed_header's tx list");
// deposit & withdraw
match handle_tx::<T>(&txid) {
Err(_) => {
runtime_io::print("[bridge-btc] handle_tx error, tx hash:");
runtime_io::print("[bridge-btc] Handle_tx error, tx hash:");
runtime_io::print(&txid[..]);
}
Ok(()) => (),
Expand Down
1 change: 0 additions & 1 deletion xrml/xbridge/bitcoin/src/header_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ pub fn work_required_retarget<T: Trait>(
params,
));
retarget = retarget / U256::from(params.target_timespan_seconds);
// retarget = retarget / U256::from(TARGET_TIMESPAN_SECONDS);
if retarget > maximum {
params.max_bits()
} else {
Expand Down
12 changes: 6 additions & 6 deletions xrml/xbridge/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,11 @@ impl<T: Trait> Module<T> {
return Err("Already signature transaction or reject to signature");
}
if !vote_state {
let reject_count: Vec<&(T::AccountId, bool)> = data
.sig_node
.iter()
.filter(|(_, vote)| *vote == false)
.collect();
data.sig_node.clone().push((who, vote_state));
let sig_node = data.sig_node.clone();
let reject_count: Vec<&(T::AccountId, bool)> =
sig_node.iter().filter(|(_, vote)| *vote == false).collect();
data.sig_node.push((who, vote_state));
runtime_io::print("Veto signature");
if reject_count.len() + 1 >= sign_num {
runtime_io::print("Clear TxProposal");
<TxProposal<T>>::kill();
Expand All @@ -598,6 +597,7 @@ impl<T: Trait> Module<T> {
} else {
return Err("No signature");
};
runtime_io::print("Signature pass");
data.sig_node.push((who, vote_state));
if sigs.len() >= sign_num {
data.sig_status = VoteResult::FinishWithFavor;
Expand Down
106 changes: 61 additions & 45 deletions xrml/xbridge/bitcoin/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ pub fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
// start genesis block: (genesis, blocknumber)
genesis: (
BlockHeader {
version: 536870912,
version: 980090880,
previous_header_hash: H256::from_reversed_str(
"00000000f1c80c38f9bd6ebf9ca796d92122e5b2a1539ac06e09252a1a7e3d01",
"00000000000000ab706b663326210d03780fea6ecfe0cc59c78f0c7dddba9cc2",
),
merkle_root_hash: H256::from_reversed_str(
"815ca8bbed88af8afaa6c4995acba6e6e7453e705e0bc7039472aa3b6191a707",
"91ee572484dabc6edf5a8da44a4fb55b5040facf66624b2a37c4f633070c60c8",
),
time: 1546999089,
bits: Compact::new(436290411),
nonce: 562223693,
time: 1550454022,
bits: Compact::new(436283074),
nonce: 47463732,
},
1451572,
1457525,
),
params_info: Params::new(
520159231, // max_bits
Expand Down Expand Up @@ -158,18 +158,18 @@ pub fn new_test_ext_err_genesisblock() -> runtime_io::TestExternalities<Blake2Ha
// start genesis block: (genesis, blocknumber)
genesis: (
BlockHeader {
version: 536870912,
version: 980090880,
previous_header_hash: H256::from_reversed_str(
"000000000009a5e5b8b7154bcac5f28f43e22bc3b61883ce65c8caf09f9fa03b",
"00000000000000ab706b663326210d03780fea6ecfe0cc59c78f0c7dddba9cc2",
),
merkle_root_hash: H256::from_reversed_str(
"89323c349e2ec768dcb5b0740eb221103a4490a00e1d137e5c9a4082521bff5e",
"91ee572484dabc6edf5a8da44a4fb55b5040facf66624b2a37c4f633070c60c8",
),
time: 1549942457,
time: 1550454022,
bits: Compact::new(436283074),
nonce: 2885019376,
nonce: 47463732,
},
1456835,
1457525,
),
params_info: Params::new(
520159231, // max_bits
Expand Down Expand Up @@ -509,18 +509,18 @@ pub fn new_test_ext2() -> runtime_io::TestExternalities<Blake2Hasher> {
// start genesis block: (genesis, blocknumber)
genesis: (
BlockHeader {
version: 536870912,
version: 980090880,
previous_header_hash: H256::from_reversed_str(
"000000000009a5e5b8b7154bcac5f28f43e22bc3b61883ce65c8caf09f9fa03b",
"00000000000000ab706b663326210d03780fea6ecfe0cc59c78f0c7dddba9cc2",
),
merkle_root_hash: H256::from_reversed_str(
"89323c349e2ec768dcb5b0740eb221103a4490a00e1d137e5c9a4082521bff5e",
"91ee572484dabc6edf5a8da44a4fb55b5040facf66624b2a37c4f633070c60c8",
),
time: 1549942457,
time: 1550454022,
bits: Compact::new(436283074),
nonce: 2885019376,
nonce: 47463732,
},
1456835,
1457525,
),
params_info: Params::new(
520159231, // max_bits
Expand Down Expand Up @@ -581,18 +581,18 @@ pub fn new_test_ext3() -> runtime_io::TestExternalities<Blake2Hasher> {
// start genesis block: (genesis, blocknumber)
genesis: (
BlockHeader {
version: 536870912,
version: 980090880,
previous_header_hash: H256::from_reversed_str(
"000000000009a5e5b8b7154bcac5f28f43e22bc3b61883ce65c8caf09f9fa03b",
"00000000000000ab706b663326210d03780fea6ecfe0cc59c78f0c7dddba9cc2",
),
merkle_root_hash: H256::from_reversed_str(
"89323c349e2ec768dcb5b0740eb221103a4490a00e1d137e5c9a4082521bff5e",
"91ee572484dabc6edf5a8da44a4fb55b5040facf66624b2a37c4f633070c60c8",
),
time: 1549942457,
time: 1550454022,
bits: Compact::new(436283074),
nonce: 2885019376,
nonce: 47463732,
},
1456835,
1457525,
),
params_info: Params::new(
520159231, // max_bits
Expand Down Expand Up @@ -693,15 +693,16 @@ pub fn test_multi_address() {
}

fn create_multi_address(pubkeys: Vec<Vec<u8>>) -> Address {
let mut build = Builder::default().push_opcode(Opcode::OP_2);
let mut build = Builder::default().push_opcode(Opcode::OP_3);
for (i, pubkey) in pubkeys.iter().enumerate() {
build = build.push_bytes(pubkey);
}
let script = build
.push_opcode(Opcode::OP_3)
.push_opcode(Opcode::OP_4)
.push_opcode(Opcode::OP_CHECKMULTISIG)
.into_script();

println!("script:{:?}", script.to_bytes().to_vec());
println!("script:{:?}", Script::from(script.to_bytes().to_vec()));
let multisig_address = Address {
kind: keys::Type::P2SH,
network: keys::Network::Testnet,
Expand All @@ -712,32 +713,48 @@ fn create_multi_address(pubkeys: Vec<Vec<u8>>) -> Address {

#[test]
fn test_create_multi_address() {
//hot

let pubkey1_bytes =
hex::decode("03f72c448a0e59f48d4adef86cba7b278214cece8e56ef32ba1d179e0a8129bdba").unwrap();
let pubkey2_bytes =
hex::decode("0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd").unwrap();
let pubkey3_bytes =
hex::decode("0311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae40").unwrap();
let pubkey2_bytes =
hex::decode("02e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2").unwrap();
let pubkey1_bytes =
hex::decode("023e505c48a955e759ce61145dc4a9a7447425290b8483f4e36f05169e7967c86d").unwrap();
let pubkey4_bytes =
hex::decode("0306117a360e5dbe10e1938a047949c25a86c0b0e08a0a7c1e611b97de6b2917dd").unwrap();
hex::decode("0227e54b65612152485a812b8856e92f41f64788858466cc4d8df674939a5538c3").unwrap();

//cold
let pubkey5_bytes =
hex::decode("02a79800dfed17ad4c78c52797aa3449925692bc8c83de469421080f42d27790ee").unwrap();
let pubkey6_bytes =
hex::decode("03ece1a20b5468b12fd7beda3e62ef6b2f6ad9774489e9aff1c8bc684d87d70780").unwrap();
let pubkey7_bytes =
hex::decode("02e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2").unwrap();
let pubkey8_bytes =
hex::decode("020699bf931859cafdacd8ac4d3e055eae7551427487e281e3efba618bdd395f2f").unwrap();

let mut hot_keys = Vec::new();
let mut cold_keys = Vec::new();
hot_keys.push(pubkey1_bytes.clone());
hot_keys.push(pubkey2_bytes.clone());
hot_keys.push(pubkey3_bytes.clone());
hot_keys.push(pubkey4_bytes.clone());
cold_keys.push(pubkey1_bytes);
cold_keys.push(pubkey2_bytes);
cold_keys.push(pubkey3_bytes);
hot_keys.push(pubkey1_bytes);
hot_keys.push(pubkey2_bytes);
hot_keys.push(pubkey3_bytes);
hot_keys.push(pubkey4_bytes);

cold_keys.push(pubkey5_bytes);
cold_keys.push(pubkey6_bytes);
cold_keys.push(pubkey7_bytes);
cold_keys.push(pubkey8_bytes);

let hot_addr = create_multi_address(hot_keys);
let cold_addr = create_multi_address(cold_keys);
println!("hot_addr:{:?}", hot_addr.to_string());
println!("cold_addr:{:?}", cold_addr.to_string());

let layout_addr = cold_addr.layout().to_vec();
let layout = [
196, 10, 18, 79, 99, 6, 23, 210, 211, 220, 115, 137, 86, 4, 75, 195, 77, 76, 168, 39, 29,
191, 246, 217, 153,
196, 96, 201, 52, 180, 27, 175, 109, 29, 168, 76, 211, 20, 252, 208, 243, 210, 16, 105, 83,
0, 42, 109, 109, 135,
];

assert_eq!(layout_addr, layout);
Expand All @@ -747,8 +764,8 @@ fn test_create_multi_address() {
assert_eq!(cold_addr, addr);

let pks = [
169, 20, 10, 18, 79, 99, 6, 23, 210, 211, 220, 115, 137, 86, 4, 75, 195, 77, 76, 168, 39,
29, 135,
169, 20, 96, 201, 52, 180, 27, 175, 109, 29, 168, 76, 211, 20, 252, 208, 243, 210, 16, 105,
83, 0, 135,
];

let pk = addr.hash.clone().to_vec();
Expand Down Expand Up @@ -790,7 +807,6 @@ fn test_sign_withdraw() {
let tx1 = hex::decode("01000000019d15247f7f75ffd6e9377ea928f476bcaf9ab542563429b97ee2ef89f2c9d4a101000000b5004830450221008c9147795b2ddf923d5dad3c9fcfde6394aa2629b9a10ca8f93a5c6d4293a7490220687aeb3318b35450fda4d45cc54177f3d6f898d15ea1f8705a77c7116cb44fe8014c695221023e505c48a955e759ce61145dc4a9a7447425290b8483f4e36f05169e7967c86d2102e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2210311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae4053aeffffffff01e8030000000000001976a914023dbd259dd15fc43da1a758ea7b2bfaec97893488ac00000000").unwrap();
let tx2 = hex::decode("01000000019d15247f7f75ffd6e9377ea928f476bcaf9ab542563429b97ee2ef89f2c9d4a101000000fdfd00004830450221008c9147795b2ddf923d5dad3c9fcfde6394aa2629b9a10ca8f93a5c6d4293a7490220687aeb3318b35450fda4d45cc54177f3d6f898d15ea1f8705a77c7116cb44fe80147304402204b999fbf18b944a3f6446ca56d094d70699a1e44c8636b06fc2267434e9200ae022073327aca6cdad35075c9c8bb2759a24753906ef030ccb513d8a515648ab46d0e014c695221023e505c48a955e759ce61145dc4a9a7447425290b8483f4e36f05169e7967c86d2102e34d10113f2dd162e8d8614a4afbb8e2eb14eddf4036042b35d12cf5529056a2210311252930af8ba766b9c7a6580d8dc4bbf9b0befd17a8ef7fabac275bba77ae4053aeffffffff01e8030000000000001976a914023dbd259dd15fc43da1a758ea7b2bfaec97893488ac00000000").unwrap();
let tx: Transaction = deserialize(Reader::new(&tx2)).unwrap();
XBridgeOfBTC::init_trusteeaddress().unwrap();
XBridgeOfBTC::apply_sign_withdraw(0, tx, true).unwrap();
})
}
8 changes: 4 additions & 4 deletions xrml/xbridge/bitcoin/src/tx/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> TxHandler<'a> {
let txid = self.0;
let tx_info = <TxFor<T>>::get(txid);

for (_index, output) in tx_info.raw_tx.outputs.iter().enumerate() {
for (_, output) in tx_info.raw_tx.outputs.iter().enumerate() {
let script = &output.script_pubkey;
let into_script: Script = script.clone().into();

Expand Down Expand Up @@ -196,7 +196,7 @@ fn remove_pending_deposit<T: Trait>(input_address: &keys::Address, who: &T::Acco
if let Some(record) = <PendingDepositMap<T>>::get(input_address) {
for r in record {
deposit_token::<T>(who, r.balance);
runtime_io::print("[bridge-btc] handle_output PendingDepositMap ");
runtime_io::print("[bridge-btc] Handle pending deposit");
runtime_io::print(r.balance);
}
<PendingDepositMap<T>>::remove(input_address);
Expand All @@ -215,14 +215,14 @@ fn insert_pending_deposit<T: Trait>(input_address: &keys::Address, txid: H256, b
key.push(k);
<PendingDepositMap<T>>::insert(input_address, key);

runtime_io::print("[bridge-btc](Some)handle_output PendingDeposit token: ");
runtime_io::print("[bridge-btc] Add pending peposit");
}
None => {
let mut cache: Vec<DepositCache> = Vec::new();
cache.push(k);
<PendingDepositMap<T>>::insert(input_address, cache);

runtime_io::print("[bridge-btc](None)handle_output PendingDeposit token: ");
runtime_io::print("[bridge-btc] New pending peposit");
}
};
}
Loading

0 comments on commit 3e715dc

Please sign in to comment.