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"
|
2019-08-13 04:27:54 +00:00
|
|
|
"strings"
|
2019-09-10 13:43:01 +00:00
|
|
|
"sync"
|
2019-08-12 17:09:56 +00:00
|
|
|
|
2020-02-13 00:10:07 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
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-08-13 04:27:54 +00:00
|
|
|
"golang.org/x/xerrors"
|
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-09-10 13:43:01 +00:00
|
|
|
lk sync.Mutex // TODO: this can be split per paych
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2019-09-09 13:59:07 +00:00
|
|
|
type VoucherInfo struct {
|
2020-02-13 00:10:07 +00:00
|
|
|
Voucher *paych.SignedVoucher
|
2019-09-09 13:59:07 +00:00
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
type ChannelInfo struct {
|
2019-09-10 13:43:01 +00:00
|
|
|
Channel address.Address
|
|
|
|
Control address.Address
|
|
|
|
Target address.Address
|
|
|
|
|
2019-11-04 19:03:11 +00:00
|
|
|
Direction uint64
|
2019-09-10 13:43:01 +00:00
|
|
|
Vouchers []*VoucherInfo
|
2020-02-21 17:26:44 +00:00
|
|
|
NextLane uint64
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dskeyForChannel(addr address.Address) datastore.Key {
|
2019-08-12 19:05:57 +00:00
|
|
|
return datastore.NewKey(addr.String())
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) putChannelInfo(ci *ChannelInfo) error {
|
|
|
|
k := dskeyForChannel(ci.Channel)
|
|
|
|
|
2019-11-05 16:56:43 +00:00
|
|
|
b, err := cborrpc.Dump(ci)
|
|
|
|
if err != nil {
|
2019-08-12 17:09:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:56:43 +00:00
|
|
|
return ps.ds.Put(k, b)
|
2019-08-12 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) getChannelInfo(addr address.Address) (*ChannelInfo, error) {
|
|
|
|
k := dskeyForChannel(addr)
|
|
|
|
|
|
|
|
b, err := ps.ds.Get(k)
|
2019-09-06 22:39:47 +00:00
|
|
|
if err == datastore.ErrNotFound {
|
|
|
|
return nil, ErrChannelNotTracked
|
|
|
|
}
|
2019-08-12 17:09:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ci ChannelInfo
|
2019-11-04 19:03:11 +00:00
|
|
|
if err := ci.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
|
2019-08-12 17:09:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ci, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) TrackChannel(ch *ChannelInfo) error {
|
2019-09-10 13:43:01 +00:00
|
|
|
ps.lk.Lock()
|
|
|
|
defer ps.lk.Unlock()
|
|
|
|
|
2019-09-16 17:23:48 +00:00
|
|
|
return ps.trackChannel(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) trackChannel(ch *ChannelInfo) error {
|
2019-08-12 17:09:56 +00:00
|
|
|
_, err := ps.getChannelInfo(ch.Channel)
|
|
|
|
switch err {
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
case nil:
|
|
|
|
return fmt.Errorf("already tracking channel: %s", ch.Channel)
|
2019-09-06 22:39:47 +00:00
|
|
|
case ErrChannelNotTracked:
|
2019-08-12 17:09:56 +00:00
|
|
|
return ps.putChannelInfo(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Store) ListChannels() ([]address.Address, error) {
|
2019-09-10 13:43:01 +00:00
|
|
|
ps.lk.Lock()
|
|
|
|
defer ps.lk.Unlock()
|
|
|
|
|
2019-08-12 19:05:57 +00:00
|
|
|
res, err := ps.ds.Query(dsq.Query{KeysOnly: true})
|
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
|
|
|
|
|
|
|
var out []address.Address
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
addr, err := address.NewFromString(strings.TrimPrefix(res.Key, "/"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed reading paych key (%q) from datastore: %w", res.Key, err)
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
out = append(out, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
2019-08-09 21:42:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:43:01 +00:00
|
|
|
func (ps *Store) findChan(filter func(*ChannelInfo) bool) (address.Address, error) {
|
|
|
|
res, err := ps.ds.Query(dsq.Query{})
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2020-05-27 20:53:20 +00:00
|
|
|
defer res.Close() //nolint:errcheck
|
2019-09-10 13:43:01 +00:00
|
|
|
|
|
|
|
var ci ChannelInfo
|
|
|
|
|
|
|
|
for {
|
|
|
|
res, ok := res.NextSync()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:03:11 +00:00
|
|
|
if err := ci.UnmarshalCBOR(bytes.NewReader(res.Value)); err != nil {
|
2019-09-10 13:43:01 +00:00
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filter(&ci) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := address.NewFromString(strings.TrimPrefix(res.Key, "/"))
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("failed reading paych key (%q) from datastore: %w", res.Key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.Undef, nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:26:44 +00:00
|
|
|
func (ps *Store) AllocateLane(ch address.Address) (uint64, error) {
|
2019-09-10 13:43:01 +00:00
|
|
|
ps.lk.Lock()
|
|
|
|
defer ps.lk.Unlock()
|
|
|
|
|
|
|
|
ci, err := ps.getChannelInfo(ch)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := ci.NextLane
|
|
|
|
ci.NextLane++
|
|
|
|
|
|
|
|
return out, ps.putChannelInfo(ci)
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:59:07 +00:00
|
|
|
func (ps *Store) VouchersForPaych(ch address.Address) ([]*VoucherInfo, error) {
|
2019-08-12 19:51:01 +00:00
|
|
|
ci, err := ps.getChannelInfo(ch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ci.Vouchers, nil
|
2019-08-09 21:42:56 +00:00
|
|
|
}
|