Skip to content

Commit

Permalink
chain: create a new work module for proof-of-work
Browse files Browse the repository at this point in the history
This extracts the `difficulty` module from `block` and the
`equihash_solution` module from the crate root.  The PoW calculations
are significantly more complicated than the other block code and pretty
dissimilar from it, so it makes more sense to create a common proof of
work module.

The `EquihashSolution` and `EQUIHASH_SOLUTION_SIZE` are renamed to
`equihash::Solution` and `equihash::SOLUTION_SIZE` and imported that
way, except in `block/header.rs`, to avoid a conflict with the
`equihash` crate.  In the future it would be better to encapsulate the
equihash solution check into the `equihash::Solution` type so that
callers only need to import our `work::equihash`.

The test organization leaves a little to be desired but I think that
this can be improved as we fill out the proof of work implementation.
  • Loading branch information
hdevalence committed Aug 17, 2020
1 parent dad6340 commit 855b89d
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 290 deletions.
1 change: 0 additions & 1 deletion zebra-chain/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Definitions of block datastructures.
#![allow(clippy::unit_arg)]

mod difficulty;
mod hash;
mod header;
mod height;
Expand Down
8 changes: 4 additions & 4 deletions zebra-chain/src/block/header.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::{DateTime, Duration, Utc};

use crate::equihash_solution::EquihashSolution;
use crate::merkle_tree::MerkleTreeRootHash;
use crate::serialization::ZcashSerialize;
use crate::work::{difficulty::CompactDifficulty, equihash::Solution};

use super::{difficulty::CompactDifficulty, BlockHeaderHash, Error};
use super::{BlockHeaderHash, Error};

/// Block header.
///
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct BlockHeader {
pub nonce: [u8; 32],

/// The Equihash solution.
pub solution: EquihashSolution,
pub solution: Solution,
}

impl BlockHeader {
Expand All @@ -80,7 +80,7 @@ impl BlockHeader {
self.zcash_serialize(&mut input)
.expect("serialization into a vec can't fail");

let input = &input[0..EquihashSolution::INPUT_LENGTH];
let input = &input[0..Solution::INPUT_LENGTH];

equihash::is_valid_solution(n, k, input, nonce, solution)?;

Expand Down
5 changes: 2 additions & 3 deletions zebra-chain/src/block/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{TimeZone, Utc};
use std::io;

use crate::block::difficulty::CompactDifficulty;
use crate::equihash_solution::EquihashSolution;
use crate::merkle_tree::MerkleTreeRootHash;
use crate::serialization::ZcashDeserializeInto;
use crate::serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize};
use crate::work::{difficulty::CompactDifficulty, equihash};

use super::Block;
use super::BlockHeader;
Expand Down Expand Up @@ -71,7 +70,7 @@ impl ZcashDeserialize for BlockHeader {
time: Utc.timestamp(reader.read_u32::<LittleEndian>()? as i64, 0),
difficulty_threshold: CompactDifficulty(reader.read_u32::<LittleEndian>()?),
nonce: reader.read_32_bytes()?,
solution: EquihashSolution::zcash_deserialize(reader)?,
solution: equihash::Solution::zcash_deserialize(reader)?,
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/block/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::*;

use crate::block::{difficulty::CompactDifficulty, light_client::LightClientRootHash};
use crate::equihash_solution::EquihashSolution;
use crate::block::light_client::LightClientRootHash;
use crate::merkle_tree::MerkleTreeRootHash;
use crate::serialization::{
sha256d, SerializationError, ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize,
};
use crate::transaction::LockTime;
use crate::work::{difficulty::CompactDifficulty, equihash};
use crate::Network;

use crate::test::generate;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Arbitrary for BlockHeader {
(0i64..(u32::MAX as i64)),
any::<CompactDifficulty>(),
any::<[u8; 32]>(),
any::<EquihashSolution>(),
any::<equihash::Solution>(),
)
.prop_map(
|(
Expand Down
276 changes: 0 additions & 276 deletions zebra-chain/src/equihash_solution.rs

This file was deleted.

2 changes: 1 addition & 1 deletion zebra-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod addresses;
pub mod amount;
pub mod block;
pub mod commitments;
pub mod equihash_solution;
pub mod keys;
pub mod notes;
pub mod parameters;
Expand All @@ -26,6 +25,7 @@ pub mod serialization;
pub mod transaction;
pub mod treestate;
pub mod types;
pub mod work;

pub use ed25519_zebra;
pub use redjubjub;
Expand Down
7 changes: 7 additions & 0 deletions zebra-chain/src/work.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Proof-of-work implementation.
pub mod difficulty;
pub mod equihash;

#[cfg(test)]
mod tests;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! Tests for difficulty and work
use super::*;

use crate::block::Block;
Expand Down
Loading

0 comments on commit 855b89d

Please sign in to comment.