Skip to content

Commit

Permalink
Remove redundant segment commitments RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Jul 25, 2023
1 parent 5c25da4 commit 981f079
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 106 deletions.
73 changes: 5 additions & 68 deletions crates/sc-consensus-subspace-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,20 @@ use sp_consensus_slots::Slot;
use sp_consensus_subspace::{FarmerPublicKey, FarmerSignature, SubspaceApi as SubspaceRuntimeApi};
use sp_core::crypto::ByteArray;
use sp_core::H256;
use sp_runtime::traits::{Block as BlockT, Zero};
use sp_runtime::traits::Block as BlockT;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::error::Error;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use subspace_core_primitives::{
Piece, PieceIndex, SegmentCommitment, SegmentHeader, SegmentIndex, Solution,
};
use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex, Solution};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_networking::libp2p::Multiaddr;
use subspace_rpc_primitives::{
FarmerAppInfo, NodeSyncStatus, RewardSignatureResponse, RewardSigningInfo, SlotInfo,
SolutionResponse, MAX_SEGMENT_INDEXES_PER_REQUEST,
SolutionResponse, MAX_SEGMENT_HEADERS_PER_REQUEST,
};
use tracing::{debug, error, warn};

Expand Down Expand Up @@ -109,12 +107,6 @@ pub trait SubspaceRpcApi {
)]
fn subscribe_node_sync_status_change(&self);

#[method(name = "subspace_segmentCommitments")]
async fn segment_commitments(
&self,
segment_indexes: Vec<SegmentIndex>,
) -> RpcResult<Vec<Option<SegmentCommitment>>>;

#[method(name = "subspace_segmentHeaders")]
async fn segment_headers(
&self,
Expand Down Expand Up @@ -645,73 +637,18 @@ where
Ok(())
}

// TODO: Remove as unnecessary, `segment_headers` can be used instead
async fn segment_commitments(
&self,
segment_indexes: Vec<SegmentIndex>,
) -> RpcResult<Vec<Option<SegmentCommitment>>> {
if segment_indexes.len() > MAX_SEGMENT_INDEXES_PER_REQUEST {
error!(
"segment_indexes length exceed the limit: {} ",
segment_indexes.len()
);

return Err(JsonRpseeError::Custom(format!(
"segment_indexes length exceed the limit {MAX_SEGMENT_INDEXES_PER_REQUEST}"
)));
};

let runtime_api = self.client.runtime_api();
let best_hash = self.client.info().best_hash;
let best_block_number = self.client.info().best_number;

let segment_commitment_result: Result<Vec<_>, JsonRpseeError> = segment_indexes
.into_iter()
.map(|segment_index| {
let api_result = runtime_api
.segment_commitment(best_hash, segment_index)
.map_err(|_| {
JsonRpseeError::Custom(
"Internal error during `segment_commitment` call".to_string(),
)
});

api_result.map(|maybe_segment_commitment| {
// This is not a very nice hack due to the fact that at the time first block is
// produced extrinsics with segment headers are not yet in runtime.
if maybe_segment_commitment.is_none() && best_block_number.is_zero() {
self.segment_headers_store
.get_segment_header(segment_index)
.map(|segment_header| segment_header.segment_commitment())
} else {
maybe_segment_commitment
}
})
})
.collect();

if let Err(ref err) = segment_commitment_result {
error!(
"Failed to get data from runtime API (segment_commitment): {}",
err
);
}

segment_commitment_result
}

async fn segment_headers(
&self,
segment_indexes: Vec<SegmentIndex>,
) -> RpcResult<Vec<Option<SegmentHeader>>> {
if segment_indexes.len() > MAX_SEGMENT_INDEXES_PER_REQUEST {
if segment_indexes.len() > MAX_SEGMENT_HEADERS_PER_REQUEST {
error!(
"segment_indexes length exceed the limit: {} ",
segment_indexes.len()
);

return Err(JsonRpseeError::Custom(format!(
"segment_indexes length exceed the limit {MAX_SEGMENT_INDEXES_PER_REQUEST}"
"segment_indexes length exceed the limit {MAX_SEGMENT_HEADERS_PER_REQUEST}"
)));
};

Expand Down
8 changes: 1 addition & 7 deletions crates/subspace-farmer/src/node_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) mod node_rpc_client;
use async_trait::async_trait;
use futures::Stream;
use std::pin::Pin;
use subspace_core_primitives::{Piece, PieceIndex, SegmentCommitment, SegmentHeader, SegmentIndex};
use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex};
use subspace_rpc_primitives::{
FarmerAppInfo, NodeSyncStatus, RewardSignatureResponse, RewardSigningInfo, SlotInfo,
SolutionResponse,
Expand Down Expand Up @@ -50,12 +50,6 @@ pub trait NodeClient: Clone + Send + Sync + 'static {
&self,
) -> Result<Pin<Box<dyn Stream<Item = NodeSyncStatus> + Send + 'static>>, Error>;

/// Get segment commitments for the segments
async fn segment_commitments(
&self,
segment_indexes: Vec<SegmentIndex>,
) -> Result<Vec<Option<SegmentCommitment>>, Error>;

/// Get segment headers for the segments
async fn segment_headers(
&self,
Expand Down
12 changes: 1 addition & 11 deletions crates/subspace-farmer/src/node_client/node_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonrpsee::rpc_params;
use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
use std::pin::Pin;
use std::sync::Arc;
use subspace_core_primitives::{Piece, PieceIndex, SegmentCommitment, SegmentHeader, SegmentIndex};
use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex};
use subspace_rpc_primitives::{
FarmerAppInfo, NodeSyncStatus, RewardSignatureResponse, RewardSigningInfo, SlotInfo,
SolutionResponse,
Expand Down Expand Up @@ -141,16 +141,6 @@ impl NodeClient for NodeRpcClient {
)))
}

async fn segment_commitments(
&self,
segment_indexes: Vec<SegmentIndex>,
) -> Result<Vec<Option<SegmentCommitment>>, RpcError> {
Ok(self
.client
.request("subspace_segmentCommitments", rpc_params![&segment_indexes])
.await?)
}

async fn segment_headers(
&self,
segment_indexes: Vec<SegmentIndex>,
Expand Down
32 changes: 14 additions & 18 deletions crates/subspace-farmer/src/utils/piece_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,21 @@ where
let segment_commitment = match maybe_segment_commitment {
Some(segment_commitment) => segment_commitment,
None => {
let segment_commitments = match self
.node_client
.segment_commitments(vec![segment_index])
.await
{
Ok(segment_commitments) => segment_commitments,
Err(error) => {
error!(
%piece_index,
?error,
"Failed tor retrieve segment commitment from node"
);
return None;
}
};
let segment_headers =
match self.node_client.segment_headers(vec![segment_index]).await {
Ok(segment_headers) => segment_headers,
Err(error) => {
error!(
%piece_index,
?error,
"Failed tor retrieve segment headers from node"
);
return None;
}
};

let segment_commitment = match segment_commitments.into_iter().next().flatten()
{
Some(segment_commitment) => segment_commitment,
let segment_commitment = match segment_headers.into_iter().next().flatten() {
Some(segment_header) => segment_header.segment_commitment(),
None => {
error!(
%piece_index,
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-rpc-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use subspace_core_primitives::{
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_networking::libp2p::Multiaddr;

/// Defines a limit for segment indexes array. It affects storage access on the runtime side.
pub const MAX_SEGMENT_INDEXES_PER_REQUEST: usize = 300;
/// Defines a limit for number of segments that can be requested over RPC
pub const MAX_SEGMENT_HEADERS_PER_REQUEST: usize = 300;

/// Information necessary for farmer application
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down

0 comments on commit 981f079

Please sign in to comment.