Moved consensus related constants to their own core module.

This commit is contained in:
Ignotus Peverell 2016-11-09 15:51:24 -08:00
parent ba5479841e
commit 4ade3ec1b8
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
8 changed files with 31 additions and 53 deletions

View file

@ -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.

View file

@ -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<W: ser::Writeable> Hashed for W {
@ -112,4 +110,3 @@ impl Hashed for [u8] {
Hash(ret)
}
}

View file

@ -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<Hash>);
impl Iterator for HPairIter {

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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