2020-02-13 00:28:23 +00:00
|
|
|
package paychmgr
|
2019-08-09 21:42:56 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-04 19:03:11 +00:00
|
|
|
"bytes"
|
2019-09-06 22:39:47 +00:00
|
|
|
"errors"
|
2019-08-12 17:09:56 +00:00
|
|
|
"fmt"
|
2020-07-28 23:16:47 +00:00
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-08-12 17:09:56 +00:00
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
cborutil "github.com/filecoin-project/go-cbor-util"
|
2020-02-13 00:10:07 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
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"
|
2019-08-12 19:05:57 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2019-08-12 17:09:56 +00:00
|
|
|
dsq "github.com/ipfs/go-datastore/query"
|
2019-09-16 16:40:26 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-12-19 20:28:17 +00:00
|
|
|
cborrpc "github.com/filecoin-project/go-cbor-util"
|
2020-02-13 00:10:07 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2019-08-09 21:42:56 +00:00
|
|
|
)
|
|
|
|
|
2019-09-06 22:39:47 +00:00
|
|
|
var ErrChannelNotTracked = errors.New("channel not tracked")
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
type Store struct {
|
2019-08-09 21:42:56 +00:00
|
|
|
ds datastore.Batching
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:40:26 +00:00
|
|
|
func NewStore(ds dtypes.MetadataDS) *Store {
|
2019-08-12 19:05:57 +00:00
|
|
|
ds = namespace.Wrap(ds, datastore.NewKey("/paych/"))
|
2019-08-12 17:09:56 +00:00
|
|
|
return &Store{
|
2019-08-09 21:42:56 +00:00
|
|
|
ds: ds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
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-08-20 16:09:52 +00:00
|
|
|
Voucher *paych.SignedVoucher
|
|
|
|
Proof []byte
|
|
|
|
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
|
2019-08-12 17:09:56 +00:00
|
|
|
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
|
2020-09-03 06:22:40 +00:00
|
|
|
// Control is the address of the local node
|
2019-09-10 13:43:01 +00:00
|
|
|
Control address.Address
|
2020-09-03 06:22:40 +00:00
|
|
|
// 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
|
2020-09-03 06:22:40 +00:00
|
|
|
// 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
|
|
|
|
// 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
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
// infoForVoucher gets the VoucherInfo for the given voucher.
|
|
|
|
// returns nil if the channel doesn't have the voucher.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
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
|
2020-08-17 15:13:13 +00:00
|
|
|
func (ps *Store) TrackChannel(ci *ChannelInfo) (*ChannelInfo, error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
_, err := ps.ByAddress(*ci.Channel)
|
|
|
|
switch err {
|
|
|
|
default:
|
2020-08-17 15:13:13 +00:00
|
|
|
return nil, err
|
2020-07-28 23:16:47 +00:00
|
|
|
case nil:
|
2020-08-17 15:13:13 +00:00
|
|
|
return nil, fmt.Errorf("already tracking channel: %s", ci.Channel)
|
2020-07-28 23:16:47 +00:00
|
|
|
case ErrChannelNotTracked:
|
2020-08-17 15:13:13 +00:00
|
|
|
err = ps.putChannelInfo(ci)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps.ByAddress(*ci.Channel)
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
// ListChannels returns the addresses of all channels that have been created
|
|
|
|
func (ps *Store) ListChannels() ([]address.Address, error) {
|
|
|
|
cis, err := ps.findChans(func(ci *ChannelInfo) bool {
|
|
|
|
return ci.Channel != nil
|
|
|
|
}, 0)
|
2019-08-12 17:09:56 +00:00
|
|
|
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)
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return addrs, nil
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
func (ps *Store) findChan(filter func(ci *ChannelInfo) bool) (*ChannelInfo, error) {
|
|
|
|
cis, err := ps.findChans(filter, 1)
|
|
|
|
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
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return &cis[0], err
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
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.
|
|
|
|
func (ps *Store) findChans(filter func(*ChannelInfo) bool, max int) ([]ChannelInfo, error) {
|
|
|
|
res, err := ps.ds.Query(dsq.Query{Prefix: dsKeyChannelInfo})
|
2019-08-12 17:09:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-27 20:53:20 +00:00
|
|
|
defer res.Close() //nolint:errcheck
|
2019-08-12 17:09:56 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
var stored ChannelInfo
|
|
|
|
var matches []ChannelInfo
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
for {
|
|
|
|
res, ok := res.NextSync()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
if res.Error != nil {
|
2019-08-12 17:09:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:47:48 +00:00
|
|
|
ci, err := unmarshallChannelInfo(&stored, res.Value)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
2020-07-28 23:16:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filter(ci) {
|
|
|
|
continue
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
func (ps *Store) AllocateLane(ch address.Address) (uint64, error) {
|
|
|
|
ci, err := ps.ByAddress(ch)
|
2019-09-10 13:43:01 +00:00
|
|
|
if err != nil {
|
2020-07-28 23:16:47 +00:00
|
|
|
return 0, err
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
out := ci.NextLane
|
|
|
|
ci.NextLane++
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return out, ps.putChannelInfo(ci)
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
// VouchersForPaych gets the vouchers for the given channel
|
|
|
|
func (ps *Store) VouchersForPaych(ch address.Address) ([]*VoucherInfo, error) {
|
|
|
|
ci, err := ps.ByAddress(ch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return ci.Vouchers, nil
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
func (ps *Store) MarkVoucherSubmitted(ci *ChannelInfo, sv *paych.SignedVoucher) error {
|
|
|
|
err := ci.markVoucherSubmitted(sv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ps.putChannelInfo(ci)
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
// ByAddress gets the channel that matches the given address
|
|
|
|
func (ps *Store) ByAddress(addr address.Address) (*ChannelInfo, error) {
|
|
|
|
return ps.findChan(func(ci *ChannelInfo) bool {
|
|
|
|
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
|
|
|
|
func (ps *Store) SaveNewMessage(channelID string, mcid cid.Cid) error {
|
|
|
|
k := dskeyForMsg(mcid)
|
|
|
|
|
|
|
|
b, err := cborrpc.Dump(&MsgInfo{ChannelID: channelID, MsgCid: mcid})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps.ds.Put(k, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveMessageResult is called when the result of a message is received
|
|
|
|
func (ps *Store) SaveMessageResult(mcid cid.Cid, msgErr error) error {
|
|
|
|
minfo, err := ps.GetMessage(mcid)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps.ds.Put(k, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByMessageCid gets the channel associated with a message
|
|
|
|
func (ps *Store) ByMessageCid(mcid cid.Cid) (*ChannelInfo, error) {
|
|
|
|
minfo, err := ps.GetMessage(mcid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ci, err := ps.findChan(func(ci *ChannelInfo) bool {
|
|
|
|
return ci.ChannelID == minfo.ChannelID
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ci, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMessage gets the message info for a given message CID
|
|
|
|
func (ps *Store) GetMessage(mcid cid.Cid) (*MsgInfo, error) {
|
|
|
|
k := dskeyForMsg(mcid)
|
|
|
|
|
|
|
|
val, err := ps.ds.Get(k)
|
|
|
|
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(from address.Address, to address.Address) (*ChannelInfo, error) {
|
|
|
|
return ps.findChan(func(ci *ChannelInfo) bool {
|
|
|
|
if ci.Direction != DirOutbound {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ci.Settling {
|
|
|
|
return false
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
2020-07-28 23:16:47 +00:00
|
|
|
return ci.Control == from && ci.Target == to
|
|
|
|
})
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
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.
|
|
|
|
func (ps *Store) WithPendingAddFunds() ([]ChannelInfo, error) {
|
|
|
|
return ps.findChans(func(ci *ChannelInfo) bool {
|
|
|
|
if ci.Direction != DirOutbound {
|
|
|
|
return false
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
2020-07-28 23:16:47 +00:00
|
|
|
return ci.CreateMsg != nil || ci.AddFundsMsg != nil
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:47:48 +00:00
|
|
|
// ByChannelID gets channel info by channel ID
|
|
|
|
func (ps *Store) ByChannelID(channelID string) (*ChannelInfo, error) {
|
|
|
|
var stored ChannelInfo
|
|
|
|
|
|
|
|
res, err := ps.ds.Get(dskeyForChannel(channelID))
|
|
|
|
if err != nil {
|
|
|
|
if err == datastore.ErrNotFound {
|
|
|
|
return nil, ErrChannelNotTracked
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmarshallChannelInfo(&stored, res)
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:25:58 +00:00
|
|
|
// CreateChannel creates an outbound channel for the given from / to
|
2020-08-06 12:47:48 +00:00
|
|
|
func (ps *Store) CreateChannel(from address.Address, to address.Address, createMsgCid cid.Cid, amt types.BigInt) (*ChannelInfo, error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
ci := &ChannelInfo{
|
|
|
|
Direction: DirOutbound,
|
|
|
|
NextLane: 0,
|
|
|
|
Control: from,
|
|
|
|
Target: to,
|
|
|
|
CreateMsg: &createMsgCid,
|
|
|
|
PendingAmount: amt,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the new channel
|
|
|
|
err := ps.putChannelInfo(ci)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
// Save a reference to the create message
|
|
|
|
err = ps.SaveNewMessage(ci.ChannelID, createMsgCid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return ci, err
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 12:47:48 +00:00
|
|
|
// RemoveChannel removes the channel with the given channel ID
|
|
|
|
func (ps *Store) RemoveChannel(channelID string) error {
|
|
|
|
return ps.ds.Delete(dskeyForChannel(channelID))
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
// The datastore key used to identify the channel info
|
2020-08-06 12:47:48 +00:00
|
|
|
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
|
|
|
|
func (ps *Store) putChannelInfo(ci *ChannelInfo) error {
|
2020-08-06 12:47:48 +00:00
|
|
|
if len(ci.ChannelID) == 0 {
|
|
|
|
ci.ChannelID = uuid.New().String()
|
|
|
|
}
|
|
|
|
k := dskeyForChannel(ci.ChannelID)
|
2019-09-10 13:43:01 +00:00
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
b, err := marshallChannelInfo(ci)
|
2019-09-10 13:43:01 +00:00
|
|
|
if err != nil {
|
2020-07-28 23:16:47 +00:00
|
|
|
return err
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
return ps.ds.Put(k, b)
|
2019-09-10 13:43:01 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:47:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return stored, nil
|
2019-08-09 21:42:56 +00:00
|
|
|
}
|