Skip to content

Commit

Permalink
Merge pull request #495 from ipfs-force-community/feat/get-deal-by-de…
Browse files Browse the repository at this point in the history
…al-id

feat: get deal by deal id
  • Loading branch information
0x5459 authored Dec 4, 2023
2 parents 34304a6 + 5e34461 commit dd49bab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
61 changes: 47 additions & 14 deletions cli/storage-deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"

"github.com/ipfs-force-community/droplet/v2/storageprovider"

Expand Down Expand Up @@ -623,30 +624,62 @@ var dealsPendingPublish = &cli.Command{
}

var getDealCmd = &cli.Command{
Name: "get",
Usage: "Print a storage deal",
ArgsUsage: "<proposal cid>",
Name: "get",
Usage: "Print a storage deal",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "deal-id",
Usage: "deal id assign by chain, eg. 1",
},
&cli.StringFlag{
Name: "proposal-cid",
Usage: "cid of deal proposal",
},
},
Action: func(cliCtx *cli.Context) error {
if cliCtx.NArg() != 1 {
return fmt.Errorf("expected 1 arguments")
if !cliCtx.IsSet("deal-id") && !cliCtx.IsSet("proposal-cid") {
return fmt.Errorf("must specify deal id or proposal cid")
}
var dealID abi.DealID
var proposalCid cid.Cid

api, closer, err := NewMarketNode(cliCtx)
if err != nil {
return err
if cliCtx.IsSet("deal-id") {
dealID = abi.DealID(cliCtx.Int64("deal-id"))
} else {
var err error
proposalCid, err = cid.Decode(cliCtx.String("proposal-cid"))
if err != nil {
return err
}
}
defer closer()

proposalCid, err := cid.Decode(cliCtx.Args().First())
api, closer, err := NewMarketNode(cliCtx)
if err != nil {
return err
}

defer closer()
ctx := ReqContext(cliCtx)

deal, err := api.MarketGetDeal(ctx, proposalCid)
if err != nil {
return err
var deal *market.MinerDeal
if cliCtx.IsSet("deal-id") {
deals, err := api.MarketListIncompleteDeals(ctx, &market.StorageDealQueryParams{
DealID: dealID,
Page: market.Page{
Limit: 1,
},
})
if err != nil {
return err
}
if len(deals) == 0 {
return fmt.Errorf("deal %d not found", dealID)
}
deal = &deals[0]
} else {
deal, err = api.MarketGetDeal(ctx, proposalCid)
if err != nil {
return err
}
}

return outputStorageDeal(deal)
Expand Down
7 changes: 6 additions & 1 deletion models/badger/storage_deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ func (sdr *storageDealRepo) ListDealByAddr(ctx context.Context, miner address.Ad
}

func (sdr *storageDealRepo) ListDeal(ctx context.Context, params *types.StorageDealQueryParams) ([]*types.MinerDeal, error) {
var count int
var storageDeals []*types.MinerDeal
end := params.Limit + params.Offset

Expand All @@ -243,6 +242,7 @@ func (sdr *storageDealRepo) ListDeal(ctx context.Context, params *types.StorageD
state != storagemarket.StorageDealExpired && state != storagemarket.StorageDealError
}

var count int
if err := travelCborAbleDS(ctx, sdr.ds, func(deal *types.MinerDeal) (stop bool, err error) {
if count >= end {
return true, nil
Expand All @@ -264,6 +264,11 @@ func (sdr *storageDealRepo) ListDeal(ctx context.Context, params *types.StorageD
if len(params.PieceCID) != 0 && deal.Proposal.PieceCID.String() != params.PieceCID {
return false, nil
}
// attention: deal id can not be zero in mainnet, but it can be zero in testnet
// is is a compromise for forward compatibility
if params.DealID != 0 && deal.DealID != params.DealID {
return false, nil
}
if count >= params.Offset && count < end {
storageDeals = append(storageDeals, deal)
}
Expand Down
5 changes: 5 additions & 0 deletions models/mysql/storage_deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ func (sdr *storageDealRepo) ListDeal(ctx context.Context, params *types.StorageD
if len(params.PieceCID) != 0 {
query.Where("cdp_piece_cid = ?", params.PieceCID)
}
// attention: deal id can not be zero in mainnet, but it can be zero in testnet
// is is a compromise for forward compatibility
if params.DealID != 0 {
query.Where("deal_id = ?", params.DealID)
}
if discardFailedDeal {
states := []storagemarket.StorageDealStatus{storagemarket.StorageDealFailing,
storagemarket.StorageDealExpired, storagemarket.StorageDealError, storagemarket.StorageDealSlashed}
Expand Down

0 comments on commit dd49bab

Please sign in to comment.