2020-07-14 11:45:45 +00:00
|
|
|
package stmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-07-14 11:45:45 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (sm *StateManager) ParentStateTsk(tsk types.TipSetKey) (*state.StateTree, error) {
|
2020-07-14 11:45:45 +00:00
|
|
|
ts, err := sm.cs.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
2020-09-12 03:07:52 +00:00
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
2020-09-14 12:17:45 +00:00
|
|
|
return sm.ParentState(ts)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (sm *StateManager) ParentState(ts *types.TipSet) (*state.StateTree, error) {
|
2020-07-14 11:45:45 +00:00
|
|
|
cst := cbor.NewCborStore(sm.cs.Blockstore())
|
2020-09-12 03:07:52 +00:00
|
|
|
version := sm.GetNtwkVersion(context.TODO(), ts.Height()-1)
|
|
|
|
state, err := state.LoadStateTree(cst, sm.parentState(ts), version)
|
2020-07-14 11:45:45 +00:00
|
|
|
if err != nil {
|
2020-09-12 03:07:52 +00:00
|
|
|
return nil, xerrors.Errorf("load state tree: %w", err)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
return state, nil
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (sm *StateManager) StateTree(st cid.Cid, ntwkVersion network.Version) (*state.StateTree, error) {
|
2020-07-14 11:45:45 +00:00
|
|
|
cst := cbor.NewCborStore(sm.cs.Blockstore())
|
2020-09-12 03:07:52 +00:00
|
|
|
state, err := state.LoadStateTree(cst, st, ntwkVersion)
|
2020-07-14 11:45:45 +00:00
|
|
|
if err != nil {
|
2020-09-12 03:07:52 +00:00
|
|
|
return nil, xerrors.Errorf("load state tree: %w", err)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
return state, nil
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (sm *StateManager) LoadActor(_ context.Context, addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
|
|
|
state, err := sm.ParentState(ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
2020-09-12 03:07:52 +00:00
|
|
|
return state.GetActor(addr)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (sm *StateManager) LoadActorTsk(_ context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
|
|
|
state, err := sm.ParentStateTsk(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|
2020-09-12 03:07:52 +00:00
|
|
|
return state.GetActor(addr)
|
2020-07-14 11:45:45 +00:00
|
|
|
}
|