From c03497434b0551b050839b24838f91d1fcb71775 Mon Sep 17 00:00:00 2001 From: Yapcheekian Date: Mon, 7 Apr 2025 13:42:09 +0800 Subject: [PATCH] feat: upgrade `rust-bitcoin` crate --- Cargo.lock | 119 +++++++++++++++++- Cargo.toml | 2 +- src/bin/electrs.rs | 3 +- src/bin/tx-fingerprint-stats.rs | 37 ++++-- src/chain.rs | 19 ++- src/daemon.rs | 68 ++++++----- src/new_index/fetch.rs | 13 +- src/new_index/mempool.rs | 11 +- src/new_index/query.rs | 2 +- src/new_index/schema.rs | 22 ++-- src/rest.rs | 207 +++++++++++++++----------------- src/util/block.rs | 16 ++- src/util/fees.rs | 11 +- src/util/mod.rs | 10 +- src/util/script.rs | 38 +++--- src/util/transaction.rs | 37 +++--- 16 files changed, 369 insertions(+), 246 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77a44d0d..d84875d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -32,6 +32,12 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "ascii" version = "1.0.0" @@ -79,6 +85,16 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base58ck" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c8d66485a3a2ea485c1913c4572ce0256067a5377ac8c75c4960e1cda98605f" +dependencies = [ + "bitcoin-internals", + "bitcoin_hashes 0.14.0", +] + [[package]] name = "base64" version = "0.10.1" @@ -100,6 +116,12 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf9ff0bbfd639f15c74af777d81383cf53efb7c93613f6cab67c6c11e05bbf8b" +[[package]] +name = "bech32" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d965446196e3b7decd44aa7ee49e31d630118f90ef12f97900f262eb915c951d" + [[package]] name = "bincode" version = "1.3.3" @@ -136,7 +158,7 @@ version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a41df6ad9642c5c15ae312dd3d074de38fd3eb7cc87ad4ce10f90292a83fe4d" dependencies = [ - "bech32", + "bech32 0.8.1", "bitcoin_hashes 0.10.0", "secp256k1 0.20.3", "serde", @@ -148,12 +170,55 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42b2a9a8e3c7544f5ce2b475f2f56580a3102b37e0ee001558ad4faedcf56cf4" dependencies = [ - "bech32", + "bech32 0.8.1", "bitcoin_hashes 0.10.0", "secp256k1 0.22.1", "serde", ] +[[package]] +name = "bitcoin" +version = "0.32.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6bc65742dea50536e35ad42492b234c27904a27f0abdcbce605015cb4ea026" +dependencies = [ + "base58ck", + "bech32 0.11.0", + "bitcoin-internals", + "bitcoin-io", + "bitcoin-units", + "bitcoin_hashes 0.14.0", + "hex-conservative", + "hex_lit", + "secp256k1 0.29.1", + "serde", +] + +[[package]] +name = "bitcoin-internals" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bdbe14aa07b06e6cfeffc529a1f099e5fbe249524f8125358604df99a4bed2" +dependencies = [ + "serde", +] + +[[package]] +name = "bitcoin-io" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf" + +[[package]] +name = "bitcoin-units" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5285c8bcaa25876d07f37e3d30c303f2609179716e11d688f51e8f1fe70063e2" +dependencies = [ + "bitcoin-internals", + "serde", +] + [[package]] name = "bitcoin_hashes" version = "0.7.6" @@ -169,6 +234,17 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcoin_hashes" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16" +dependencies = [ + "bitcoin-io", + "hex-conservative", + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -582,6 +658,21 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-conservative" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "hex_lit" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" + [[package]] name = "http" version = "0.2.6" @@ -827,7 +918,7 @@ dependencies = [ "arrayref", "base64 0.13.0", "bincode", - "bitcoin 0.28.0", + "bitcoin 0.32.5", "bounded-vec-deque", "clap", "crossbeam-channel", @@ -1384,6 +1475,17 @@ dependencies = [ "serde", ] +[[package]] +name = "secp256k1" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" +dependencies = [ + "bitcoin_hashes 0.14.0", + "secp256k1-sys 0.10.1", + "serde", +] + [[package]] name = "secp256k1-sys" version = "0.4.2" @@ -1402,6 +1504,15 @@ dependencies = [ "cc", ] +[[package]] +name = "secp256k1-sys" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4387882333d3aa8cb20530a17c69a3752e97837832f34f6dccc760e715001d9" +dependencies = [ + "cc", +] + [[package]] name = "secp256k1-zkp" version = "0.6.0" diff --git a/Cargo.toml b/Cargo.toml index c5ec87b2..83e1978f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ electrum-discovery = ["electrum-client"] arrayref = "0.3.6" base64 = "0.13.0" bincode-do-not-use-directly = { version = "1.3.1", package = "bincode" } -bitcoin = { version = "0.28", features = [ "use-serde" ] } +bitcoin = { version = "0.32", features = [ "serde" ] } bounded-vec-deque = "0.1.1" clap = "2.33.3" crossbeam-channel = "0.5.0" diff --git a/src/bin/electrs.rs b/src/bin/electrs.rs index 0c649355..094fcc25 100644 --- a/src/bin/electrs.rs +++ b/src/bin/electrs.rs @@ -4,6 +4,7 @@ extern crate log; extern crate electrs; +use bitcoin::p2p::Magic; use error_chain::ChainedError; use std::process; use std::sync::{Arc, RwLock}; @@ -50,7 +51,7 @@ fn run_server(config: Arc) -> Result<()> { config.daemon_rpc_addr, config.cookie_getter(), config.network_type, - config.magic, + config.magic.map(|m| Magic::from_bytes(m.to_be_bytes())), signal.clone(), &metrics, )?); diff --git a/src/bin/tx-fingerprint-stats.rs b/src/bin/tx-fingerprint-stats.rs index 5b38561f..ab8c0f46 100644 --- a/src/bin/tx-fingerprint-stats.rs +++ b/src/bin/tx-fingerprint-stats.rs @@ -9,8 +9,9 @@ fn main() { use std::collections::HashSet; use std::sync::Arc; - use bitcoin::blockdata::script::Script; use bitcoin::consensus::encode::deserialize; + use bitcoin::p2p::Magic; + use bitcoin::ScriptBuf; use electrs::{ chain::Transaction, config::Config, @@ -35,7 +36,7 @@ fn main() { config.daemon_rpc_addr, config.cookie_getter(), config.network_type, - config.magic, + config.magic.map(|m| Magic::from_bytes(m.to_be_bytes())), signal, &metrics, ) @@ -62,7 +63,7 @@ fn main() { } let tx: Transaction = deserialize(value).expect("failed to parse Transaction"); - let txid = tx.txid(); + let txid = tx.compute_txid(); iter.next(); @@ -71,7 +72,7 @@ fn main() { continue; } // skip coinbase txs - if tx.is_coin_base() { + if tx.is_coinbase() { continue; } @@ -91,12 +92,26 @@ fn main() { .collect(), ); - let total_out: u64 = tx.output.iter().map(|out| out.value).sum(); - let small_out = tx.output.iter().map(|out| out.value).min().unwrap(); - let large_out = tx.output.iter().map(|out| out.value).max().unwrap(); - - let total_in: u64 = prevouts.values().map(|out| out.value).sum(); - let smallest_in = prevouts.values().map(|out| out.value).min().unwrap(); + let total_out: u64 = tx.output.iter().map(|out| out.value.to_sat()).sum(); + let small_out = tx + .output + .iter() + .map(|out| out.value.to_sat()) + .min() + .unwrap(); + let large_out = tx + .output + .iter() + .map(|out| out.value.to_sat()) + .max() + .unwrap(); + + let total_in: u64 = prevouts.values().map(|out| out.value.to_sat()).sum(); + let smallest_in = prevouts + .values() + .map(|out| out.value.to_sat()) + .min() + .unwrap(); let fee = total_in - total_out; @@ -119,7 +134,7 @@ fn main() { // test for sending back to one of the spent spks let has_reuse = { - let prev_spks: HashSet