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

chore: Upgrade bitcoin to version 0.32.2 #314

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
235 changes: 222 additions & 13 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ members = [
resolver = "2"

[workspace.dependencies]
bitcoin = "0.28.1"
bitcoin = "0.32.2"
byteorder = "1.4.3"
canbench-rs = { version = "0.1.1" }
candid = "0.10.6"
candid_parser = { version = "0.1.4" }
ciborium = "0.2.1"
clap = { version = "4.0.11", features = ["derive"] }
futures = "0.3.28"
getrandom = { version = "0.2", default-features = false, features = ["custom"] }
hex = "0.4.3"
ic-btc-canister = { path = "./canister" }
ic-btc-interface = { path = "./interface" }
Expand All @@ -46,6 +47,7 @@ ic-http = { path = "./ic-http" }
ic-metrics-encoder = "1.0.0"
ic-stable-structures = "0.5.2"
lazy_static = "1.4.0"
primitive-types = "0.12"
serde = "1.0.171"
serde_bytes = "0.11"
serde_json = "1.0.94"
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ path = "src/main.rs"
bench = false

[dependencies]
bitcoin = { workspace = true, features = ["use-serde"]}
bitcoin = { workspace = true, features = ["serde"]}
canbench-rs = { workspace = true }
candid = { workspace = true }
getrandom = { workspace = true }
hex = { workspace = true }
ic-cdk = { workspace = true }
ic-cdk-macros = { workspace = true }
Expand Down
9 changes: 7 additions & 2 deletions benchmarks/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bitcoin::consensus::Decodable;
use bitcoin::{consensus::Encodable, Block as BitcoinBlock, BlockHeader};
use bitcoin::{block::Header as BlockHeader, consensus::Encodable, Block as BitcoinBlock};
use canbench_rs::{bench, bench_fn, BenchResult};
use ic_btc_canister::{types::BlockHeaderBlob, with_state_mut};
use ic_btc_interface::{InitConfig, Network};
Expand All @@ -22,7 +22,7 @@ fn init() {
.split('\n')
.map(|block_hex| {
let block_bytes = hex::decode(block_hex).unwrap();
Block::new(BitcoinBlock::consensus_decode(block_bytes.as_slice()).unwrap())
Block::new(BitcoinBlock::consensus_decode(&mut block_bytes.as_slice()).unwrap())
})
.collect(),
);
Expand Down Expand Up @@ -173,3 +173,8 @@ fn pre_upgrade_with_many_unstable_blocks() -> BenchResult {
}

fn main() {}

getrandom::register_custom_getrandom!(always_fail);
pub fn always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Err(getrandom::Error::UNSUPPORTED)
}
89 changes: 77 additions & 12 deletions bootstrap/main-state-builder/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 bootstrap/main-state-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bitcoin = "0.28.1"
bitcoin = "0.32.2"
clap = { version = "4.0.11", features = ["derive"] }
ciborium = "0.2.1"
hex = "0.4.3"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/main-state-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn read_block(reader: &mut BufReader<File>) -> Block {
let mut block = String::new();
reader.read_line(&mut block).unwrap();
let block = hex::decode(block.replace('\n', "")).unwrap();
Block::new(BitcoinBlock::consensus_decode(block.as_slice()).unwrap())
Block::new(BitcoinBlock::consensus_decode(&mut block.as_slice()).unwrap())
}

