Skip to content

Commit

Permalink
feat: enable output-format by return root-event as well
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks authored and jmg-duarte committed Nov 4, 2024
1 parent 819a080 commit 9067b41
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 376 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ rand = { version = "0.8.5", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
rand_xorshift = "0.3"
rocksdb = { version = "0.21" }
scale-decode = { version = "0.13.1", default-features = false }
scale-encode = { version = "0.7.1", default-features = false }
scale-info = { version = "2.11.1", default-features = false }
serde = { version = "1.0.197", default-features = false }
serde-big-array = { version = "0.3.2" }
Expand All @@ -111,9 +113,6 @@ tracing-subscriber = "0.3.18"
url = "2.5.0"
uuid = "1.8.0"

scale-decode = { version = "0.13.1", default-features = false }
scale-encode = { version = "0.7.1", default-features = false }

# Testing
rstest = { version = "0.22.0" }

Expand Down
11 changes: 8 additions & 3 deletions cli/polka-storage-provider/server/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,20 @@ impl StorageProviderRpcServer for RpcServerState {
));
}

let result = self
let submission_result = self
.xt_client
.publish_signed_storage_deals(&self.xt_keypair, vec![deal], true)
.await?
.expect("requested to return submission-result");
let events = if let Ok(events) = submission_result {
events
} else {
return Err(RpcError::internal_error("pallet returned an error", None));
};

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

// We always publish only 1 deal
let deal_id = published_deals[0].deal_id;
Expand Down Expand Up @@ -145,7 +150,7 @@ impl StorageProviderRpcServer for RpcServerState {
tracing::info!("{:?}", precommit_result);
});

Ok(result.event[0].deal_id)
Ok(events[0].variant.deal_id)
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/polka-storage/storagext-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ codec.workspace = true
frame-support = { workspace = true, features = ["std"] }
hex = { workspace = true, features = ["serde", "std"] }
primitives-proofs = { workspace = true }
scale-decode.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
subxt = { workspace = true, features = ["jsonrpsee", "substrate-compat"] }
Expand Down
109 changes: 59 additions & 50 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,10 @@ use primitives_proofs::DealId;
use storagext::{
deser::DeserializablePath,
multipair::{DebugPair, MultiPairSigner},
runtime::{market::events as MarketEvents, HashOfPsc, SubmissionResult},
runtime::{
market::{events as MktEvents, Event as MktEvent},
HashOfPsc, SubmissionResult,
},
types::market::DealProposal as SxtDealProposal,
MarketClientExt, PolkaStorageConfig,
};
Expand All @@ -18,6 +21,25 @@ use url::Url;
use super::display_submission_result;
use crate::{missing_keypair_error, operation_takes_a_while, OutputFormat};

macro_rules! trace_submission_result {
($submission_result:expr, $format:expr $(,$par1:expr)*) => (
if let Some(result) = $submission_result {
if let Ok(events) = result {
tracing::debug!(
$format,
events[0].hash,
$($par1),*
);
Ok(Some(Ok(events)))
} else {
Ok(Some(result))
}
} else {
Ok(None)
}
)
}

