Skip to content

Commit

Permalink
Merge rust-bitcoin/rust-bitcoin#983: Edition 2018
Browse files Browse the repository at this point in the history
9f0c687 Enable edition 2018 (Tobin C. Harding)
dca0d67 Fix in preparation for next edition (Tobin C. Harding)

Pull request description:

  This PR supersedes #635 at the permission of @Kixunil in the thread of that PR.

  Do a minimal set of changes to enable edition 2018.  Patch 1 is  the biggest change set, it is done with `cargo`, no other manual changes are included in patch 1.  It can verified by running `cargo fix --edition` on master and checking the diffs are the same.

  Patch 2 enables 2018 and includes all the manual changes required to get the code to build (with _all_ the feature combinations :)

ACKs for top commit:
  dunxen:
    re-ACK  9f0c687
  apoelstra:
    re-ACK 9f0c687
  RCasatta:
    ACK 9f0c687,

Tree-SHA512: 7c23554adb4c1dd932af1e80f04397bad0418b4fdae2d0fe243c3f19ba1169686a386bffd38a3c02871c7544b5a7bc8af525b50617a2695726408c091700f081
  • Loading branch information
apoelstra committed May 18, 2022
2 parents 588e837 + decd9df commit bb495a2
Show file tree
Hide file tree
Showing 46 changed files with 552 additions and 549 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -10,6 +10,7 @@ description = "General purpose library for using and interoperating with Bitcoin
keywords = [ "crypto", "bitcoin" ]
readme = "README.md"
exclude = ["./test_data"]
edition = "2018"

# Please don't forget to add relevant features to docs.rs below
[features]
Expand Down
44 changes: 22 additions & 22 deletions src/blockdata/block.rs
Expand Up @@ -20,22 +20,22 @@
//! these blocks and the blockchain.
//!

use prelude::*;
use crate::prelude::*;

use core::fmt;

use util;
use util::Error::{BlockBadTarget, BlockBadProofOfWork};
use util::hash::bitcoin_merkle_root;
use hashes::{Hash, HashEngine};
use hash_types::{Wtxid, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment};
use util::uint::Uint256;
use consensus::encode::Encodable;
use network::constants::Network;
use blockdata::transaction::Transaction;
use blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
use blockdata::script;
use VarInt;
use crate::util;
use crate::util::Error::{BlockBadTarget, BlockBadProofOfWork};
use crate::util::hash::bitcoin_merkle_root;
use crate::hashes::{Hash, HashEngine};
use crate::hash_types::{Wtxid, BlockHash, TxMerkleNode, WitnessMerkleNode, WitnessCommitment};
use crate::util::uint::Uint256;
use crate::consensus::encode::Encodable;
use crate::network::constants::Network;
use crate::blockdata::transaction::Transaction;
use crate::blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
use crate::blockdata::script;
use crate::VarInt;

/// A block header, which contains all the block's information except
/// the actual transactions
Expand Down Expand Up @@ -359,13 +359,13 @@ impl ::std::error::Error for Bip34Error {}

