From a202f9d19e1d841b88441b3b6e6252487a7692cd Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Wed, 24 Mar 2021 14:36:21 +0200 Subject: [PATCH] add cancel-retrieval-deal cmd --- api/api_full.go | 3 +++ api/apistruct/struct.go | 6 ++++++ api/mocks/mock_full.go | 15 +++++++++++++++ cli/client.go | 27 +++++++++++++++++++++++++++ documentation/en/api-methods.md | 16 ++++++++++++++++ node/impl/client/client.go | 23 +++++++++++++++++++++++ 6 files changed, 90 insertions(+) diff --git a/api/api_full.go b/api/api_full.go index b66b0e402..00dc86797 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -340,6 +340,9 @@ type FullNode interface { // which are stuck due to insufficient funds ClientRetrieveTryRestartInsufficientFunds(ctx context.Context, paymentChannel address.Address) error //perm:write + // ClientCancelRetrievalDeal cancels an ongoing retrieval deal based on DealID + ClientCancelRetrievalDeal(ctx context.Context, dealid retrievalmarket.DealID) error //perm:write + // ClientUnimport removes references to the specified file from filestore //ClientUnimport(path string) diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index 34cf52fce..afd177f86 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -154,6 +154,8 @@ type FullNodeStruct struct { ClientCancelDataTransfer func(p0 context.Context, p1 datatransfer.TransferID, p2 peer.ID, p3 bool) error `perm:"write"` + ClientCancelRetrievalDeal func(p0 context.Context, p1 retrievalmarket.DealID) error `perm:"write"` + ClientDataTransferUpdates func(p0 context.Context) (<-chan api.DataTransferChannel, error) `perm:"write"` ClientDealPieceCID func(p0 context.Context, p1 cid.Cid) (api.DataCIDSize, error) `perm:"read"` @@ -978,6 +980,10 @@ func (s *FullNodeStruct) ClientCancelDataTransfer(p0 context.Context, p1 datatra return s.Internal.ClientCancelDataTransfer(p0, p1, p2, p3) } +func (s *FullNodeStruct) ClientCancelRetrievalDeal(p0 context.Context, p1 retrievalmarket.DealID) error { + return s.Internal.ClientCancelRetrievalDeal(p0, p1) +} + func (s *FullNodeStruct) ClientDataTransferUpdates(p0 context.Context) (<-chan api.DataTransferChannel, error) { return s.Internal.ClientDataTransferUpdates(p0) } diff --git a/api/mocks/mock_full.go b/api/mocks/mock_full.go index 8fd646d9a..8de04cf4c 100644 --- a/api/mocks/mock_full.go +++ b/api/mocks/mock_full.go @@ -9,6 +9,7 @@ import ( address "github.com/filecoin-project/go-address" bitfield "github.com/filecoin-project/go-bitfield" datatransfer "github.com/filecoin-project/go-data-transfer" + retrievalmarket "github.com/filecoin-project/go-fil-markets/retrievalmarket" storagemarket "github.com/filecoin-project/go-fil-markets/storagemarket" auth "github.com/filecoin-project/go-jsonrpc/auth" multistore "github.com/filecoin-project/go-multistore" @@ -445,6 +446,20 @@ func (mr *MockFullNodeMockRecorder) ClientCancelDataTransfer(arg0, arg1, arg2, a return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClientCancelDataTransfer", reflect.TypeOf((*MockFullNode)(nil).ClientCancelDataTransfer), arg0, arg1, arg2, arg3) } +// ClientCancelRetrievalDeal mocks base method +func (m *MockFullNode) ClientCancelRetrievalDeal(arg0 context.Context, arg1 retrievalmarket.DealID) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ClientCancelRetrievalDeal", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// ClientCancelRetrievalDeal indicates an expected call of ClientCancelRetrievalDeal +func (mr *MockFullNodeMockRecorder) ClientCancelRetrievalDeal(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClientCancelRetrievalDeal", reflect.TypeOf((*MockFullNode)(nil).ClientCancelRetrievalDeal), arg0, arg1) +} + // ClientDataTransferUpdates mocks base method func (m *MockFullNode) ClientDataTransferUpdates(arg0 context.Context) (<-chan api.DataTransferChannel, error) { m.ctrl.T.Helper() diff --git a/cli/client.go b/cli/client.go index 98f4b0229..ecc31e1e5 100644 --- a/cli/client.go +++ b/cli/client.go @@ -95,6 +95,7 @@ var clientCmd = &cli.Command{ WithCategory("util", clientListTransfers), WithCategory("util", clientRestartTransfer), WithCategory("util", clientCancelTransfer), + WithCategory("util", clientCancelRetrievalDeal), }, } @@ -1975,6 +1976,32 @@ var clientCancelTransfer = &cli.Command{ }, } +var clientCancelRetrievalDeal = &cli.Command{ + Name: "cancel-retrieval-deal", + Usage: "Cancel a retrieval deal by DealID", + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: "dealid", + Usage: "specify retrieval deal by DealID", + Required: true, + }, + }, + Action: func(cctx *cli.Context) error { + api, closer, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + ctx := ReqContext(cctx) + + if cctx.Int64("dealid") == 0 { + return errors.New("deal id cannot be 0") + } + + return api.ClientCancelRetrievalDeal(ctx, retrievalmarket.DealID(cctx.Int64("dealid"))) + }, +} + var clientListTransfers = &cli.Command{ Name: "list-transfers", Usage: "List ongoing data transfers for deals", diff --git a/documentation/en/api-methods.md b/documentation/en/api-methods.md index b8764d5b1..82e7f3e05 100644 --- a/documentation/en/api-methods.md +++ b/documentation/en/api-methods.md @@ -35,6 +35,7 @@ * [Client](#Client) * [ClientCalcCommP](#ClientCalcCommP) * [ClientCancelDataTransfer](#ClientCancelDataTransfer) + * [ClientCancelRetrievalDeal](#ClientCancelRetrievalDeal) * [ClientDataTransferUpdates](#ClientDataTransferUpdates) * [ClientDealPieceCID](#ClientDealPieceCID) * [ClientDealSize](#ClientDealSize) @@ -921,6 +922,21 @@ Inputs: Response: `{}` +### ClientCancelRetrievalDeal +ClientCancelRetrievalDeal cancels an ongoing retrieval deal based on DealID + + +Perms: write + +Inputs: +```json +[ + 5 +] +``` + +Response: `{}` + ### ClientDataTransferUpdates diff --git a/node/impl/client/client.go b/node/impl/client/client.go index ac526ac60..1f5203754 100644 --- a/node/impl/client/client.go +++ b/node/impl/client/client.go @@ -475,6 +475,29 @@ func (a *API) ClientListImports(ctx context.Context) ([]api.Import, error) { return out, nil } +func (a *API) ClientCancelRetrievalDeal(ctx context.Context, dealid retrievalmarket.DealID) error { + cerr := make(chan error) + go func() { + err := a.Retrieval.CancelDeal(dealid) + + select { + case cerr <- err: + case <-ctx.Done(): + } + }() + + select { + case err := <-cerr: + if err != nil { + return xerrors.Errorf("canceling retrieval deal erred: %w", err) + } + + return nil + case <-ctx.Done(): + return xerrors.Errorf("canceling retrieval deal context timeout: %w", ctx.Err()) + } +} + func (a *API) ClientRetrieve(ctx context.Context, order api.RetrievalOrder, ref *api.FileRef) error { events := make(chan marketevents.RetrievalEvent) go a.clientRetrieve(ctx, order, ref, events)