Skip to content

Commit

Permalink
Merge rust-bitcoin#2885: Remove usage of blockdata from paths
Browse files Browse the repository at this point in the history
a42bcdc Remove usage of blockdata from paths (Tobin C. Harding)

Pull request description:

  the `blockdata` directory is code organisation thing, all the types/modules are re-exported from other places. In preparation for, and to make easier, the `primitives` crate smashing work - remove all explicit usage of `blockdata`.

  Note that the few instances remain as they seem required e.g.,

    `pub(in crate::blockdata::script)`

  Refactor only, no logic changes.

  Done as part of rust-bitcoin#2883

ACKs for top commit:
  apoelstra:
    ACK a42bcdc lgtm! I consider this "trivial" and will one-ACK merge it

Tree-SHA512: 310605e5203cf04aaeb91fe5512677b8f1438b183916686ba2cdc41ffdc18af7a0676206724e8a14c50ce6ed8faa9d48c69a2d5149eb1f56ae9c5f276fc5200f
  • Loading branch information
apoelstra committed Jun 20, 2024
2 parents 6908d45 + a42bcdc commit 817e54f
Show file tree
Hide file tree
Showing 44 changed files with 122 additions and 126 deletions.
2 changes: 1 addition & 1 deletion bitcoin/src/address/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use core::fmt;
use internals::write_err;

use crate::address::{Address, NetworkUnchecked};
use crate::blockdata::script::{witness_program, witness_version};
use crate::prelude::*;
use crate::script::{witness_program, witness_version};
use crate::Network;

/// Error while generating address from script.
Expand Down
14 changes: 7 additions & 7 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ use bech32::primitives::hrp::Hrp;
use hashes::{sha256, HashEngine};
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};

use crate::blockdata::constants::{
use crate::consensus::Params;
use crate::constants::{
PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, SCRIPT_ADDRESS_PREFIX_MAIN,
SCRIPT_ADDRESS_PREFIX_TEST,
};
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
};
use crate::consensus::Params;
use crate::crypto::key::{
CompressedPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey,
};
use crate::network::{Network, NetworkKind};
use crate::prelude::*;
use crate::script::witness_program::WitnessProgram;
use crate::script::witness_version::WitnessVersion;
use crate::script::{
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
};
use crate::taproot::TapNodeHash;

