This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request paritytech#74 from subspace/farmer-lib
Farmer bin to lib transition - 1st step
- Loading branch information
Showing
8 changed files
with
92 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use subspace_core_primitives::PIECE_SIZE; | ||
|
||
pub(crate) type Tag = [u8; 8]; | ||
pub(crate) type Salt = [u8; 8]; | ||
pub(crate) const BATCH_SIZE: u64 = (16 * 1024 * 1024 / PIECE_SIZE) as u64; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use anyhow::Error; | ||
use log::info; | ||
use schnorrkel::{context::SigningContext, Keypair, PublicKey, SecretKey, Signature}; | ||
use std::fs; | ||
use std::path::Path; | ||
use subspace_solving::SOLUTION_SIGNING_CONTEXT; | ||
|
||
pub struct Identity { | ||
keypair: Keypair, | ||
ctx: SigningContext, | ||
} | ||
|
||
impl Identity { | ||
pub fn open_or_create(path: &Path) -> Result<Identity, Error> { | ||
let identity_file = path.join("identity.bin"); | ||
let keypair = if identity_file.exists() { | ||
info!("Opening existing keypair"); // TODO: turn this into a channel | ||
Keypair::from_bytes(&fs::read(identity_file)?).map_err(Error::msg)? | ||
} else { | ||
info!("Generating new keypair"); // TODO: turn this into a channel | ||
let new_keypair = Keypair::generate(); | ||
fs::write(identity_file, new_keypair.to_bytes())?; | ||
new_keypair | ||
}; | ||
Ok(Identity { | ||
keypair, | ||
ctx: schnorrkel::context::signing_context(SOLUTION_SIGNING_CONTEXT), | ||
}) | ||
} | ||
|
||
pub fn public_key(&self) -> PublicKey { | ||
self.keypair.public | ||
} | ||
|
||
pub fn secret_key(&self) -> SecretKey { | ||
self.keypair.secret.clone() | ||
} | ||
|
||
pub fn sign(&self, data: &[u8]) -> Signature { | ||
self.keypair.sign(self.ctx.bytes(data)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! subspace-farmer implementation overview | ||
//! | ||
//! The application typically runs two processes in parallel: plotting and farming. | ||
//! | ||
//! During plotting we create a binary plot file, which contains subspace-encoded pieces one | ||
//! after another as well as RocksDB key-value database with tags, where key is tag (first 8 bytes | ||
//! of `hmac(encoding, salt)`) and value is an offset of corresponding encoded piece in the plot (we | ||
//! can do this because all pieces have the same size). So for every 4096 bytes we also store a | ||
//! record with 8-bytes tag and 8-bytes index (+some overhead of RocksDB itself). | ||
//! | ||
//! During farming we receive a global challenge and need to find a solution, given target and | ||
//! solution range. In order to find solution we derive local challenge as our target and do range | ||
//! query in RocksDB. For that we interpret target as 64-bit unsigned integer, and find all of the | ||
//! keys in tags database that are `target ± solution range` (while also handing overflow/underflow) | ||
//! converted back to bytes. | ||
#![feature(try_blocks)] | ||
#![feature(hash_drain_filter)] | ||
|
||
pub(crate) mod identity; | ||
|
||
pub use identity::Identity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters