Skip to content

Commit

Permalink
Consensus changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulksnv committed Aug 31, 2023
1 parent fdea1b9 commit e6e31e0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sc-subspace-block-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ sc-network = { version = "0.10.0-dev", git = "https://github.com/subspace/substr
sc-network-common = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
sc-network-sync = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71", default-features = false }
sp-api = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
sp-consensus-subspace = { version = "0.1.0", path = "../sp-consensus-subspace" }
sc-transaction-pool-api = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
sp-runtime = { version = "24.0.0", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
thiserror = "1.0.38"
Expand Down
38 changes: 30 additions & 8 deletions crates/sc-subspace-block-relay/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use crate::protocol::compact_block::{CompactBlockClient, CompactBlockServer};
use crate::utils::{NetworkPeerHandle, NetworkWrapper, RequestResponseErr};
use crate::{ProtocolBackend, ProtocolClient, ProtocolServer, RelayError, LOG_TARGET};
use crate::{
ProtocolBackend, ProtocolClient, ProtocolServer, ProtocolUnitInfo, RelayError, LOG_TARGET,
};
use async_trait::async_trait;
use codec::{Compact, CompactLen, Decode, Encode};
use futures::channel::oneshot;
Expand All @@ -21,6 +23,8 @@ use sc_network_sync::block_relay_protocol::{
};
use sc_service::SpawnTaskHandle;
use sc_transaction_pool_api::{InPoolTransaction, TransactionPool, TxHash};
use sp_api::ProvideRuntimeApi;
use sp_consensus_subspace::{FarmerPublicKey, SubspaceApi};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor, One, Zero};
use sp_runtime::Justifications;
Expand All @@ -45,6 +49,10 @@ const MAX_RESPONSE_SIZE: NonZeroUsize = NonZeroUsize::new(8 * 1024 * 1024).expec
/// Maximum blocks in the response.
const MAX_RESPONSE_BLOCKS: NonZeroU32 = NonZeroU32::new(128).expect("Not zero; qed");

/// If the encoded size of the extrinsic is less than the threshold,
/// return the full extrinsic along with the tx hash.
const TX_SIZE_THRESHOLD: NonZeroUsize = NonZeroUsize::new(32).expect("Not zero; qed");

/// Initial request for a single block.
#[derive(Encode, Decode)]
struct InitialRequest<Block: BlockT, ProtocolRequest> {
Expand Down Expand Up @@ -603,7 +611,8 @@ struct ConsensusBackend<Block: BlockT, Client, Pool: TransactionPool> {
impl<Block, Client, Pool> ConsensusBackend<Block, Client, Pool>
where
Block: BlockT,
Client: HeaderBackend<Block> + BlockBackend<Block>,
Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,
Client::Api: SubspaceApi<Block, FarmerPublicKey>,
Pool: TransactionPool<Block = Block> + 'static,
{
fn new(
Expand Down Expand Up @@ -645,17 +654,29 @@ impl<Block, Client, Pool> ProtocolBackend<BlockHash<Block>, TxHash<Pool>, Extrin
for ConsensusBackend<Block, Client, Pool>
where
Block: BlockT,
Client: HeaderBackend<Block> + BlockBackend<Block>,
Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,
Client::Api: SubspaceApi<Block, FarmerPublicKey>,
Pool: TransactionPool<Block = Block> + 'static,
{
fn download_unit_members(
&self,
block_hash: &BlockHash<Block>,
) -> Result<Vec<(TxHash<Pool>, Extrinsic<Block>)>, RelayError> {
let tx = block_transactions(block_hash, self.client.as_ref())?;
Ok(tx
) -> Result<Vec<ProtocolUnitInfo<TxHash<Pool>, Extrinsic<Block>>>, RelayError> {
let txns = block_transactions(block_hash, self.client.as_ref())?;
Ok(txns
.into_iter()
.map(|extrinsic| (self.transaction_pool.hash_of(&extrinsic), extrinsic))
.map(|extrinsic| {
let send_tx = extrinsic.encoded_size() <= TX_SIZE_THRESHOLD.get()
|| self
.client
.runtime_api()
.is_inherent(*block_hash, &extrinsic)
.unwrap_or(false);
ProtocolUnitInfo {
id: self.transaction_pool.hash_of(&extrinsic),
unit: if send_tx { Some(extrinsic) } else { None },
}
})
.collect())
}

Expand Down Expand Up @@ -728,7 +749,8 @@ pub fn build_consensus_relay<Block, Client, Pool>(
) -> BlockRelayParams<Block>
where
Block: BlockT,
Client: HeaderBackend<Block> + BlockBackend<Block> + 'static,
Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block> + 'static,
Client::Api: SubspaceApi<Block, FarmerPublicKey>,
Pool: TransactionPool<Block = Block> + 'static,
{
let (tx, request_receiver) = async_channel::bounded(NUM_PEER_HINT.get());
Expand Down
21 changes: 19 additions & 2 deletions crates/sc-subspace-block-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ pub(crate) trait ProtocolServer<DownloadUnitId> {

/// The relay user specific backend interface
pub(crate) trait ProtocolBackend<DownloadUnitId, ProtocolUnitId, ProtocolUnit> {
/// Returns all the protocol units for the given download unit
/// Returns the protocol units for the given download unit, to be returned
/// with the initial response. Some of the items may have the full entry
/// along with the Id (e.g) consensus may choose to return the full
/// transaction for inherents/small transactions in the block. And return
/// only the Tx hash for the remaining extrinsics. Further protocol
/// handshake would be used only for resolving these remaining items.
fn download_unit_members(
&self,
id: &DownloadUnitId,
) -> Result<Vec<(ProtocolUnitId, ProtocolUnit)>, RelayError>;
) -> Result<Vec<ProtocolUnitInfo<ProtocolUnitId, ProtocolUnit>>, RelayError>;

/// Returns the protocol unit for the given download/protocol unit
fn protocol_unit(
Expand All @@ -119,3 +124,15 @@ pub(crate) trait ProtocolBackend<DownloadUnitId, ProtocolUnitId, ProtocolUnit> {
protocol_unit_id: &ProtocolUnitId,
) -> Result<Option<ProtocolUnit>, RelayError>;
}

/// The protocol unit info carried in the initial response
#[derive(Encode, Decode)]
struct ProtocolUnitInfo<ProtocolUnitId, ProtocolUnit> {
/// The protocol unit Id
id: ProtocolUnitId,

/// The server can optionally return the protocol unit
/// as part of the initial response. No further
/// action is needed on client side to resolve it
unit: Option<ProtocolUnit>,
}
41 changes: 6 additions & 35 deletions crates/sc-subspace-block-relay/src/protocol/compact_block.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
//! Compact block implementation.

use crate::utils::NetworkPeerHandle;
use crate::{ProtocolBackend, ProtocolClient, ProtocolServer, RelayError, Resolved, LOG_TARGET};
use crate::{
ProtocolBackend, ProtocolClient, ProtocolServer, ProtocolUnitInfo, RelayError, Resolved,
LOG_TARGET,
};
use async_trait::async_trait;
use codec::{Decode, Encode};
use std::collections::BTreeMap;
use std::num::NonZeroUsize;
use std::sync::Arc;
use tracing::{trace, warn};

/// If the encoded size of the protocol unit is less than the threshold,
/// return the full protocol unit along with the protocol unit Id in the
/// compact response. This catches the common cases like inherents with
/// no segment headers. Since inherents are not gossiped, this causes
/// a local miss/extra round trip. This threshold based scheme could be
/// replaced by using the is_inherent() API if needed
const PROTOCOL_UNIT_SIZE_THRESHOLD: NonZeroUsize = NonZeroUsize::new(32).expect("Not zero; qed");

/// Request messages
#[derive(Encode, Decode)]
pub(crate) enum CompactBlockRequest<DownloadUnitId, ProtocolUnitId> {
Expand All @@ -37,18 +31,6 @@ pub(crate) enum CompactBlockResponse<DownloadUnitId, ProtocolUnitId, ProtocolUni
MissingEntries(MissingEntriesResponse<ProtocolUnit>),
}

/// The protocol unit info carried in the compact response
#[derive(Encode, Decode)]
struct ProtocolUnitInfo<ProtocolUnitId, ProtocolUnit> {
/// The protocol unit Id
id: ProtocolUnitId,

/// The server can optionally return the protocol unit
/// as part of the initial response. No further
/// action is needed on client side to resolve it
unit: Option<ProtocolUnit>,
}

/// The compact response
#[derive(Encode, Decode)]
pub(crate) struct InitialResponse<DownloadUnitId, ProtocolUnitId, ProtocolUnit> {
Expand Down Expand Up @@ -288,21 +270,10 @@ where
return Err(RelayError::UnexpectedInitialRequest);
}

// Return the hash of the members in the download unit.
let members = self.backend.download_unit_members(download_unit_id)?;
// Return the info of the members in the download unit.
let response = InitialResponse {
download_unit_id: download_unit_id.clone(),
protocol_units: members
.into_iter()
.map(|(id, unit)| {
let unit = if unit.encoded_size() <= PROTOCOL_UNIT_SIZE_THRESHOLD.get() {
Some(unit)
} else {
None
};
ProtocolUnitInfo { id, unit }
})
.collect(),
protocol_units: self.backend.download_unit_members(download_unit_id)?,
};
Ok(CompactBlockResponse::Initial(response))
}
Expand Down

0 comments on commit e6e31e0

Please sign in to comment.