#[rustfmt::skip] // Keep public re-exports separate.
Expand Down
8 changes: 5 additions & 3 deletions bitcoin/src/bip152.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,13 @@ mod test {
use hex::FromHex;

use super::*;
use crate::blockdata::locktime::absolute;
use crate::blockdata::transaction;
use crate::consensus::encode::{deserialize, serialize};
use crate::locktime::absolute;
use crate::merkle_tree::TxMerkleNode;
use crate::{Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Txid, Witness};
use crate::{
transaction, Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxOut, Txid,
Witness,
};

fn dummy_tx(nonce: &[u8]) -> Transaction {
Transaction {
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ use hashes::{sha256d, siphash24};
use internals::write_err;
use io::{BufRead, Write};

use crate::blockdata::block::{Block, BlockHash};
use crate::blockdata::script::Script;
use crate::blockdata::transaction::OutPoint;
use crate::block::{Block, BlockHash};
use crate::consensus::encode::VarInt;
use crate::consensus::{Decodable, Encodable};
use crate::internal_macros::impl_hashencode;
use crate::prelude::*;
use crate::script::Script;
use crate::transaction::OutPoint;

/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845
const P: u8 = 19;
Expand Down
8 changes: 4 additions & 4 deletions bitcoin/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ use hashes::{sha256d, HashEngine};
use io::{BufRead, Write};

use super::Weight;
use crate::blockdata::script;
use crate::blockdata::transaction::{Transaction, Wtxid};
use crate::consensus::{encode, Decodable, Encodable, Params};
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
use crate::merkle_tree::{MerkleNode as _, TxMerkleNode, WitnessMerkleNode};
use crate::pow::{CompactTarget, Target, Work};
use crate::prelude::*;
use crate::VarInt;
use crate::transaction::{Transaction, Wtxid};
use crate::{script, VarInt};

hashes::hash_newtype! {
/// A bitcoin block hash.
Expand Down Expand Up @@ -367,7 +366,8 @@ impl Block {
match push.map_err(|_| Bip34Error::NotPresent)? {
script::Instruction::PushBytes(b) => {
// Check that the number is encoded in the minimal way.
let h = b.read_scriptint()
let h = b
.read_scriptint()
.map_err(|_e| Bip34Error::UnexpectedPush(b.as_bytes().to_vec()))?;
if h < 0 {
Err(Bip34Error::NegativeHeight)
Expand Down
13 changes: 6 additions & 7 deletions bitcoin/src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
use hashes::sha256d;
use internals::impl_array_newtype;

use crate::blockdata::block::{self, Block};
use crate::blockdata::locktime::absolute;
use crate::blockdata::opcodes::all::*;
use crate::blockdata::script;
use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
use crate::blockdata::witness::Witness;
use crate::block::{self, Block};
use crate::consensus::Params;
use crate::internal_macros::impl_array_newtype_stringify;
use crate::locktime::absolute;
use crate::network::Network;
use crate::opcodes::all::*;
use crate::pow::CompactTarget;
use crate::{Amount, BlockHash};
use crate::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
use crate::witness::Witness;
use crate::{script, Amount, BlockHash};

/// How many seconds between blocks we expect on average.
pub const TARGET_BLOCK_SPACING: u32 = 600;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub mod fee_rate {
fn fee_convenience_functions_agree() {
use hex::test_hex_unwrap as hex;

use crate::blockdata::transaction::Transaction;
use crate::consensus::Decodable;
use crate::transaction::Transaction;

const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";

Expand Down
22 changes: 11 additions & 11 deletions bitcoin/src/blockdata/script/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ use core::ops::{
use secp256k1::{Secp256k1, Verification};

use super::PushBytes;
use crate::blockdata::fee_rate::FeeRate;
use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self, Opcode};
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{
bytes_to_asm_fmt, Builder, Instruction, InstructionIndices, Instructions,
RedeemScriptSizeError, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
};
use crate::consensus::Encodable;
use crate::key::{PublicKey, UntweakedPublicKey, WPubkeyHash};
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
use crate::policy::DUST_RELAY_TX_FEE;
use crate::prelude::*;
use crate::script::witness_version::WitnessVersion;
use crate::script::{
bytes_to_asm_fmt, Builder, Instruction, InstructionIndices, Instructions,
RedeemScriptSizeError, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
};
use crate::taproot::{LeafVersion, TapLeafHash, TapNodeHash};
use crate::FeeRate;

/// Bitcoin script slice.
///
/// *[See also the `bitcoin::blockdata::script` module](crate::blockdata::script).*
/// *[See also the `bitcoin::script` module](crate::script).*
///
/// `Script` is a script slice, the most primitive script type. It's usually seen in its borrowed
/// form `&Script`. It is always encoded as a series of bytes representing the opcodes and data
Expand Down Expand Up @@ -368,7 +368,7 @@ impl Script {
)]
#[inline]
pub fn is_provably_unspendable(&self) -> bool {
use crate::blockdata::opcodes::Class::{IllegalOp, ReturnOp};
use crate::opcodes::Class::{IllegalOp, ReturnOp};

match self.0.first() {
Some(b) => {
Expand Down Expand Up @@ -685,7 +685,7 @@ delegate_index!(
#[cfg(test)]
mod tests {
use super::*;
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::script::witness_program::WitnessProgram;

#[test]
fn shortest_witness_program() {
Expand Down
10 changes: 5 additions & 5 deletions bitcoin/src/blockdata/script/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use core::fmt;

use secp256k1::XOnlyPublicKey;

use crate::blockdata::locktime::absolute;
use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self, Opcode};
use crate::blockdata::script::{opcode_to_verify, write_scriptint, PushBytes, Script, ScriptBuf};
use crate::blockdata::transaction::Sequence;
use crate::key::PublicKey;
use crate::locktime::absolute;
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
use crate::prelude::*;
use crate::script::{opcode_to_verify, write_scriptint, PushBytes, Script, ScriptBuf};
use crate::transaction::Sequence;

/// An Object which can be used to construct a script piece by piece.
#[derive(PartialEq, Eq, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/script/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: CC0-1.0

use crate::blockdata::opcodes::{self, Opcode};
use crate::blockdata::script::{read_uint_iter, Error, PushBytes, Script, ScriptBuf, UintError};
use crate::opcodes::{self, Opcode};
use crate::script::{read_uint_iter, Error, PushBytes, Script, ScriptBuf, UintError};

/// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/blockdata/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ use core::ops::{Deref, DerefMut};
use hashes::{hash160, sha256};
use io::{BufRead, Write};

use crate::blockdata::constants::{MAX_REDEEM_SCRIPT_SIZE, MAX_WITNESS_SCRIPT_SIZE};
use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self, Opcode};
use crate::consensus::{encode, Decodable, Encodable};
use crate::constants::{MAX_REDEEM_SCRIPT_SIZE, MAX_WITNESS_SCRIPT_SIZE};
use crate::internal_macros::impl_asref_push_bytes;
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
use crate::prelude::*;
use crate::OutPoint;

Expand Down
14 changes: 7 additions & 7 deletions bitcoin/src/blockdata/script/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use core::ops::Deref;
use hex::FromHex;
use secp256k1::{Secp256k1, Verification};

use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::{self, Opcode};
use crate::blockdata::script::witness_program::WitnessProgram;
use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{
opcode_to_verify, Builder, Instruction, PushBytes, Script, ScriptHash, WScriptHash,
};
use crate::key::{
PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash,
};
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
use crate::prelude::*;
use crate::script::witness_program::WitnessProgram;
use crate::script::witness_version::WitnessVersion;
use crate::script::{
opcode_to_verify, Builder, Instruction, PushBytes, Script, ScriptHash, WScriptHash,
};
use crate::taproot::TapNodeHash;

/// An owned, growable script.
Expand Down
3 changes: 1 addition & 2 deletions bitcoin/src/blockdata/script/push_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ mod primitive {
};

use super::PushBytesError;
use crate::blockdata::script::Error;
#[allow(unused)]
use crate::prelude::*;
use crate::script::scriptint_parse;
use crate::script::{scriptint_parse, Error};

#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
fn check_limit(_: usize) -> Result<(), PushBytesError> { Ok(()) }
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/script/witness_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use core::fmt;
use internals::array_vec::ArrayVec;
use secp256k1::{Secp256k1, Verification};

use crate::blockdata::script::witness_version::WitnessVersion;
use crate::blockdata::script::{PushBytes, Script, WScriptHash, WitnessScriptSizeError};
use crate::crypto::key::{CompressedPublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
use crate::script::witness_version::WitnessVersion;
use crate::script::{PushBytes, Script, WScriptHash, WitnessScriptSizeError};
use crate::taproot::TapNodeHash;

/// The minimum byte size of a segregated witness program.
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/blockdata/script/witness_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use bech32::Fe32;
use internals::write_err;
use units::{parse, ParseIntError};

use crate::blockdata::opcodes::all::*;
use crate::blockdata::opcodes::Opcode;
use crate::blockdata::script::Instruction;
use crate::opcodes::all::*;
use crate::opcodes::Opcode;
use crate::script::Instruction;

/// Version of the segregated witness program.
///
Expand Down
17 changes: 8 additions & 9 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ use io::{BufRead, Write};
use units::parse::{self, PrefixedHexError, UnprefixedHexError};

use super::Weight;
use crate::blockdata::locktime::absolute::{self, Height, Time};
use crate::blockdata::locktime::relative::{self, TimeOverflowError};
use crate::blockdata::script::{Script, ScriptBuf};
use crate::blockdata::witness::Witness;
use crate::blockdata::FeeRate;
use crate::consensus::{encode, Decodable, Encodable};
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
use crate::locktime::absolute::{self, Height, Time};
use crate::locktime::relative::{self, TimeOverflowError};
use crate::prelude::*;
use crate::script::{Script, ScriptBuf};
#[cfg(doc)]
use crate::sighash::{EcdsaSighashType, TapSighashType};
use crate::{Amount, SignedAmount, VarInt};
use crate::witness::Witness;
use crate::{Amount, FeeRate, SignedAmount, VarInt};

#[rustfmt::skip] // Keep public re-exports separate.
#[cfg(feature = "bitcoinconsensus")]
Expand Down Expand Up @@ -1618,8 +1617,8 @@ mod tests {
use hex::{test_hex_unwrap as hex, FromHex};

use super::*;
use crate::blockdata::constants::WITNESS_SCALE_FACTOR;
use crate::consensus::encode::{deserialize, serialize};
use crate::constants::WITNESS_SCALE_FACTOR;
use crate::sighash::EcdsaSighashType;

const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
Expand Down Expand Up @@ -1729,7 +1728,7 @@ mod tests {

#[test]
fn is_coinbase() {
use crate::blockdata::constants;
use crate::constants;
use crate::network::Network;

let genesis = constants::genesis_block(Network::Bitcoin);
Expand Down Expand Up @@ -2030,7 +2029,7 @@ mod tests {
fn transaction_verify() {
use std::collections::HashMap;

use crate::blockdata::witness::Witness;
use crate::witness::Witness;

// a random recent segwit transaction from blockchain using both old and segwit inputs
let mut spending: Transaction = deserialize(hex!("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700")
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl Witness {
/// This does not guarantee that this represents a P2TR [`Witness`]. It
/// merely gets the second to last or third to last element depending on
/// the first byte of the last element being equal to 0x50. See
/// [Script::is_p2tr](crate::blockdata::script::Script::is_p2tr) to
/// [Script::is_p2tr](crate::script::Script::is_p2tr) to
/// check whether this is actually a Taproot witness.
pub fn tapscript(&self) -> Option<&Script> {
let len = self.len();
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use io::{BufRead, Cursor, Read, Write};

use crate::bip152::{PrefilledTransaction, ShortId};
use crate::bip158::{FilterHash, FilterHeader};
use crate::blockdata::block::{self, BlockHash};
use crate::blockdata::transaction::{Transaction, TxIn, TxOut};
use crate::block::{self, BlockHash};
use crate::consensus::{DecodeError, IterReader};
use crate::merkle_tree::TxMerkleNode;
#[cfg(feature = "std")]
Expand All @@ -34,6 +33,7 @@ use crate::p2p::{
};
use crate::prelude::*;
use crate::taproot::TapLeafHash;
use crate::transaction::{Transaction, TxIn, TxOut};

/// Encoding error.
#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/consensus/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use core::fmt;
use internals::write_err;

use crate::amount::Amount;
use crate::blockdata::script::Script;
use crate::blockdata::transaction::{OutPoint, Transaction, TxOut};
#[cfg(doc)]
use crate::consensus;
use crate::consensus::encode;
use crate::script::Script;
use crate::transaction::{OutPoint, Transaction, TxOut};

/// Verifies spend of an input script.
///
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use internals::array_vec::ArrayVec;
use internals::write_err;
use io::{Read, Write};

use crate::blockdata::script::ScriptBuf;
use crate::crypto::ecdsa;
use crate::internal_macros::impl_asref_push_bytes;
use crate::network::NetworkKind;
use crate::prelude::*;
use crate::script::ScriptBuf;
use crate::taproot::{TapNodeHash, TapTweakHash};

#[rustfmt::skip] // Keep public re-exports separate.
Expand Down
Loading

0 comments on commit 817e54f

Please sign in to comment.