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 24, 2024
1 parent e9763ff commit 0ea6109
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 207 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
16 changes: 5 additions & 11 deletions cli/polka-storage-provider/server/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use jsonrpsee::server::Server;
use polka_storage_provider_common::rpc::{RpcError, ServerInfo, StorageProviderRpcServer};
use primitives_commitment::commd::compute_unsealed_sector_commitment;
use storagext::{
runtime::{market::events as MarketEvents, ResultType},
types::market::{ClientDealProposal as SxtClientDealProposal, DealProposal as SxtDealProposal},
MarketClientExt,
};
Expand Down Expand Up @@ -110,19 +111,12 @@ impl StorageProviderRpcServer for RpcServerState {
// it just requires some API design
let result = self
.xt_client
.publish_signed_storage_deals(&self.xt_keypair, vec![deal])
.await?;

let published_deals = result
.events
.find::<storagext::runtime::market::events::DealPublished>()
.collect::<Result<Vec<_>, _>>()
.map_err(|err| RpcError::internal_error(err, None))?;
.publish_signed_storage_deals(&self.xt_keypair, vec![deal], ResultType::Finalisation)
.await?
.expect("requested to return submission-result");

// We currently just support a single deal and if there's no published deals,
// an error MUST've happened
debug_assert_eq!(published_deals.len(), 1);

let unsealed_dir = self.unsealed_piece_storage_dir.clone();
let sector_size = self.server_info.seal_proof.sector_size();

Expand Down Expand Up @@ -151,7 +145,7 @@ impl StorageProviderRpcServer for RpcServerState {
tracing::info!("{:?}", comm_d);
});

Ok(published_deals[0].deal_id)
Ok(result.event.deal_id)
}
}

Expand Down
122 changes: 67 additions & 55 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::{market::events as MarketEvents, HashOfPsc, ResultType, SubmissionResult},
types::market::DealProposal as SxtDealProposal,
MarketClientExt, PolkaStorageConfig,
};
Expand All @@ -15,6 +15,7 @@ use subxt::ext::sp_core::{
};
use url::Url;

use super::display_submission_result;
use crate::{missing_keypair_error, operation_takes_a_while, OutputFormat};

#[derive(Debug, Subcommand)]
Expand Down Expand Up @@ -81,6 +82,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 +112,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,26 +124,34 @@ impl MarketCommand {
self,
client: Client,
account_keypair: MultiPairSigner,
result_type: ResultType,
output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Client: MarketClientExt,
{
operation_takes_a_while();

let submission_result = match self {
match self {
MarketCommand::AddBalance { amount } => {
Self::add_balance(client, account_keypair, amount).await?
let opt_result =
Self::add_balance(client, account_keypair, amount, result_type).await?;
display_submission_result::<_>(opt_result, output_format)?;
}
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?
let opt_result =
Self::settle_deal_payments(client, account_keypair, deal_ids, result_type)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
}
MarketCommand::WithdrawBalance { amount } => {
Self::withdraw_balance(client, account_keypair, amount).await?
let opt_result =
Self::withdraw_balance(client, account_keypair, amount, result_type).await?;
display_submission_result::<_>(opt_result, output_format)?;
}
MarketCommand::PublishStorageDeals {
deals,
Expand All @@ -156,51 +166,41 @@ 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?
let opt_result = Self::publish_storage_deals(
client,
account_keypair,
client_keypair,
deals,
result_type,
)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
}
_unsigned => unreachable!("unsigned commands should have been previously handled"),
};

let hash = submission_result.hash;
// 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
.iter()
.flat_map(|event| {
event.map(|details| details.as_root_event::<storagext::runtime::Event>())
})
.filter_map(|event| match event {
Ok(storagext::runtime::Event::Market(e)) => Some(Ok(e)),
Err(err) => Some(Err(err)),
_ => None,
});
for event in submission_results {
let event = event?;
let output = output_format.format(&event)?;
match output_format {
OutputFormat::Plain => println!("[{}] {}", hash, output),
OutputFormat::Json => println!("{}", output),
}
}
Ok(())
}

async fn add_balance<Client>(
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::BalanceAdded>>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client.add_balance(&account_keypair, amount).await?;
tracing::debug!(
"[{}] Successfully added {} to Market Balance",
submission_result.hash,
amount
);
let submission_result = client
.add_balance(&account_keypair, amount, result_type)
.await?;
if submission_result.is_some() {
tracing::debug!(
"[{}] Successfully added {} to Market Balance",
submission_result.as_ref().expect("never none").hash,
amount
);
}

Ok(submission_result)
}
Expand All @@ -210,7 +210,8 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
client_keypair: MultiPairSigner,
deals: Vec<SxtDealProposal>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::DealPublished>>, subxt::Error>
where
Client: MarketClientExt,
{
Expand All @@ -219,12 +220,15 @@ 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
);
if submission_result.is_some() {
tracing::debug!(
"[{}] Successfully published storage deals",
submission_result.as_ref().expect("never none").hash,
);
}

Ok(submission_result)
}
Expand All @@ -233,17 +237,20 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
deal_ids: Vec<u64>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::DealsSettled>>, 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
);
if submission_result.is_some() {
tracing::debug!(
"[{}] Successfully settled deal payments",
submission_result.as_ref().expect("never none").hash
);
}

Ok(submission_result)
}
Expand All @@ -252,16 +259,21 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::BalanceWithdrawn>>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client.withdraw_balance(&account_keypair, amount).await?;
tracing::debug!(
"[{}] Successfully withdrew {} from Market Balance",
submission_result.hash,
amount
);
let submission_result = client
.withdraw_balance(&account_keypair, amount, result_type)
.await?;
if submission_result.is_some() {
tracing::debug!(
"[{}] Successfully withdrew {} from Market Balance",
submission_result.as_ref().expect("never none").hash,
amount
);
}

Ok(submission_result)
}
Expand Down
30 changes: 30 additions & 0 deletions cli/polka-storage/storagext-cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
pub mod market;
pub mod storage_provider;
pub mod system;

use storagext::runtime::{HashOfPsc, SubmissionResult};

use crate::OutputFormat;

pub(crate) fn display_submission_result<Event>(
opt_result: Option<SubmissionResult<HashOfPsc, Event>>,
_output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Event: subxt::events::StaticEvent,
{
if let Some(result) = opt_result {
// TODO(@neutrinoks,24.10.24): Check if we can return as root event instead to enable this
// display possibility again.
// let output = output_format.format(&result.event)?;
// match output_format {
// OutputFormat::Plain => println!("[{}] {}", result.hash, output),
// OutputFormat::Json => println!("{}", output),
// }
println!(
"[{}] {}::{}",
result.hash,
<Event as subxt::events::StaticEvent>::PALLET,
<Event as subxt::events::StaticEvent>::EVENT
);
}

Ok(())
}
Loading

0 comments on commit 0ea6109

Please sign in to comment.