Add maxPrice option to client retrieval

- Use best offer if multiple offers returned
This commit is contained in:
Ingar Shu 2020-07-09 11:47:12 -07:00
parent 56dd3f40e0
commit 5411ebb806
No known key found for this signature in database
GPG Key ID: BE3D9CE79F22E769

View File

@ -502,6 +502,10 @@ var clientRetrieveCmd = &cli.Command{
Name: "miner",
Usage: "miner address for retrieval, if not present it'll use local discovery",
},
&cli.StringFlag{
Name: "maxPrice",
Usage: "maximum price the client is willing to consider",
},
&cli.StringFlag{
Name: "pieceCid",
Usage: "require data to be retrieved from a specific Piece CID",
@ -560,6 +564,11 @@ var clientRetrieveCmd = &cli.Command{
minerStrAddr := cctx.String("miner")
if minerStrAddr == "" { // Local discovery
offers, err := fapi.ClientFindData(ctx, file, pieceCid)
// sort by price low to high
sort.Slice(offers, func(i, j int) bool {
return offers[i].MinPrice.LessThan(offers[j].MinPrice)
})
if err != nil {
return err
}
@ -584,6 +593,17 @@ var clientRetrieveCmd = &cli.Command{
return fmt.Errorf("The received offer errored: %s", offer.Err)
}
if cctx.String("maxPrice") != "" {
maxPrice, err := types.ParseFIL(cctx.String("maxPrice"))
if err != nil {
return err
}
if offer.MinPrice.GreaterThan(types.BigInt(maxPrice)) {
return xerrors.Errorf("Failed to find offer satisfying maxPrice: %w", maxPrice)
}
}
ref := &lapi.FileRef{
Path: cctx.Args().Get(1),
IsCAR: cctx.Bool("car"),