2019-07-25 22:15:03 +00:00
|
|
|
package state
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-30 00:46:56 +00:00
|
|
|
"fmt"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
2019-07-25 22:15:03 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-09-10 02:05:24 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 10:23:05 +00:00
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-12 10:23:05 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
var log = logging.Logger("statetree")
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
type StateTree struct {
|
|
|
|
root *hamt.Node
|
2019-07-25 22:15:03 +00:00
|
|
|
Store *hamt.CborIpldStore
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
actorcache map[address.Address]*types.Actor
|
2019-07-05 14:29:17 +00:00
|
|
|
snapshot cid.Cid
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStateTree(cst *hamt.CborIpldStore) (*StateTree, error) {
|
|
|
|
return &StateTree{
|
|
|
|
root: hamt.NewNode(cst),
|
2019-07-25 22:15:03 +00:00
|
|
|
Store: cst,
|
2019-07-12 10:23:05 +00:00
|
|
|
actorcache: make(map[address.Address]*types.Actor),
|
2019-07-05 14:29:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadStateTree(cst *hamt.CborIpldStore, c cid.Cid) (*StateTree, error) {
|
|
|
|
nd, err := hamt.LoadNode(context.Background(), cst, c)
|
|
|
|
if err != nil {
|
2019-07-30 16:04:36 +00:00
|
|
|
log.Errorf("loading hamt node %s failed: %s", c, err)
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StateTree{
|
|
|
|
root: nd,
|
2019-07-25 22:15:03 +00:00
|
|
|
Store: cst,
|
2019-07-12 10:23:05 +00:00
|
|
|
actorcache: make(map[address.Address]*types.Actor),
|
2019-07-05 14:29:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
iaddr, err := st.lookupID(addr)
|
|
|
|
if err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return xerrors.Errorf("ID lookup failed: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
addr = iaddr
|
|
|
|
}
|
|
|
|
|
|
|
|
cact, ok := st.actorcache[addr]
|
|
|
|
if ok {
|
|
|
|
if act == cact {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 09:45:22 +00:00
|
|
|
st.actorcache[addr] = act
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return st.root.Set(context.TODO(), string(addr.Bytes()), act)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateTree) lookupID(addr address.Address) (address.Address, error) {
|
2019-07-12 23:52:25 +00:00
|
|
|
act, err := st.GetActor(actors.InitActorAddress)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return address.Undef, xerrors.Errorf("getting init actor: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
var ias actors.InitActorState
|
2019-07-25 22:15:03 +00:00
|
|
|
if err := st.Store.Get(context.TODO(), act.Head, &ias); err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return address.Undef, xerrors.Errorf("loading init actor state: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return ias.Lookup(st.Store, addr)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
|
2019-07-30 00:46:56 +00:00
|
|
|
if addr == address.Undef {
|
|
|
|
return nil, fmt.Errorf("GetActor called on undefined address")
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
iaddr, err := st.lookupID(addr)
|
|
|
|
if err != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
if xerrors.Is(err, hamt.ErrNotFound) {
|
2019-09-10 23:03:17 +00:00
|
|
|
return nil, xerrors.Errorf("resolution lookup failed (%s): %w", addr, types.ErrActorNotFound)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-09-10 19:58:45 +00:00
|
|
|
return nil, xerrors.Errorf("address resolution: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
addr = iaddr
|
|
|
|
}
|
|
|
|
|
|
|
|
cact, ok := st.actorcache[addr]
|
|
|
|
if ok {
|
|
|
|
return cact, nil
|
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
var act types.Actor
|
|
|
|
err := st.root.Find(context.TODO(), string(addr.Bytes()), &act)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == hamt.ErrNotFound {
|
2019-07-22 18:17:42 +00:00
|
|
|
return nil, types.ErrActorNotFound
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
st.actorcache[addr] = &act
|
|
|
|
|
|
|
|
return &act, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateTree) Flush() (cid.Cid, error) {
|
|
|
|
for addr, act := range st.actorcache {
|
|
|
|
if err := st.root.Set(context.TODO(), string(addr.Bytes()), act); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 10:23:05 +00:00
|
|
|
st.actorcache = make(map[address.Address]*types.Actor)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
if err := st.root.Flush(context.TODO()); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return st.Store.Put(context.TODO(), st.root)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateTree) Snapshot() error {
|
|
|
|
ss, err := st.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st.snapshot = ss
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) (address.Address, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
var out address.Address
|
2019-07-12 23:52:25 +00:00
|
|
|
err := st.MutateActor(actors.InitActorAddress, func(initact *types.Actor) error {
|
|
|
|
var ias actors.InitActorState
|
2019-07-25 22:15:03 +00:00
|
|
|
if err := st.Store.Get(context.TODO(), initact.Head, &ias); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
oaddr, err := ias.AddActor(st.Store, addr)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out = oaddr
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
ncid, err := st.Store.Put(context.TODO(), &ias)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
initact.Head = ncid
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.SetActor(out, act); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateTree) Revert() error {
|
2019-07-25 22:15:03 +00:00
|
|
|
nd, err := hamt.LoadNode(context.Background(), st.Store, st.snapshot)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st.root = nd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) MutateActor(addr address.Address, f func(*types.Actor) error) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
act, err := st.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(act); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.SetActor(addr, act)
|
|
|
|
}
|