2020-08-05 12:20:13 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-22 10:56:32 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-09-23 01:32:02 +00:00
|
|
|
init_ "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-ipld-format"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Surgeon is an object used to fetch and manipulate state.
|
|
|
|
type Surgeon struct {
|
|
|
|
ctx context.Context
|
|
|
|
api api.FullNode
|
2020-08-14 12:51:43 +00:00
|
|
|
stores *Stores
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSurgeon returns a state surgeon, an object used to fetch and manipulate
|
|
|
|
// state.
|
2020-08-14 12:51:43 +00:00
|
|
|
func NewSurgeon(ctx context.Context, api api.FullNode, stores *Stores) *Surgeon {
|
2020-08-05 12:20:13 +00:00
|
|
|
return &Surgeon{
|
|
|
|
ctx: ctx,
|
|
|
|
api: api,
|
|
|
|
stores: stores,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 20:41:16 +00:00
|
|
|
func (sg *Surgeon) GetStateTreeRootFromTipset(tsk types.TipSetKey) (cid.Cid, error) {
|
|
|
|
ts, err := sg.api.ChainGetTipSet(sg.ctx, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts.ParentState(), nil
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:20:13 +00:00
|
|
|
// GetMaskedStateTree trims the state tree at the supplied tipset to contain
|
|
|
|
// only the state of the actors in the retain set. It also "dives" into some
|
|
|
|
// singleton system actors, like the init actor, to trim the state so as to
|
|
|
|
// compute a minimal state tree. In the future, thid method will dive into
|
|
|
|
// other system actors like the power actor and the market actor.
|
2020-08-28 20:41:16 +00:00
|
|
|
func (sg *Surgeon) GetMaskedStateTree(previousRoot cid.Cid, retain []address.Address) (cid.Cid, error) {
|
2020-09-23 01:32:02 +00:00
|
|
|
// TODO: this will need to be parameterized on network version.
|
2020-09-27 11:02:34 +00:00
|
|
|
st, err := state.LoadStateTree(sg.stores.CBORStore, previousRoot)
|
2020-09-23 01:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
initActor, initState, err := sg.loadInitActor(st)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
err = sg.retainInitEntries(initState, retain)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
err = sg.saveInitActor(initActor, initState, st)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
// resolve all addresses to ID addresses.
|
|
|
|
resolved, err := sg.resolveAddresses(retain, initState)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
st, err = sg.transplantActors(st, resolved)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
root, err := st.Flush(sg.ctx)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccessedActors identifies the actors that were accessed during the
|
|
|
|
// execution of a message.
|
|
|
|
func (sg *Surgeon) GetAccessedActors(ctx context.Context, a api.FullNode, mid cid.Cid) ([]address.Address, error) {
|
|
|
|
log.Printf("calculating accessed actors during execution of message: %s", mid)
|
|
|
|
msgInfo, err := a.StateSearchMsg(ctx, mid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-16 17:39:37 +00:00
|
|
|
if msgInfo == nil {
|
|
|
|
return nil, fmt.Errorf("message info is nil")
|
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-28 20:41:16 +00:00
|
|
|
msgObj, err := a.ChainGetMessage(ctx, mid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:20:13 +00:00
|
|
|
ts, err := a.ChainGetTipSet(ctx, msgInfo.TipSet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-28 20:41:16 +00:00
|
|
|
trace, err := a.StateCall(ctx, msgObj, ts.Parents())
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not replay msg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessed := make(map[address.Address]struct{})
|
2020-09-27 11:02:34 +00:00
|
|
|
|
2020-08-05 12:20:13 +00:00
|
|
|
var recur func(trace *types.ExecutionTrace)
|
|
|
|
recur = func(trace *types.ExecutionTrace) {
|
|
|
|
accessed[trace.Msg.To] = struct{}{}
|
|
|
|
accessed[trace.Msg.From] = struct{}{}
|
|
|
|
for _, s := range trace.Subcalls {
|
|
|
|
recur(&s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recur(&trace.ExecutionTrace)
|
|
|
|
|
|
|
|
ret := make([]address.Address, 0, len(accessed))
|
|
|
|
for k := range accessed {
|
|
|
|
ret = append(ret, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteCAR recursively writes the tree referenced by the root as a CAR into the
|
|
|
|
// supplied io.Writer.
|
2020-08-05 14:52:57 +00:00
|
|
|
func (sg *Surgeon) WriteCAR(w io.Writer, roots ...cid.Cid) error {
|
2020-08-05 12:20:13 +00:00
|
|
|
carWalkFn := func(nd format.Node) (out []*format.Link, err error) {
|
|
|
|
for _, link := range nd.Links() {
|
|
|
|
if link.Cid.Prefix().Codec == cid.FilCommitmentSealed || link.Cid.Prefix().Codec == cid.FilCommitmentUnsealed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, link)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2020-08-05 14:52:57 +00:00
|
|
|
return car.WriteCarWithWalker(sg.ctx, sg.stores.DAGService, roots, w, carWalkFn)
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 11:43:28 +00:00
|
|
|
// WriteCARIncluding writes a CAR including only the CIDs that are listed in
|
|
|
|
// the include set. This leads to an intentially sparse tree with dangling links.
|
|
|
|
func (sg *Surgeon) WriteCARIncluding(w io.Writer, include map[cid.Cid]struct{}, roots ...cid.Cid) error {
|
|
|
|
carWalkFn := func(nd format.Node) (out []*format.Link, err error) {
|
|
|
|
for _, link := range nd.Links() {
|
|
|
|
if _, ok := include[link.Cid]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if link.Cid.Prefix().Codec == cid.FilCommitmentSealed || link.Cid.Prefix().Codec == cid.FilCommitmentUnsealed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, link)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
return car.WriteCarWithWalker(sg.ctx, sg.stores.DAGService, roots, w, carWalkFn)
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
// transplantActors plucks the state from the supplied actors at the given
|
2020-08-05 12:20:13 +00:00
|
|
|
// tipset, and places it into the supplied state map.
|
2020-09-27 11:02:34 +00:00
|
|
|
func (sg *Surgeon) transplantActors(src *state.StateTree, pluck []address.Address) (*state.StateTree, error) {
|
|
|
|
log.Printf("transplanting actor states: %v", pluck)
|
|
|
|
|
|
|
|
dst, err := state.NewStateTree(sg.stores.CBORStore, src.Version())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:20:13 +00:00
|
|
|
for _, a := range pluck {
|
2020-09-27 11:02:34 +00:00
|
|
|
actor, err := src.GetActor(a)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
2020-09-27 11:02:34 +00:00
|
|
|
return nil, fmt.Errorf("get actor %s failed: %w", a, err)
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
err = dst.SetActor(a, actor)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
2020-09-27 11:02:34 +00:00
|
|
|
return nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
// recursive copy of the actor state.
|
|
|
|
err = vm.Copy(context.TODO(), sg.stores.Blockstore, sg.stores.Blockstore, actor.Head)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
2020-09-27 11:02:34 +00:00
|
|
|
return nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actorState, err := sg.api.ChainReadObj(sg.ctx, actor.Head)
|
|
|
|
if err != nil {
|
2020-09-27 11:02:34 +00:00
|
|
|
return nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := sg.stores.CBORStore.Put(sg.ctx, &cbg.Deferred{Raw: actorState})
|
|
|
|
if err != nil {
|
2020-09-27 11:02:34 +00:00
|
|
|
return nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cid != actor.Head {
|
|
|
|
panic("mismatched cids")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
return dst, nil
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// saveInitActor saves the state of the init actor to the provided state map.
|
2020-09-27 11:02:34 +00:00
|
|
|
func (sg *Surgeon) saveInitActor(initActor *types.Actor, initState init_.State, st *state.StateTree) error {
|
2020-08-05 12:20:13 +00:00
|
|
|
log.Printf("saving init actor into state tree")
|
|
|
|
|
|
|
|
// Store the state of the init actor.
|
|
|
|
cid, err := sg.stores.CBORStore.Put(sg.ctx, initState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-23 01:32:02 +00:00
|
|
|
actor := *initActor
|
|
|
|
actor.Head = cid
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
err = st.SetActor(init_.Address, &actor)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-27 11:02:34 +00:00
|
|
|
cid, _ = st.Flush(sg.ctx)
|
2020-08-05 12:20:13 +00:00
|
|
|
log.Printf("saved init actor into state tree; new root: %s", cid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// retainInitEntries takes an old init actor state, and retains only the
|
|
|
|
// entries in the retain set, returning a new init actor state.
|
2020-09-24 11:31:03 +00:00
|
|
|
func (sg *Surgeon) retainInitEntries(state init_.State, retain []address.Address) error {
|
2020-08-05 12:20:13 +00:00
|
|
|
log.Printf("retaining init actor entries for addresses: %v", retain)
|
|
|
|
|
2020-09-24 11:31:03 +00:00
|
|
|
m := make(map[address.Address]struct{}, len(retain))
|
|
|
|
for _, a := range retain {
|
|
|
|
m[a] = struct{}{}
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 11:31:03 +00:00
|
|
|
var remove []address.Address
|
|
|
|
_ = state.ForEachActor(func(id abi.ActorID, address address.Address) error {
|
|
|
|
if _, ok := m[address]; !ok {
|
|
|
|
remove = append(remove, address)
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
2020-09-24 11:31:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-09-24 11:31:03 +00:00
|
|
|
err := state.Remove(remove...)
|
|
|
|
log.Printf("new init actor state: %+v", state)
|
|
|
|
return err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolveAddresses resolved the requested addresses from the provided
|
|
|
|
// InitActor state, returning a slice of length len(orig), where each index
|
|
|
|
// contains the resolved address.
|
2020-09-23 01:32:02 +00:00
|
|
|
func (sg *Surgeon) resolveAddresses(orig []address.Address, ist init_.State) (ret []address.Address, err error) {
|
2020-08-05 12:20:13 +00:00
|
|
|
log.Printf("resolving addresses: %v", orig)
|
|
|
|
|
|
|
|
ret = make([]address.Address, len(orig))
|
|
|
|
for i, addr := range orig {
|
2020-09-23 01:32:02 +00:00
|
|
|
resolved, found, err := ist.ResolveAddress(addr)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-23 01:32:02 +00:00
|
|
|
if !found {
|
2020-08-15 23:03:58 +00:00
|
|
|
return nil, fmt.Errorf("address not found: %s", addr)
|
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
ret[i] = resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("resolved addresses: %v", ret)
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadInitActor loads the init actor state from a given tipset.
|
2020-09-27 11:02:34 +00:00
|
|
|
func (sg *Surgeon) loadInitActor(st *state.StateTree) (*types.Actor, init_.State, error) {
|
2020-09-24 11:31:03 +00:00
|
|
|
actor, err := st.GetActor(init_.Address)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
2020-09-23 01:32:02 +00:00
|
|
|
return nil, nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 11:31:03 +00:00
|
|
|
initState, err := init_.Load(sg.stores.ADTStore, actor)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
2020-09-23 01:32:02 +00:00
|
|
|
return nil, nil, err
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("loaded init actor state: %+v", initState)
|
|
|
|
|
2020-09-23 01:32:02 +00:00
|
|
|
return actor, initState, nil
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|