Skip to content

Commit

Permalink
farmer: Fix farmer side of the records_root call.
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin committed Jul 28, 2022
1 parent 79b59b2 commit 8ec51dc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/subspace-farmer/src/bench_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl RpcClient for BenchRpcClient {
Ok(())
}

async fn records_root(&self, _: u64) -> Result<Option<Sha256Hash>, Error> {
Ok(Some(Default::default()))
async fn records_roots(&self, _: Vec<u64>) -> Result<Vec<Option<Sha256Hash>>, Error> {
Ok(Default::default())
}
}
4 changes: 2 additions & 2 deletions crates/subspace-farmer/src/mock_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl RpcClient for MockRpcClient {
Ok(())
}

async fn records_root(&self, _: u64) -> Result<Option<Sha256Hash>, MockError> {
Ok(None)
async fn records_roots(&self, _: Vec<u64>) -> Result<Vec<Option<Sha256Hash>>, MockError> {
Ok(Default::default())
}
}
7 changes: 5 additions & 2 deletions crates/subspace-farmer/src/node_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ impl RpcClient for NodeRpcClient {
.await?)
}

async fn records_root(&self, segment_index: u64) -> Result<Option<Sha256Hash>, RpcError> {
async fn records_roots(
&self,
segment_indexes: Vec<u64>,
) -> Result<Vec<Option<Sha256Hash>>, RpcError> {
Ok(self
.client
.request("subspace_recordsRoot", rpc_params![&segment_index])
.request("subspace_recordsRoots", rpc_params![&segment_indexes])
.await?)
}
}
7 changes: 5 additions & 2 deletions crates/subspace-farmer/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub trait RpcClient: Clone + Send + Sync + 'static {
/// Acknowledge receiving of archived segments
async fn acknowledge_archived_segment(&self, segment_index: u64) -> Result<(), Error>;

/// Get records root for the segment
async fn records_root(&self, segment_index: u64) -> Result<Option<Sha256Hash>, Error>;
/// Get records roots for the segments
async fn records_roots(
&self,
segment_indexes: Vec<u64>,
) -> Result<Vec<Option<Sha256Hash>>, Error>;
}
41 changes: 31 additions & 10 deletions crates/subspace-farmer/src/single_plot_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::sync::Arc;
use std::{fs, io, mem};
use subspace_archiving::archiver::is_piece_valid;
use subspace_core_primitives::{
FlatPieces, Piece, PieceIndex, PieceIndexHash, PublicKey, PIECE_SIZE,
FlatPieces, Piece, PieceIndex, PieceIndexHash, PublicKey, Sha256Hash, PIECE_SIZE,
};
use subspace_networking::libp2p::identity::sr25519;
use subspace_networking::libp2p::Multiaddr;
Expand Down Expand Up @@ -733,23 +733,44 @@ impl<RC: RpcClient> VerifyingPlotter<RC> {
return Err(PiecesVerificationError::InvalidFarmerProtocolInfo);
}

for (piece, piece_index) in pieces.as_pieces().zip(piece_indexes) {
let segment_index: u64 = piece_index / records_per_segment as u64;
let position: u64 = piece_index % records_per_segment as u64;
let segment_indexes = piece_indexes
.iter()
.map(|piece_index| piece_index / records_per_segment as u64)
.collect::<Vec<_>>();

let roots = self
.verification_client
.records_roots(segment_indexes)
.await
.map_err(|err| PiecesVerificationError::RpcError(err))?;

let verified_roots = roots
.into_iter()
.zip(piece_indexes.iter())
.map(|(root, piece_index)| {
if let Some(root) = root {
Ok(root)
} else {
error!(?piece_index, "No records root found for piece_index.");

let root = self
.verification_client
.records_root(segment_index)
.await
.map_err(|err| PiecesVerificationError::RpcError(err))?
.ok_or(PiecesVerificationError::NoRecordsRootFound)?;
Err(PiecesVerificationError::NoRecordsRootFound)
}
})
.collect::<Result<Vec<Sha256Hash>, PiecesVerificationError>>()?;

for ((piece, piece_index), root) in
pieces.as_pieces().zip(piece_indexes).zip(verified_roots)
{
let position: u64 = piece_index % records_per_segment as u64;

if !is_piece_valid(
piece,
root,
position as usize,
self.farmer_protocol_info.record_size as usize,
) {
error!(?piece_index, "Piece validation failed.");

return Err(PiecesVerificationError::InvalidPieces);
}
}
Expand Down

0 comments on commit 8ec51dc

Please sign in to comment.