2019-12-10 04:19:59 +00:00
|
|
|
package retrievaladapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-17 10:46:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sharedutils"
|
2019-12-10 04:19:59 +00:00
|
|
|
|
2019-12-17 03:17:46 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-01-10 17:13:12 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
|
|
retrievaltoken "github.com/filecoin-project/go-fil-markets/shared/tokenamount"
|
|
|
|
retrievaltypes "github.com/filecoin-project/go-fil-markets/shared/types"
|
2019-12-17 03:17:46 +00:00
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
payapi "github.com/filecoin-project/lotus/node/impl/paych"
|
|
|
|
"github.com/filecoin-project/lotus/paych"
|
|
|
|
)
|
|
|
|
|
|
|
|
type retrievalClientNode struct {
|
|
|
|
pmgr *paych.Manager
|
|
|
|
payapi payapi.PaychAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRetrievalClientNode returns a new node adapter for a retrieval client that talks to the
|
|
|
|
// Lotus Node
|
|
|
|
func NewRetrievalClientNode(pmgr *paych.Manager, payapi payapi.PaychAPI) retrievalmarket.RetrievalClientNode {
|
|
|
|
return &retrievalClientNode{pmgr: pmgr, payapi: payapi}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreatePaymentChannel sets up a new payment channel if one does not exist
|
|
|
|
// between a client and a miner and insures the client has the given amount of funds available in the channel
|
2019-12-17 03:17:46 +00:00
|
|
|
func (rcn *retrievalClientNode) GetOrCreatePaymentChannel(ctx context.Context, clientAddress address.Address, minerAddress address.Address, clientFundsAvailable retrievaltoken.TokenAmount) (address.Address, error) {
|
2019-12-17 10:46:39 +00:00
|
|
|
paych, _, err := rcn.pmgr.GetPaych(ctx, clientAddress, minerAddress, sharedutils.FromSharedTokenAmount(clientFundsAvailable))
|
2019-12-10 04:19:59 +00:00
|
|
|
return paych, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate late creates a lane within a payment channel so that calls to
|
|
|
|
// CreatePaymentVoucher will automatically make vouchers only for the difference
|
|
|
|
// in total
|
2019-12-17 03:17:46 +00:00
|
|
|
func (rcn *retrievalClientNode) AllocateLane(paymentChannel address.Address) (uint64, error) {
|
2019-12-10 04:19:59 +00:00
|
|
|
return rcn.pmgr.AllocateLane(paymentChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePaymentVoucher creates a new payment voucher in the given lane for a
|
|
|
|
// given payment channel so that all the payment vouchers in the lane add up
|
|
|
|
// to the given amount (so the payment voucher will be for the difference)
|
2019-12-17 03:17:46 +00:00
|
|
|
func (rcn *retrievalClientNode) CreatePaymentVoucher(ctx context.Context, paymentChannel address.Address, amount retrievaltoken.TokenAmount, lane uint64) (*retrievaltypes.SignedVoucher, error) {
|
2019-12-17 10:46:39 +00:00
|
|
|
voucher, err := rcn.payapi.PaychVoucherCreate(ctx, paymentChannel, sharedutils.FromSharedTokenAmount(amount), lane)
|
2019-12-17 03:17:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 10:46:39 +00:00
|
|
|
return sharedutils.ToSharedSignedVoucher(voucher)
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|