Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration to bitcoin 0.30 #145

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 88 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod cache;
pub mod store;

pub use nakamoto_common::bitcoin::blockdata::block::{Block, BlockHeader};
pub use nakamoto_common::bitcoin::blockdata::block::{Block, Header as BlockHeader};
pub use nakamoto_common::bitcoin::blockdata::transaction::Transaction;
pub use nakamoto_common::bitcoin::hash_types::BlockHash;
pub use nakamoto_common::block::tree::*;
40 changes: 18 additions & 22 deletions chain/src/block/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::ops::ControlFlow;

use common::bitcoin::block::ValidationError;
use common::bitcoin::CompactTarget;
use common::block::Target;
use nakamoto_common as common;
use nakamoto_common::bitcoin;
use nakamoto_common::bitcoin::blockdata::block::BlockHeader;
use nakamoto_common::bitcoin::blockdata::block::Header as BlockHeader;
use nakamoto_common::bitcoin::consensus::params::Params;
use nakamoto_common::bitcoin::hash_types::BlockHash;
use nakamoto_common::bitcoin::network::constants::Network;
use nakamoto_common::bitcoin::util::BitArray;
use nakamoto_common::bitcoin::network::Network;

use nakamoto_common::bitcoin::util::uint::Uint256;
use nakamoto_common::block::tree::{self, BlockReader, BlockTree, Branch, Error, ImportResult};
use nakamoto_common::block::{
self,
Expand Down Expand Up @@ -76,7 +77,7 @@ pub struct BlockCache<S: Store> {
checkpoints: BTreeMap<Height, BlockHash>,
params: Params,
/// Total cumulative work on the active chain.
chainwork: Uint256,
chainwork: Work,
store: S,
}

Expand Down Expand Up @@ -240,20 +241,16 @@ impl<S: Store<Header = BlockHeader>> BlockCache<S> {
//
// We do this because it's cheap to verify and prevents flooding attacks.
let target = header.target();
match header.validate_pow(&target) {
match header.validate_pow(target) {
Ok(_) => {
let limit = self.params.pow_limit;
let limit = self.params.pow_limit.to_target();
if target > limit {
return Err(Error::InvalidBlockTarget(target, limit));
}
}
Err(bitcoin::util::Error::BlockBadProofOfWork) => {
Err(ValidationError::BadProofOfWork) => {
return Err(Error::InvalidBlockPoW);
}
Err(bitcoin::util::Error::BlockBadTarget) => unreachable! {
// The only way to get a 'bad target' error is to pass a different target
// than the one specified in the header.
},
Err(_) => unreachable! {
// We've handled all possible errors above.
},
Expand Down Expand Up @@ -288,7 +285,9 @@ impl<S: Store<Header = BlockHeader>> BlockCache<S> {

let mut best_branch = None;
let mut best_hash = tip.hash();
let mut best_work = Uint256::zero();
// FIXME: this need to be a Work by network? or the min is equal for
// every network?
let mut best_work = Work::MAINNET_MIN;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be general and not with mainet only


for branch in candidates.iter() {
// Total work included in this branch.
Expand Down Expand Up @@ -453,15 +452,12 @@ impl<S: Store<Header = BlockHeader>> BlockCache<S> {
self.next_difficulty_target(tip.height, tip.time, tip.target(), &self.params)
};

let target = BlockHeader::u256_from_compact_target(compact_target);
let target = Target::from_compact(CompactTarget::from_consensus(compact_target));

match header.validate_pow(&target) {
Err(bitcoin::util::Error::BlockBadProofOfWork) => {
match header.validate_pow(target) {
Err(ValidationError::BadProofOfWork) => {
return Err(Error::InvalidBlockPoW);
}
Err(bitcoin::util::Error::BlockBadTarget) => {
return Err(Error::InvalidBlockTarget(header.target(), target));
}
Err(_) => unreachable!(),
Ok(_) => {}
}
Expand Down Expand Up @@ -497,10 +493,10 @@ impl<S: Store<Header = BlockHeader>> BlockCache<S> {
let pow_limit_bits = block::pow_limit_bits(&params.network);

for (height, header) in self.iter().rev() {
if header.bits != pow_limit_bits
if header.bits.to_consensus() != pow_limit_bits
|| height % self.params.difficulty_adjustment_interval() == 0
{
return header.bits;
return header.bits.to_consensus();
}
}
pow_limit_bits
Expand Down Expand Up @@ -682,7 +678,7 @@ impl<S: Store<Header = BlockHeader>> BlockReader for BlockCache<S> {
}

/// Get the "chainwork", ie. the total accumulated proof-of-work of the active chain.
fn chain_work(&self) -> Uint256 {
fn chain_work(&self) -> Work {
self.chainwork
}

Expand Down
Loading
Loading