Skip to content

Commit

Permalink
feat: add submission result type flag
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed Oct 14, 2024
1 parent 2b15fc6 commit 87897f4
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 71 deletions.
54 changes: 34 additions & 20 deletions cli/polka-storage/storagext-cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use primitives_proofs::DealId;
use storagext::{
deser::DeserializablePath,
multipair::{DebugPair, MultiPairSigner},
runtime::SubmissionResult,
runtime::{ResultType, SubmissionResult},
types::market::DealProposal as SxtDealProposal,
MarketClientExt, PolkaStorageConfig,
};
Expand Down Expand Up @@ -81,6 +81,7 @@ impl MarketCommand {
account_keypair: Option<MultiPairSigner>,
n_retries: u32,
retry_interval: Duration,
result_type: ResultType,
output_format: OutputFormat,
) -> Result<(), anyhow::Error> {
let client = storagext::Client::new(node_rpc, n_retries, retry_interval).await?;
Expand Down Expand Up @@ -110,7 +111,7 @@ impl MarketCommand {
return Err(missing_keypair_error::<Self>().into());
};
else_
.with_keypair(client, account_keypair, output_format)
.with_keypair(client, account_keypair, result_type, output_format)
.await?;
}
};
Expand All @@ -122,6 +123,7 @@ impl MarketCommand {
self,
client: Client,
account_keypair: MultiPairSigner,
result_type: ResultType,
output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Expand All @@ -131,17 +133,17 @@ impl MarketCommand {

let submission_result = match self {
MarketCommand::AddBalance { amount } => {
Self::add_balance(client, account_keypair, amount).await?
Self::add_balance(client, account_keypair, amount, result_type).await?
}
MarketCommand::SettleDealPayments { deal_ids } => {
if deal_ids.is_empty() {
bail!("No deals provided to settle");
}

Self::settle_deal_payments(client, account_keypair, deal_ids).await?
Self::settle_deal_payments(client, account_keypair, deal_ids, result_type).await?
}
MarketCommand::WithdrawBalance { amount } => {
Self::withdraw_balance(client, account_keypair, amount).await?
Self::withdraw_balance(client, account_keypair, amount, result_type).await?
}
MarketCommand::PublishStorageDeals {
deals,
Expand All @@ -156,17 +158,23 @@ impl MarketCommand {
client_ed25519_key.map(DebugPair::into_inner)
)
.expect("client is required to submit at least one key, this should've been handled by clap's ArgGroup");
Self::publish_storage_deals(client, account_keypair, client_keypair, deals).await?
Self::publish_storage_deals(
client,
account_keypair,
client_keypair,
deals,
result_type,
)
.await?
}
_unsigned => unreachable!("unsigned commands should have been previously handled"),
};

let hash = submission_result.hash;
let (hash, events) = submission_result.unwrap_both();
// This monstrosity first converts incoming events into a "generic" (subxt generated) event,
// and then we extract only the Market events. We could probably extract this into a proper
// iterator but the effort to improvement ratio seems low (for 2 pallets at least).
let submission_results = submission_result
.events
let events = events
.iter()
.flat_map(|event| {
event.map(|details| details.as_root_event::<storagext::runtime::Event>())
Expand All @@ -176,7 +184,7 @@ impl MarketCommand {
Err(err) => Some(Err(err)),
_ => None,
});
for event in submission_results {
for event in events {
let event = event?;
let output = output_format.format(&event)?;
match output_format {
Expand All @@ -191,14 +199,17 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client.add_balance(&account_keypair, amount).await?;
let submission_result = client
.add_balance(&account_keypair, amount, result_type)
.await?;
tracing::debug!(
"[{}] Successfully added {} to Market Balance",
submission_result.hash,
submission_result,
amount
);

Expand All @@ -210,6 +221,7 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
client_keypair: MultiPairSigner,
deals: Vec<SxtDealProposal>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: MarketClientExt,
Expand All @@ -219,11 +231,12 @@ impl MarketCommand {
&account_keypair,
&client_keypair,
deals.into_iter().map(Into::into).collect(),
result_type,
)
.await?;
tracing::debug!(
"[{}] Successfully published storage deals",
submission_result.hash
submission_result
);

Ok(submission_result)
Expand All @@ -233,17 +246,15 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
deal_ids: Vec<u64>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client
.settle_deal_payments(&account_keypair, deal_ids)
.settle_deal_payments(&account_keypair, deal_ids, result_type)
.await?;
tracing::debug!(
"[{}] Successfully settled deal payments",
submission_result.hash
);
tracing::debug!("[{}] Successfully settled deal payments", submission_result,);

Ok(submission_result)
}
Expand All @@ -252,14 +263,17 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client.withdraw_balance(&account_keypair, amount).await?;
let submission_result = client
.withdraw_balance(&account_keypair, amount, result_type)
.await?;
tracing::debug!(
"[{}] Successfully withdrew {} from Market Balance",
submission_result.hash,
submission_result,
amount
);

Expand Down
71 changes: 46 additions & 25 deletions cli/polka-storage/storagext-cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use storagext::{
multipair::MultiPairSigner,
runtime::{
runtime_types::pallet_storage_provider::sector::ProveCommitSector as RuntimeProveCommitSector,
SubmissionResult,
ResultType, SubmissionResult,
},
types::storage_provider::{
FaultDeclaration as SxtFaultDeclaration, ProveCommitSector as SxtProveCommitSector,
Expand Down Expand Up @@ -95,6 +95,7 @@ impl StorageProviderCommand {
account_keypair: Option<MultiPairSigner>,
n_retries: u32,
retry_interval: Duration,
result_type: ResultType,
output_format: OutputFormat,
) -> Result<(), anyhow::Error> {
let client = storagext::Client::new(node_rpc, n_retries, retry_interval).await?;
Expand Down Expand Up @@ -123,7 +124,7 @@ impl StorageProviderCommand {
return Err(missing_keypair_error::<Self>().into());
};
else_
.with_keypair(client, account_keypair, output_format)
.with_keypair(client, account_keypair, result_type, output_format)
.await?;
}
};
Expand All @@ -135,6 +136,7 @@ impl StorageProviderCommand {
self,
client: Client,
account_keypair: MultiPairSigner,
result_type: ResultType,
output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Expand All @@ -147,32 +149,43 @@ impl StorageProviderCommand {
peer_id,
post_proof,
} => {
Self::register_storage_provider(client, account_keypair, peer_id, post_proof)
.await?
Self::register_storage_provider(
client,
account_keypair,
peer_id,
post_proof,
result_type,
)
.await?
}
StorageProviderCommand::PreCommit { pre_commit_sectors } => {
Self::pre_commit(client, account_keypair, pre_commit_sectors).await?
Self::pre_commit(client, account_keypair, pre_commit_sectors, result_type).await?
}
StorageProviderCommand::ProveCommit {
prove_commit_sectors,
} => Self::prove_commit(client, account_keypair, prove_commit_sectors).await?,
} => {
Self::prove_commit(client, account_keypair, prove_commit_sectors, result_type)
.await?
}
StorageProviderCommand::SubmitWindowedProofOfSpaceTime { windowed_post } => {
Self::submit_windowed_post(client, account_keypair, windowed_post).await?
Self::submit_windowed_post(client, account_keypair, windowed_post, result_type)
.await?
}
StorageProviderCommand::DeclareFaults { faults } => {
Self::declare_faults(client, account_keypair, faults).await?
Self::declare_faults(client, account_keypair, faults, result_type).await?
}
StorageProviderCommand::DeclareFaultsRecovered { recoveries } => {
Self::declare_faults_recovered(client, account_keypair, recoveries).await?
Self::declare_faults_recovered(client, account_keypair, recoveries, result_type)
.await?
}
_unsigned => unreachable!("unsigned commands should have been previously handled"),
};

let (hash, events) = submission_result.unwrap_both();
// This monstrosity first converts incoming events into a "generic" (subxt generated) event,
// and then we extract only the Market events. We could probably extract this into a proper
// iterator but the effort to improvement ratio seems low (for 2 pallets at least).
let submission_results = submission_result
.events
let events = events
.iter()
.flat_map(|event| {
event.map(|details| details.as_root_event::<storagext::runtime::Event>())
Expand All @@ -182,11 +195,11 @@ impl StorageProviderCommand {
Err(err) => Some(Err(err)),
_ => None,
});
for event in submission_results {
for event in events {
let event = event?;
let output = output_format.format(&event)?;
match output_format {
OutputFormat::Plain => println!("[{}] {}", submission_result.hash, output),
OutputFormat::Plain => println!("[{}] {}", hash, output),
OutputFormat::Json => println!("{}", output),
}
}
Expand All @@ -198,16 +211,17 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
peer_id: String,
post_proof: RegisteredPoStProof,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
{
let submission_result = client
.register_storage_provider(&account_keypair, peer_id.clone(), post_proof)
.register_storage_provider(&account_keypair, peer_id.clone(), post_proof, result_type)
.await?;
tracing::debug!(
"[{}] Successfully registered {}, seal: {:?} in Storage Provider Pallet",
submission_result.hash,
submission_result,
peer_id,
post_proof
);
Expand All @@ -219,6 +233,7 @@ impl StorageProviderCommand {
client: Client,
account_keypair: MultiPairSigner,
pre_commit_sectors: Vec<SxtSectorPreCommitInfo>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
Expand All @@ -230,11 +245,11 @@ impl StorageProviderCommand {
.unzip();

let submission_result = client
.pre_commit_sectors(&account_keypair, pre_commit_sectors)
.pre_commit_sectors(&account_keypair, pre_commit_sectors, result_type)
.await?;
tracing::debug!(
"[{}] Successfully pre-commited sectors {:?}.",
submission_result.hash,
submission_result,
sector_numbers
);

Expand All @@ -245,6 +260,7 @@ impl StorageProviderCommand {
client: Client,
account_keypair: MultiPairSigner,
prove_commit_sectors: Vec<SxtProveCommitSector>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
Expand All @@ -260,11 +276,11 @@ impl StorageProviderCommand {
})
.unzip();
let submission_result = client
.prove_commit_sectors(&account_keypair, prove_commit_sectors)
.prove_commit_sectors(&account_keypair, prove_commit_sectors, result_type)
.await?;
tracing::debug!(
"[{}] Successfully proven sector {:?}.",
submission_result.hash,
submission_result,
sector_numbers
);

Expand All @@ -275,14 +291,15 @@ impl StorageProviderCommand {
client: Client,
account_keypair: MultiPairSigner,
windowed_post: SxtSubmitWindowedPoStParams,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
{
let submission_result = client
.submit_windowed_post(&account_keypair, windowed_post.into())
.submit_windowed_post(&account_keypair, windowed_post.into(), result_type)
.await?;
tracing::debug!("[{}] Successfully submitted proof.", submission_result.hash);
tracing::debug!("[{}] Successfully submitted proof.", submission_result);

Ok(submission_result)
}
Expand All @@ -291,12 +308,15 @@ impl StorageProviderCommand {
client: Client,
account_keypair: MultiPairSigner,
faults: Vec<SxtFaultDeclaration>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
{
let submission_result = client.declare_faults(&account_keypair, faults).await?;
tracing::debug!("[{}] Successfully declared faults.", submission_result.hash);
let submission_result = client
.declare_faults(&account_keypair, faults, result_type)
.await?;
tracing::debug!("[{}] Successfully declared faults.", submission_result);

Ok(submission_result)
}
Expand All @@ -305,14 +325,15 @@ impl StorageProviderCommand {
client: Client,
account_keypair: MultiPairSigner,
recoveries: Vec<SxtRecoveryDeclaration>,
result_type: ResultType,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
where
Client: StorageProviderClientExt,
{
let submission_result = client
.declare_faults_recovered(&account_keypair, recoveries)
.declare_faults_recovered(&account_keypair, recoveries, result_type)
.await?;
tracing::debug!("[{}] Successfully declared faults.", submission_result.hash);
tracing::debug!("[{}] Successfully declared faults.", submission_result);

Ok(submission_result)
}
Expand Down
Loading

0 comments on commit 87897f4

Please sign in to comment.