Skip to content

Commit

Permalink
feat(storagext): Add terminate sectors extrinsic (#465)
Browse files Browse the repository at this point in the history
Co-authored-by: José Duarte <[email protected]>
  • Loading branch information
aidan46 and jmg-duarte authored Oct 23, 2024
1 parent 31108c1 commit e9763ff
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
29 changes: 29 additions & 0 deletions cli/polka-storage/storagext-cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use storagext::{
RecoveryDeclaration as SxtRecoveryDeclaration,
SectorPreCommitInfo as SxtSectorPreCommitInfo,
SubmitWindowedPoStParams as SxtSubmitWindowedPoStParams,
TerminationDeclaration as SxtTerminationDeclaration,
},
PolkaStorageConfig, StorageProviderClientExt,
};
Expand Down Expand Up @@ -82,6 +83,12 @@ pub enum StorageProviderCommand {
#[arg(value_parser = <Vec<SxtRecoveryDeclaration> as DeserializablePath>::deserialize_json)]
recoveries: std::vec::Vec<SxtRecoveryDeclaration>,
},

/// Terminate sectors.
TerminateSectors {
#[arg(value_parser = <Vec<SxtTerminationDeclaration> as DeserializablePath>::deserialize_json)]
terminations: std::vec::Vec<SxtTerminationDeclaration>,
},
}

impl StorageProviderCommand {
Expand Down Expand Up @@ -165,6 +172,9 @@ impl StorageProviderCommand {
StorageProviderCommand::DeclareFaultsRecovered { recoveries } => {
Self::declare_faults_recovered(client, account_keypair, recoveries).await?
}
StorageProviderCommand::TerminateSectors { terminations } => {
Self::terminate_sectors(client, account_keypair, terminations).await?
}
_unsigned => unreachable!("unsigned commands should have been previously handled"),
};

Expand Down Expand Up @@ -316,4 +326,23 @@ impl StorageProviderCommand {

Ok(submission_result)
}

async fn terminate_sectors<Client>(
client: Client,
account_keypair: MultiPairSigner,
terminations: Vec<SxtTerminationDeclaration>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
{
let submission_result = client
.terminate_sectors(&account_keypair, terminations)
.await?;
tracing::debug!(
"[{}] Successfully terminated sectors.",
submission_result.hash
);

Ok(submission_result)
}
}
28 changes: 27 additions & 1 deletion cli/polka-storage/storagext/src/clients/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use crate::{
},
storage_provider::calls::types::register_storage_provider::PeerId,
},
types::storage_provider::{FaultDeclaration, RecoveryDeclaration, SectorPreCommitInfo},
types::storage_provider::{
FaultDeclaration, RecoveryDeclaration, SectorPreCommitInfo, TerminationDeclaration,
},
BlockNumber, Currency, PolkaStorageConfig,
};
pub trait StorageProviderClientExt {
Expand Down Expand Up @@ -71,6 +73,14 @@ pub trait StorageProviderClientExt {
where
Keypair: subxt::tx::Signer<PolkaStorageConfig>;

fn terminate_sectors<Keypair>(
&self,
account_keypair: &Keypair,
terminations: Vec<TerminationDeclaration>,
) -> impl Future<Output = Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>>
where
Keypair: subxt::tx::Signer<PolkaStorageConfig>;

fn retrieve_storage_provider(
&self,
account_id: &AccountId32,
Expand Down Expand Up @@ -217,6 +227,22 @@ impl StorageProviderClientExt for crate::runtime::client::Client {
self.traced_submission(&payload, account_keypair).await
}

#[tracing::instrument(level = "debug", skip_all)]
async fn terminate_sectors<Keypair>(
&self,
account_keypair: &Keypair,
terminations: Vec<TerminationDeclaration>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let payload = runtime::tx()
.storage_provider()
.terminate_sectors(terminations.into());

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

#[tracing::instrument(level = "debug", skip_all)]
async fn retrieve_storage_provider(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ impl std::fmt::Display for Event {
)
.collect::<String>()
)),
Event::SectorsTerminated {
owner,
terminations,
} => f.write_fmt(format_args!(
"Sectors terminated: {{ owner: {}, terminations: [{}]",
owner,
itertools::Itertools::intersperse(
terminations
.0
.iter()
.map(|termination| format!("{termination:?}")),
", ".to_string()
)
.collect::<String>()
)),
}
}
}
4 changes: 4 additions & 0 deletions cli/polka-storage/storagext/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub mod display;
path = "pallet_storage_provider::fault::RecoveryDeclaration",
derive = "::serde::Serialize"
),
derive_for_type(
path = "pallet_storage_provider::sector::TerminationDeclaration",
derive = "::serde::Serialize"
),
derive_for_type(
path = "pallet_market::pallet::ActiveDealState",
derive = "::serde::Serialize"
Expand Down
49 changes: 49 additions & 0 deletions cli/polka-storage/storagext/src/types/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::{
sector::{
ProveCommitSector as RuntimeProveCommitSector,
SectorPreCommitInfo as RuntimeSectorPreCommitInfo,
TerminateSectorsParams as RuntimeTerminateSectorsParams,
TerminationDeclaration as RuntimeTerminationDeclaration,
},
},
},
Expand Down Expand Up @@ -273,6 +275,32 @@ impl Into<RuntimeSubmitWindowedPoStParams> for SubmitWindowedPoStParams {
}
}

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

impl From<TerminationDeclaration> for RuntimeTerminationDeclaration {
fn from(value: TerminationDeclaration) -> 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<TerminationDeclaration>> for RuntimeTerminateSectorsParams {
fn from(value: Vec<TerminationDeclaration>) -> Self {
Self {
terminations: bounded_vec::BoundedVec(value.into_iter().map(Into::into).collect()),
}
}
}

#[cfg(test)]
mod tests {
use std::{collections::BTreeSet, str::FromStr};
Expand All @@ -286,6 +314,7 @@ mod tests {
market::DealProposal,
storage_provider::{
FaultDeclaration, PoStProof, RecoveryDeclaration, SubmitWindowedPoStParams,
TerminationDeclaration,
},
},
PolkaStorageConfig,
Expand Down Expand Up @@ -448,4 +477,24 @@ mod tests {
}
);
}

#[test]
fn ensure_serde_for_termination_declaration() {
let termination = serde_json::from_str::<Vec<TerminationDeclaration>>(
r#"[{
"deadline": 69,
"partition": 420,
"sectors": [1, 2]
}]"#,
)
.unwrap();
assert_eq!(
termination,
vec![TerminationDeclaration {
deadline: 69,
partition: 420,
sectors: BTreeSet::from([1, 2]),
}]
)
}
}
21 changes: 20 additions & 1 deletion docs/src/pallets/storage-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,26 @@ Where the termination declarations contain:

#### <a class="header" id="terminate_sectors.example" href="#terminate_sectors.example">Example</a>

TODO(@aidan46, #463, 2024/10/18): Add storagext example after implementation
Storage provider `//Alice` terminating sectors[^terminate_sectors] on deadline 0, partition 0, sector 1.


```bash
storagext-cli --sr25519-key "//Alice" storage-provider terminate-sectors @terminate-sectors.json
```

Where `terminate-sectors.json` is a file with contents similar to:

```json
[
{
"deadline": 0,
"partition": 0,
"sectors": [1]
}
]
```

[^terminate_sectors]: Read more about the `terminate-sectors` command in [_Storagext CLI/Subcommand `storage-provider`/`terminate-sectors`_](../storagext-cli/storage-provider.md#terminate-sectors)

## Events

Expand Down

0 comments on commit e9763ff

Please sign in to comment.