Skip to content

Commit

Permalink
DO NOT MERGE: Test RPC method for object fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Sep 9, 2024
1 parent dbe8e35 commit 429fdb4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/sc-consensus-subspace-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
async-oneshot = "0.5.9"
futures = "0.3.29"
futures-timer = "3.0.3"
hex = "0.4.3"
jsonrpsee = { version = "0.23.2", features = ["server", "macros"] }
parity-scale-codec = "3.6.12"
parking_lot = "0.12.2"
Expand All @@ -25,6 +26,7 @@ sc-consensus-subspace = { version = "0.1.0", path = "../sc-consensus-subspace" }
sc-rpc = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sc-rpc-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sc-utils = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
serde = "1.0.206"
sp-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-consensus = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
sp-consensus-subspace = { version = "0.1.0", path = "../sp-consensus-subspace" }
Expand All @@ -37,6 +39,7 @@ subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primiti
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-object-fetching = { version = "0.1.0", path = "../subspace-object-fetching" }
subspace-rpc-primitives = { version = "0.1.0", path = "../subspace-rpc-primitives" }
thiserror = "1.0.63"
tracing = "0.1.40"
48 changes: 48 additions & 0 deletions crates/sc-consensus-subspace-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ impl From<Error> for ErrorObjectOwned {
}
}

/// Hex-encoded object data.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Encode,
Decode,
serde::Serialize,
serde::Deserialize,
)]
#[serde(transparent)]
pub struct HexData(#[serde(with = "hex")] pub Vec<u8>);

/// Provides rpc methods for interacting with Subspace.
#[rpc(client, server)]
pub trait SubspaceRpcApi {
Expand Down Expand Up @@ -186,6 +203,12 @@ pub trait SubspaceRpcApi {
item = GlobalObjectMapping,
)]
fn subscribe_filtered_object_mappings(&self, hashes: Vec<Blake3HashHex>);

#[method(name = "subspace_fetchArchivedObject")]
async fn fetch_archived_object(
&self,
mapping: subspace_core_primitives::objects::GlobalObject,
) -> Result<HexData, Error>;
}

#[derive(Default)]
Expand Down Expand Up @@ -249,6 +272,9 @@ where
pub kzg: Kzg,
/// Erasure coding instance
pub erasure_coding: ErasureCoding,
/// DSN object piece getter
pub object_piece_getter:
Arc<dyn subspace_object_fetching::ObjectPieceGetter + Send + Sync + 'static>,
}

/// Implements the [`SubspaceRpcApiServer`] trait for interacting with Subspace.
Expand Down Expand Up @@ -280,6 +306,7 @@ where
erasure_coding: ErasureCoding,
deny_unsafe: DenyUnsafe,
_block: PhantomData<Block>,
object_fetcher: subspace_object_fetching::ObjectFetcher,
}

/// [`SubspaceRpc`] is used for notifying subscribers about arrival of new slots and for
Expand Down Expand Up @@ -338,6 +365,9 @@ where
erasure_coding: config.erasure_coding,
deny_unsafe: config.deny_unsafe,
_block: PhantomData,
object_fetcher: subspace_object_fetching::ObjectFetcher {
piece_getter: config.object_piece_getter,
},
})
}
}
Expand Down Expand Up @@ -930,4 +960,22 @@ where
pipe_from_stream(pending, mapping_stream).boxed(),
);
}

async fn fetch_archived_object(
&self,
mapping: subspace_core_primitives::objects::GlobalObject,
) -> Result<HexData, Error> {
self.deny_unsafe.check_if_safe()?;

let object = self
.object_fetcher
.fetch_object(mapping)
.await
.map_err(|error| {
error!("Failed to fetch object: {}", error);
Error::StringError(format!("Failed to fetch object: {error}"))
})?;

Ok(HexData(object))
}
}
1 change: 1 addition & 0 deletions crates/subspace-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-object-fetching = { version = "0.1.0", path = "../subspace-object-fetching" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }
subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-primitives" }
substrate-frame-rpc-system = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
Expand Down
10 changes: 10 additions & 0 deletions crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,15 @@ where
// We replace the Substrate implementation of metrics server with our own.
config.base.prometheus_config.take();

let object_piece_getter = Arc::new(PieceProvider::new(
node.clone(),
Some(SegmentCommitmentPieceValidator::new(
node.clone(),
subspace_link.kzg().clone(),
segment_headers_store.clone(),
)),
));

let rpc_handlers = task_spawner::spawn_tasks(SpawnTasksParams {
network: network_service.clone(),
client: client.clone(),
Expand Down Expand Up @@ -1231,6 +1240,7 @@ where
kzg: subspace_link.kzg().clone(),
erasure_coding: subspace_link.erasure_coding().clone(),
backend: backend.clone(),
object_piece_getter: object_piece_getter.clone(),
};

rpc::create_full(deps).map_err(Into::into)
Expand Down
5 changes: 5 additions & 0 deletions crates/subspace-service/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ where
pub erasure_coding: ErasureCoding,
/// Backend used by the node.
pub backend: Arc<B>,
/// DSN object piece getter
pub object_piece_getter:
Arc<dyn subspace_object_fetching::ObjectPieceGetter + Send + Sync + 'static>,
}

/// Instantiate all full RPC extensions.
Expand Down Expand Up @@ -127,6 +130,7 @@ where
kzg,
erasure_coding,
backend,
object_piece_getter,
} = deps;

let chain_name = chain_spec.name().to_string();
Expand All @@ -150,6 +154,7 @@ where
kzg,
erasure_coding,
deny_unsafe,
object_piece_getter,
})?
.into_rpc(),
)?;
Expand Down

0 comments on commit 429fdb4

Please sign in to comment.