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 5 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
17 changes: 0 additions & 17 deletions .dockerignore

This file was deleted.

20 changes: 19 additions & 1 deletion docs/src/storagext-cli/market.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,28 @@ The `retrieve-balance` command checks the balance of a given market account.
| ------------ | ------------------------------------ |
| `ACCOUNT_ID` | The IDs of the account being checked |

### <a class="header" id="settle-deal-payments.example" href="#settle-deal-payments.example">Example</a>
### <a class="header" id="retrieve-balance.example" href="#retrieve-balance.example">Example</a>

```bash
storagext-cli market retrieve-balance "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" # Alice's account
```

> This command **is not signed**, and does not need to be called using any of the `--X-key` flags.

## `retrieve-deal`

The `retrieve-deal` command fetches detailed information about a specific storage deal from the chain. This command allows users to check the current status, parameters, and other relevant details of an existing deal. If the specified deal is not found, it could indicate that the deal was never published, has already been completed, or was terminated.

### Parameters

| Name | Description |
| --------- | ---------------------------------- |
| `DEAL_ID` | The ID of the deal being retrieved |

### <a class="header" id="retrieve-deal.example" href="#retrieve-deal.example">Example</a>

```bash
storagext-cli market market retrieve-deal 0
```

> This command **is not signed**, and does not need to be called using any of the `--X-key` flags.
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
Loading