Skip to content

Commit

Permalink
Restructure modules and add more stats to SimpleStats callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarq committed Jul 15, 2020
1 parent 6315dd5 commit 7e58df2
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 183 deletions.
1 change: 0 additions & 1 deletion src/blockchain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod parser;
pub mod proto;
pub mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::path::{Path, PathBuf};

use byteorder::{LittleEndian, ReadBytesExt};

use crate::blockchain::parser::reader::BlockchainRead;
use crate::blockchain::proto::block::Block;
use crate::blockchain::utils::reader::BlockchainRead;
use crate::errors::{OpError, OpErrorKind, OpResult};

/// Holds all necessary data about a raw blk file
Expand Down
14 changes: 7 additions & 7 deletions src/blockchain/parser/chain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::cell::RefCell;
use std::collections::HashMap;

use crate::blockchain::parser::blkfile::BlkFile;
use crate::blockchain::parser::index::{get_block_index, BlockIndexRecord};
use crate::blockchain::proto::block::Block;
use crate::blockchain::utils::arr_to_hex_swapped;
use crate::blockchain::utils::blkfile::BlkFile;
use crate::common::utils;
use crate::errors::OpResult;
use crate::ParserOptions;

Expand Down Expand Up @@ -60,18 +60,18 @@ impl<'a> ChainStorage<'a> {
if block.header.hash != genesis_hash {
panic!(
"Hash of genesis doesn't match!\n -> expected: {}\n -> got: {}\n",
arr_to_hex_swapped(&genesis_hash),
arr_to_hex_swapped(&block.header.hash),
utils::arr_to_hex_swapped(&genesis_hash),
utils::arr_to_hex_swapped(&block.header.hash),
);
}
} else {
let prev_hash = self.blocks.get(self.index - 1).unwrap().block_hash;
if block.header.value.prev_hash != prev_hash {
panic!(
"prev_hash for block {} doesn't match!\n -> expected: {}\n -> got: {}\n",
arr_to_hex_swapped(&block.header.hash),
arr_to_hex_swapped(&block.header.value.prev_hash),
arr_to_hex_swapped(&prev_hash)
utils::arr_to_hex_swapped(&block.header.hash),
utils::arr_to_hex_swapped(&block.header.value.prev_hash),
utils::arr_to_hex_swapped(&prev_hash)
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/blockchain/parser/index.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use byteorder::ReadBytesExt;
use rusty_leveldb::{LdbIterator, Options, DB};
use std::convert::TryInto;
use std::fmt;
use std::io::Cursor;
use std::path::Path;

use byteorder::ReadBytesExt;
use rusty_leveldb::{LdbIterator, Options, DB};

use crate::errors::OpResult;

const BLOCK_VALID_CHAIN: usize = 4;
Expand Down
2 changes: 2 additions & 0 deletions src/blockchain/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use std::time::{Duration, Instant};
use crate::blockchain::proto::block::Block;
use crate::ParserOptions;

mod blkfile;
pub mod chain;
mod index;
mod reader;
pub mod types;

/// Small struct to hold statistics together
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod tests {
use super::*;
use crate::blockchain::parser::types::{Bitcoin, Coin};
use crate::blockchain::proto::script;
use crate::blockchain::utils::{arr_to_hex, arr_to_hex_swapped};
use crate::common::utils;
use byteorder::{LittleEndian, ReadBytesExt};
use std::io::{BufReader, Cursor};

Expand Down Expand Up @@ -205,15 +205,15 @@ mod tests {
assert_eq!(0x00000001, block.header.value.version);
assert_eq!(
"0000000000000000000000000000000000000000000000000000000000000000",
arr_to_hex(&block.header.value.prev_hash)
utils::arr_to_hex(&block.header.value.prev_hash)
);
assert_eq!(
"3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a",
arr_to_hex(&block.header.value.merkle_root)
utils::arr_to_hex(&block.header.value.merkle_root)
);
assert_eq!(
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
arr_to_hex_swapped(&block.header.hash)
utils::arr_to_hex_swapped(&block.header.hash)
);

// Check against computed merkle root
Expand All @@ -230,12 +230,12 @@ mod tests {
assert_eq!(0x01, block.txs[0].value.in_count.value);
assert_eq!(
"0000000000000000000000000000000000000000000000000000000000000000",
arr_to_hex_swapped(&block.txs[0].value.inputs[0].outpoint.txid)
utils::arr_to_hex_swapped(&block.txs[0].value.inputs[0].outpoint.txid)
);
assert_eq!(0xffffffff, block.txs[0].value.inputs[0].outpoint.index);
assert_eq!(0x4d, block.txs[0].value.inputs[0].script_len.value);
assert_eq!("04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73",
arr_to_hex(&block.txs[0].value.inputs[0].script_sig));
utils::arr_to_hex(&block.txs[0].value.inputs[0].script_sig));
assert_eq!(0xffffffff, block.txs[0].value.inputs[0].seq_no);

// Tx Outputs
Expand All @@ -248,7 +248,7 @@ mod tests {

let script_pubkey = &block.txs[0].value.outputs[0].out.script_pubkey;
assert_eq!("4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac",
arr_to_hex(&script_pubkey));
utils::arr_to_hex(&script_pubkey));
assert_eq!(0x00000000, block.txs[0].value.tx_locktime);

assert_eq!(
Expand Down
30 changes: 22 additions & 8 deletions src/blockchain/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::From;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::blockchain::utils::hex_to_arr32_swapped;
use crate::common::utils;
use crate::errors::{OpError, OpErrorKind, OpResult};

/// Trait to specify the underlying coin of a blockchain
Expand Down Expand Up @@ -38,7 +38,9 @@ impl Coin for Bitcoin {
0x00
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
utils::hex_to_arr32_swapped(
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".bitcoin").join("blocks")
Expand All @@ -57,7 +59,9 @@ impl Coin for TestNet3 {
0x6f
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")
utils::hex_to_arr32_swapped(
"000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".bitcoin").join("testnet3")
Expand All @@ -75,7 +79,9 @@ impl Coin for Namecoin {
0x34
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("000000000062b72c5e2ceb45fbc8587e807c155b0da735e6483dfba2f0a9c770")
utils::hex_to_arr32_swapped(
"000000000062b72c5e2ceb45fbc8587e807c155b0da735e6483dfba2f0a9c770",
)
}
fn default_folder(&self) -> PathBuf {
PathBuf::from(".namecoin")
Expand All @@ -93,7 +99,9 @@ impl Coin for Litecoin {
0x30
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2")
utils::hex_to_arr32_swapped(
"12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".litecoin").join("blocks")
Expand All @@ -111,7 +119,9 @@ impl Coin for Dogecoin {
0x1e
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691")
utils::hex_to_arr32_swapped(
"1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".dogecoin").join("blocks")
Expand All @@ -129,7 +139,9 @@ impl Coin for Myriadcoin {
0x32
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("00000ffde4c020b5938441a0ea3d314bf619eff0b38f32f78f7583cffa1ea485")
utils::hex_to_arr32_swapped(
"00000ffde4c020b5938441a0ea3d314bf619eff0b38f32f78f7583cffa1ea485",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".myriadcoin").join("blocks")
Expand All @@ -147,7 +159,9 @@ impl Coin for Unobtanium {
0x82
}
fn genesis(&self) -> [u8; 32] {
hex_to_arr32_swapped("000004c2fc5fffb810dccc197d603690099a68305232e552d96ccbe8e2c52b75")
utils::hex_to_arr32_swapped(
"000004c2fc5fffb810dccc197d603690099a68305232e552d96ccbe8e2c52b75",
)
}
fn default_folder(&self) -> PathBuf {
Path::new(".unobtanium").join("blocks")
Expand Down
8 changes: 4 additions & 4 deletions src/blockchain/proto/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::blockchain::proto::header::BlockHeader;
use crate::blockchain::proto::tx::Tx;
use crate::blockchain::proto::varuint::VarUint;
use crate::blockchain::proto::Hashed;
use crate::blockchain::utils::{arr_to_hex_swapped, merkle_root};
use crate::common::utils;

/// Basic block structure which holds all information
pub struct Block {
Expand All @@ -28,7 +28,7 @@ impl Block {
/// Computes merkle root for all containing transactions
#[inline]
pub fn compute_merkle_root(&self) -> [u8; 32] {
merkle_root(&self.txs.iter().map(|tx| tx.hash).collect::<Vec<[u8; 32]>>())
utils::merkle_root(&self.txs.iter().map(|tx| tx.hash).collect::<Vec<[u8; 32]>>())
}

/// Calculates merkle root and verifies it against the field in BlockHeader.
Expand All @@ -38,8 +38,8 @@ impl Block {
if merkle_root != self.header.value.merkle_root {
panic!(
"Invalid merkle_root!\n -> expected: {}\n -> got: {}\n",
&arr_to_hex_swapped(&self.header.value.merkle_root),
&arr_to_hex_swapped(&merkle_root)
&utils::arr_to_hex_swapped(&self.header.value.merkle_root),
&utils::arr_to_hex_swapped(&merkle_root)
);
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/blockchain/proto/header.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt;

use crate::blockchain::proto::ToRaw;
use crate::blockchain::utils::arr_to_hex_swapped;
use crate::blockchain::utils::le::u32_to_array;
use crate::common::utils;

/// Block Header definition. Exact 80 bytes long
#[derive(Clone)]
Expand Down Expand Up @@ -38,12 +37,12 @@ impl BlockHeader {
impl ToRaw for BlockHeader {
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(80);
bytes.extend_from_slice(&u32_to_array(self.version));
bytes.extend_from_slice(&utils::le::u32_to_array(self.version));
bytes.extend_from_slice(&self.prev_hash);
bytes.extend_from_slice(&self.merkle_root);
bytes.extend_from_slice(&u32_to_array(self.timestamp));
bytes.extend_from_slice(&u32_to_array(self.bits));
bytes.extend_from_slice(&u32_to_array(self.nonce));
bytes.extend_from_slice(&utils::le::u32_to_array(self.timestamp));
bytes.extend_from_slice(&utils::le::u32_to_array(self.bits));
bytes.extend_from_slice(&utils::le::u32_to_array(self.nonce));
bytes
}
}
Expand All @@ -52,8 +51,8 @@ impl fmt::Debug for BlockHeader {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BlockHeader")
.field("version", &self.version)
.field("prev_hash", &arr_to_hex_swapped(&self.prev_hash))
.field("merkle_root", &arr_to_hex_swapped(&self.merkle_root))
.field("prev_hash", &utils::arr_to_hex_swapped(&self.prev_hash))
.field("merkle_root", &utils::arr_to_hex_swapped(&self.merkle_root))
.field("timestamp", &self.timestamp)
.field("bits", &self.bits)
.field("nonce", &self.nonce)
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/proto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::blockchain::utils::{arr_to_hex_swapped, sha256};
use crate::common::utils;

pub mod block;
pub mod header;
Expand All @@ -26,7 +26,7 @@ impl<T: ToRaw> Hashed<T> {
#[inline]
pub fn double_sha256(value: T) -> Hashed<T> {
Hashed {
hash: sha256(&sha256(&value.to_bytes())),
hash: utils::sha256(&utils::sha256(&value.to_bytes())),
value,
}
}
Expand All @@ -40,7 +40,7 @@ impl<T: ToRaw> Hashed<T> {
impl<T: fmt::Debug> fmt::Debug for Hashed<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Hashed")
.field("hash", &arr_to_hex_swapped(&self.hash))
.field("hash", &utils::arr_to_hex_swapped(&self.hash))
.field("value", &self.value)
.finish()
}
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/proto/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use rust_base58::ToBase58;

use crate::blockchain::proto::opcodes;
use crate::blockchain::utils::{self, ridemp160, sha256};
use crate::common::utils;

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ScriptError {
Expand Down Expand Up @@ -420,7 +420,7 @@ pub fn eval_from_stack(stack: Stack, version_id: u8) -> EvaluatedScript {

/// Takes full ECDSA public key (65 bytes) and a version id
fn public_key_to_addr(pub_key: &[u8], version: u8) -> String {
let h160 = ridemp160(&sha256(pub_key));
let h160 = utils::ridemp160(&utils::sha256(pub_key));
hash_160_to_address(&h160, version)
}

Expand All @@ -430,7 +430,7 @@ fn hash_160_to_address(h160: &[u8], version: u8) -> String {
vh160.push(version);
vh160.extend_from_slice(&h160);

let h3 = sha256(&sha256(&vh160));
let h3 = utils::sha256(&utils::sha256(&vh160));

let mut addr = vh160;
addr.extend_from_slice(&h3[0..4]);
Expand Down
14 changes: 7 additions & 7 deletions src/blockchain/proto/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use crate::blockchain::proto::script;
use crate::blockchain::proto::varuint::VarUint;
use crate::blockchain::proto::ToRaw;
use crate::blockchain::utils::{self, arr_to_hex_swapped, le};
use crate::common::utils;

/// Simple transaction struct
/// Please note: The txid is not stored here. See Hashed.
Expand Down Expand Up @@ -70,7 +70,7 @@ impl ToRaw for Tx {
Vec::with_capacity((4 + self.in_count.value + self.out_count.value + 4) as usize);

// Serialize version
bytes.extend_from_slice(&le::u32_to_array(self.tx_version));
bytes.extend_from_slice(&utils::le::u32_to_array(self.tx_version));
// Serialize all TxInputs
bytes.extend_from_slice(&self.in_count.to_bytes());
for i in &self.inputs {
Expand All @@ -82,7 +82,7 @@ impl ToRaw for Tx {
bytes.extend_from_slice(&o.out.to_bytes());
}
// Serialize locktime
bytes.extend_from_slice(&le::u32_to_array(self.tx_locktime));
bytes.extend_from_slice(&utils::le::u32_to_array(self.tx_locktime));
bytes
}
}
Expand All @@ -98,15 +98,15 @@ impl ToRaw for TxOutpoint {
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(32 + 4);
bytes.extend_from_slice(&self.txid);
bytes.extend_from_slice(&le::u32_to_array(self.index));
bytes.extend_from_slice(&utils::le::u32_to_array(self.index));
bytes
}
}

impl fmt::Debug for TxOutpoint {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("TxOutpoint")
.field("txid", &arr_to_hex_swapped(&self.txid))
.field("txid", &utils::arr_to_hex_swapped(&self.txid))
.field("index", &self.index)
.finish()
}
Expand All @@ -128,7 +128,7 @@ impl ToRaw for TxInput {
bytes.extend_from_slice(&self.outpoint.to_bytes());
bytes.extend_from_slice(&self.script_len.to_bytes());
bytes.extend_from_slice(&self.script_sig);
bytes.extend_from_slice(&le::u32_to_array(self.seq_no));
bytes.extend_from_slice(&utils::le::u32_to_array(self.seq_no));
bytes
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ impl ToRaw for TxOutput {
#[inline]
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(8 + 5 + self.script_len.value as usize);
bytes.extend_from_slice(&le::u64_to_array(self.value));
bytes.extend_from_slice(&utils::le::u64_to_array(self.value));
bytes.extend_from_slice(&self.script_len.to_bytes());
bytes.extend_from_slice(&self.script_pubkey);
bytes
Expand Down
Loading

0 comments on commit 7e58df2

Please sign in to comment.