Skip to content

Commit

Permalink
Removing BitcoinHash trait to adopt new bitcoin crate API
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 18, 2020
1 parent f9d8cab commit af6ff09
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 595 deletions.
1,155 changes: 594 additions & 561 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ latest_rust = [] # use latest Rust features (otherwise, support Rust 1.34)
[dependencies]
base64 = "0.10"
bincode = "1.0"
bitcoin = { version = "0.23", features = ["use-serde"] }
bitcoin_hashes = "0.7.6"
bitcoin = { version = "0.24", features = ["use-serde"] }
configure_me = "0.3.4"
configure_me_codegen = "0.3.14"
crossbeam-channel = "0.3"
Expand Down
7 changes: 3 additions & 4 deletions src/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::metrics::{CounterVec, Histogram, HistogramOpts, HistogramVec, MetricO
use crate::signal::Waiter;
use crate::store::{DBStore, Row, WriteStore};
use crate::util::{spawn_thread, HeaderList, SyncChannel};
use bitcoin::BitcoinHash;

struct Parser {
magic: u32,
Expand Down Expand Up @@ -88,7 +87,7 @@ impl Parser {
let mut rows = Vec::<Row>::new();
let timer = self.duration.with_label_values(&["index"]).start_timer();
for block in blocks {
let blockhash = block.bitcoin_hash();
let blockhash = block.block_hash();
if let Some(header) = self.current_headers.header_by_blockhash(&blockhash) {
if self
.indexed_blockhashes
Expand Down Expand Up @@ -265,7 +264,7 @@ pub fn index_blk_files(
mod tests {

use super::*;
use bitcoin_hashes::Hash;
use bitcoin::hashes::Hash;
use hex::decode as hex_decode;

#[test]
Expand All @@ -275,7 +274,7 @@ mod tests {
let blocks = parse_blocks(raw_blocks, magic).unwrap();
assert_eq!(blocks.len(), 2);
assert_eq!(
blocks[1].bitcoin_hash().into_inner().to_vec(),
blocks[1].block_hash().into_inner().to_vec(),
hex_decode("d55acd552414cc44a761e8d6b64a4d555975e208397281d115336fc500000000").unwrap()
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl TransactionCache {
#[cfg(test)]
mod tests {
use super::*;
use bitcoin_hashes::Hash;
use bitcoin::hashes::Hash;

#[test]
fn test_sized_lru_cache_hit_and_miss() {
Expand Down
9 changes: 4 additions & 5 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin::hash_types::{BlockHash, Txid};
use bitcoin::network::constants::Network;
use bitcoin_hashes::hex::{FromHex, ToHex};
use bitcoin_hashes::Hash;
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::Hash;
use serde_json::{from_str, from_value, Map, Value};
use std::collections::{HashMap, HashSet};
use std::io::{BufRead, BufReader, Lines, Write};
Expand All @@ -19,7 +19,6 @@ use crate::errors::*;
use crate::metrics::{HistogramOpts, HistogramVec, Metrics};
use crate::signal::Waiter;
use crate::util::HeaderList;
use bitcoin::BitcoinHash;

fn parse_hash<T: Hash>(value: &Value) -> Result<T> {
Ok(T::from_hex(
Expand Down Expand Up @@ -497,7 +496,7 @@ impl Daemon {
let block = block_from_value(
self.request("getblock", json!([blockhash.to_hex(), /*verbose=*/ false]))?,
)?;
assert_eq!(block.bitcoin_hash(), *blockhash);
assert_eq!(block.block_hash(), *blockhash);
Ok(block)
}

Expand Down Expand Up @@ -626,7 +625,7 @@ impl Daemon {
let mut blockhash = null_hash;
for header in &result {
assert_eq!(header.prev_blockhash, blockhash);
blockhash = header.bitcoin_hash();
blockhash = header.block_hash();
}
assert_eq!(blockhash, *tip);
Ok(result)
Expand Down
7 changes: 3 additions & 4 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::util::{
full_hash, hash_prefix, spawn_thread, Bytes, FullHash, HashPrefix, HeaderEntry, HeaderList,
HeaderMap, SyncChannel, HASH_PREFIX_LEN,
};
use bitcoin::BitcoinHash;

#[derive(Serialize, Deserialize)]
pub struct TxInKey {
Expand Down Expand Up @@ -195,7 +194,7 @@ pub fn index_transaction<'a>(
}

pub fn index_block<'a>(block: &'a Block, height: usize) -> impl 'a + Iterator<Item = Row> {
let blockhash = block.bitcoin_hash();
let blockhash = block.block_hash();
// Persist block hash and header
let row = Row {
key: bincode::serialize(&BlockKey {
Expand Down Expand Up @@ -263,7 +262,7 @@ fn read_indexed_headers(store: &dyn ReadStore) -> HeaderList {
assert_eq!(
headers
.last()
.map(BlockHeader::bitcoin_hash)
.map(BlockHeader::block_hash)
.unwrap_or(null_hash),
latest_blockhash
);
Expand Down Expand Up @@ -410,7 +409,7 @@ impl Index {
}

let rows_iter = batch.iter().flat_map(|block| {
let blockhash = block.bitcoin_hash();
let blockhash = block.block_hash();
let height = *height_map
.get(&blockhash)
.unwrap_or_else(|| panic!("missing header for block {}", blockhash));
Expand Down
4 changes: 2 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode::deserialize;
use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid};
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
use bitcoin_hashes::hex::ToHex;
use bitcoin_hashes::Hash;
use bitcoin::hashes::hex::ToHex;
use bitcoin::hashes::Hash;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use serde_json::Value;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin_hashes::hex::{FromHex, ToHex};
use bitcoin_hashes::{sha256d::Hash as Sha256dHash, Hash};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256d::Hash as Sha256dHash, Hash};
use error_chain::ChainedError;
use serde_json::{from_str, Value};
use std::collections::HashMap;
Expand Down
26 changes: 12 additions & 14 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::BlockHash;
use bitcoin::util::hash::BitcoinHash;
use std::collections::HashMap;
use std::convert::TryInto;
use std::fmt;
Expand Down Expand Up @@ -73,7 +72,7 @@ fn hash_headers(headers: Vec<BlockHeader>) -> Vec<HashedHeader> {
// header[i] -> header[i-1] (i.e. header.last() is the tip)
let hashed_headers =
Vec::<HashedHeader>::from_iter(headers.into_iter().map(|header| HashedHeader {
blockhash: header.bitcoin_hash(),
blockhash: header.block_hash(),
header,
}));
for i in 1..hashed_headers.len() {
Expand Down Expand Up @@ -285,8 +284,7 @@ mod tests {
fn test_headers() {
use bitcoin::blockdata::block::BlockHeader;
use bitcoin::hash_types::{BlockHash, TxMerkleNode};
use bitcoin::util::hash::BitcoinHash;
use bitcoin_hashes::Hash;
use bitcoin::hashes::Hash;

use super::HeaderList;

Expand All @@ -308,7 +306,7 @@ mod tests {
nonce: 0,
}];
for _height in 1..10 {
let prev_blockhash = headers.last().unwrap().bitcoin_hash();
let prev_blockhash = headers.last().unwrap().block_hash();
let header = BlockHeader {
version: 1,
prev_blockhash,
Expand All @@ -329,7 +327,7 @@ mod tests {
for h in 0..3 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}
Expand All @@ -343,7 +341,7 @@ mod tests {
for h in 0..6 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}
Expand All @@ -357,15 +355,15 @@ mod tests {
for h in 0..10 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}

// Reorg the chain and test apply() on it
for h in 8..10 {
headers[h].nonce += 1;
headers[h].prev_blockhash = headers[h - 1].bitcoin_hash()
headers[h].prev_blockhash = headers[h - 1].block_hash()
}
// Test reorging the chain
let ordered = header_list.order(headers[8..10].to_vec());
Expand All @@ -376,19 +374,19 @@ mod tests {
for h in 0..10 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}

// Test "trimming" the chain
header_list.apply(vec![], headers[7].bitcoin_hash());
header_list.apply(vec![], headers[7].block_hash());
assert_eq!(header_list.len(), 8);
assert_eq!(header_list.tip(), headers[7].bitcoin_hash());
assert_eq!(header_list.tip(), headers[7].block_hash());
for h in 0..8 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}
Expand All @@ -402,7 +400,7 @@ mod tests {
for h in 0..10 {
let entry = header_list.header_by_height(h).unwrap();
assert_eq!(entry.header, headers[h]);
assert_eq!(entry.hash, headers[h].bitcoin_hash());
assert_eq!(entry.hash, headers[h].block_hash());
assert_eq!(entry.height, h);
assert_eq!(header_list.header_by_blockhash(&entry.hash), Some(entry));
}
Expand Down

0 comments on commit af6ff09

Please sign in to comment.