Skip to content

Commit

Permalink
feat(storagext): prove commit
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder committed Jul 29, 2024
1 parent b7f0653 commit b60c384
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/polka-storage/storagext-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cid = { workspace = true, features = ["scale-codec"] }
clap = { workspace = true, features = ["derive"] }
codec.workspace = true
frame-support = { workspace = true, features = ["std"] }
hex.workspace = true
hex = { workspace = true, features = ["std"] }
primitives-proofs = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
52 changes: 45 additions & 7 deletions cli/polka-storage/storagext-cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use std::{path::PathBuf, str::FromStr};

use crate::deser::PreCommitSector;
use crate::deser::{PreCommitSector, ProveCommitSector};
use anyhow::bail;
use clap::Subcommand;
use storagext::{storage_provider::StorageProviderClient, PolkaStorageConfig, RegisteredPoStProof};
use subxt::ext::sp_core::crypto::Ss58Codec;
use url::Url;

fn parse_post_proof(src: &str) -> Result<RegisteredPoStProof, anyhow::Error> {
let post_proof = match src {
"2KiB" => RegisteredPoStProof::StackedDRGWindow2KiBV1P1,
unknown => bail!("Unknown PoSt Proof type: {}", unknown),
};

Ok(post_proof)
}

#[derive(Debug, Clone)]
pub struct PreCommitSectorWrapper(PreCommitSector);

Expand All @@ -25,13 +34,22 @@ impl PreCommitSectorWrapper {
}
}

fn parse_post_proof(src: &str) -> Result<RegisteredPoStProof, anyhow::Error> {
let post_proof = match src {
"2KiB" => RegisteredPoStProof::StackedDRGWindow2KiBV1P1,
unknown => bail!("Unknown PoSt Proof type: {}", unknown),
};
#[derive(Debug, Clone)]
pub struct ProveCommitSectorWrapper(ProveCommitSector);

Ok(post_proof)
impl ProveCommitSectorWrapper {
/// Attempt to parse a command-line argument into [`ProveCommitSector`].
///
/// The command-line argument may be a valid JSON object, or a file path starting with @.
pub(crate) fn parse(src: &str) -> Result<Self, anyhow::Error> {
Ok(Self(if let Some(stripped) = src.strip_prefix('@') {
let path = PathBuf::from_str(stripped)?.canonicalize()?;
let mut file = std::fs::File::open(path)?;
serde_json::from_reader(&mut file)
} else {
serde_json::from_str(src)
}?))
}
}

#[derive(Debug, Subcommand)]
Expand All @@ -58,6 +76,12 @@ pub enum StorageProviderCommand {
#[arg(value_parser = PreCommitSectorWrapper::parse)]
pre_commit_sector: PreCommitSectorWrapper,
},
/// Proves sector that has been previously pre-committed.
/// After proving, a deal in a sector is considered Active.
ProveCommit {
#[arg(value_parser = ProveCommitSectorWrapper::parse)]
prove_commit_sector: ProveCommitSectorWrapper,
},
}

impl StorageProviderCommand {
Expand Down Expand Up @@ -112,6 +136,20 @@ impl StorageProviderCommand {
sector_number
);
}
StorageProviderCommand::ProveCommit {
prove_commit_sector,
} => {
let sector_number = prove_commit_sector.0.sector_number;
let block_hash = client
.prove_commit_sector(&account_keypair, prove_commit_sector.0.into())
.await?;

tracing::info!(
"[{}] Successfully proven sector {}.",
block_hash,
sector_number
);
}
}
Ok(())
}
Expand Down
36 changes: 36 additions & 0 deletions cli/polka-storage/storagext-cli/src/deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ impl Into<storagext::SectorPreCommitInfo> for PreCommitSector {
}
}

/// Deserialize hex-string as `Vec<u8>`.
/// JSON doesn't have 'bytes' format, so we specify that bytes need to be just a hex-string and we provide deserializer for it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct HexBytesWrapper(pub(crate) Vec<u8>);

impl<'de> serde::de::Deserialize<'de> for HexBytesWrapper {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let bytes = hex::decode(s.as_str()).map_err(|e| {
serde::de::Error::custom(format!("failed to parse bytes from hex string: {e:?}"))
})?;
Ok(Self(bytes))
}
}

#[derive(Debug, Clone, serde::Deserialize)]
pub(crate) struct ProveCommitSector {
/// Number of a sector that has been previously pre-committed.
pub sector_number: SectorNumber,
/// Proof bytes as a hex string.
/// If empty it fails validation, it has any bytes it succeeds.
pub proof: HexBytesWrapper,
}

impl Into<storagext::ProveCommitSector> for ProveCommitSector {
fn into(self) -> storagext::ProveCommitSector {
storagext::ProveCommitSector {
sector_number: self.sector_number,
proof: self.proof.0.clone(),
}
}
}

#[cfg(test)]
mod test {
//! These tests basically ensure that the underlying parsers aren't broken without warning.
Expand Down
17 changes: 17 additions & 0 deletions cli/polka-storage/storagext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,20 @@ impl From<SectorPreCommitInfo>
}
}
}

#[derive(CloneNoBound)]
pub struct ProveCommitSector {
pub sector_number: SectorNumber,
pub proof: Vec<u8>,
}

impl From<ProveCommitSector>
for runtime::runtime_types::pallet_storage_provider::sector::ProveCommitSector
{
fn from(value: ProveCommitSector) -> Self {
Self {
sector_number: value.sector_number,
proof: value.proof.into_bounded_byte_vec(),
}
}
}
6 changes: 6 additions & 0 deletions cli/polka-storage/storagext/src/runtime/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ impl IntoBoundedByteVec for String {
bounded_vec::BoundedVec(self.into_bytes())
}
}

impl IntoBoundedByteVec for Vec<u8> {
fn into_bounded_byte_vec(self) -> bounded_vec::BoundedVec<u8> {
bounded_vec::BoundedVec(self)
}
}
26 changes: 25 additions & 1 deletion cli/polka-storage/storagext/src/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use subxt::ext::sp_core::crypto::Ss58Codec;

use crate::{
runtime::{self, bounded_vec::IntoBoundedByteVec},
PolkaStorageConfig, RegisteredPoStProof, SectorPreCommitInfo,
PolkaStorageConfig, ProveCommitSector, RegisteredPoStProof, SectorPreCommitInfo,
};

/// The maximum number of deal IDs supported.
Expand Down Expand Up @@ -70,4 +70,28 @@ impl StorageProviderClient {
.traced_submission(&payload, account_keypair)
.await
}

#[tracing::instrument(
level = "trace",
skip_all,
fields(
address = account_keypair.account_id().to_ss58check(),
)
)]
pub async fn prove_commit_sector<Keypair>(
&self,
account_keypair: &Keypair,
prove_commit_sector: ProveCommitSector,
) -> Result<<PolkaStorageConfig as subxt::Config>::Hash, subxt::Error>
where
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let payload = runtime::tx()
.storage_provider()
.prove_commit_sector(prove_commit_sector.into());

self.client
.traced_submission(&payload, account_keypair)
.await
}
}

0 comments on commit b60c384

Please sign in to comment.