Skip to content

Commit

Permalink
feat: adapt maat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed Oct 29, 2024
1 parent 66bd5dc commit 504a2ae
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 380 deletions.
47 changes: 12 additions & 35 deletions cli/polka-storage/storagext-cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use primitives_proofs::DealId;
use storagext::{
deser::DeserializablePath,
multipair::{DebugPair, MultiPairSigner},
runtime::{
market::{events as MktEvents, Event as MktEvent},
HashOfPsc, SubmissionResult,
},
runtime::{market::events as MktEvents, HashOfPsc, SubmissionResult},
types::market::DealProposal as SxtDealProposal,
MarketClientExt, PolkaStorageConfig,
};
Expand All @@ -19,26 +16,9 @@ use subxt::ext::sp_core::{
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)
}
)
}
use crate::{
missing_keypair_error, operation_takes_a_while, trace_submission_result, OutputFormat,
};

#[derive(Debug, Subcommand)]
#[command(name = "market", about = "CLI Client to the Market Pallet", version)]
Expand Down Expand Up @@ -166,7 +146,7 @@ impl MarketCommand {
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 @@ -180,13 +160,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 @@ -209,7 +189,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 @@ -222,7 +202,7 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
amount: u128,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::BalanceAdded>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvents::BalanceAdded>>, subxt::Error>
where
Client: MarketClientExt,
{
Expand All @@ -242,7 +222,7 @@ impl MarketCommand {
client_keypair: MultiPairSigner,
deals: Vec<SxtDealProposal>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::DealPublished>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvents::DealPublished>>, subxt::Error>
where
Client: MarketClientExt,
{
Expand All @@ -267,7 +247,7 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
deal_ids: Vec<u64>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::DealsSettled>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvents::DealsSettled>>, subxt::Error>
where
Client: MarketClientExt,
{
Expand All @@ -287,10 +267,7 @@ impl MarketCommand {
account_keypair: MultiPairSigner,
amount: u128,
wait_for_finalization: bool,
) -> Result<
Option<SubmissionResult<HashOfPsc, MktEvent, MktEvents::BalanceWithdrawn>>,
subxt::Error,
>
) -> Result<Option<SubmissionResult<HashOfPsc, MktEvents::BalanceWithdrawn>>, subxt::Error>
where
Client: MarketClientExt,
{
Expand Down
66 changes: 55 additions & 11 deletions cli/polka-storage/storagext-cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,64 @@ use storagext::runtime::{HashOfPsc, SubmissionResult};

use crate::OutputFormat;

pub(crate) fn display_submission_result<Event, Variant>(
opt_result: Option<SubmissionResult<HashOfPsc, Event, Variant>>,
#[macro_export]
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)
}
)
}

pub(crate) fn display_submission_result<Variant: std::fmt::Debug>(
opt_result: Option<SubmissionResult<HashOfPsc, Variant>>,
output_format: OutputFormat,
) -> Result<(), anyhow::Error>
where
Event: scale_decode::DecodeAsType + std::fmt::Display + serde::Serialize,
{
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),
) -> Result<(), anyhow::Error> {
if opt_result.is_none() {
return Ok(());
}
let result = opt_result.expect("expect some, checked before");

match result {
Ok(events) => {
println!("[{}]: {:?}", events[0].hash, events[0].variant);
match &events[0].event {
storagext::runtime::Event::Market(e) => {
display::<_>(events[0].hash, e, output_format)?
}
storagext::runtime::Event::StorageProvider(e) => {
display::<_>(events[0].hash, e, output_format)?
}
_ => return Ok(()),
}
}
Err(msg) => {
println!("Extrinsic failed: {msg}");
}
}

Ok(())
}

fn display<E>(hash: HashOfPsc, event: E, output_format: OutputFormat) -> Result<(), anyhow::Error>
where
E: std::fmt::Display + serde::Serialize,
{
let output = output_format.format(&event)?;
match output_format {
OutputFormat::Plain => println!("[{}] {}", hash, output),
OutputFormat::Json => println!("{}", output),
}
Ok(())
}
63 changes: 18 additions & 45 deletions cli/polka-storage/storagext-cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use storagext::{
multipair::MultiPairSigner,
runtime::{
runtime_types::pallet_storage_provider::sector::ProveCommitSector as RuntimeProveCommitSector,
storage_provider::{events as SpEvents, Event as SpEvent},
HashOfPsc, SubmissionResult,
storage_provider::events as SpEvents, HashOfPsc, SubmissionResult,
},
types::storage_provider::{
FaultDeclaration as SxtFaultDeclaration, ProveCommitSector as SxtProveCommitSector,
Expand All @@ -22,26 +21,9 @@ use storagext::{
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)
}
)
}
use crate::{
missing_keypair_error, operation_takes_a_while, trace_submission_result, OutputFormat,
};

