lotus/paychmgr/state.go

92 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/specs-actors/actors/util/adt"
2020-07-09 22:27:39 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/account"
"github.com/filecoin-project/go-address"
2020-02-12 23:52:19 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
"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 {
sm stateManagerAPI
2020-07-28 23:16:47 +00:00
}
func (ca *stateAccessor) loadPaychActorState(ctx context.Context, ch address.Address) (*types.Actor, *paych.State, error) {
2020-02-25 20:35:15 +00:00
var pcast paych.State
2020-07-28 23:16:47 +00:00
act, err := ca.sm.LoadActorState(ctx, ch, &pcast, nil)
2019-09-24 21:13:47 +00:00
if err != nil {
return nil, nil, err
}
return act, &pcast, nil
}
2020-07-28 23:16:47 +00:00
func (ca *stateAccessor) loadStateChannelInfo(ctx context.Context, ch address.Address, dir uint64) (*ChannelInfo, error) {
_, st, err := ca.loadPaychActorState(ctx, ch)
2020-07-09 22:27:39 +00:00
if err != nil {
return nil, err
}
var account account.State
2020-07-28 23:16:47 +00:00
_, err = ca.sm.LoadActorState(ctx, st.From, &account, nil)
2020-07-09 22:27:39 +00:00
if err != nil {
return nil, err
}
from := account.Address
2020-07-28 23:16:47 +00:00
_, err = ca.sm.LoadActorState(ctx, st.To, &account, nil)
2020-07-09 22:27:39 +00:00
if err != nil {
return nil, err
}
to := account.Address
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,
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
}
func (ca *stateAccessor) nextLaneFromState(ctx context.Context, st *paych.State) (uint64, error) {
store := ca.sm.AdtStore(ctx)
laneStates, err := adt.AsArray(store, st.LaneStates)
if err != nil {
return 0, err
}
if laneStates.Length() == 0 {
return 0, nil
2020-07-09 22:27:39 +00:00
}
2020-08-13 14:14:11 +00:00
maxID := int64(0)
if err := laneStates.ForEach(nil, func(i int64) error {
2020-08-13 14:14:11 +00:00
if i > maxID {
maxID = i
2020-07-09 22:27:39 +00:00
}
return nil
2020-08-13 14:14:11 +00:00
}); err != nil {
return 0, err
}
2020-08-13 14:14:11 +00:00
return uint64(maxID + 1), nil
2020-07-09 22:27:39 +00:00
}