From 4ade3ec1b8c06f1c6d048bd1d61942d3c629464e Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Wed, 9 Nov 2016 15:51:24 -0800 Subject: [PATCH] Moved consensus related constants to their own core module. --- core/src/core/block.rs | 3 +-- core/src/core/hash.rs | 9 +++------ core/src/core/mod.rs | 24 +++++++++--------------- core/src/core/transaction.rs | 13 +++++-------- core/src/lib.rs | 1 + core/src/pow/cuckoo.rs | 3 ++- core/src/pow/mod.rs | 19 +++---------------- core/src/ser.rs | 12 +++++++----- 8 files changed, 31 insertions(+), 53 deletions(-) diff --git a/core/src/core/block.rs b/core/src/core/block.rs index 3985705e0..009d975cf 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -23,9 +23,8 @@ use std::collections::HashSet; use core::Committed; use core::{Input, Output, Proof, TxProof, Transaction}; use core::transaction::merkle_inputs_outputs; -use core::{PROOFSIZE, REWARD}; +use consensus::{PROOFSIZE, REWARD, MAX_IN_OUT_LEN}; use core::hash::{Hash, Hashed, ZERO_HASH}; -use core::transaction::MAX_IN_OUT_LEN; use ser::{self, Readable, Reader, Writeable, Writer}; /// Block header, fairly standard compared to other blockchains. diff --git a/core/src/core/hash.rs b/core/src/core/hash.rs index 770a45ae0..8ae400c61 100644 --- a/core/src/core/hash.rs +++ b/core/src/core/hash.rs @@ -60,7 +60,7 @@ pub const ZERO_HASH: Hash = Hash([0; 32]); /// Serializer that outputs a hash of the serialized object pub struct HashWriter { - state: Keccak + state: Keccak, } impl HashWriter { @@ -71,9 +71,7 @@ impl HashWriter { impl Default for HashWriter { fn default() -> HashWriter { - HashWriter { - state: Keccak::new_sha3_256() - } + HashWriter { state: Keccak::new_sha3_256() } } } @@ -90,7 +88,7 @@ impl ser::Writer for HashWriter { /// A trait for types that have a canonical hash pub trait Hashed { - fn hash(&self) -> Hash; + fn hash(&self) -> Hash; } impl Hashed for W { @@ -112,4 +110,3 @@ impl Hashed for [u8] { Hash(ret) } } - diff --git a/core/src/core/mod.rs b/core/src/core/mod.rs index af5f7368c..dbb79fff3 100644 --- a/core/src/core/mod.rs +++ b/core/src/core/mod.rs @@ -19,25 +19,17 @@ pub mod hash; pub mod transaction; #[allow(dead_code)] -pub use self::block::{Block, BlockHeader}; -pub use self::transaction::{Transaction, Input, Output, TxProof}; -use self::hash::{Hash, Hashed, ZERO_HASH}; -use ser::{Writeable, Writer, Error}; - use std::fmt; use std::cmp::Ordering; use secp::{self, Secp256k1}; use secp::pedersen::*; -/// The block subsidy amount -pub const REWARD: u64 = 1_000_000_000; - -/// Block interval, in seconds -pub const BLOCK_TIME_SEC: u8 = 15; - -/// Cuckoo-cycle proof size (cycle length) -pub const PROOFSIZE: usize = 42; +use consensus::PROOFSIZE; +pub use self::block::{Block, BlockHeader}; +pub use self::transaction::{Transaction, Input, Output, TxProof}; +use self::hash::{Hash, Hashed, ZERO_HASH}; +use ser::{Writeable, Writer, Error}; /// Implemented by types that hold inputs and outputs including Pedersen /// commitments. Handles the collection of the commitments as well as their @@ -156,8 +148,10 @@ impl Writeable for HPair { } } /// An iterator over hashes in a vector that pairs them to build a row in a -/// Merkle tree. If the vector has an odd number of hashes, it appends a zero hash -/// See https://bitcointalk.org/index.php?topic=102395.0 CVE-2012-2459 (block merkle calculation exploit) +/// Merkle tree. If the vector has an odd number of hashes, it appends a zero +/// hash +/// See https://bitcointalk.org/index.php?topic=102395.0 CVE-2012-2459 (block +/// merkle calculation exploit) /// for the argument against duplication of last hash struct HPairIter(Vec); impl Iterator for HPairIter { diff --git a/core/src/core/transaction.rs b/core/src/core/transaction.rs index 33c715dbd..a221ebf2c 100644 --- a/core/src/core/transaction.rs +++ b/core/src/core/transaction.rs @@ -14,18 +14,15 @@ //! Transactions -use core::Committed; -use core::MerkleRow; -use core::hash::{Hash, Hashed}; -use ser::{self, Reader, Writer, Readable, Writeable}; - use secp::{self, Secp256k1, Message, Signature}; use secp::key::SecretKey; use secp::pedersen::{RangeProof, Commitment}; -/// The maximum number of inputs or outputs a transaction may have -/// and be deserializable. -pub const MAX_IN_OUT_LEN: u64 = 50000; +use consensus::MAX_IN_OUT_LEN; +use core::Committed; +use core::MerkleRow; +use core::hash::{Hash, Hashed}; +use ser::{self, Reader, Writer, Readable, Writeable}; /// A proof that a transaction did not create (or remove) funds. Includes both /// the transaction's Pedersen commitment and the signature that guarantees diff --git a/core/src/lib.rs b/core/src/lib.rs index aef09db2b..ae4ccdaf5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -31,6 +31,7 @@ extern crate tiny_keccak; #[macro_use] pub mod macros; +pub mod consensus; pub mod core; pub mod genesis; pub mod pow; diff --git a/core/src/pow/cuckoo.rs b/core/src/pow/cuckoo.rs index 544aef68b..1673c36ee 100644 --- a/core/src/pow/cuckoo.rs +++ b/core/src/pow/cuckoo.rs @@ -23,7 +23,8 @@ use std::cmp; use crypto::digest::Digest; use crypto::sha2::Sha256; -use core::{Proof, PROOFSIZE}; +use consensus::PROOFSIZE; +use core::Proof; use pow::siphash::siphash24; const MAXPATHLEN: usize = 8192; diff --git a/core/src/pow/mod.rs b/core/src/pow/mod.rs index d2069721f..574249c5b 100644 --- a/core/src/pow/mod.rs +++ b/core/src/pow/mod.rs @@ -27,28 +27,14 @@ mod cuckoo; use time; -use core::{Block, Proof, PROOFSIZE}; +use consensus::{SIZESHIFT, EASINESS}; +use core::{Block, Proof}; use core::hash::{Hash, Hashed}; use pow::cuckoo::{Cuckoo, Miner, Error}; use ser; use ser::{Writeable, Writer}; -/// Default Cuckoo Cycle size shift used is 28. We may decide to increase it. -/// when difficuty increases. -const SIZESHIFT: u32 = 28; - -/// Default Cuckoo Cycle easiness, high enough to have good likeliness to find -/// a solution. -const EASINESS: u32 = 50; - -/// Max target hash, lowest difficulty -pub const MAX_TARGET: [u32; PROOFSIZE] = - [0xfff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, - 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, - 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, - 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff]; - /// Subset of a block header that goes into hashing for proof of work. /// Basically the whole thing minus the PoW solution itself and the total /// difficulty (yet unknown). We also add the count of every variable length @@ -169,6 +155,7 @@ fn pow_size(b: &Block, target: Proof, sizeshift: u32) -> Result<(Proof, u64), Er #[cfg(test)] mod test { use super::*; + use consensus::MAX_TARGET; use core::Proof; use genesis; diff --git a/core/src/ser.rs b/core/src/ser.rs index fbf0a2406..795f1b63a 100644 --- a/core/src/ser.rs +++ b/core/src/ser.rs @@ -49,9 +49,11 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::IOErr(ref e) => write!(f, "{}", e), - Error::UnexpectedData { expected: ref e, received: ref r } => write!(f, "expected {:?}, got {:?}", e, r), + Error::UnexpectedData { expected: ref e, received: ref r } => { + write!(f, "expected {:?}, got {:?}", e, r) + } Error::CorruptedData => f.write_str("corrupted data"), - Error::TooLargeReadErr(ref s) => f.write_str(&s) + Error::TooLargeReadErr(ref s) => f.write_str(&s), } } } @@ -60,7 +62,7 @@ impl error::Error for Error { fn cause(&self) -> Option<&error::Error> { match *self { Error::IOErr(ref e) => Some(e), - _ => None + _ => None, } } @@ -69,7 +71,7 @@ impl error::Error for Error { Error::IOErr(ref e) => error::Error::description(e), Error::UnexpectedData { expected: _, received: _ } => "unexpected data", Error::CorruptedData => "corrupted data", - Error::TooLargeReadErr(ref s) => s + Error::TooLargeReadErr(ref s) => s, } } } @@ -90,7 +92,7 @@ pub enum SerializationMode { /// Serialize the data that defines the object Hash, /// Serialize everything that a signer of the object should know - SigHash + SigHash, } /// Implementations defined how different numbers and binary structures are