#[derive(Debug, Subcommand)]
#[command(name = "market", about = "CLI Client to the Market Pallet", version)]
pub(crate) enum MarketCommand {
Expand Down Expand Up @@ -135,14 +157,16 @@ impl MarketCommand {
where
Client: MarketClientExt,
{
operation_takes_a_while();
if wait_for_finalization {
operation_takes_a_while();
}

match self {
MarketCommand::AddBalance { amount } => {
let opt_result =
Self::add_balance(client, account_keypair, amount, wait_for_finalization)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
display_submission_result::<_, _>(opt_result, output_format)?;
}
MarketCommand::SettleDealPayments { deal_ids } => {
if deal_ids.is_empty() {
Expand All @@ -156,13 +180,13 @@ impl MarketCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
display_submission_result::<_, _>(opt_result, output_format)?;
}
MarketCommand::WithdrawBalance { amount } => {
let opt_result =
Self::withdraw_balance(client, account_keypair, amount, wait_for_finalization)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
display_submission_result::<_, _>(opt_result, output_format)?;
}
MarketCommand::PublishStorageDeals {
deals,
Expand All @@ -185,7 +209,7 @@ impl MarketCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_>(opt_result, output_format)?;
display_submission_result::<_, _>(opt_result, output_format)?;
}
_unsigned => unreachable!("unsigned commands should have been previously handled"),
};
Expand All @@ -198,23 +222,18 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
amount: u128,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::BalanceAdded>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::BalanceAdded>>, subxt::Error>
where
Client: MarketClientExt,
{
let submission_result = client
.add_balance(&account_keypair, amount, wait_for_finalization)
.await?;
if let Some(result) = submission_result {
tracing::debug!(
"[{}] Successfully added {} to Market Balance",
result.hash[0],
amount
);
Ok(Some(result))
} else {
Ok(None)
}
trace_submission_result!(
submission_result,
"[{}] Successfully added {} to Market Balance",
amount
)
}

async fn publish_storage_deals<Client>(
Expand All @@ -223,10 +242,11 @@ impl MarketCommand {
client_keypair: MultiPairSigner,
deals: Vec<SxtDealProposal>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::DealPublished>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::DealPublished>>, subxt::Error>
where
Client: MarketClientExt,
{
let n_deals = deals.len();
let submission_result = client
.publish_storage_deals(
&account_keypair,
Expand All @@ -235,63 +255,52 @@ impl MarketCommand {
wait_for_finalization,
)
.await?;
if let Some(result) = submission_result {
tracing::debug!(
"[{}] Successfully published {} storage deals",
result.hash[0],
result.len()
);
Ok(Some(result))
} else {
Ok(None)
}
trace_submission_result!(
submission_result,
"[{}] Successfully published {} storage deals",
n_deals
)
}

async fn settle_deal_payments<Client>(
client: Client,
account_keypair: MultiPairSigner,
deal_ids: Vec<u64>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::DealsSettled>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::DealsSettled>>, subxt::Error>
where
Client: MarketClientExt,
{
let n_deal_ids = deal_ids.len();
let submission_result = client
.settle_deal_payments(&account_keypair, deal_ids, wait_for_finalization)
.await?;
if let Some(result) = submission_result {
tracing::debug!(
"[{}] Successfully settled {} deal payments",
result.hash[0],
result.len()
);
Ok(Some(result))
} else {
Ok(None)
}
trace_submission_result!(
submission_result,
"[{}] Successfully settled {} deal payments",
n_deal_ids
)
}

async fn withdraw_balance<Client>(
client: Client,
account_keypair: MultiPairSigner,
amount: u128,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MarketEvents::BalanceWithdrawn>>, subxt::Error>
) -> Result<
Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::BalanceWithdrawn>>,
subxt::Error,
>
where
Client: MarketClientExt,
{
let submission_result = client
.withdraw_balance(&account_keypair, amount, wait_for_finalization)
.await?;
if let Some(result) = submission_result {
tracing::debug!(
"[{}] Successfully withdrew {} from Market Balance",
result.hash[0],
amount
);
Ok(Some(result))
} else {
Ok(None)
}
trace_submission_result!(
submission_result,
"[{}] Successfully withdrew {} from Market Balance",
amount
)
}
}
28 changes: 10 additions & 18 deletions cli/polka-storage/storagext-cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@ use storagext::runtime::{HashOfPsc, SubmissionResult};

use crate::OutputFormat;

pub(crate) fn display_submission_result<Event>(
opt_result: Option<SubmissionResult<HashOfPsc, Event>>,
_output_format: OutputFormat,
pub(crate) fn display_submission_result<Event, Variant>(
opt_result: Option<SubmissionResult<HashOfPsc, Event, Variant>>,
output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Event: subxt::events::StaticEvent,
Event: scale_decode::DecodeAsType + std::fmt::Display + serde::Serialize,
{
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[0],
<Event as subxt::events::StaticEvent>::PALLET,
<Event as subxt::events::StaticEvent>::EVENT
);
if let Some(Ok(events)) = opt_result {
let output = output_format.format(&events[0].event)?;
match output_format {
OutputFormat::Plain => println!("[{}] {}", events[0].hash, output),
OutputFormat::Json => println!("{}", output),
}
}

Ok(())
Expand Down
Loading

0 comments on commit 9067b41

Please sign in to comment.