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

Commit

Permalink
Add database from piece index to file offsets and visa versa (parityt…
Browse files Browse the repository at this point in the history
…ech#278)

This commit also:

* Moves plotter state to separate structure
* Substitute piece index with piece offset in commitment db
  • Loading branch information
i1i1 committed Mar 13, 2022
1 parent a865835 commit 14d4fe1
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 145 deletions.
26 changes: 24 additions & 2 deletions crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,28 @@ impl RootBlock {
}
}

/// Index of piece on disk
pub type PieceOffset = u64;
/// Piece index in consensus
pub type PieceIndex = u64;

/// Hash of `PieceIndex`
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct PieceIndexHash(pub Sha256Hash);

impl From<PieceIndex> for PieceIndexHash {
fn from(index: PieceIndex) -> Self {
Self::from_index(index)
}
}

impl PieceIndexHash {
/// Constructs `PieceIndexHash` from `PieceIndex`
pub fn from_index(index: PieceIndex) -> Self {
Self(crypto::sha256_hash(&index.to_le_bytes()))
}
}

/// Farmer solution for slot challenge.
#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
Expand All @@ -467,7 +489,7 @@ pub struct Solution<AccountId> {
/// Address for receiving block reward
pub reward_address: AccountId,
/// Index of encoded piece
pub piece_index: u64,
pub piece_index: PieceIndex,
/// Encoding
pub encoding: Piece,
/// Signature of the tag
Expand All @@ -485,7 +507,7 @@ impl<AccountId: Clone> Solution<AccountId> {
Self {
public_key,
reward_address,
piece_index: 0u64,
piece_index: 0,
encoding: Piece::default(),
signature: Signature::default(),
local_challenge: LocalChallenge::default(),
Expand Down
28 changes: 17 additions & 11 deletions crates/subspace-farmer/src/commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rocksdb::DB;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use subspace_core_primitives::{FlatPieces, Salt, Tag, PIECE_SIZE};
use subspace_core_primitives::{FlatPieces, PieceOffset, Salt, Tag, PIECE_SIZE};
use thiserror::Error;

const BATCH_SIZE: u64 = (16 * 1024 * 1024 / PIECE_SIZE) as u64;
Expand Down Expand Up @@ -132,8 +132,8 @@ impl Commitments {
.map(|piece| subspace_solving::create_tag(piece, salt))
.collect();

for (tag, index) in tags.iter().zip(batch_start..) {
db.put(tag, index.to_le_bytes())
for (tag, offset) in tags.iter().zip(batch_start..) {
db.put(tag, offset.to_le_bytes())
.map_err(CommitmentError::CommitmentDb)?;
}
}
Expand Down Expand Up @@ -216,15 +216,20 @@ impl Commitments {
}

/// Finds the commitment/s falling in the range of the challenge
pub(crate) fn find_by_range(&self, target: Tag, range: u64, salt: Salt) -> Option<(Tag, u64)> {
pub(crate) fn find_by_range(
&self,
target: Tag,
range: u64,
salt: Salt,
) -> Option<(Tag, PieceOffset)> {
let db_entry = self.get_local_db_entry(&salt)?;

let db_guard = db_entry.try_lock()?;
let db = db_guard.clone()?;

let mut iter = db.raw_iterator();

let mut solutions: Vec<(Tag, u64)> = Vec::new();
let mut solutions = Vec::new();

let (lower, is_lower_overflowed) = u64::from_be_bytes(target).overflowing_sub(range / 2);
let (upper, is_upper_overflowed) = u64::from_be_bytes(target).overflowing_add(range / 2);
Expand All @@ -240,29 +245,30 @@ impl Commitments {
iter.seek_to_first();
while let Some(tag) = iter.key() {
let tag = tag.try_into().unwrap();
let index = iter.value().unwrap();
let offset = iter.value().unwrap();
if u64::from_be_bytes(tag) <= upper {
solutions.push((tag, u64::from_le_bytes(index.try_into().unwrap())));
solutions.push((tag, u64::from_le_bytes(offset.try_into().unwrap())));
iter.next();
} else {
break;
}
}

iter.seek(lower.to_be_bytes());
while let Some(tag) = iter.key() {
let tag = tag.try_into().unwrap();
let index = iter.value().unwrap();
let offset = iter.value().unwrap();

solutions.push((tag, u64::from_le_bytes(index.try_into().unwrap())));
solutions.push((tag, u64::from_le_bytes(offset.try_into().unwrap())));
iter.next();
}
} else {
iter.seek(lower.to_be_bytes());
while let Some(tag) = iter.key() {
let tag = tag.try_into().unwrap();
let index = iter.value().unwrap();
let offset = iter.value().unwrap();
if u64::from_be_bytes(tag) <= upper {
solutions.push((tag, u64::from_le_bytes(index.try_into().unwrap())));
solutions.push((tag, u64::from_le_bytes(offset.try_into().unwrap())));
iter.next();
} else {
break;
Expand Down
20 changes: 12 additions & 8 deletions crates/subspace-farmer/src/farming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use futures::{future, future::Either};
use log::{debug, error, info, trace, warn};
use std::sync::mpsc;
use std::time::Instant;
use subspace_core_primitives::PublicKey;
use subspace_core_primitives::{LocalChallenge, Salt, Solution};
use subspace_core_primitives::{LocalChallenge, PublicKey, Salt, Solution};
use subspace_rpc_primitives::{BlockSignature, BlockSigningInfo, SlotInfo, SolutionResponse};
use thiserror::Error;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -51,10 +50,13 @@ impl Farming {
// Get a handle for the background task, so that we can wait on it later if we want to
let farming_handle = tokio::spawn(async move {
match future::select(
Box::pin(async move {
subscribe_to_slot_info(&client, &plot, &commitments, &identity, reward_adress)
.await
}),
Box::pin(subscribe_to_slot_info(
&client,
&plot,
&commitments,
&identity,
reward_adress,
)),
stop_receiver,
)
.await
Expand Down Expand Up @@ -135,8 +137,10 @@ async fn subscribe_to_slot_info<T: RpcClient>(
slot_info.solution_range,
slot_info.salt,
) {
Some((tag, piece_index)) => {
let encoding = plot.read(piece_index).map_err(FarmingError::PlotRead)?;
Some((tag, piece_offset)) => {
let (encoding, piece_index) = plot
.read_piece_with_index(piece_offset)
.map_err(FarmingError::PlotRead)?;
let solution = Solution {
public_key: identity.public_key().to_bytes().into(),
reward_address,
Expand Down
4 changes: 1 addition & 3 deletions crates/subspace-farmer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
//! 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)]
#![feature(int_log)]
#![feature(try_blocks, hash_drain_filter, int_log, io_error_other)]

pub(crate) mod commitments;
pub(crate) mod farming;
Expand Down
Loading

0 comments on commit 14d4fe1

Please sign in to comment.