lotus/paychmgr/state.go

82 lines
1.9 KiB
Go
Raw Normal View History

package paychmgr
2019-09-24 21:13:47 +00:00
import (
"context"
"github.com/filecoin-project/go-address"
2020-02-12 23:52:19 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
xerrors "golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
2019-09-24 21:13:47 +00:00
)
func (pm *Manager) loadPaychState(ctx context.Context, ch address.Address) (*types.Actor, *actors.PaymentChannelActorState, error) {
var pcast actors.PaymentChannelActorState
act, err := pm.sm.LoadActorState(ctx, ch, &pcast, nil)
if err != nil {
return nil, nil, err
}
return act, &pcast, nil
}
2020-02-12 23:52:19 +00:00
func findLane(states []*paych.LaneState, lane uint64) *paych.LaneState {
var ls *paych.LaneState
for _, laneState := range states {
if uint64(laneState.ID) == lane {
ls = laneState
break
}
}
return ls
}
func (pm *Manager) laneState(ctx context.Context, ch address.Address, lane uint64) (paych.LaneState, error) {
2019-09-24 21:13:47 +00:00
_, state, err := pm.loadPaychState(ctx, ch)
if err != nil {
2020-02-12 23:52:19 +00:00
return paych.LaneState{}, err
2019-09-24 21:13:47 +00:00
}
// TODO: we probably want to call UpdateChannelState with all vouchers to be fully correct
// (but technically dont't need to)
// TODO: make sure this is correct
2020-02-12 23:52:19 +00:00
ls := findLane(state.LaneStates, lane)
if ls == nil {
ls = &paych.LaneState{
2020-02-13 00:10:07 +00:00
ID: lane,
2019-09-24 21:13:47 +00:00
Redeemed: types.NewInt(0),
Nonce: 0,
}
}
vouchers, err := pm.store.VouchersForPaych(ch)
if err != nil {
if err == ErrChannelNotTracked {
return *ls, nil
}
2020-02-12 23:52:19 +00:00
return paych.LaneState{}, err
2019-09-24 21:13:47 +00:00
}
for _, v := range vouchers {
for range v.Voucher.Merges {
2020-02-12 23:52:19 +00:00
return paych.LaneState{}, xerrors.Errorf("paych merges not handled yet")
2019-09-24 21:13:47 +00:00
}
2020-02-13 00:10:07 +00:00
if v.Voucher.Lane != lane {
2019-09-24 21:13:47 +00:00
continue
}
2020-02-13 00:10:07 +00:00
if v.Voucher.Nonce < ls.Nonce {
2019-09-24 21:13:47 +00:00
log.Warnf("Found outdated voucher: ch=%s, lane=%d, v.nonce=%d lane.nonce=%d", ch, lane, v.Voucher.Nonce, ls.Nonce)
continue
}
2020-02-13 00:10:07 +00:00
ls.Nonce = v.Voucher.Nonce
2019-09-24 21:13:47 +00:00
ls.Redeemed = v.Voucher.Amount
}
return *ls, nil
}