lotus/paychmgr/store.go

527 lines
14 KiB
Go
Raw Normal View History

package paychmgr
2019-08-09 21:42:56 +00:00
import (
2019-11-04 19:03:11 +00:00
"bytes"
2021-12-11 21:03:00 +00:00
"context"
2019-09-06 22:39:47 +00:00
"errors"
"fmt"
2020-07-28 23:16:47 +00:00
"github.com/google/uuid"
"golang.org/x/xerrors"
2020-07-28 23:16:47 +00:00
"github.com/filecoin-project/lotus/chain/types"
cborutil "github.com/filecoin-project/go-cbor-util"
2020-07-28 23:16:47 +00:00
"github.com/ipfs/go-cid"
2019-08-09 21:42:56 +00:00
"github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
2019-09-16 16:40:26 +00:00
"github.com/filecoin-project/go-address"
cborrpc "github.com/filecoin-project/go-cbor-util"
2020-02-13 00:10:07 +00:00
2020-09-28 21:25:58 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
2019-08-09 21:42:56 +00:00
)
2019-09-06 22:39:47 +00:00
var ErrChannelNotTracked = errors.New("channel not tracked")
type Store struct {
2019-08-09 21:42:56 +00:00
ds datastore.Batching
}
func NewStore(ds datastore.Batching) *Store {
return &Store{
2019-08-09 21:42:56 +00:00
ds: ds,
}
}
const (
DirInbound = 1
DirOutbound = 2
)
2020-07-28 23:16:47 +00:00
const (
dsKeyChannelInfo = "ChannelInfo"
dsKeyMsgCid = "MsgCid"
)
2019-09-09 13:59:07 +00:00
type VoucherInfo struct {
2020-09-28 21:25:58 +00:00
Voucher *paych.SignedVoucher
Proof []byte // ignored
Submitted bool
2019-09-09 13:59:07 +00:00
}
2020-07-28 23:16:47 +00:00
// ChannelInfo keeps track of information about a channel
type ChannelInfo struct {
2020-07-28 23:16:47 +00:00
// ChannelID is a uuid set at channel creation
ChannelID string
// Channel address - may be nil if the channel hasn't been created yet
Channel *address.Address
// Control is the address of the local node
Control address.Address
// Target is the address of the remote node (on the other end of the channel)
2020-07-28 23:16:47 +00:00
Target address.Address
// Direction indicates if the channel is inbound (Control is the "to" address)
// or outbound (Control is the "from" address)
2019-11-04 19:03:11 +00:00
Direction uint64
2020-07-28 23:16:47 +00:00
// Vouchers is a list of all vouchers sent on the channel
Vouchers []*VoucherInfo
// NextLane is the number of the next lane that should be used when the
// client requests a new lane (eg to create a voucher for a new deal)
NextLane uint64
// Amount added to the channel.
// Note: This amount is only used by GetPaych to keep track of how much
// has locally been added to the channel. It should reflect the channel's
// Balance on chain as long as all operations occur on the same datastore.
Amount types.BigInt
// AvailableAmount indicates how much afil is non-reserved
2022-01-04 19:33:49 +00:00
AvailableAmount types.BigInt
// PendingAvailableAmount is available amount that we're awaiting confirmation of
PendingAvailableAmount types.BigInt
2020-07-28 23:16:47 +00:00
// PendingAmount is the amount that we're awaiting confirmation of
PendingAmount types.BigInt
// CreateMsg is the CID of a pending create message (while waiting for confirmation)
CreateMsg *cid.Cid
// AddFundsMsg is the CID of a pending add funds message (while waiting for confirmation)
AddFundsMsg *cid.Cid
// Settling indicates whether the channel has entered into the settling state
Settling bool
}
func (ci *ChannelInfo) from() address.Address {
if ci.Direction == DirOutbound {
return ci.Control
}
return ci.Target
}
func (ci *ChannelInfo) to() address.Address {
if ci.Direction == DirOutbound {
return ci.Target
}
return ci.Control
}
// infoForVoucher gets the VoucherInfo for the given voucher.
// returns nil if the channel doesn't have the voucher.
2020-09-28 21:25:58 +00:00
func (ci *ChannelInfo) infoForVoucher(sv *paych.SignedVoucher) (*VoucherInfo, error) {
for _, v := range ci.Vouchers {
eq, err := cborutil.Equals(sv, v.Voucher)
if err != nil {
return nil, err
}
if eq {
return v, nil
}
}
return nil, nil
}
2020-09-28 21:25:58 +00:00
func (ci *ChannelInfo) hasVoucher(sv *paych.SignedVoucher) (bool, error) {
vi, err := ci.infoForVoucher(sv)
return vi != nil, err
}
// markVoucherSubmitted marks the voucher, and any vouchers of lower nonce
// in the same lane, as being submitted.
// Note: This method doesn't write anything to the store.
2020-09-28 21:25:58 +00:00
func (ci *ChannelInfo) markVoucherSubmitted(sv *paych.SignedVoucher) error {
vi, err := ci.infoForVoucher(sv)
if err != nil {
return err
}
if vi == nil {
return xerrors.Errorf("cannot submit voucher that has not been added to channel")
}
// Mark the voucher as submitted
vi.Submitted = true
// Mark lower-nonce vouchers in the same lane as submitted (lower-nonce
// vouchers are superseded by the submitted voucher)
for _, vi := range ci.Vouchers {
if vi.Voucher.Lane == sv.Lane && vi.Voucher.Nonce < sv.Nonce {
vi.Submitted = true
}
}
return nil
}
// wasVoucherSubmitted returns true if the voucher has been submitted
2020-09-28 21:25:58 +00:00
func (ci *ChannelInfo) wasVoucherSubmitted(sv *paych.SignedVoucher) (bool, error) {
vi, err := ci.infoForVoucher(sv)
if err != nil {
return false, err
}
if vi == nil {
return false, xerrors.Errorf("cannot submit voucher that has not been added to channel")
}
return vi.Submitted, nil
}
2020-07-28 23:16:47 +00:00
// TrackChannel stores a channel, returning an error if the channel was already
// being tracked
2021-12-11 21:03:00 +00:00
func (ps *Store) TrackChannel(ctx context.Context, ci *ChannelInfo) (*ChannelInfo, error) {
_, err := ps.ByAddress(ctx, *ci.Channel)
2020-07-28 23:16:47 +00:00
switch err {
default:
return nil, err
2020-07-28 23:16:47 +00:00
case nil:
return nil, fmt.Errorf("already tracking channel: %s", ci.Channel)
2020-07-28 23:16:47 +00:00
case ErrChannelNotTracked:
2021-12-11 21:03:00 +00:00
err = ps.putChannelInfo(ctx, ci)
if err != nil {
return nil, err
}
2021-12-11 21:03:00 +00:00
return ps.ByAddress(ctx, *ci.Channel)
}
}
2020-07-28 23:16:47 +00:00
// ListChannels returns the addresses of all channels that have been created
2021-12-11 21:03:00 +00:00
func (ps *Store) ListChannels(ctx context.Context) ([]address.Address, error) {
cis, err := ps.findChans(ctx, func(ci *ChannelInfo) bool {
2020-07-28 23:16:47 +00:00
return ci.Channel != nil
}, 0)
if err != nil {
return nil, err
}
2020-07-28 23:16:47 +00:00
addrs := make([]address.Address, 0, len(cis))
for _, ci := range cis {
addrs = append(addrs, *ci.Channel)
}
2020-07-28 23:16:47 +00:00
return addrs, nil
}
2020-07-28 23:16:47 +00:00
// findChan finds a single channel using the given filter.
// If there isn't a channel that matches the filter, returns ErrChannelNotTracked
2021-12-11 21:03:00 +00:00
func (ps *Store) findChan(ctx context.Context, filter func(ci *ChannelInfo) bool) (*ChannelInfo, error) {
cis, err := ps.findChans(ctx, filter, 1)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
2019-09-16 17:23:48 +00:00
2020-07-28 23:16:47 +00:00
if len(cis) == 0 {
return nil, ErrChannelNotTracked
}
2020-07-28 23:16:47 +00:00
return &cis[0], err
}
2020-07-28 23:16:47 +00:00
// findChans loops over all channels, only including those that pass the filter.
// max is the maximum number of channels to return. Set to zero to return unlimited channels.
2021-12-11 21:03:00 +00:00
func (ps *Store) findChans(ctx context.Context, filter func(*ChannelInfo) bool, max int) ([]ChannelInfo, error) {
res, err := ps.ds.Query(ctx, dsq.Query{Prefix: dsKeyChannelInfo})
if err != nil {
return nil, err
}
defer res.Close() //nolint:errcheck
2020-07-28 23:16:47 +00:00
var stored ChannelInfo
var matches []ChannelInfo
for {
res, ok := res.NextSync()
if !ok {
break
}
if res.Error != nil {
return nil, err
}
ci, err := unmarshallChannelInfo(&stored, res.Value)
if err != nil {
2020-07-28 23:16:47 +00:00
return nil, err
}
if !filter(ci) {
continue
}
2020-07-28 23:16:47 +00:00
matches = append(matches, *ci)
// If we've reached the maximum number of matches, return.
// Note that if max is zero we return an unlimited number of matches
// because len(matches) will always be at least 1.
if len(matches) == max {
return matches, nil
}
}
2020-07-28 23:16:47 +00:00
return matches, nil
2019-08-09 21:42:56 +00:00
}
2020-07-28 23:16:47 +00:00
// AllocateLane allocates a new lane for the given channel
2021-12-11 21:03:00 +00:00
func (ps *Store) AllocateLane(ctx context.Context, ch address.Address) (uint64, error) {
ci, err := ps.ByAddress(ctx, ch)
if err != nil {
2020-07-28 23:16:47 +00:00
return 0, err
}
2020-07-28 23:16:47 +00:00
out := ci.NextLane
ci.NextLane++
2021-12-11 21:03:00 +00:00
return out, ps.putChannelInfo(ctx, ci)
2020-07-28 23:16:47 +00:00
}
2020-07-28 23:16:47 +00:00
// VouchersForPaych gets the vouchers for the given channel
2021-12-11 21:03:00 +00:00
func (ps *Store) VouchersForPaych(ctx context.Context, ch address.Address) ([]*VoucherInfo, error) {
ci, err := ps.ByAddress(ctx, ch)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
2020-07-28 23:16:47 +00:00
return ci.Vouchers, nil
}
2021-12-11 21:03:00 +00:00
func (ps *Store) MarkVoucherSubmitted(ctx context.Context, ci *ChannelInfo, sv *paych.SignedVoucher) error {
err := ci.markVoucherSubmitted(sv)
if err != nil {
return err
}
2021-12-11 21:03:00 +00:00
return ps.putChannelInfo(ctx, ci)
}
2020-07-28 23:16:47 +00:00
// ByAddress gets the channel that matches the given address
2021-12-11 21:03:00 +00:00
func (ps *Store) ByAddress(ctx context.Context, addr address.Address) (*ChannelInfo, error) {
return ps.findChan(ctx, func(ci *ChannelInfo) bool {
2020-07-28 23:16:47 +00:00
return ci.Channel != nil && *ci.Channel == addr
})
}
// MsgInfo stores information about a create channel / add funds message
// that has been sent
type MsgInfo struct {
// ChannelID links the message to a channel
ChannelID string
// MsgCid is the CID of the message
MsgCid cid.Cid
// Received indicates whether a response has been received
Received bool
// Err is the error received in the response
Err string
}
// The datastore key used to identify the message
func dskeyForMsg(mcid cid.Cid) datastore.Key {
return datastore.KeyWithNamespaces([]string{dsKeyMsgCid, mcid.String()})
}
// SaveNewMessage is called when a message is sent
2021-12-11 21:03:00 +00:00
func (ps *Store) SaveNewMessage(ctx context.Context, channelID string, mcid cid.Cid) error {
2020-07-28 23:16:47 +00:00
k := dskeyForMsg(mcid)
b, err := cborrpc.Dump(&MsgInfo{ChannelID: channelID, MsgCid: mcid})
if err != nil {
return err
}
2021-12-11 21:03:00 +00:00
return ps.ds.Put(ctx, k, b)
2020-07-28 23:16:47 +00:00
}
// SaveMessageResult is called when the result of a message is received
2021-12-11 21:03:00 +00:00
func (ps *Store) SaveMessageResult(ctx context.Context, mcid cid.Cid, msgErr error) error {
minfo, err := ps.GetMessage(ctx, mcid)
2020-07-28 23:16:47 +00:00
if err != nil {
return err
}
k := dskeyForMsg(mcid)
minfo.Received = true
if msgErr != nil {
minfo.Err = msgErr.Error()
}
b, err := cborrpc.Dump(minfo)
if err != nil {
return err
}
2021-12-11 21:03:00 +00:00
return ps.ds.Put(ctx, k, b)
2020-07-28 23:16:47 +00:00
}
// ByMessageCid gets the channel associated with a message
2021-12-11 21:03:00 +00:00
func (ps *Store) ByMessageCid(ctx context.Context, mcid cid.Cid) (*ChannelInfo, error) {
minfo, err := ps.GetMessage(ctx, mcid)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
2021-12-11 21:03:00 +00:00
ci, err := ps.findChan(ctx, func(ci *ChannelInfo) bool {
2020-07-28 23:16:47 +00:00
return ci.ChannelID == minfo.ChannelID
})
if err != nil {
return nil, err
}
return ci, err
}
// GetMessage gets the message info for a given message CID
2021-12-11 21:03:00 +00:00
func (ps *Store) GetMessage(ctx context.Context, mcid cid.Cid) (*MsgInfo, error) {
2020-07-28 23:16:47 +00:00
k := dskeyForMsg(mcid)
2021-12-11 21:03:00 +00:00
val, err := ps.ds.Get(ctx, k)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
var minfo MsgInfo
if err := minfo.UnmarshalCBOR(bytes.NewReader(val)); err != nil {
return nil, err
}
return &minfo, nil
}
// OutboundActiveByFromTo looks for outbound channels that have not been
// settled, with the given from / to addresses
func (ps *Store) OutboundActiveByFromTo(ctx context.Context, sma stateManagerAPI, from address.Address, to address.Address) (*ChannelInfo, error) {
2021-12-11 21:03:00 +00:00
return ps.findChan(ctx, func(ci *ChannelInfo) bool {
2020-07-28 23:16:47 +00:00
if ci.Direction != DirOutbound {
return false
}
if ci.Settling {
return false
}
if ci.Channel != nil {
_, st, err := sma.GetPaychState(ctx, *ci.Channel, nil)
if err != nil {
return false
}
sat, err := st.SettlingAt()
if err != nil {
return false
}
if sat != 0 {
return false
}
}
2020-07-28 23:16:47 +00:00
return ci.Control == from && ci.Target == to
})
}
2020-07-28 23:16:47 +00:00
// WithPendingAddFunds is used on startup to find channels for which a
// create channel or add funds message has been sent, but lotus shut down
// before the response was received.
2021-12-11 21:03:00 +00:00
func (ps *Store) WithPendingAddFunds(ctx context.Context) ([]ChannelInfo, error) {
return ps.findChans(ctx, func(ci *ChannelInfo) bool {
2020-07-28 23:16:47 +00:00
if ci.Direction != DirOutbound {
return false
}
2020-07-28 23:16:47 +00:00
return ci.CreateMsg != nil || ci.AddFundsMsg != nil
}, 0)
}
// ByChannelID gets channel info by channel ID
2021-12-11 21:03:00 +00:00
func (ps *Store) ByChannelID(ctx context.Context, channelID string) (*ChannelInfo, error) {
var stored ChannelInfo
2021-12-11 21:03:00 +00:00
res, err := ps.ds.Get(ctx, dskeyForChannel(channelID))
if err != nil {
if err == datastore.ErrNotFound {
return nil, ErrChannelNotTracked
}
return nil, err
}
return unmarshallChannelInfo(&stored, res)
}
// CreateChannel creates an outbound channel for the given from / to
2022-01-04 19:33:49 +00:00
func (ps *Store) CreateChannel(ctx context.Context, from address.Address, to address.Address, createMsgCid cid.Cid, amt, avail types.BigInt) (*ChannelInfo, error) {
2020-07-28 23:16:47 +00:00
ci := &ChannelInfo{
2022-01-04 19:33:49 +00:00
Direction: DirOutbound,
NextLane: 0,
Control: from,
Target: to,
CreateMsg: &createMsgCid,
PendingAmount: amt,
PendingAvailableAmount: avail,
2020-07-28 23:16:47 +00:00
}
// Save the new channel
2021-12-11 21:03:00 +00:00
err := ps.putChannelInfo(ctx, ci)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
2020-07-28 23:16:47 +00:00
// Save a reference to the create message
2021-12-11 21:03:00 +00:00
err = ps.SaveNewMessage(ctx, ci.ChannelID, createMsgCid)
2020-07-28 23:16:47 +00:00
if err != nil {
return nil, err
}
2020-07-28 23:16:47 +00:00
return ci, err
}
// RemoveChannel removes the channel with the given channel ID
2021-12-11 21:03:00 +00:00
func (ps *Store) RemoveChannel(ctx context.Context, channelID string) error {
return ps.ds.Delete(ctx, dskeyForChannel(channelID))
}
2020-07-28 23:16:47 +00:00
// The datastore key used to identify the channel info
func dskeyForChannel(channelID string) datastore.Key {
return datastore.KeyWithNamespaces([]string{dsKeyChannelInfo, channelID})
2020-07-28 23:16:47 +00:00
}
// putChannelInfo stores the channel info in the datastore
2021-12-11 21:03:00 +00:00
func (ps *Store) putChannelInfo(ctx context.Context, ci *ChannelInfo) error {
if len(ci.ChannelID) == 0 {
ci.ChannelID = uuid.New().String()
}
k := dskeyForChannel(ci.ChannelID)
2020-07-28 23:16:47 +00:00
b, err := marshallChannelInfo(ci)
if err != nil {
2020-07-28 23:16:47 +00:00
return err
}
2021-12-11 21:03:00 +00:00
return ps.ds.Put(ctx, k, b)
}
2020-07-28 23:16:47 +00:00
// TODO: This is a hack to get around not being able to CBOR marshall a nil
// address.Address. It's been fixed in address.Address but we need to wait
// for the change to propagate to specs-actors before we can remove this hack.
var emptyAddr address.Address
func init() {
addr, err := address.NewActorAddress([]byte("empty"))
2019-08-12 19:51:01 +00:00
if err != nil {
2020-07-28 23:16:47 +00:00
panic(err)
}
emptyAddr = addr
}
func marshallChannelInfo(ci *ChannelInfo) ([]byte, error) {
// See note above about CBOR marshalling address.Address
if ci.Channel == nil {
ci.Channel = &emptyAddr
}
return cborrpc.Dump(ci)
}
func unmarshallChannelInfo(stored *ChannelInfo, value []byte) (*ChannelInfo, error) {
if err := stored.UnmarshalCBOR(bytes.NewReader(value)); err != nil {
2019-08-12 19:51:01 +00:00
return nil, err
}
2020-07-28 23:16:47 +00:00
// See note above about CBOR marshalling address.Address
if stored.Channel != nil && *stored.Channel == emptyAddr {
stored.Channel = nil
}
2022-01-04 22:31:22 +00:00
// backwards compat
if stored.AvailableAmount.Int == nil {
stored.AvailableAmount = types.NewInt(0)
stored.PendingAvailableAmount = types.NewInt(0)
}
2020-07-28 23:16:47 +00:00
return stored, nil
2019-08-09 21:42:56 +00:00
}