Skip to content

Commit

Permalink
fix(storagext): implement declare faults_recovered
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte committed Aug 15, 2024
1 parent 4c7fed3 commit e89bd64
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 6 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
2 changes: 1 addition & 1 deletion cli/polka-storage/storagext-cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{ArgGroup, Subcommand};
use primitives_proofs::DealId;
use storagext::{market::MarketClient, PolkaStorageConfig};
use storagext::{clients::MarketClient, PolkaStorageConfig};
use subxt::ext::sp_core::{
crypto::Ss58Codec, ecdsa::Pair as ECDSAPair, ed25519::Pair as Ed25519Pair,
sr25519::Pair as Sr25519Pair,
Expand Down
30 changes: 28 additions & 2 deletions cli/polka-storage/storagext-cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::bail;
use clap::Subcommand;
use primitives_proofs::RegisteredPoStProof;
use storagext::{storage_provider::StorageProviderClient, PolkaStorageConfig};
use storagext::{
clients::StorageProviderClient, FaultDeclaration, PolkaStorageConfig, RecoveryDeclaration,
};
use subxt::ext::sp_core::crypto::Ss58Codec;
use url::Url;

Expand Down Expand Up @@ -54,6 +56,18 @@ pub enum StorageProviderCommand {
#[arg(value_parser = <SubmitWindowedPoStParams as ParseablePath>::parse_json)]
windowed_post: SubmitWindowedPoStParams,
},

/// Declare faulty sectors.
DeclareFaults {
#[arg(value_parser = <Vec<FaultDeclaration> as ParseablePath>::parse_json)]
faults: Vec<FaultDeclaration>,
},

/// Declare recovered faulty sectors.
DeclareFaultsRecovered {
#[arg(value_parser = <Vec<RecoveryDeclaration> as ParseablePath>::parse_json)]
recoveries: Vec<RecoveryDeclaration>,
},
}

impl StorageProviderCommand {
Expand Down Expand Up @@ -123,7 +137,19 @@ impl StorageProviderCommand {
.submit_windowed_post(&account_keypair, windowed_post.into())
.await?;

tracing::info!("[{}] Successfully submitted proof.", block_hash,);
tracing::info!("[{}] Successfully submitted proof.", block_hash);
}
StorageProviderCommand::DeclareFaults { faults } => {
let block_hash = client.declare_faults(&account_keypair, faults).await?;

tracing::info!("[{}] Successfully declared faults.", block_hash);
}
StorageProviderCommand::DeclareFaultsRecovered { recoveries } => {
let block_hash = client
.declare_faults_recovered(&account_keypair, recoveries)
.await?;

tracing::info!("[{}] Successfully declared faults.", block_hash);
}
}
Ok(())
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions cli/polka-storage/storagext/src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod market;
mod storage_provider;

pub use market::MarketClient;
pub use storage_provider::StorageProviderClient;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{
self, bounded_vec::IntoBoundedByteVec,
runtime_types::pallet_storage_provider::proofs::SubmitWindowedPoStParams,
},
BlockNumber, PolkaStorageConfig, ProveCommitSector, RegisteredPoStProof, SectorPreCommitInfo,
BlockNumber, FaultDeclaration, PolkaStorageConfig, ProveCommitSector, RecoveryDeclaration,
RegisteredPoStProof, SectorPreCommitInfo,
};

/// The maximum number of deal IDs supported.
Expand Down Expand Up @@ -121,4 +122,52 @@ 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 declare_faults<Keypair>(
&self,
account_keypair: &Keypair,
faults: Vec<FaultDeclaration>,
) -> Result<<PolkaStorageConfig as subxt::Config>::Hash, subxt::Error>
where
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let payload = runtime::tx()
.storage_provider()
.declare_faults(faults.into());

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

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

self.client
.traced_submission(&payload, account_keypair)
.await
}
}
144 changes: 142 additions & 2 deletions cli/polka-storage/storagext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub mod market;
pub mod clients;
pub mod runtime;
pub mod storage_provider;

use std::collections::BTreeSet;

