2020-02-13 00:28:23 +00:00
|
|
|
package paychmgr
|
2019-09-24 21:13:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-07-09 22:27:39 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/account"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-12 23:52:19 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/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
|
|
|
}
|
|
|
|
|
|
|
|
func (ca *stateAccessor) loadPaychState(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.loadPaychState(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
|
|
|
|
|
|
|
|
ci := &ChannelInfo{
|
2020-07-28 23:16:47 +00:00
|
|
|
Channel: &ch,
|
2020-07-09 22:27:39 +00:00
|
|
|
Direction: dir,
|
|
|
|
NextLane: nextLaneFromState(st),
|
|
|
|
}
|
|
|
|
|
|
|
|
if dir == DirOutbound {
|
|
|
|
ci.Control = from
|
|
|
|
ci.Target = to
|
|
|
|
} else {
|
|
|
|
ci.Control = to
|
|
|
|
ci.Target = from
|
|
|
|
}
|
|
|
|
|
|
|
|
return ci, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextLaneFromState(st *paych.State) uint64 {
|
|
|
|
if len(st.LaneStates) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
maxLane := st.LaneStates[0].ID
|
|
|
|
for _, state := range st.LaneStates {
|
|
|
|
if state.ID > maxLane {
|
|
|
|
maxLane = state.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxLane + 1
|
|
|
|
}
|