fn main() {
Expand Down
22 changes: 11 additions & 11 deletions bootstrap/state-builder/src/build_address_utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! --network testnet \
//! --output balances.bin \
//! --utxos-dump-path utxos-dump.csv
use bitcoin::{Address as BitcoinAddress, Script, Txid as BitcoinTxid};
use bitcoin::{Address as BitcoinAddress, ScriptBuf, Txid as BitcoinTxid};
use clap::Parser;
use ic_btc_canister::types::{into_bitcoin_network, Address, AddressUtxo};
use ic_btc_interface::Network;
Expand Down Expand Up @@ -51,7 +51,7 @@ fn main() {
let line = line.unwrap();
let parts: Vec<_> = line.split(',').collect();

let txid = Txid::from(BitcoinTxid::from_str(parts[1]).unwrap().to_vec());
let txid = Txid::from(BitcoinTxid::from_str(parts[1]).unwrap().as_ref());
let vout: u32 = parts[2].parse().unwrap();
let address_str = parts[5];
let height: u32 = parts[0].parse().unwrap();
Expand All @@ -63,16 +63,16 @@ fn main() {

// Load the address. The UTXO dump tool we use doesn't output all the addresses
// we support, so if parsing the address itself fails, we try parsing the script directly.
let address = if let Ok(address) = BitcoinAddress::from_str(address_str) {
Some(address)
} else {
BitcoinAddress::from_script(
&Script::from(hex::decode(script).expect("script must be valid hex")),
into_bitcoin_network(args.network),
)
};
let address = BitcoinAddress::from_str(address_str)
.map(|address| address.assume_checked())
.or_else(|_| {
BitcoinAddress::from_script(
&ScriptBuf::from(hex::decode(script).expect("script must be valid hex")),
into_bitcoin_network(args.network),
)
});

if let Some(address) = address {
if let Ok(address) = address {
let address: Address = address.into();

address_utxos
Expand Down
22 changes: 11 additions & 11 deletions bootstrap/state-builder/src/build_balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! --network testnet \
//! --output balances.bin \
//! --utxos-dump-path utxos-dump.csv
use bitcoin::{Address as BitcoinAddress, Script};
use bitcoin::{Address as BitcoinAddress, ScriptBuf};
use clap::Parser;
use ic_btc_canister::types::{into_bitcoin_network, Address};
use ic_btc_interface::Network;
Expand Down Expand Up @@ -59,16 +59,16 @@ fn main() {

// Load the address. The UTXO dump tool we use doesn't output all the addresses
// we support, so if parsing the address itself fails, we try parsing the script directly.
let address = if let Ok(address) = BitcoinAddress::from_str(address_str) {
Some(address)
} else {
BitcoinAddress::from_script(
&Script::from(hex::decode(script).expect("script must be valid hex")),
into_bitcoin_network(args.network),
)
};

if let Some(address) = address {
let address = BitcoinAddress::from_str(address_str)
.map(|address| address.assume_checked())
.or_else(|_| {
BitcoinAddress::from_script(
&ScriptBuf::from(hex::decode(script).expect("script must be valid hex")),
into_bitcoin_network(args.network),
)
});

if let Ok(address) = address {
let address: Address = address.into();

// Update the balance of the address.
Expand Down
10 changes: 5 additions & 5 deletions bootstrap/state-builder/src/build_utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! --network testnet \
//! --output output-dir \
//! --utxos-dump-path utxos-dump.csv
use bitcoin::{Address, Txid as BitcoinTxid};
use bitcoin::{Address, Amount, Txid as BitcoinTxid};
use clap::Parser;
use ic_btc_canister::{types::TxOut, with_state, with_state_mut};
use ic_btc_interface::{Flag, InitConfig, Network};
Expand Down Expand Up @@ -81,7 +81,7 @@ fn main() {
let line = line.unwrap();
let parts: Vec<_> = line.split(',').collect();

let txid = Txid::from(BitcoinTxid::from_str(parts[1]).unwrap().to_vec());
let txid = Txid::from(BitcoinTxid::from_str(parts[1]).unwrap().as_ref());
let vout: u32 = parts[2].parse().unwrap();
let amount: u64 = parts[3].parse().unwrap();
let script = parts[6];
Expand All @@ -96,15 +96,15 @@ fn main() {
// Instead of using the scripts from the database, we can infer the script from the
// address. Otherwise, we use the script in the chainstate database as-is.
let script = match Address::from_str(address_str) {
Ok(address) => address.script_pubkey().as_bytes().to_vec(),
Ok(address) => address.assume_checked().script_pubkey().as_bytes().to_vec(),
Err(_) => hex::decode(script).unwrap(),
};

// Insert the UTXO
let outpoint = OutPoint { txid, vout };
if !bitcoin::Script::from(script.clone()).is_provably_unspendable() {
if !bitcoin::ScriptBuf::from(script.clone()).is_op_return() {
let txout = TxOut {
value: amount,
value: Amount::from_sat(amount),
script_pubkey: script,
};

Expand Down
8 changes: 4 additions & 4 deletions bootstrap/state-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! --network testnet \
//! --blocks-path /path/to/data/testnet3 \
//! --tip 000000002ce019cc4a8f2af62b3ecf7c30a19d29828b25268a0194dbac3cac50
use bitcoin::{consensus::Decodable, BlockHash, BlockHeader};
use bitcoin::{block::Header as BlockHeader, consensus::Decodable, io::Cursor, BlockHash};
use byteorder::{LittleEndian, ReadBytesExt};
use clap::Parser;
use ic_btc_canister::{
Expand All @@ -21,7 +21,7 @@ use rusty_leveldb::{Options, DB};
use std::{
collections::BTreeMap,
fs::File,
io::{Cursor, Read, Seek, SeekFrom, Write},
io::{Read, Seek, SeekFrom, Write},
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -50,7 +50,7 @@ struct Args {
}

// How to read Bitcoin's varint format.
trait VarIntRead: std::io::Read {
trait VarIntRead: bitcoin::io::Read {
fn read_varint(&mut self) -> usize {
let mut n = 0;
loop {
Expand Down Expand Up @@ -100,7 +100,7 @@ fn get_block_info(
block_hash: &BlockHash,
) -> Option<(Height, FileNumber, FileOffset, BlockHash)> {
let mut key: Vec<u8> = vec![98];
key.extend(block_hash.to_vec());
key.extend((block_hash.as_ref() as &[u8]).to_vec());

let value = db.get(&key).unwrap();

Expand Down
Loading
Loading