use cid::Cid;
use codec::Encode;
use frame_support::CloneNoBound;
use primitives_proofs::{DealId, RegisteredPoStProof, RegisteredSealProof, SectorNumber};
use runtime::runtime_types::bounded_collections::{bounded_btree_set, bounded_vec};
use subxt::{self, ext::sp_runtime::MultiSignature, tx::Signer, utils::Static};

pub use crate::runtime::{
Expand Down Expand Up @@ -162,3 +164,141 @@ impl From<ProveCommitSector>
}
}
}

#[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
pub struct FaultDeclaration {
pub deadline: u64,
pub partition: u32,
pub sectors: BTreeSet<u64>,
}

impl From<FaultDeclaration>
for runtime::runtime_types::pallet_storage_provider::fault::FaultDeclaration
{
fn from(value: FaultDeclaration) -> Self {
Self {
deadline: value.deadline,
partition: value.partition,
// Converts from BTreeSet -> Vec -> BoundedBTreeSet because subxt...
sectors: bounded_btree_set::BoundedBTreeSet(value.sectors.into_iter().collect()),
}
}
}

impl From<Vec<FaultDeclaration>>
for runtime::runtime_types::pallet_storage_provider::fault::DeclareFaultsParams
{
fn from(value: Vec<FaultDeclaration>) -> Self {
Self {
faults: bounded_vec::BoundedVec(value.into_iter().map(Into::into).collect()),
}
}
}
#[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
pub struct RecoveryDeclaration {
pub deadline: u64,
pub partition: u32,
pub sectors: BTreeSet<u64>,
}

impl From<RecoveryDeclaration>
for runtime::runtime_types::pallet_storage_provider::fault::RecoveryDeclaration
{
fn from(value: RecoveryDeclaration) -> Self {
Self {
deadline: value.deadline,
partition: value.partition,
// Converts from BTreeSet -> Vec -> BoundedBTreeSet because subxt...
sectors: bounded_btree_set::BoundedBTreeSet(value.sectors.into_iter().collect()),
}
}
}

impl From<Vec<RecoveryDeclaration>>
for runtime::runtime_types::pallet_storage_provider::fault::DeclareFaultsRecoveredParams
{
fn from(value: Vec<RecoveryDeclaration>) -> Self {
Self {
recoveries: bounded_vec::BoundedVec(value.into_iter().map(Into::into).collect()),
}
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;

use crate::{FaultDeclaration, RecoveryDeclaration};

#[test]
fn ensure_deserialization_faults() {
let declaration = r#"
{
"deadline": 0,
"partition": 0,
"sectors": [0, 1]
}
"#;
let result: FaultDeclaration = serde_json::from_str(declaration).unwrap();
let expected = FaultDeclaration {
deadline: 0,
partition: 0,
sectors: BTreeSet::from_iter([0, 1].into_iter()),
};
assert_eq!(expected, result);
}

#[test]
fn ensure_deserialization_faults_vec() {
let declaration = r#"
[{
"deadline": 0,
"partition": 0,
"sectors": [0, 1]
}]
"#;
let result: Vec<FaultDeclaration> = serde_json::from_str(declaration).unwrap();
let expected = vec![FaultDeclaration {
deadline: 0,
partition: 0,
sectors: BTreeSet::from_iter([0, 1].into_iter()),
}];
assert_eq!(expected, result);
}

#[test]
fn ensure_deserialization_recoveries() {
let declaration = r#"
{
"deadline": 0,
"partition": 0,
"sectors": [0, 1]
}
"#;
let result: RecoveryDeclaration = serde_json::from_str(declaration).unwrap();
let expected = RecoveryDeclaration {
deadline: 0,
partition: 0,
sectors: BTreeSet::from_iter([0, 1].into_iter()),
};
assert_eq!(expected, result);
}

#[test]
fn ensure_deserialization_recoveries_vec() {
let declaration = r#"
[{
"deadline": 0,
"partition": 0,
"sectors": [0, 1]
}]
"#;
let result: Vec<RecoveryDeclaration> = serde_json::from_str(declaration).unwrap();
let expected = vec![RecoveryDeclaration {
deadline: 0,
partition: 0,
sectors: BTreeSet::from_iter([0, 1].into_iter()),
}];
assert_eq!(expected, result);
}
}

0 comments on commit e89bd64

Please sign in to comment.