Skip to content

Commit

Permalink
fix bug for push no prev error for genesis (paritytech#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aton authored Mar 22, 2019
1 parent 4c1fb67 commit aaeba68
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 54 deletions.
Binary file modified cli/src/chainx_runtime.compact.wasm
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion xrml/xaccounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ impl<T: Trait> Module<T> {
};
TrusteeSessionInfoLen::<T>::insert(chain, number);

Self::deposit_event(RawEvent::NewTrustees(chain, session_number, trustee_list, hot_address, cold_address));
Self::deposit_event(RawEvent::NewTrustees(
chain,
session_number,
trustee_list,
hot_address,
cold_address,
));
}
}

Expand Down
41 changes: 23 additions & 18 deletions xrml/xbridge/bitcoin/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{BlockHashFor, BlockHeaderFor, Module, Trait};

#[cfg(feature = "std")]
use crate::hash_strip;
use xsupport::{debug, error};
use xsupport::{debug, error, info};

pub enum ChainErr {
/// Unknown parent
Expand Down Expand Up @@ -97,27 +97,32 @@ pub fn update_confirmed_header<T: Trait>(header_info: &BlockHeaderInfo) -> (H256
let confirmations = Module::<T>::confirmation_number();
let mut prev_hash = header_info.header.previous_header_hash.clone();
for _ in 1..confirmations {
if let Some(info) = Module::<T>::block_header_for(prev_hash) {
if let Some(info) = Module::<T>::block_header_for(&prev_hash) {
prev_hash = info.header.previous_header_hash
} else {
// no not have prev hash in storage, return genesis header info
let (header, height) = Module::<T>::genesis_info();
return (header.hash(), height);
}
}
// prev_hash is confirmed header, prev block must be existed!
BlockHeaderFor::<T>::mutate(&prev_hash, |info| {
if let Some(header) = info {
handle_confirm_block::<T>(header);
header.confirmed = true
} else {
error!(
"[update_confirmed_header]|prev block not exist!|prev:{:?}",
prev_hash
// if not find current header info, jump out of loop
info!(
"[update_confirmed_header]|not find for hash:{:?}, current reverse count:{:}",
prev_hash, confirmations
);
assert!(false, "prev block not exist at this point!");
break;
}
});
}

if let Some(mut header) = Module::<T>::block_header_for(&prev_hash) {
handle_confirm_block::<T>(&header);
header.confirmed = true;
BlockHeaderFor::<T>::insert(&prev_hash, header);
} else {
// no not have prev hash in storage, return genesis header info
info!(
"[update_confirmed_header]|not find prev header, use genesis instead|prev:{:?}",
prev_hash
);
let (header, height) = Module::<T>::genesis_info();
return (header.hash(), height);
}

(prev_hash, header_info.height - confirmations)
}

Expand Down
71 changes: 37 additions & 34 deletions xrml/xbridge/bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use types::{

use xsupport::{debug, ensure_with_errorlog, error, info, warn};
#[cfg(feature = "std")]
use xsupport::{u8array_to_hex, u8array_to_string, trustees};
use xsupport::{trustees, u8array_to_hex, u8array_to_string};

pub type AddrStr = XString;

Expand Down Expand Up @@ -258,48 +258,51 @@ impl<T: Trait> TrusteeForChain<T::AccountId, ()> for Module<T> {
let mut trustee_info_list = Vec::new();
for trustee in candidates {
let key = (trustee.clone(), Chain::Bitcoin);
if let Some(props) = xaccounts::Module::<T>::trustee_intention_props_of(&key) {
#[allow(unreachable_patterns)]
let hot_key = match props.hot_entity {
TrusteeEntity::Bitcoin(pubkey) => {
if Self::check_address(&pubkey).is_err() {
error!("[generate_new_trustees]|[btc] this hot pubkey not valid!|hot pubkey:{:}", u8array_to_hex(&pubkey));
continue;
}
pubkey
}
_ => {
warn!("[generate_new_trustees]|[btc] this trustee do not have BITCOIN hot entity|who:{:}", trustee);
let props = xaccounts::Module::<T>::trustee_intention_props_of(&key)
.ok_or_else(|| {
error!(
"[generate_new_trustees]|[btc] the candidate must be a trustee|who:{:}",
trustee
);
""
})
.expect("[btc] the candidate must be a trustee; qed");

#[allow(unreachable_patterns)]
let hot_key = match props.hot_entity {
TrusteeEntity::Bitcoin(pubkey) => {
if Self::check_address(&pubkey).is_err() {
error!("[generate_new_trustees]|[btc] this hot pubkey not valid!|hot pubkey:{:}", u8array_to_hex(&pubkey));
continue;
}
};
#[allow(unreachable_patterns)]
let cold_key = match props.cold_entity {
TrusteeEntity::Bitcoin(pubkey) => {
if Self::check_address(&pubkey).is_err() {
error!("[generate_new_trustees]|[btc] this hot pubkey not valid!|cold pubkey:{:}", u8array_to_hex(&pubkey));
continue;
}
pubkey
}
_ => {
warn!("[generate_new_trustees]|[btc] this trustee do not have BITCOIN cold entity|who:{:}", trustee);
pubkey
}
_ => {
warn!("[generate_new_trustees]|[btc] this trustee do not have BITCOIN hot entity|who:{:}", trustee);
continue;
}
};
#[allow(unreachable_patterns)]
let cold_key = match props.cold_entity {
TrusteeEntity::Bitcoin(pubkey) => {
if Self::check_address(&pubkey).is_err() {
error!("[generate_new_trustees]|[btc] this hot pubkey not valid!|cold pubkey:{:}", u8array_to_hex(&pubkey));
continue;
}
};
trustee_info_list.push((trustee.clone(), (hot_key, cold_key)));
} else {
error!(
"[generate_new_trustees]|[btc] the candidate must be a trustee|who:{:}",
trustee
);
assert!(false, "[btc] the candidate must be a trustee");
}
pubkey
}
_ => {
warn!("[generate_new_trustees]|[btc] this trustee do not have BITCOIN cold entity|who:{:}", trustee);
continue;
}
};
trustee_info_list.push((trustee.clone(), (hot_key, cold_key)));
// just get max trustee count
if trustee_info_list.len() as u32 > config.max_trustee_count {
break;
}
}

if (trustee_info_list.len() as u32) < config.min_trustee_count {
error!("[update_trustee_addr]|trustees is less than [{:}] people, can't generate trustee addr|trustees:{:?}", config.min_trustee_count, candidates);
return Err("trustees is less than required people, can't generate trustee addr");
Expand Down
6 changes: 5 additions & 1 deletion xrml/xbridge/bitcoin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ impl std::fmt::Debug for TrusteeAddrInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use rustc_hex::ToHex;
let hex: String = self.redeem_script.to_hex();
write!(f, "TrusteeAddrInfo {{ addr: {:?}, redeem_script: {:?} }}", self.addr, hex)
write!(
f,
"TrusteeAddrInfo {{ addr: {:?}, redeem_script: {:?} }}",
self.addr, hex
)
}
}

Expand Down

0 comments on commit aaeba68

Please sign in to comment.