#[cfg(test)]
mod tests {
use hashes::hex::FromHex;
use crate::hashes::hex::FromHex;

use blockdata::block::{Block, BlockHeader};
use consensus::encode::{deserialize, serialize};
use util::uint::Uint256;
use util::Error::{BlockBadTarget, BlockBadProofOfWork};
use network::constants::Network;
use crate::blockdata::block::{Block, BlockHeader};
use crate::consensus::encode::{deserialize, serialize};
use crate::util::uint::Uint256;
use crate::util::Error::{BlockBadTarget, BlockBadProofOfWork};
use crate::network::constants::Network;

#[test]
fn test_coinbase_and_bip34() {
Expand Down Expand Up @@ -508,10 +508,10 @@ mod tests {
#[cfg(all(test, feature = "unstable"))]
mod benches {
use super::Block;
use EmptyWrite;
use consensus::{deserialize, Encodable};
use crate::EmptyWrite;
use crate::consensus::{deserialize, Encodable};
use test::{black_box, Bencher};
use network::stream_reader::StreamReader;
use crate::network::stream_reader::StreamReader;

#[bench]
#[allow(deprecated)]
Expand Down
30 changes: 15 additions & 15 deletions src/blockdata/constants.rs
Expand Up @@ -19,19 +19,19 @@
//! single transaction.
//!

use prelude::*;
use crate::prelude::*;

use core::default::Default;

use hashes::hex::{self, HexIterator};
use hashes::sha256d;
use blockdata::opcodes;
use blockdata::script;
use blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn};
use blockdata::block::{Block, BlockHeader};
use blockdata::witness::Witness;
use network::constants::Network;
use util::uint::Uint256;
use crate::hashes::hex::{self, HexIterator};
use crate::hashes::sha256d;
use crate::blockdata::opcodes;
use crate::blockdata::script;
use crate::blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn};
use crate::blockdata::block::{Block, BlockHeader};
use crate::blockdata::witness::Witness;
use crate::network::constants::Network;
use crate::util::uint::Uint256;

/// The maximum allowable sequence number
pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF;
Expand Down Expand Up @@ -179,12 +179,12 @@ pub fn genesis_block(network: Network) -> Block {
#[cfg(test)]
mod test {
use core::default::Default;
use hashes::hex::FromHex;
use crate::hashes::hex::FromHex;

use network::constants::Network;
use consensus::encode::serialize;
use blockdata::constants::{genesis_block, bitcoin_genesis_tx};
use blockdata::constants::{MAX_SEQUENCE, COIN_VALUE};
use crate::network::constants::Network;
use crate::consensus::encode::serialize;
use crate::blockdata::constants::{genesis_block, bitcoin_genesis_tx};
use crate::blockdata::constants::{MAX_SEQUENCE, COIN_VALUE};

#[test]
fn bitcoin_genesis_first_transaction() {
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/opcodes.rs
Expand Up @@ -22,7 +22,7 @@

#[cfg(feature = "serde")] use serde;

#[cfg(feature = "serde")] use prelude::*;
#[cfg(feature = "serde")] use crate::prelude::*;

use core::{fmt, convert::From};

Expand Down
50 changes: 25 additions & 25 deletions src/blockdata/script.rs
Expand Up @@ -23,28 +23,28 @@
//! This module provides the structures and functions needed to support scripts.
//!

use prelude::*;
use crate::prelude::*;

use io;
use crate::io;
use core::{fmt, default::Default};
use core::ops::Index;

#[cfg(feature = "serde")] use serde;

use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash};
use blockdata::opcodes;
use consensus::{encode, Decodable, Encodable};
use hashes::{Hash, hex};
use policy::DUST_RELAY_TX_FEE;
use crate::hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash};
use crate::blockdata::opcodes;
use crate::consensus::{encode, Decodable, Encodable};
use crate::hashes::{Hash, hex};
use crate::policy::DUST_RELAY_TX_FEE;
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use core::convert::From;
use OutPoint;
use crate::OutPoint;

use util::key::PublicKey;
use util::address::WitnessVersion;
use util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
use crate::util::key::PublicKey;
use crate::util::address::WitnessVersion;
use crate::util::taproot::{LeafVersion, TapBranchHash, TapLeafHash};
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
use crate::schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};

/// A Bitcoin script.
#[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -557,7 +557,7 @@ impl Script {

/// Checks whether a script can be proven to have no satisfying input.
pub fn is_provably_unspendable(&self) -> bool {
use blockdata::opcodes::Class::{ReturnOp, IllegalOp};
use crate::blockdata::opcodes::Class::{ReturnOp, IllegalOp};

match self.0.first() {
Some(b) => {
Expand All @@ -572,7 +572,7 @@ impl Script {

/// Returns the minimum value an output with this script should have in order to be
/// broadcastable on today's Bitcoin network.
pub fn dust_value(&self) -> ::Amount {
pub fn dust_value(&self) -> crate::Amount {
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
// otherwise allow users to create transactions which likely can never be broadcast/confirmed.
let sats = DUST_RELAY_TX_FEE as u64 / 1000 * // The default dust relay fee is 3000 satoshi/kB (i.e. 3 sat/vByte)
Expand All @@ -588,7 +588,7 @@ impl Script {
self.consensus_encode(&mut sink()).expect("sinks don't error") as u64 // The serialized size of this script_pubkey
};

::Amount::from_sat(sats)
crate::Amount::from_sat(sats)
}

/// Iterates over the script in the form of `Instruction`s, which are an enum covering opcodes,
Expand Down Expand Up @@ -617,7 +617,7 @@ impl Script {
/// Shorthand for [`Self::verify_with_flags`] with flag [bitcoinconsensus::VERIFY_ALL].
#[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify (&self, index: usize, amount: ::Amount, spending: &[u8]) -> Result<(), Error> {
pub fn verify (&self, index: usize, amount: crate::Amount, spending: &[u8]) -> Result<(), Error> {
self.verify_with_flags(index, amount, spending, ::bitcoinconsensus::VERIFY_ALL)
}

Expand All @@ -630,7 +630,7 @@ impl Script {
/// * `flags` - Verification flags, see [`bitcoinconsensus::VERIFY_ALL`] and similar.
#[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify_with_flags<F: Into<u32>>(&self, index: usize, amount: ::Amount, spending: &[u8], flags: F) -> Result<(), Error> {
pub fn verify_with_flags<F: Into<u32>>(&self, index: usize, amount: crate::Amount, spending: &[u8], flags: F) -> Result<(), Error> {
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.as_sat(), spending, index, flags.into())?)
}

Expand Down Expand Up @@ -1075,11 +1075,11 @@ mod test {
use super::*;
use super::build_scriptint;

use hashes::hex::{FromHex, ToHex};
use consensus::encode::{deserialize, serialize};
use blockdata::opcodes;
use util::key::PublicKey;
use util::psbt::serialize::Serialize;
use crate::hashes::hex::{FromHex, ToHex};
use crate::consensus::encode::{deserialize, serialize};
use crate::blockdata::opcodes;
use crate::util::key::PublicKey;
use crate::util::psbt::serialize::Serialize;

#[test]
fn script() {
Expand Down Expand Up @@ -1440,7 +1440,7 @@ mod test {
// a random segwit transaction from the blockchain using native segwit
let spent = Builder::from(Vec::from_hex("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d").unwrap()).into_script();
let spending = Vec::from_hex("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000").unwrap();
spent.verify(0, ::Amount::from_sat(18393430), spending.as_slice()).unwrap();
spent.verify(0, crate::Amount::from_sat(18393430), spending.as_slice()).unwrap();
}

#[test]
Expand All @@ -1449,7 +1449,7 @@ mod test {
// well-known scriptPubKey types.
let script_p2wpkh = Builder::new().push_int(0).push_slice(&[42; 20]).into_script();
assert!(script_p2wpkh.is_v0_p2wpkh());
assert_eq!(script_p2wpkh.dust_value(), ::Amount::from_sat(294));
assert_eq!(script_p2wpkh.dust_value(), crate::Amount::from_sat(294));

let script_p2pkh = Builder::new()
.push_opcode(opcodes::all::OP_DUP)
Expand All @@ -1459,7 +1459,7 @@ mod test {
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.dust_value(), ::Amount::from_sat(546));
assert_eq!(script_p2pkh.dust_value(), crate::Amount::from_sat(546));
}

#[test]
Expand Down
62 changes: 31 additions & 31 deletions src/blockdata/transaction.rs
Expand Up @@ -23,27 +23,27 @@
//! This module provides the structures and functions needed to support transactions.
//!

use prelude::*;
use crate::prelude::*;

use io;
use crate::io;
use core::{fmt, str, default::Default};
#[cfg(feature = "std")] use std::error;

use hashes::{self, Hash, sha256d};
use hashes::hex::FromHex;
use crate::hashes::{self, Hash, sha256d};
use crate::hashes::hex::FromHex;

use util::endian;
use blockdata::constants::WITNESS_SCALE_FACTOR;
#[cfg(feature="bitcoinconsensus")] use blockdata::script;
use blockdata::script::Script;
use blockdata::witness::Witness;
use consensus::{encode, Decodable, Encodable};
use consensus::encode::MAX_VEC_SIZE;
use hash_types::{Sighash, Txid, Wtxid};
use VarInt;
use crate::util::endian;
use crate::blockdata::constants::WITNESS_SCALE_FACTOR;
#[cfg(feature="bitcoinconsensus")] use crate::blockdata::script;
use crate::blockdata::script::Script;
use crate::blockdata::witness::Witness;
use crate::consensus::{encode, Decodable, Encodable};
use crate::consensus::encode::MAX_VEC_SIZE;
use crate::hash_types::{Sighash, Txid, Wtxid};
use crate::VarInt;

#[cfg(doc)]
use util::sighash::SchnorrSighashType;
use crate::util::sighash::SchnorrSighashType;

/// Used for signature hash for invalid use of SIGHASH_SINGLE.
const UINT256_ONE: [u8; 32] = [
Expand Down Expand Up @@ -590,7 +590,7 @@ impl Transaction {
let flags: u32 = flags.into();
for (idx, input) in self.input.iter().enumerate() {
if let Some(output) = spent(&input.previous_output) {
output.script_pubkey.verify_with_flags(idx, ::Amount::from_sat(output.value), tx.as_slice(), flags)?;
output.script_pubkey.verify_with_flags(idx, crate::Amount::from_sat(output.value), tx.as_slice(), flags)?;
} else {
return Err(script::Error::UnknownSpentOutput(input.previous_output.clone()));
}
Expand Down Expand Up @@ -895,17 +895,17 @@ mod tests {
use super::*;

use core::str::FromStr;
use blockdata::constants::WITNESS_SCALE_FACTOR;
use blockdata::script::Script;
use consensus::encode::serialize;
use consensus::encode::deserialize;
use crate::blockdata::constants::WITNESS_SCALE_FACTOR;
use crate::blockdata::script::Script;
use crate::consensus::encode::serialize;
use crate::consensus::encode::deserialize;

use hashes::Hash;
use hashes::hex::FromHex;
use crate::hashes::Hash;
use crate::hashes::hex::FromHex;

use hash_types::*;
use crate::hash_types::*;
use super::EcdsaSighashType;
use util::sighash::SighashCache;
use crate::util::sighash::SighashCache;

#[test]
fn test_outpoint() {
Expand Down Expand Up @@ -958,8 +958,8 @@ mod tests {

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

let genesis = constants::genesis_block(Network::Bitcoin);
assert! (genesis.txdata[0].is_coin_base());
Expand Down Expand Up @@ -1541,10 +1541,10 @@ mod tests {
#[test]
#[cfg(feature="bitcoinconsensus")]
fn test_transaction_verify () {
use hashes::hex::FromHex;
use std::collections::HashMap;
use blockdata::script;
use blockdata::witness::Witness;
use crate::hashes::hex::FromHex;
use crate::blockdata::script;
use crate::blockdata::witness::Witness;

// a random recent segwit transaction from blockchain using both old and segwit inputs
let mut spending: Transaction = deserialize(Vec::from_hex("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700")
Expand Down Expand Up @@ -1601,9 +1601,9 @@ mod tests {
#[cfg(all(test, feature = "unstable"))]
mod benches {
use super::Transaction;
use EmptyWrite;
use consensus::{deserialize, Encodable};
use hashes::hex::FromHex;
use crate::EmptyWrite;
use crate::consensus::{deserialize, Encodable};
use crate::hashes::hex::FromHex;
use test::{black_box, Bencher};

const SOME_TX: &'static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000";
Expand Down

0 comments on commit bb495a2

Please sign in to comment.