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

Some manual format improvements #335

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# see https://editorconfig.org for more options, and setup instructions for your editor

[*]
indent_style = space
indent_size = 4
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/deserialize_script.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate bitcoin;

use bitcoin::util::address::Address;
use bitcoin::network::constants::Network;
use bitcoin::blockdata::script;
use bitcoin::consensus::encode;
use bitcoin::network::constants::Network;
use bitcoin::util::address::Address;

fn do_test(data: &[u8]) {
let s: Result<script::Script, _> = encode::deserialize(data);
Expand Down
3 changes: 1 addition & 2 deletions fuzz/fuzz_targets/outpoint_string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

extern crate bitcoin;

use bitcoin::blockdata::transaction::OutPoint;
Expand Down Expand Up @@ -33,7 +32,7 @@ fn do_test(data: &[u8]) {
let string = deser.to_string();
match OutPoint::from_str(&string) {
Ok(destring) => assert_eq!(destring, deser),
Err(_) => panic!()
Err(_) => panic!(),
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

use hashes::{sha256d, Hash};

use util;
use util::Error::{BlockBadTarget, BlockBadProofOfWork};
use util::hash::{BitcoinHash, MerkleRoot, bitcoin_merkle_root};
use util::uint::Uint256;
use consensus::encode::Encodable;
use network::constants::Network;
use blockdata::transaction::Transaction;
use blockdata::constants::max_target;
use blockdata::transaction::Transaction;
use consensus::encode::Encodable;
use hashes::HashEngine;
use network::constants::Network;
use util;
use util::hash::{bitcoin_merkle_root, BitcoinHash, MerkleRoot};
use util::uint::Uint256;
use util::Error::{BlockBadProofOfWork, BlockBadTarget};

/// A block header, which contains all the block's information except
/// the actual transactions
Expand Down Expand Up @@ -58,18 +58,17 @@ pub struct Block {
/// The block header
pub header: BlockHeader,
/// List of transactions contained in the block
pub txdata: Vec<Transaction>
pub txdata: Vec<Transaction>,
}

impl Block {
/// check if merkle root of header matches merkle root of the transaction list
pub fn check_merkle_root (&self) -> bool {
pub fn check_merkle_root(&self) -> bool {
self.header.merkle_root == self.merkle_root()
}

/// check if witness commitment in coinbase is matching the transaction list
pub fn check_witness_commitment(&self) -> bool {

// witness commitment is optional if there are no transactions using SegWit in the block
if self.txdata.iter().all(|t| t.input.iter().all(|i| i.witness.is_empty())) {
return true;
Expand Down Expand Up @@ -104,8 +103,8 @@ impl Block {

/// Merkle root of transactions hashed for witness
pub fn witness_root(&self) -> sha256d::Hash {
let mut txhashes = vec!(sha256d::Hash::default());
txhashes.extend(self.txdata.iter().skip(1).map(|t|t.bitcoin_hash()));
let mut txhashes = vec![sha256d::Hash::default()];
txhashes.extend(self.txdata.iter().skip(1).map(|t| t.bitcoin_hash()));
bitcoin_merkle_root(txhashes)
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

use std::default::Default;

use blockdata::block::{Block, BlockHeader};
use blockdata::opcodes;
use blockdata::script;
use blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn};
use blockdata::block::{Block, BlockHeader};
use blockdata::transaction::{OutPoint, Transaction, TxIn, TxOut};
use network::constants::Network;
use util::misc::hex_bytes;
use util::uint::Uint256;
Expand All @@ -44,7 +44,6 @@ pub const MAX_BLOCK_WEIGHT: u32 = 4_000_000;
/// The minimum transaction weight for a valid serialized transaction
pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60;


/// In Bitcoind this is insanely described as ~((u256)0 >> 32)
pub fn max_target(_: Network) -> Uint256 {
Uint256::from_u64(0xFFFF).unwrap() << 208
Expand All @@ -68,10 +67,11 @@ fn bitcoin_genesis_tx() -> Transaction {
};

// Inputs
let in_script = script::Builder::new().push_scriptint(486604799)
.push_scriptint(4)
.push_slice(b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")
.into_script();
let in_script = script::Builder::new()
.push_scriptint(486604799)
.push_scriptint(4)
.push_slice(b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks")
.into_script();
ret.input.push(TxIn {
previous_output: OutPoint::null(),
script_sig: in_script,
Expand All @@ -86,7 +86,7 @@ fn bitcoin_genesis_tx() -> Transaction {
.into_script();
ret.output.push(TxOut {
value: 50 * COIN_VALUE,
script_pubkey: out_script
script_pubkey: out_script,
});

// end
Expand All @@ -105,9 +105,9 @@ pub fn genesis_block(network: Network) -> Block {
merkle_root: txdata[0].txid(),
time: 1231006505,
bits: 0x1d00ffff,
nonce: 2083236893
nonce: 2083236893,
},
txdata: txdata
txdata: txdata,
}
}
Network::Testnet => {
Expand All @@ -119,9 +119,9 @@ pub fn genesis_block(network: Network) -> Block {
merkle_root: txdata[0].txid(),
time: 1296688602,
bits: 0x1d00ffff,
nonce: 414098458
nonce: 414098458,
},
txdata: txdata
txdata: txdata,
}
}
Network::Regtest => {
Expand All @@ -133,23 +133,23 @@ pub fn genesis_block(network: Network) -> Block {
merkle_root: txdata[0].txid(),
time: 1296688602,
bits: 0x207fffff,
nonce: 2
nonce: 2,
},
txdata: txdata
txdata: txdata,
}
}
}
}

#[cfg(test)]
mod test {
use std::default::Default;
use hex::decode as hex_decode;
use std::default::Default;

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

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/blockdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
//! transactions which make up the Bitcoin system.
//!

pub mod block;
pub mod constants;
pub mod opcodes;
pub mod script;
pub mod transaction;
pub mod block;

23 changes: 11 additions & 12 deletions src/blockdata/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,10 @@ impl fmt::Debug for All {
all::OP_NUMEQUAL => write!(f, "NUMEQUAL"),
all::OP_NUMEQUALVERIFY => write!(f, "NUMEQUALVERIFY"),
all::OP_NUMNOTEQUAL => write!(f, "NUMNOTEQUAL"),
all::OP_LESSTHAN => write!(f, "LESSTHAN"),
all::OP_GREATERTHAN => write!(f, "GREATERTHAN"),
all::OP_LESSTHANOREQUAL => write!(f, "LESSTHANOREQUAL"),
all::OP_GREATERTHANOREQUAL => write!(f, "GREATERTHANOREQUAL"),
all::OP_LESSTHAN => write!(f, "LESSTHAN"),
all::OP_GREATERTHAN => write!(f, "GREATERTHAN"),
all::OP_LESSTHANOREQUAL => write!(f, "LESSTHANOREQUAL"),
all::OP_GREATERTHANOREQUAL => write!(f, "GREATERTHANOREQUAL"),
all::OP_MIN => write!(f, "MIN"),
all::OP_MAX => write!(f, "MAX"),
all::OP_WITHIN => write!(f, "WITHIN"),
Expand Down Expand Up @@ -745,7 +745,7 @@ pub enum Class {
/// Does nothing
NoOp,
/// Any opcode not covered above
Ordinary(Ordinary)
Ordinary(Ordinary),
}

display_from_debug!(Class);
Expand Down Expand Up @@ -810,8 +810,8 @@ impl Ordinary {
/// Encode as a byte
#[inline]
pub fn into_u8(&self) -> u8 {
*self as u8
}
*self as u8
}
}

#[cfg(test)]
Expand Down Expand Up @@ -994,10 +994,10 @@ mod tests {
roundtrip!(unique, OP_NUMEQUAL);
roundtrip!(unique, OP_NUMEQUALVERIFY);
roundtrip!(unique, OP_NUMNOTEQUAL);
roundtrip!(unique, OP_LESSTHAN );
roundtrip!(unique, OP_GREATERTHAN );
roundtrip!(unique, OP_LESSTHANOREQUAL );
roundtrip!(unique, OP_GREATERTHANOREQUAL );
roundtrip!(unique, OP_LESSTHAN);
roundtrip!(unique, OP_GREATERTHAN);
roundtrip!(unique, OP_LESSTHANOREQUAL);
roundtrip!(unique, OP_GREATERTHANOREQUAL);
roundtrip!(unique, OP_MIN);
roundtrip!(unique, OP_MAX);
roundtrip!(unique, OP_WITHIN);
Expand Down Expand Up @@ -1094,4 +1094,3 @@ mod tests {
assert_eq!(unique.len(), 256);
}
}

Loading