diff --git a/core/src/core/block.rs b/core/src/core/block.rs index 90cd5c2cb..563d9675d 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -36,7 +36,6 @@ pub struct BlockHeader { pub td: u64, // total difficulty up to this block pub utxo_merkle: Hash, pub tx_merkle: Hash, - pub total_fees: u64, pub nonce: u64, pub pow: Proof, } @@ -50,7 +49,6 @@ impl Default for BlockHeader { td: 0, utxo_merkle: ZERO_HASH, tx_merkle: ZERO_HASH, - total_fees: 0, nonce: 0, pow: Proof::zero(), } @@ -66,8 +64,7 @@ impl Writeable for BlockHeader { [write_fixed_bytes, &self.previous], [write_i64, self.timestamp.to_timespec().sec], [write_fixed_bytes, &self.utxo_merkle], - [write_fixed_bytes, &self.tx_merkle], - [write_u64, self.total_fees]); + [write_fixed_bytes, &self.tx_merkle]); // make sure to not introduce any variable length data before the nonce to // avoid complicating PoW try!(writer.write_u64(self.nonce)); @@ -118,14 +115,12 @@ impl Writeable for Block { /// from a binary stream. impl Readable<Block> for Block { fn read(reader: &mut Reader) -> Result<Block, ser::Error> { - let (height, previous, timestamp, utxo_merkle, tx_merkle, total_fees, nonce) = - ser_multiread!(reader, + let (height, previous, timestamp, utxo_merkle, tx_merkle, nonce) = ser_multiread!(reader, read_u64, read_32_bytes, read_i64, read_32_bytes, read_32_bytes, - read_u64, read_u64); // cuckoo cycle of 42 nodes @@ -157,7 +152,6 @@ impl Readable<Block> for Block { td: td, utxo_merkle: Hash::from_vec(utxo_merkle), tx_merkle: Hash::from_vec(tx_merkle), - total_fees: total_fees, pow: Proof(pow), nonce: nonce, }, @@ -179,7 +173,7 @@ impl Committed for Block { &self.outputs } fn overage(&self) -> i64 { - (REWARD as i64) - (self.header.total_fees as i64) + (REWARD as i64) - (self.total_fees() as i64) } } @@ -235,12 +229,10 @@ impl Block { outputs.sort_by_key(|out| out.hash()); // calculate the overall Merkle tree and fees - let fees = txs.iter().map(|tx| tx.fee).sum(); Ok(Block { header: BlockHeader { height: prev.height + 1, - total_fees: fees, timestamp: time::now(), ..Default::default() }, @@ -255,6 +247,10 @@ impl Block { self.header.hash() } + pub fn total_fees(&self) -> u64 { + self.proofs.iter().map(|p| p.fee).sum() + } + /// Matches any output with a potential spending input, eliminating them /// from the block. Provides a simple way to compact the block. The /// elimination is stable with respect to inputs and outputs order. @@ -312,11 +308,7 @@ impl Block { Block { // compact will fix the merkle tree - header: BlockHeader { - total_fees: self.header.total_fees + other.header.total_fees, - pow: self.header.pow.clone(), - ..self.header - }, + header: BlockHeader { pow: self.header.pow.clone(), ..self.header }, inputs: all_inputs, outputs: all_outputs, proofs: all_proofs, 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<W: ser::Writeable> 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..0340493b8 100644 --- a/core/src/core/mod.rs +++ b/core/src/core/mod.rs @@ -156,8 +156,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 { diff --git a/core/src/genesis.rs b/core/src/genesis.rs index 1fa92d7bb..58a76d68f 100644 --- a/core/src/genesis.rs +++ b/core/src/genesis.rs @@ -31,7 +31,7 @@ pub fn genesis() -> core::Block { core::Block { header: core::BlockHeader { height: 0, - previous: core::hash::ZERO_HASH, + previous: core::hash::Hash([0xff; 32]), timestamp: time::Tm { tm_year: 1997, tm_mon: 7, @@ -41,7 +41,6 @@ pub fn genesis() -> core::Block { td: 0, utxo_merkle: core::hash::Hash::from_vec(empty_h.to_vec()), tx_merkle: core::hash::Hash::from_vec(empty_h.to_vec()), - total_fees: 0, nonce: 0, pow: core::Proof::zero(), // TODO get actual PoW solution }, diff --git a/core/src/pow/mod.rs b/core/src/pow/mod.rs index d2069721f..5e3395978 100644 --- a/core/src/pow/mod.rs +++ b/core/src/pow/mod.rs @@ -61,7 +61,6 @@ struct PowHeader { pub timestamp: time::Tm, pub utxo_merkle: Hash, pub tx_merkle: Hash, - pub total_fees: u64, pub n_in: u64, pub n_out: u64, pub n_proofs: u64, @@ -78,7 +77,6 @@ impl Writeable for PowHeader { try!(writer.write_i64(self.timestamp.to_timespec().sec)); try!(writer.write_fixed_bytes(&self.utxo_merkle)); try!(writer.write_fixed_bytes(&self.tx_merkle)); - try!(writer.write_u64(self.total_fees)); try!(writer.write_u64(self.n_in)); try!(writer.write_u64(self.n_out)); writer.write_u64(self.n_proofs) @@ -95,7 +93,6 @@ impl PowHeader { timestamp: h.timestamp, utxo_merkle: h.utxo_merkle, tx_merkle: h.tx_merkle, - total_fees: h.total_fees, n_in: b.inputs.len() as u64, n_out: b.outputs.len() as u64, n_proofs: b.proofs.len() as u64, 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