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 23, 2024
1 parent e9763ff commit 50fbf67
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 172 deletions.
22 changes: 11 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 @@ -108,21 +109,20 @@ impl StorageProviderRpcServer for RpcServerState {
// however, due to https://github.com/paritytech/subxt/issues/1668 it may wrongly fail.
// Fixing this requires the xt_client not wait for the finalization, it's not hard to do
// it just requires some API design
let result = self
let event: MarketEvents::DealPublished = self
.xt_client
.publish_signed_storage_deals(&self.xt_keypair, vec![deal])
.await?;
.publish_signed_storage_deals(&self.xt_keypair, vec![deal], ResultType::Event)
.await?
.unwrap_event();

let published_deals = result
.events
.find::<storagext::runtime::market::events::DealPublished>()
.collect::<Result<Vec<_>, _>>()
.map_err(|err| RpcError::internal_error(err, None))?;
// let published_deals = result
// .unwrap_events()
// .find::<storagext::runtime::market::events::DealPublished>()
// .collect::<Result<Vec<_>, _>>()
// .map_err(|err| RpcError::internal_error(err, None))?;

// 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 +151,7 @@ impl StorageProviderRpcServer for RpcServerState {
tracing::info!("{:?}", comm_d);
});

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

Expand Down
95 changes: 50 additions & 45 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 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,26 +123,38 @@ impl MarketCommand {
self,
client: Client,
account_keypair: MultiPairSigner,
output_format: OutputFormat,
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 (hash, event) = Self::add_balance(client, account_keypair, amount, result_type)
.await?
.unwrap_both();
println!("{hash:?}: {event:?}");
}
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 (hash, event) =
Self::settle_deal_payments(client, account_keypair, deal_ids, result_type)
.await?
.unwrap_both();
println!("{hash:?}: {event:?}");
}
MarketCommand::WithdrawBalance { amount } => {
Self::withdraw_balance(client, account_keypair, amount).await?
let (hash, event) =
Self::withdraw_balance(client, account_keypair, amount, result_type)
.await?
.unwrap_both();
println!("{hash:?}: {event:?}");
}
MarketCommand::PublishStorageDeals {
deals,
Expand All @@ -156,49 +169,38 @@ 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 (hash, event) = Self::publish_storage_deals(
client,
account_keypair,
client_keypair,
deals,
result_type,
)
.await?
.unwrap_both();
println!("{hash:?}: {event:?}");
}
_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<SubmissionResult<HashOfPsc, MarketEvents::BalanceAdded>, 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,7 +212,8 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
client_keypair: MultiPairSigner,
deals: Vec<SxtDealProposal>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<SubmissionResult<HashOfPsc, MarketEvents::DealPublished>, subxt::Error>
where
Client: MarketClientExt,
{
Expand All @@ -219,11 +222,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 +237,15 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
deal_ids: Vec<u64>,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<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
);
tracing::debug!("[{}] Successfully settled deal payments", submission_result,);

Ok(submission_result)
}
Expand All @@ -252,14 +254,17 @@ impl MarketCommand {
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
) -> Result<SubmissionResult<PolkaStorageConfig>, subxt::Error>
result_type: ResultType,
) -> Result<SubmissionResult<HashOfPsc, MarketEvents::BalanceWithdrawn>, 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
Loading

0 comments on commit 50fbf67

Please sign in to comment.