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"
|
2020-03-03 23:01:35 +00:00
|
|
|
|
2020-02-25 20:54:58 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-02-21 17:13:50 +00:00
|
|
|
init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2020-02-12 07:22:55 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2019-07-05 14:29:17 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-01-22 19:53:06 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-09-10 02:05:24 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 10:23:05 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
var log = logging.Logger("statetree")
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// Stores actors state by their ID.
|
2019-07-05 14:29:17 +00:00
|
|
|
type StateTree struct {
|
|
|
|
root *hamt.Node
|
2020-02-04 22:19:05 +00:00
|
|
|
Store cbor.IpldStore
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
snaps *stateSnaps
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateSnaps struct {
|
2020-04-03 23:26:52 +00:00
|
|
|
layers []*stateSnapLayer
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateSnapLayer struct {
|
|
|
|
actors map[address.Address]streeOp
|
|
|
|
resolveCache map[address.Address]address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStateSnapLayer() *stateSnapLayer {
|
|
|
|
return &stateSnapLayer{
|
|
|
|
actors: make(map[address.Address]streeOp),
|
|
|
|
resolveCache: make(map[address.Address]address.Address),
|
|
|
|
}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type streeOp struct {
|
|
|
|
Act types.Actor
|
|
|
|
Delete bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStateSnaps() *stateSnaps {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss := &stateSnaps{}
|
|
|
|
ss.addLayer()
|
|
|
|
return ss
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) addLayer() {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers = append(ss.layers, newStateSnapLayer())
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) dropLayer() {
|
2020-04-03 01:20:49 +00:00
|
|
|
ss.layers[len(ss.layers)-1] = nil // allow it to be GCed
|
2020-04-03 00:09:41 +00:00
|
|
|
ss.layers = ss.layers[:len(ss.layers)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) mergeLastLayer() {
|
|
|
|
last := ss.layers[len(ss.layers)-1]
|
|
|
|
nextLast := ss.layers[len(ss.layers)-2]
|
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
for k, v := range last.actors {
|
|
|
|
nextLast.actors[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range last.resolveCache {
|
|
|
|
nextLast.resolveCache[k] = v
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss.dropLayer()
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
func (ss *stateSnaps) resolveAddress(addr address.Address) (address.Address, bool) {
|
|
|
|
for i := len(ss.layers) - 1; i >= 0; i-- {
|
|
|
|
resa, ok := ss.layers[i].resolveCache[addr]
|
|
|
|
if ok {
|
|
|
|
return resa, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return address.Undef, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) cacheResolveAddress(addr, resa address.Address) {
|
|
|
|
ss.layers[len(ss.layers)-1].resolveCache[addr] = resa
|
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
func (ss *stateSnaps) getActor(addr address.Address) (*types.Actor, error) {
|
|
|
|
for i := len(ss.layers) - 1; i >= 0; i-- {
|
2020-04-03 23:26:52 +00:00
|
|
|
act, ok := ss.layers[i].actors[addr]
|
2020-04-03 00:09:41 +00:00
|
|
|
if ok {
|
|
|
|
if act.Delete {
|
|
|
|
return nil, types.ErrActorNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &act.Act, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) setActor(addr address.Address, act *types.Actor) {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers[len(ss.layers)-1].actors[addr] = streeOp{Act: *act}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) deleteActor(addr address.Address) {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers[len(ss.layers)-1].actors[addr] = streeOp{Delete: true}
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
func NewStateTree(cst cbor.IpldStore) (*StateTree, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
return &StateTree{
|
2020-04-03 00:09:41 +00:00
|
|
|
root: hamt.NewNode(cst, hamt.UseTreeBitWidth(5)),
|
|
|
|
Store: cst,
|
|
|
|
snaps: newStateSnaps(),
|
2019-07-05 14:29:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
|
2020-02-14 14:14:39 +00:00
|
|
|
nd, err := hamt.LoadNode(context.Background(), cst, c, hamt.UseTreeBitWidth(5))
|
2019-07-05 14:29:17 +00:00
|
|
|
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{
|
2020-04-03 00:09:41 +00:00
|
|
|
root: nd,
|
|
|
|
Store: cst,
|
|
|
|
snaps: newStateSnaps(),
|
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-11-15 16:39:43 +00:00
|
|
|
iaddr, err := st.LookupID(addr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("ID lookup failed: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
addr = iaddr
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.setActor(addr, act)
|
|
|
|
return nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// `LookupID` gets the ID address of this actor's `addr` stored in the `InitActor`.
|
2019-11-15 16:39:43 +00:00
|
|
|
func (st *StateTree) LookupID(addr address.Address) (address.Address, error) {
|
|
|
|
if addr.Protocol() == address.ID {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
resa, ok := st.snaps.resolveAddress(addr)
|
|
|
|
if ok {
|
|
|
|
return resa, nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:54:58 +00:00
|
|
|
act, err := st.GetActor(builtin.InitActorAddr)
|
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
|
|
|
}
|
|
|
|
|
2020-02-25 20:35:15 +00:00
|
|
|
var ias init_.State
|
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
|
|
|
}
|
|
|
|
|
2020-02-22 13:10:46 +00:00
|
|
|
a, err := ias.ResolveAddress(&AdtStore{st.Store}, addr)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("resolve address %s: %w", addr, err)
|
|
|
|
}
|
2020-04-03 23:26:52 +00:00
|
|
|
|
|
|
|
st.snaps.cacheResolveAddress(addr, a)
|
|
|
|
|
2020-02-22 13:10:46 +00:00
|
|
|
return a, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// GetActor returns the actor from any type of `addr` provided.
|
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")
|
|
|
|
}
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// Transform `addr` to its ID format.
|
2019-11-15 16:39:43 +00:00
|
|
|
iaddr, err := st.LookupID(addr)
|
|
|
|
if err != nil {
|
2020-02-21 17:13:50 +00:00
|
|
|
if xerrors.Is(err, init_.ErrAddressNotFound) {
|
2020-02-22 13:10:46 +00:00
|
|
|
return nil, xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
return nil, xerrors.Errorf("address resolution: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
addr = iaddr
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
snapAct, err := st.snaps.getActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapAct != nil {
|
|
|
|
return snapAct, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
var act types.Actor
|
2019-11-15 16:39:43 +00:00
|
|
|
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
|
|
|
}
|
2019-09-30 23:55:35 +00:00
|
|
|
return nil, xerrors.Errorf("hamt find failed: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.setActor(addr, &act)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return &act, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
func (st *StateTree) DeleteActor(addr address.Address) error {
|
|
|
|
if addr == address.Undef {
|
|
|
|
return xerrors.Errorf("DeleteActor called on undefined address")
|
|
|
|
}
|
|
|
|
|
|
|
|
iaddr, err := st.LookupID(addr)
|
|
|
|
if err != nil {
|
|
|
|
if xerrors.Is(err, init_.ErrAddressNotFound) {
|
|
|
|
return xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("address resolution: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = iaddr
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
_, err = st.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.deleteActor(addr)
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
func (st *StateTree) Flush(ctx context.Context) (cid.Cid, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateTree.Flush")
|
|
|
|
defer span.End()
|
2020-04-03 00:09:41 +00:00
|
|
|
if len(st.snaps.layers) != 1 {
|
|
|
|
return cid.Undef, xerrors.Errorf("tried to flush state tree with snapshots on the stack")
|
|
|
|
}
|
2020-01-22 19:53:06 +00:00
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
for addr, sto := range st.snaps.layers[0].actors {
|
2020-04-03 00:09:41 +00:00
|
|
|
if sto.Delete {
|
|
|
|
if err := st.root.Delete(ctx, string(addr.Bytes())); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := st.root.Set(ctx, string(addr.Bytes()), &sto.Act); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
if err := st.root.Flush(ctx); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
return st.Store.Put(ctx, st.root)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
func (st *StateTree) Snapshot(ctx context.Context) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "stateTree.SnapShot")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.addLayer()
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 01:19:15 +00:00
|
|
|
func (st *StateTree) ClearSnapshot() {
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.mergeLastLayer()
|
2020-03-05 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
func (st *StateTree) RegisterNewAddress(addr address.Address) (address.Address, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
var out address.Address
|
2020-02-25 20:54:58 +00:00
|
|
|
err := st.MutateActor(builtin.InitActorAddr, func(initact *types.Actor) error {
|
2020-02-25 20:35:15 +00:00
|
|
|
var ias init_.State
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
oaddr, err := ias.MapAddressToNewID(&AdtStore{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
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
type AdtStore struct{ cbor.IpldStore }
|
2020-02-12 07:22:55 +00:00
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
func (a *AdtStore) Context() context.Context {
|
2020-02-12 07:22:55 +00:00
|
|
|
return context.TODO()
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
var _ adt.Store = (*AdtStore)(nil)
|
2020-02-12 07:22:55 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
func (st *StateTree) Revert() error {
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.dropLayer()
|
|
|
|
st.snaps.addLayer()
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|