2020-07-17 02:26:22 +00:00
|
|
|
package settler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-17 23:01:22 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/paychmgr"
|
|
|
|
|
2020-07-17 23:01:22 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-04-20 21:34:28 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
|
|
|
paychtypes "github.com/filecoin-project/go-state-types/builtin/v8/paych"
|
2020-09-21 23:01:29 +00:00
|
|
|
|
2020-07-17 02:26:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
payapi "github.com/filecoin-project/lotus/node/impl/paych"
|
2021-02-02 17:43:49 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2020-07-17 02:26:22 +00:00
|
|
|
)
|
|
|
|
|
2020-07-17 23:01:22 +00:00
|
|
|
var log = logging.Logger("payment-channel-settler")
|
|
|
|
|
2020-07-17 02:26:22 +00:00
|
|
|
// API are the dependencies need to run the payment channel settler
|
|
|
|
type API struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
full.ChainAPI
|
|
|
|
full.StateAPI
|
|
|
|
payapi.PaychAPI
|
|
|
|
}
|
|
|
|
|
2020-07-17 23:01:22 +00:00
|
|
|
type settlerAPI interface {
|
|
|
|
PaychList(context.Context) ([]address.Address, error)
|
|
|
|
PaychStatus(context.Context, address.Address) (*api.PaychStatus, error)
|
2022-04-20 21:34:28 +00:00
|
|
|
PaychVoucherCheckSpendable(context.Context, address.Address, *paychtypes.SignedVoucher, []byte, []byte) (bool, error)
|
|
|
|
PaychVoucherList(context.Context, address.Address) ([]*paychtypes.SignedVoucher, error)
|
|
|
|
PaychVoucherSubmit(context.Context, address.Address, *paychtypes.SignedVoucher, []byte, []byte) (cid.Cid, error)
|
2021-04-03 11:20:50 +00:00
|
|
|
StateWaitMsg(ctx context.Context, cid cid.Cid, confidence uint64, limit abi.ChainEpoch, allowReplaced bool) (*api.MsgLookup, error)
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 23:01:22 +00:00
|
|
|
type paymentChannelSettler struct {
|
|
|
|
ctx context.Context
|
|
|
|
api settlerAPI
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SettlePaymentChannels checks the chain for events related to payment channels settling and
|
|
|
|
// submits any vouchers for inbound channels tracked for this node
|
2021-04-05 11:23:46 +00:00
|
|
|
func SettlePaymentChannels(mctx helpers.MetricsCtx, lc fx.Lifecycle, papi API) error {
|
2021-02-02 17:43:49 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
2020-07-17 02:26:22 +00:00
|
|
|
lc.Append(fx.Hook{
|
2021-02-02 17:43:49 +00:00
|
|
|
OnStart: func(context.Context) error {
|
2021-04-05 11:23:46 +00:00
|
|
|
pcs := newPaymentChannelSettler(ctx, &papi)
|
2021-08-04 00:10:30 +00:00
|
|
|
ev, err := events.NewEvents(ctx, &papi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ev.Called(ctx, pcs.check, pcs.messageHandler, pcs.revertHandler, int(build.MessageConfidence+1), events.NoTimeout, pcs.matcher)
|
2020-07-17 02:26:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-17 23:01:22 +00:00
|
|
|
func newPaymentChannelSettler(ctx context.Context, api settlerAPI) *paymentChannelSettler {
|
|
|
|
return &paymentChannelSettler{
|
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func (pcs *paymentChannelSettler) check(ctx context.Context, ts *types.TipSet) (done bool, more bool, err error) {
|
2020-07-17 02:26:22 +00:00
|
|
|
return false, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pcs *paymentChannelSettler) messageHandler(msg *types.Message, rec *types.MessageReceipt, ts *types.TipSet, curH abi.ChainEpoch) (more bool, err error) {
|
2020-12-14 11:30:52 +00:00
|
|
|
// Ignore unsuccessful settle messages
|
|
|
|
if rec.ExitCode != 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
bestByLane, err := paychmgr.BestSpendableByLane(pcs.ctx, pcs.api, msg.To)
|
2020-07-17 02:26:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2020-07-17 23:01:22 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(bestByLane))
|
2020-07-17 02:26:22 +00:00
|
|
|
for _, voucher := range bestByLane {
|
2020-08-20 16:09:52 +00:00
|
|
|
submitMessageCID, err := pcs.api.PaychVoucherSubmit(pcs.ctx, msg.To, voucher, nil, nil)
|
2020-07-17 02:26:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2022-04-20 21:34:28 +00:00
|
|
|
go func(voucher *paychtypes.SignedVoucher, submitMessageCID cid.Cid) {
|
2020-07-17 23:01:22 +00:00
|
|
|
defer wg.Done()
|
2021-04-05 11:23:46 +00:00
|
|
|
msgLookup, err := pcs.api.StateWaitMsg(pcs.ctx, submitMessageCID, build.MessageConfidence, api.LookbackNoLimit, true)
|
2020-07-17 23:01:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("submitting voucher: %s", err.Error())
|
2021-05-21 15:39:43 +00:00
|
|
|
return
|
2020-07-17 23:01:22 +00:00
|
|
|
}
|
|
|
|
if msgLookup.Receipt.ExitCode != 0 {
|
|
|
|
log.Errorf("failed submitting voucher: %+v", voucher)
|
|
|
|
}
|
|
|
|
}(voucher, submitMessageCID)
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
2020-07-17 23:01:22 +00:00
|
|
|
wg.Wait()
|
2020-07-17 02:26:22 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pcs *paymentChannelSettler) revertHandler(ctx context.Context, ts *types.TipSet) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:00:32 +00:00
|
|
|
func (pcs *paymentChannelSettler) matcher(msg *types.Message) (matched bool, err error) {
|
2020-07-17 02:26:22 +00:00
|
|
|
// Check if this is a settle payment channel message
|
2022-04-20 21:34:28 +00:00
|
|
|
if msg.Method != builtin.MethodsPaych.Settle {
|
2020-10-30 13:00:32 +00:00
|
|
|
return false, nil
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
2020-07-17 23:01:22 +00:00
|
|
|
// Check if this payment channel is of concern to this node (i.e. tracked in payment channel store),
|
2020-07-17 02:26:22 +00:00
|
|
|
// and its inbound (i.e. we're getting vouchers that we may need to redeem)
|
2020-07-17 23:01:22 +00:00
|
|
|
trackedAddresses, err := pcs.api.PaychList(pcs.ctx)
|
2020-07-17 02:26:22 +00:00
|
|
|
if err != nil {
|
2020-10-30 13:00:32 +00:00
|
|
|
return false, err
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
|
|
|
for _, addr := range trackedAddresses {
|
|
|
|
if msg.To == addr {
|
2020-07-17 23:01:22 +00:00
|
|
|
status, err := pcs.api.PaychStatus(pcs.ctx, addr)
|
2020-07-17 02:26:22 +00:00
|
|
|
if err != nil {
|
2020-10-30 13:00:32 +00:00
|
|
|
return false, err
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
|
|
|
if status.Direction == api.PCHInbound {
|
2020-10-30 13:00:32 +00:00
|
|
|
return true, nil
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 13:00:32 +00:00
|
|
|
return false, nil
|
2020-07-17 02:26:22 +00:00
|
|
|
}
|