Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(market): enable deal state retrieval #609

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/rpc_publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ target/release/storagext-cli --sr25519-key "$PROVIDER" market add-balance 250000
wait

# Register the SP
target/release/storagext-cli --sr25519-key "//Charlie" storage-provider register "peer_id"
target/release/storagext-cli --sr25519-key "$PROVIDER" storage-provider register "peer_id"

# Set PoRep verifying key
target/release/storagext-cli --sr25519-key "$PROVIDER" proofs set-porep-verifying-key @2KiB.porep.vk.scale

DEAL_JSON=$(
jq -n \
Expand Down
17 changes: 15 additions & 2 deletions storagext/cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub(crate) enum MarketCommand {
/// The target account's ID.
account_id: <PolkaStorageConfig as subxt::Config>::AccountId,
},

/// Retrieve the deal for a given deal ID.
RetrieveDeal {
/// The target deal's ID.
deal_id: DealId,
},
}

impl MarketCommand {
Expand All @@ -97,8 +103,6 @@ impl MarketCommand {
let client = storagext::Client::new(node_rpc, n_retries, retry_interval).await?;

match self {
// Only command that doesn't need a key.
//
// NOTE: subcommand_negates_reqs does not work for this since it only negates the parents'
// requirements, and the global arguments (keys) are at the grandparent level
// https://users.rust-lang.org/t/clap-ignore-global-argument-in-sub-command/101701/8
Expand All @@ -116,6 +120,15 @@ impl MarketCommand {
tracing::error!("Could not find account {}", account_id);
}
}
MarketCommand::RetrieveDeal { deal_id } => {
if let Some(deal) = client.retrieve_deal(deal_id.clone()).await? {
tracing::debug!("Deal {:?}", deal);

println!("{}", output_format.format(&deal)?);
} else {
tracing::error!("Could not find deal {}", deal_id);
}
}
else_ => {
let Some(account_keypair) = account_keypair else {
return Err(missing_keypair_error::<Self>().into());
Expand Down
27 changes: 27 additions & 0 deletions storagext/lib/src/clients/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ pub trait MarketClientExt {
&self,
account_id: <PolkaStorageConfig as subxt::Config>::AccountId,
) -> impl Future<Output = Result<Option<BalanceEntry<u128>>, subxt::Error>>;

/// Retrieve the deal for a given deal ID.
fn retrieve_deal(
&self,
deal_id: DealId,
) -> impl Future<Output = Result<Option<DealProposal>, subxt::Error>>;
}

impl MarketClientExt for crate::runtime::client::Client {
Expand Down Expand Up @@ -279,4 +285,25 @@ impl MarketClientExt for crate::runtime::client::Client {
.fetch(&balance_table_query)
.await
}

#[tracing::instrument(level = "debug", skip_all, fields(deal_id))]
async fn retrieve_deal(&self, deal_id: DealId) -> Result<Option<DealProposal>, subxt::Error> {
let deal_table_query = runtime::storage().market().proposals(deal_id);
let Some(deal) = self
.client
.storage()
.at_latest()
.await?
.fetch(&deal_table_query)
.await?
else {
return Ok(None);
};

let deal = DealProposal::try_from(deal).map_err(|e| {
subxt::Error::Other(format!("failed to convert deal proposal: {:?}", e))
})?;

Ok(Some(deal))
}
}
31 changes: 26 additions & 5 deletions storagext/lib/src/runtime/display/market.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
use crate::runtime::{
market::Event,
runtime_types::{
pallet_market::{pallet, pallet::BalanceEntry},
polka_storage_runtime::Runtime,
use crate::{
runtime::{
market::Event,
runtime_types::{
pallet_market::pallet::{self, BalanceEntry, DealState},
polka_storage_runtime::Runtime,
},
},
types::market::DealProposal,
};

impl std::fmt::Display for DealState<u64> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DealState::Published => f.write_str("Published"),
DealState::Active(state) => f.write_fmt(format_args!("Active({{ sector_number: {}, sector_start_block: {}, last_updated_block: {:?}, slash_block: {:?} }})", state.sector_number, state.sector_start_block, state.last_updated_block, state.slash_block)),
}
}
}

impl std::fmt::Display for DealProposal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Deal Proposal {{ piece_cid: {}, piece_size: {}, provider: {}, client: {}, label: {}, start_block: {}, end_block: {}, storage_price_per_block: {}, provider_collateral: {}, state: {} }}",
self.piece_cid, self.piece_size, self.provider, self.client, self.label, self.start_block, self.end_block, self.storage_price_per_block, self.provider_collateral, self.state
))
}
}

impl std::fmt::Display for pallet::SettledDealData<Runtime> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
Expand Down