2019-12-10 04:19:59 +00:00
|
|
|
package retrievaladapter
|
|
|
|
|
|
|
|
import (
|
2020-04-21 20:48:43 +00:00
|
|
|
"bytes"
|
2019-12-10 04:19:59 +00:00
|
|
|
"context"
|
|
|
|
|
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"
|
2020-03-18 17:44:54 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/shared"
|
2020-02-12 22:32:26 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-04-21 20:48:43 +00:00
|
|
|
initactor "github.com/filecoin-project/specs-actors/actors/builtin/init"
|
2020-02-20 08:37:10 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
2020-04-21 20:48:43 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
2020-04-17 00:25:06 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-04-21 20:48:43 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-18 17:44:54 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
payapi "github.com/filecoin-project/lotus/node/impl/paych"
|
|
|
|
"github.com/filecoin-project/lotus/paychmgr"
|
2019-12-10 04:19:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type retrievalClientNode struct {
|
2020-03-18 17:44:54 +00:00
|
|
|
chainapi full.ChainAPI
|
|
|
|
pmgr *paychmgr.Manager
|
|
|
|
payapi payapi.PaychAPI
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRetrievalClientNode returns a new node adapter for a retrieval client that talks to the
|
|
|
|
// Lotus Node
|
2020-03-18 17:44:54 +00:00
|
|
|
func NewRetrievalClientNode(pmgr *paychmgr.Manager, payapi payapi.PaychAPI, chainapi full.ChainAPI) retrievalmarket.RetrievalClientNode {
|
|
|
|
return &retrievalClientNode{pmgr: pmgr, payapi: payapi, chainapi: chainapi}
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreatePaymentChannel sets up a new payment channel if one does not exist
|
2020-03-18 17:44:54 +00:00
|
|
|
// between a client and a miner and ensures the client has the given amount of
|
|
|
|
// funds available in the channel.
|
2020-04-17 00:25:06 +00:00
|
|
|
func (rcn *retrievalClientNode) GetOrCreatePaymentChannel(ctx context.Context, clientAddress address.Address, minerAddress address.Address, clientFundsAvailable abi.TokenAmount, tok shared.TipSetToken) (address.Address, cid.Cid, error) {
|
2020-03-18 17:44:54 +00:00
|
|
|
// TODO: respect the provided TipSetToken (a serialized TipSetKey) when
|
|
|
|
// querying the chain
|
2020-04-17 00:25:06 +00:00
|
|
|
return rcn.pmgr.GetPaych(ctx, clientAddress, minerAddress, clientFundsAvailable)
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate late creates a lane within a payment channel so that calls to
|
|
|
|
// CreatePaymentVoucher will automatically make vouchers only for the difference
|
|
|
|
// in total
|
2020-02-21 17:26:44 +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)
|
2020-03-18 17:44:54 +00:00
|
|
|
func (rcn *retrievalClientNode) CreatePaymentVoucher(ctx context.Context, paymentChannel address.Address, amount abi.TokenAmount, lane uint64, tok shared.TipSetToken) (*paych.SignedVoucher, error) {
|
|
|
|
// TODO: respect the provided TipSetToken (a serialized TipSetKey) when
|
|
|
|
// querying the chain
|
2020-02-12 22:32:26 +00:00
|
|
|
voucher, err := rcn.payapi.PaychVoucherCreate(ctx, paymentChannel, amount, lane)
|
2019-12-17 03:17:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-21 17:26:44 +00:00
|
|
|
return voucher, nil
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
2020-03-18 17:44:54 +00:00
|
|
|
|
|
|
|
func (rcn *retrievalClientNode) GetChainHead(ctx context.Context) (shared.TipSetToken, abi.ChainEpoch, error) {
|
|
|
|
head, err := rcn.chainapi.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return head.Key().Bytes(), head.Height(), nil
|
|
|
|
}
|
2020-04-17 00:25:06 +00:00
|
|
|
|
2020-04-21 20:48:43 +00:00
|
|
|
// WaitForPaymentChannelAddFunds waits messageCID to appear on chain. If it doesn't appear within
|
|
|
|
// defaultMsgWaitTimeout it returns error
|
2020-04-17 00:25:06 +00:00
|
|
|
func (rcn *retrievalClientNode) WaitForPaymentChannelAddFunds(messageCID cid.Cid) error {
|
2020-04-21 20:48:43 +00:00
|
|
|
_, mr, err := rcn.chainapi.StateManager.WaitForMessage(context.TODO(), messageCID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mr.ExitCode != exitcode.Ok {
|
|
|
|
return xerrors.Errorf("wait for payment channel to add funds failed. exit code: %d", mr.ExitCode)
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-17 00:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rcn *retrievalClientNode) WaitForPaymentChannelCreation(messageCID cid.Cid) (address.Address, error) {
|
2020-04-21 20:48:43 +00:00
|
|
|
_, mr, err := rcn.chainapi.StateManager.WaitForMessage(context.TODO(), messageCID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
if mr.ExitCode != exitcode.Ok {
|
|
|
|
return address.Undef, xerrors.Errorf("payment channel creation failed. exit code: %d", mr.ExitCode)
|
|
|
|
}
|
|
|
|
var retval initactor.ExecReturn
|
|
|
|
if err := retval.UnmarshalCBOR(bytes.NewReader(mr.Return)); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
return retval.RobustAddress, nil
|
2020-04-17 00:25:06 +00:00
|
|
|
}
|