Skip to content

Commit

Permalink
migration to bitcoin 0.30
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Aug 13, 2023
1 parent 9b24b04 commit 7dce803
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 115 deletions.
66 changes: 59 additions & 7 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::*;
31 changes: 16 additions & 15 deletions chain/src/block/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::ops::ControlFlow;

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::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 +75,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,17 +239,17 @@ 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(bitcoin::error::Error::BlockBadProofOfWork) => {
return Err(Error::InvalidBlockPoW);
}
Err(bitcoin::util::Error::BlockBadTarget) => unreachable! {
Err(bitcoin::error::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.
},
Expand Down Expand Up @@ -288,7 +287,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;

for branch in candidates.iter() {
// Total work included in this branch.
Expand Down Expand Up @@ -455,11 +456,11 @@ impl<S: Store<Header = BlockHeader>> BlockCache<S> {

let target = BlockHeader::u256_from_compact_target(compact_target);

match header.validate_pow(&target) {
Err(bitcoin::util::Error::BlockBadProofOfWork) => {
match header.validate_pow(target) {
Err(bitcoin::error::Error::BlockBadProofOfWork) => {
return Err(Error::InvalidBlockPoW);
}
Err(bitcoin::util::Error::BlockBadTarget) => {
Err(bitcoin::error::Error::BlockBadTarget) => {
return Err(Error::InvalidBlockTarget(header.target(), target));
}
Err(_) => unreachable!(),
Expand Down Expand Up @@ -497,10 +498,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 +683,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
2 changes: 1 addition & 1 deletion chain/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
pub mod cache;
pub mod store;

pub use nakamoto_common::bitcoin::util::bip158::BlockFilter;
pub use nakamoto_common::bitcoin::bip158::BlockFilter;
3 changes: 1 addition & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ edition = "2021"

[dependencies]
nakamoto-net = { version = "0.4.0", path = "../net" }
bitcoin = "0.29.2"
bitcoin_hashes = "0.11.0"
bitcoin = { version = "0.30.0" }
thiserror = "1.0"
fastrand = "1.3.5"
nonempty = "0.7"
Expand Down
10 changes: 3 additions & 7 deletions common/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ pub mod store;
pub mod time;
pub mod tree;

pub use bitcoin::blockdata::block::{Block, BlockHeader};
pub use bitcoin::blockdata::block::{Block, Header as BlockHeader};
pub use bitcoin::blockdata::transaction::Transaction;
pub use bitcoin::hash_types::BlockHash;

/// Difficulty target of a block.
pub type Target = bitcoin::util::uint::Uint256;

/// Block work.
pub type Work = bitcoin::util::uint::Uint256;
pub use bitcoin::pow::{Target, Work};

/// Compact difficulty bits (target) of a block.
pub type Bits = u32;
Expand Down Expand Up @@ -62,5 +57,6 @@ pub fn pow_limit_bits(network: &bitcoin::Network) -> Bits {
bitcoin::Network::Testnet => 0x1d00ffff,
bitcoin::Network::Regtest => 0x207fffff,
bitcoin::Network::Signet => 0x1e0377ae,
_ => unreachable!(),
}
}
4 changes: 2 additions & 2 deletions common/src/block/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

use std::ops::RangeInclusive;

use bitcoin_hashes::Hash;
use thiserror::Error;

pub use bitcoin::bip158::BlockFilter;
pub use bitcoin::hash_types::{FilterHash, FilterHeader};
pub use bitcoin::util::bip158::BlockFilter;

use super::Height;
use crate::bitcoin_hashes::Hash;
use crate::block::store::{self, Genesis};
use crate::network::Network;

Expand Down
20 changes: 13 additions & 7 deletions common/src/block/store.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Block header storage.
#![allow(clippy::len_without_is_empty)]
use std::borrow::Borrow;

Check failure on line 3 in common/src/block/store.rs

View workflow job for this annotation

GitHub Actions / Lint

unused import: `std::borrow::Borrow`

use crate::block::Height;

use bitcoin::blockdata::block::BlockHeader;
use bitcoin::bip158::BlockFilter;
use bitcoin::block::Header;
use bitcoin::consensus::encode;
use bitcoin::hash_types::FilterHash;
use bitcoin::util::bip158::BlockFilter;
use bitcoin::{OutPoint, Script, ScriptBuf};

Check failure on line 11 in common/src/block/store.rs

View workflow job for this annotation

GitHub Actions / Lint

unused imports: `OutPoint`, `Script`
use thiserror::Error;

use crate::network::Network;
Expand Down Expand Up @@ -35,7 +38,7 @@ pub trait Genesis {
}

/// Genesis implementation for `bitcoin`'s header.
impl Genesis for BlockHeader {
impl Genesis for Header {
fn genesis(network: Network) -> Self {
network.genesis()
}
Expand All @@ -47,9 +50,12 @@ impl Genesis for FilterHash {
use bitcoin::hashes::Hash;

let genesis = network.genesis_block();
let filter = BlockFilter::new_script_filter(&genesis, |_| {
panic!("{}: genesis block should have no inputs", source!())
})
let filter = BlockFilter::new_script_filter(
&genesis,
|_| -> Result<ScriptBuf, bitcoin::bip158::Error> {
panic!("{}: genesis block should have no inputs", source!())
},
)
.unwrap();

FilterHash::hash(&filter.content)
Expand All @@ -61,7 +67,7 @@ impl Genesis for BlockFilter {
fn genesis(network: Network) -> Self {
let genesis = network.genesis_block();

BlockFilter::new_script_filter(&genesis, |_| {
BlockFilter::new_script_filter(&genesis, |_| -> Result<ScriptBuf, bitcoin::bip158::Error> {
panic!("{}: genesis block should have no inputs", source!())
})
.unwrap()
Expand Down
Loading

0 comments on commit 7dce803

Please sign in to comment.