-
Notifications
You must be signed in to change notification settings - Fork 417
Update to rust-bitcoin v0.30.2 #2740
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
use crate::http::{BinaryResponse, JsonResponse}; | ||
use crate::utils::hex_to_uint256; | ||
use crate::utils::hex_to_work; | ||
use crate::{BlockHeaderData, BlockSourceError}; | ||
|
||
use bitcoin::blockdata::block::{Block, BlockHeader}; | ||
use bitcoin::blockdata::block::{Block, Header}; | ||
use bitcoin::consensus::encode; | ||
use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid}; | ||
use bitcoin::hashes::hex::FromHex; | ||
|
@@ -88,17 +88,21 @@ impl TryFrom<serde_json::Value> for BlockHeaderData { | |
} } | ||
|
||
Ok(BlockHeaderData { | ||
header: BlockHeader { | ||
version: get_field!("version", as_i64).try_into().map_err(|_| ())?, | ||
header: Header { | ||
version: bitcoin::blockdata::block::Version::from_consensus( | ||
get_field!("version", as_i64).try_into().map_err(|_| ())? | ||
), | ||
prev_blockhash: if let Some(hash_str) = response.get("previousblockhash") { | ||
BlockHash::from_hex(hash_str.as_str().ok_or(())?).map_err(|_| ())? | ||
BlockHash::from_str(hash_str.as_str().ok_or(())?).map_err(|_| ())? | ||
} else { BlockHash::all_zeros() }, | ||
merkle_root: TxMerkleNode::from_hex(get_field!("merkleroot", as_str)).map_err(|_| ())?, | ||
merkle_root: TxMerkleNode::from_str(get_field!("merkleroot", as_str)).map_err(|_| ())?, | ||
time: get_field!("time", as_u64).try_into().map_err(|_| ())?, | ||
bits: u32::from_be_bytes(<[u8; 4]>::from_hex(get_field!("bits", as_str)).map_err(|_| ())?), | ||
bits: bitcoin::CompactTarget::from_consensus( | ||
u32::from_be_bytes(<[u8; 4]>::from_hex(get_field!("bits", as_str)).map_err(|_| ())?) | ||
), | ||
nonce: get_field!("nonce", as_u64).try_into().map_err(|_| ())?, | ||
}, | ||
chainwork: hex_to_uint256(get_field!("chainwork", as_str)).map_err(|_| ())?, | ||
chainwork: hex_to_work(get_field!("chainwork", as_str)).map_err(|_| ())?, | ||
height: get_field!("height", as_u64).try_into().map_err(|_| ())?, | ||
}) | ||
} | ||
|
@@ -132,7 +136,7 @@ impl TryInto<(BlockHash, Option<u32>)> for JsonResponse { | |
} | ||
|
||
let hash = match &self.0["bestblockhash"] { | ||
serde_json::Value::String(hex_data) => match BlockHash::from_hex(&hex_data) { | ||
serde_json::Value::String(hex_data) => match BlockHash::from_str(&hex_data) { | ||
Err(_) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hex data")), | ||
Ok(block_hash) => block_hash, | ||
}, | ||
|
@@ -288,8 +292,8 @@ pub(crate) mod tests { | |
use super::*; | ||
use bitcoin::blockdata::constants::genesis_block; | ||
use bitcoin::hashes::Hash; | ||
use bitcoin::hashes::hex::ToHex; | ||
use bitcoin::network::constants::Network; | ||
use hex::DisplayHex; | ||
use serde_json::value::Number; | ||
use serde_json::Value; | ||
|
||
|
@@ -298,14 +302,14 @@ pub(crate) mod tests { | |
fn from(data: BlockHeaderData) -> Self { | ||
let BlockHeaderData { chainwork, height, header } = data; | ||
serde_json::json!({ | ||
"chainwork": chainwork.to_string()["0x".len()..], | ||
"chainwork": chainwork.to_be_bytes().as_hex().to_string(), | ||
"height": height, | ||
"version": header.version, | ||
"merkleroot": header.merkle_root.to_hex(), | ||
"version": header.version.to_consensus(), | ||
"merkleroot": header.merkle_root.to_string(), | ||
"time": header.time, | ||
"nonce": header.nonce, | ||
"bits": header.bits.to_hex(), | ||
"previousblockhash": header.prev_blockhash.to_hex(), | ||
"bits": header.bits.to_consensus().to_be_bytes().as_hex().to_string(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"previousblockhash": header.prev_blockhash.to_string(), | ||
}) | ||
} | ||
} | ||
|
@@ -394,7 +398,7 @@ pub(crate) mod tests { | |
#[test] | ||
fn into_block_header_from_json_response_with_valid_header_array() { | ||
let genesis_block = genesis_block(Network::Bitcoin); | ||
let best_block_header = BlockHeader { | ||
let best_block_header = Header { | ||
prev_blockhash: genesis_block.block_hash(), | ||
..genesis_block.header | ||
}; | ||
|
@@ -541,7 +545,7 @@ pub(crate) mod tests { | |
fn into_block_hash_from_json_response_without_height() { | ||
let block = genesis_block(Network::Bitcoin); | ||
let response = JsonResponse(serde_json::json!({ | ||
"bestblockhash": block.block_hash().to_hex(), | ||
"bestblockhash": block.block_hash().to_string(), | ||
})); | ||
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) { | ||
Err(e) => panic!("Unexpected error: {:?}", e), | ||
|
@@ -556,7 +560,7 @@ pub(crate) mod tests { | |
fn into_block_hash_from_json_response_with_unexpected_blocks_type() { | ||
let block = genesis_block(Network::Bitcoin); | ||
let response = JsonResponse(serde_json::json!({ | ||
"bestblockhash": block.block_hash().to_hex(), | ||
"bestblockhash": block.block_hash().to_string(), | ||
"blocks": "foo", | ||
})); | ||
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) { | ||
|
@@ -572,7 +576,7 @@ pub(crate) mod tests { | |
fn into_block_hash_from_json_response_with_invalid_height() { | ||
let block = genesis_block(Network::Bitcoin); | ||
let response = JsonResponse(serde_json::json!({ | ||
"bestblockhash": block.block_hash().to_hex(), | ||
"bestblockhash": block.block_hash().to_string(), | ||
"blocks": std::u64::MAX, | ||
})); | ||
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) { | ||
|
@@ -588,7 +592,7 @@ pub(crate) mod tests { | |
fn into_block_hash_from_json_response_with_height() { | ||
let block = genesis_block(Network::Bitcoin); | ||
let response = JsonResponse(serde_json::json!({ | ||
"bestblockhash": block.block_hash().to_hex(), | ||
"bestblockhash": block.block_hash().to_string(), | ||
"blocks": 1, | ||
})); | ||
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this could be
format!("{:064x}", chainwork)
which conveys the intent better.