2020-02-13 00:28:23 +00:00
|
|
|
package paychmgr
|
2019-09-24 21:13:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-12 23:52:19 +00:00
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-24 21:13:47 +00:00
|
|
|
)
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
type stateAccessor struct {
|
2020-08-11 14:20:05 +00:00
|
|
|
sm stateManagerAPI
|
2020-07-28 23:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
func (ca *stateAccessor) loadPaychActorState(ctx context.Context, ch address.Address) (*types.Actor, paych.State, error) {
|
|
|
|
return ca.sm.GetPaychState(ctx, ch, nil)
|
2019-09-24 21:13:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 23:16:47 +00:00
|
|
|
func (ca *stateAccessor) loadStateChannelInfo(ctx context.Context, ch address.Address, dir uint64) (*ChannelInfo, error) {
|
2020-08-17 15:13:13 +00:00
|
|
|
_, st, err := ca.loadPaychActorState(ctx, ch)
|
2020-07-09 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
// Load channel "From" account actor state
|
2020-09-23 05:19:43 +00:00
|
|
|
f, err := st.From()
|
2020-07-09 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 05:19:43 +00:00
|
|
|
from, err := ca.sm.ResolveToKeyAddress(ctx, f, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t, err := st.To()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
to, err := ca.sm.ResolveToKeyAddress(ctx, t, nil)
|
2020-07-09 22:27:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:01:16 +00:00
|
|
|
nextLane, err := ca.nextLaneFromState(ctx, st)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:27:39 +00:00
|
|
|
ci := &ChannelInfo{
|
2020-07-28 23:16:47 +00:00
|
|
|
Channel: &ch,
|
2020-07-09 22:27:39 +00:00
|
|
|
Direction: dir,
|
2020-08-11 21:01:16 +00:00
|
|
|
NextLane: nextLane,
|
2020-07-09 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if dir == DirOutbound {
|
|
|
|
ci.Control = from
|
|
|
|
ci.Target = to
|
|
|
|
} else {
|
|
|
|
ci.Control = to
|
|
|
|
ci.Target = from
|
|
|
|
}
|
|
|
|
|
|
|
|
return ci, nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
func (ca *stateAccessor) nextLaneFromState(ctx context.Context, st paych.State) (uint64, error) {
|
|
|
|
laneCount, err := st.LaneCount()
|
2020-08-11 21:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-16 04:06:04 +00:00
|
|
|
if laneCount == 0 {
|
2020-08-11 21:01:16 +00:00
|
|
|
return 0, nil
|
2020-07-09 22:27:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
maxID := uint64(0)
|
|
|
|
if err := st.ForEachLaneState(func(idx uint64, _ paych.LaneState) error {
|
|
|
|
if idx > maxID {
|
|
|
|
maxID = idx
|
2020-07-09 22:27:39 +00:00
|
|
|
}
|
2020-08-11 21:01:16 +00:00
|
|
|
return nil
|
2020-08-13 14:14:11 +00:00
|
|
|
}); err != nil {
|
2020-08-13 00:27:04 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-08-11 21:01:16 +00:00
|
|
|
|
2020-09-16 04:06:04 +00:00
|
|
|
return maxID + 1, nil
|
2020-07-09 22:27:39 +00:00
|
|
|
}
|