Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request paritytech#89 from subspace/refine-rustdoc-2
Browse files Browse the repository at this point in the history
Add some trivial improvements to solving and node crate
  • Loading branch information
liuchengxu authored Oct 28, 2021
2 parents 56df131 + 6a0cd5f commit 69f6195
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
6 changes: 3 additions & 3 deletions crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ pub enum Error<B: BlockT> {
/// Invalid encoding of a piece
#[display(fmt = "Invalid encoding for slot {}", _0)]
InvalidEncoding(Slot),
/// Invalid commitment for salt
#[display(fmt = "Invalid commitment for salt for slot {}", _0)]
InvalidCommitment(Slot),
/// Invalid tag for salt
#[display(fmt = "Invalid tag for salt for slot {}", _0)]
InvalidTag(Slot),
/// Could not fetch parent header
#[display(fmt = "Could not fetch parent header: {:?}", _0)]
FetchParentHeader(sp_blockchain::Error),
Expand Down
4 changes: 2 additions & 2 deletions crates/sc-consensus-subspace/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ pub(crate) fn verify_solution<B: BlockT + Sized>(
.try_into()
.map_err(|_error| Error::EncodingOfWrongSize)?;

if !subspace_solving::is_commitment_valid(&piece, solution.tag, salt) {
return Err(Error::InvalidCommitment(slot));
if !subspace_solving::is_tag_valid(&piece, solution.tag, salt) {
return Err(Error::InvalidTag(slot));
}

if !is_signature_valid(signing_context, solution) {
Expand Down
9 changes: 5 additions & 4 deletions crates/subspace-node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub fn new_partial(
client.clone(),
&task_manager.spawn_handle(),
);

let slot_duration = subspace_link.config().slot_duration();
let import_queue = sc_consensus_subspace::import_queue(
&subspace_link,
Expand Down Expand Up @@ -227,10 +228,10 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

let slot =
sp_consensus_subspace::inherents::InherentDataProvider::from_timestamp_and_duration(
*timestamp,
slot_duration,
);
sp_consensus_subspace::inherents::InherentDataProvider::from_timestamp_and_duration(
*timestamp,
slot_duration,
);

Ok((timestamp, slot, uncles))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-solving/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SubspaceCodec {

let farmer_public_key_hash = crypto::sha256_hash(farmer_public_key);

SubspaceCodec {
Self {
farmer_public_key_hash,
cuda_available,
}
Expand Down
13 changes: 8 additions & 5 deletions crates/subspace-solving/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@

//! Set of modules that implement utilities for solving and verifying of solutions in
//! [Subspace Network Blockchain](https://subspace.network).
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, missing_debug_implementations, missing_docs)]

mod codec;

pub use codec::SubspaceCodec;
use core::mem;
use sha2::{Digest, Sha256};
use subspace_core_primitives::{crypto, Piece, Randomness, Salt, Tag};

/// Signing context used for creating solution signatures by farmer
pub const SOLUTION_SIGNING_CONTEXT: &[u8] = b"FARMER";

/// Size of `Tag` in bytes.
pub const TAG_SIZE: usize = core::mem::size_of::<Tag>();

/// Check whether commitment tag of a piece is valid for a particular salt, which is used as a
/// Proof-of-Replication
pub fn is_commitment_valid(piece: &Piece, tag: Tag, salt: Salt) -> bool {
pub fn is_tag_valid(piece: &Piece, tag: Tag, salt: Salt) -> bool {
create_tag(piece, salt) == tag
}

/// Create a commitment tag of a piece for a particular salt
pub fn create_tag(piece: &[u8], salt: Salt) -> Tag {
crypto::hmac_sha256(salt, piece)[..mem::size_of::<Tag>()]
crypto::hmac_sha256(salt, piece)[..TAG_SIZE]
.try_into()
.expect("Slice is always of correct size; qed")
}
Expand All @@ -46,7 +49,7 @@ pub fn derive_global_challenge<Slot: Into<u64>>(epoch_randomness: &Randomness, s
let mut hasher = Sha256::new();
hasher.update(epoch_randomness);
hasher.update(&Into::<u64>::into(slot).to_le_bytes());
hasher.finalize()[..mem::size_of::<Tag>()]
hasher.finalize()[..TAG_SIZE]
.try_into()
.expect("Slice is always of correct size; qed")
}
Expand All @@ -59,7 +62,7 @@ pub fn derive_local_challenge<C: AsRef<[u8]>, H: AsRef<[u8]>>(
let mut hasher = Sha256::new();
hasher.update(global_challenge.as_ref());
hasher.update(farmer_public_key_hash.as_ref());
hasher.finalize()[..mem::size_of::<Tag>()]
hasher.finalize()[..TAG_SIZE]
.try_into()
.expect("Slice is always of correct size; qed")
}

0 comments on commit 69f6195

Please sign in to comment.