fn parse_post_proof(src: &str) -> Result<RegisteredPoStProof, String> {
match src {
Expand Down Expand Up @@ -192,7 +174,7 @@ impl StorageProviderCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::PreCommit { pre_commit_sectors } => {
let opt_result = Self::pre_commit(
Expand All @@ -202,7 +184,7 @@ impl StorageProviderCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::ProveCommit {
prove_commit_sectors,
Expand All @@ -214,7 +196,7 @@ impl StorageProviderCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::SubmitWindowedProofOfSpaceTime { windowed_post } => {
let opt_result = Self::submit_windowed_post(
Expand All @@ -224,13 +206,13 @@ impl StorageProviderCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::DeclareFaults { faults } => {
let opt_result =
Self::declare_faults(client, account_keypair, faults, wait_for_finalization)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::DeclareFaultsRecovered { recoveries } => {
let opt_result = Self::declare_faults_recovered(
Expand All @@ -240,7 +222,7 @@ impl StorageProviderCommand {
wait_for_finalization,
)
.await?;
display_submission_result::<_, _>(opt_result, output_format)?;
display_submission_result::<_>(opt_result, output_format)?;
}
StorageProviderCommand::TerminateSectors { terminations } => {
let opt_result = Self::terminate_sectors(
Expand All @@ -250,7 +232,7 @@ impl StorageProviderCommand {
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 @@ -265,7 +247,7 @@ impl StorageProviderCommand {
post_proof: RegisteredPoStProof,
wait_for_finalization: bool,
) -> Result<
Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::StorageProviderRegistered>>,
Option<SubmissionResult<HashOfPsc, SpEvents::StorageProviderRegistered>>,
subxt::Error,
>
where
Expand All @@ -292,10 +274,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
pre_commit_sectors: Vec<SxtSectorPreCommitInfo>,
wait_for_finalization: bool,
) -> Result<
Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::SectorsPreCommitted>>,
subxt::Error,
>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::SectorsPreCommitted>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand All @@ -320,7 +299,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
prove_commit_sectors: Vec<SxtProveCommitSector>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::SectorsProven>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::SectorsProven>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand Down Expand Up @@ -353,10 +332,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
windowed_post: SxtSubmitWindowedPoStParams,
wait_for_finalization: bool,
) -> Result<
Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::ValidPoStSubmitted>>,
subxt::Error,
>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::ValidPoStSubmitted>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand All @@ -375,7 +351,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
faults: Vec<SxtFaultDeclaration>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::FaultsDeclared>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::FaultsDeclared>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand All @@ -395,7 +371,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
recoveries: Vec<SxtRecoveryDeclaration>,
wait_for_finalization: bool,
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::FaultsRecovered>>, subxt::Error>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::FaultsRecovered>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand All @@ -415,10 +391,7 @@ impl StorageProviderCommand {
account_keypair: MultiPairSigner,
terminations: Vec<SxtTerminationDeclaration>,
wait_for_finalization: bool,
) -> Result<
Option<SubmissionResult<HashOfPsc, SpEvent, SpEvents::SectorsTerminated>>,
subxt::Error,
>
) -> Result<Option<SubmissionResult<HashOfPsc, SpEvents::SectorsTerminated>>, subxt::Error>
where
Client: StorageProviderClientExt,
{
Expand Down
Loading

0 comments on commit 504a2ae

Please sign in to comment.