2019-11-08 17:15:38 +00:00
|
|
|
package market
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-05-05 01:31:56 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-02-28 18:01:43 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2020-10-08 20:32:54 +00:00
|
|
|
"go.uber.org/fx"
|
2019-11-08 17:15:38 +00:00
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-11-08 17:15:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-09-23 06:18:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2020-08-05 00:29:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events/state"
|
2019-11-08 17:15:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
)
|
|
|
|
|
2020-02-28 18:01:43 +00:00
|
|
|
var log = logging.Logger("market_adapter")
|
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
// API is the dependencies need to run a fund manager
|
|
|
|
type API struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
full.ChainAPI
|
|
|
|
full.StateAPI
|
|
|
|
full.MpoolAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
// FundMgr monitors available balances and adds funds when EnsureAvailable is called
|
2019-11-08 17:15:38 +00:00
|
|
|
type FundMgr struct {
|
2020-08-05 00:29:31 +00:00
|
|
|
api fundMgrAPI
|
2019-11-08 17:15:38 +00:00
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
lk sync.RWMutex
|
2019-11-08 17:15:38 +00:00
|
|
|
available map[address.Address]types.BigInt
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
// StartFundManager creates a new fund manager and sets up event hooks to manage state changes
|
|
|
|
func StartFundManager(lc fx.Lifecycle, api API) *FundMgr {
|
|
|
|
fm := newFundMgr(&api)
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
ev := events.NewEvents(ctx, &api)
|
|
|
|
preds := state.NewStatePredicates(&api)
|
|
|
|
dealDiffFn := preds.OnStorageMarketActorChanged(preds.OnBalanceChanged(preds.AvailableBalanceChangedForAddresses(fm.getAddresses)))
|
|
|
|
match := func(oldTs, newTs *types.TipSet) (bool, events.StateChange, error) {
|
|
|
|
return dealDiffFn(ctx, oldTs.Key(), newTs.Key())
|
|
|
|
}
|
2020-08-15 00:44:53 +00:00
|
|
|
return ev.StateChanged(fm.checkFunc, fm.stateChanged, fm.revert, 0, events.NoTimeout, match)
|
2020-08-05 00:29:31 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return fm
|
|
|
|
}
|
|
|
|
|
|
|
|
type fundMgrAPI interface {
|
|
|
|
StateMarketBalance(context.Context, address.Address, types.TipSetKey) (api.MarketBalance, error)
|
2020-08-12 20:17:21 +00:00
|
|
|
MpoolPushMessage(context.Context, *types.Message, *api.MessageSendSpec) (*types.SignedMessage, error)
|
2020-08-06 01:33:11 +00:00
|
|
|
StateLookupID(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
2020-08-05 00:29:31 +00:00
|
|
|
}
|
2019-11-08 17:15:38 +00:00
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
func newFundMgr(api fundMgrAPI) *FundMgr {
|
|
|
|
return &FundMgr{
|
|
|
|
api: api,
|
2019-11-08 17:15:38 +00:00
|
|
|
available: map[address.Address]types.BigInt{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
// checkFunc tells the events api to simply proceed (we always want to watch)
|
|
|
|
func (fm *FundMgr) checkFunc(ts *types.TipSet) (done bool, more bool, err error) {
|
|
|
|
return false, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// revert handles reverts to balances
|
|
|
|
func (fm *FundMgr) revert(ctx context.Context, ts *types.TipSet) error {
|
|
|
|
// TODO: Is it ok to just ignore this?
|
|
|
|
log.Warn("balance change reverted; TODO: actually handle this!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateChanged handles balance changes monitored on the chain from one tipset to the next
|
|
|
|
func (fm *FundMgr) stateChanged(ts *types.TipSet, ts2 *types.TipSet, states events.StateChange, h abi.ChainEpoch) (more bool, err error) {
|
|
|
|
changedBalances, ok := states.(state.ChangedBalances)
|
|
|
|
if !ok {
|
|
|
|
panic("Expected state.ChangedBalances")
|
|
|
|
}
|
|
|
|
// overwrite our in memory cache with new values from chain (chain is canonical)
|
|
|
|
fm.lk.Lock()
|
|
|
|
for addr, balanceChange := range changedBalances {
|
2020-08-15 00:44:53 +00:00
|
|
|
if fm.available[addr].Int != nil {
|
|
|
|
log.Infof("State balance change recorded, prev: %s, new: %s", fm.available[addr].String(), balanceChange.To.String())
|
|
|
|
}
|
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
fm.available[addr] = balanceChange.To
|
|
|
|
}
|
|
|
|
fm.lk.Unlock()
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fm *FundMgr) getAddresses() []address.Address {
|
|
|
|
fm.lk.RLock()
|
|
|
|
defer fm.lk.RUnlock()
|
|
|
|
addrs := make([]address.Address, 0, len(fm.available))
|
|
|
|
for addr := range fm.available {
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureAvailable looks at the available balance in escrow for a given
|
|
|
|
// address, and if less than the passed in amount, adds the difference
|
2020-05-05 01:31:56 +00:00
|
|
|
func (fm *FundMgr) EnsureAvailable(ctx context.Context, addr, wallet address.Address, amt types.BigInt) (cid.Cid, error) {
|
2020-08-06 01:33:11 +00:00
|
|
|
idAddr, err := fm.api.StateLookupID(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2019-11-08 17:15:38 +00:00
|
|
|
fm.lk.Lock()
|
2020-10-09 20:18:43 +00:00
|
|
|
defer fm.lk.Unlock()
|
|
|
|
|
2020-08-15 00:44:53 +00:00
|
|
|
bal, err := fm.api.StateMarketBalance(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateAvail := types.BigSub(bal.Escrow, bal.Locked)
|
|
|
|
|
2020-08-06 01:33:11 +00:00
|
|
|
avail, ok := fm.available[idAddr]
|
2019-11-08 17:15:38 +00:00
|
|
|
if !ok {
|
2020-08-15 00:44:53 +00:00
|
|
|
avail = stateAvail
|
2019-11-08 17:15:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
toAdd := types.BigSub(amt, avail)
|
|
|
|
if toAdd.LessThan(types.NewInt(0)) {
|
|
|
|
toAdd = types.NewInt(0)
|
2019-11-08 17:15:38 +00:00
|
|
|
}
|
2020-08-06 01:33:11 +00:00
|
|
|
fm.available[idAddr] = big.Add(avail, toAdd)
|
2019-11-08 17:15:38 +00:00
|
|
|
|
2020-08-15 00:44:53 +00:00
|
|
|
log.Infof("Funds operation w/ Expected Balance: %s, In State: %s, Requested: %s, Adding: %s", avail.String(), stateAvail.String(), amt.String(), toAdd.String())
|
|
|
|
|
2020-07-30 00:27:22 +00:00
|
|
|
if toAdd.LessThanEqual(big.Zero()) {
|
|
|
|
return cid.Undef, nil
|
2020-08-05 00:29:31 +00:00
|
|
|
}
|
2020-02-13 03:50:37 +00:00
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
params, err := actors.SerializeParams(&addr)
|
|
|
|
if err != nil {
|
2020-10-09 20:18:43 +00:00
|
|
|
fm.available[idAddr] = avail
|
2020-08-05 00:29:31 +00:00
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2019-11-08 17:15:38 +00:00
|
|
|
|
2020-08-05 00:29:31 +00:00
|
|
|
smsg, err := fm.api.MpoolPushMessage(ctx, &types.Message{
|
2020-09-23 06:18:52 +00:00
|
|
|
To: market.Address,
|
2020-08-05 00:29:31 +00:00
|
|
|
From: wallet,
|
|
|
|
Value: toAdd,
|
2020-10-08 20:32:54 +00:00
|
|
|
Method: market.Methods.AddBalance,
|
2020-08-05 00:29:31 +00:00
|
|
|
Params: params,
|
2020-08-12 20:17:21 +00:00
|
|
|
}, nil)
|
2020-08-05 00:29:31 +00:00
|
|
|
if err != nil {
|
2020-10-09 20:18:43 +00:00
|
|
|
fm.available[idAddr] = avail
|
2020-08-05 00:29:31 +00:00
|
|
|
return cid.Undef, err
|
2020-07-30 00:27:22 +00:00
|
|
|
}
|
2020-08-05 00:29:31 +00:00
|
|
|
|
|
|
|
return smsg.Cid(), nil
|
2019-11-08 17:15:38 +00:00
|
|
|
}
|