2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-12-06 00:27:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2020-04-24 22:08:16 +00:00
|
|
|
vstate "github.com/filecoin-project/chain-validation/state"
|
2020-07-23 02:05:11 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-27 22:17:08 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-07-23 02:05:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
2019-10-17 04:53:25 +00:00
|
|
|
)
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
var _ vstate.VMWrapper = &StateWrapper{}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
type StateWrapper struct {
|
|
|
|
// The blockstore underlying the state tree and storage.
|
|
|
|
bs blockstore.Blockstore
|
2020-03-03 01:24:07 +00:00
|
|
|
|
|
|
|
ds datastore.Batching
|
2019-10-17 04:53:25 +00:00
|
|
|
// HAMT-CBOR store on top of the blockstore.
|
2020-02-04 22:19:05 +00:00
|
|
|
cst cbor.IpldStore
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
// CID of the root of the state tree.
|
|
|
|
stateRoot cid.Cid
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func NewState() *StateWrapper {
|
2020-07-23 02:05:11 +00:00
|
|
|
bs := blockstore.NewTemporary()
|
2020-02-27 22:17:08 +00:00
|
|
|
cst := cbor.NewCborStore(bs)
|
|
|
|
// Put EmptyObjectCid value in the store. When an actor is initially created its Head is set to this value.
|
|
|
|
_, err := cst.Put(context.TODO(), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
treeImpl, err := state.NewStateTree(cst)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Never returns error, the error return should be removed.
|
|
|
|
}
|
|
|
|
root, err := treeImpl.Flush(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-03 01:24:07 +00:00
|
|
|
return &StateWrapper{
|
|
|
|
bs: bs,
|
|
|
|
ds: datastore.NewMapDatastore(),
|
|
|
|
cst: cst,
|
|
|
|
stateRoot: root,
|
|
|
|
}
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 22:08:16 +00:00
|
|
|
func (s *StateWrapper) NewVM() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) Root() cid.Cid {
|
|
|
|
return s.stateRoot
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:08:16 +00:00
|
|
|
// StoreGet the value at key from vm store
|
|
|
|
func (s *StateWrapper) StoreGet(key cid.Cid, out runtime.CBORUnmarshaler) error {
|
2020-02-27 22:17:08 +00:00
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
2020-04-24 22:08:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tree.Store.Get(context.Background(), key, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorePut `value` into vm store
|
|
|
|
func (s *StateWrapper) StorePut(value runtime.CBORMarshaler) (cid.Cid, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
2020-04-24 22:08:16 +00:00
|
|
|
return tree.Store.Put(context.Background(), value)
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Actor(addr address.Address) (vstate.Actor, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fcActor, err := tree.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &actorWrapper{*fcActor}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) SetActorState(addr address.Address, balance abi.TokenAmount, actorState runtime.CBORMarshaler) (vstate.Actor, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// actor should exist
|
|
|
|
act, err := tree.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add the state to the store and get a new head cid
|
|
|
|
actHead, err := tree.Store.Put(context.Background(), actorState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// update the actor object with new head and balance parameter
|
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: act.Code,
|
|
|
|
Nonce: act.Nonce,
|
|
|
|
// updates
|
|
|
|
Head: actHead,
|
|
|
|
Balance: balance,
|
|
|
|
}}
|
|
|
|
if err := tree.SetActor(addr, &actr.Actor); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return actr, s.flush(tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) CreateActor(code cid.Cid, addr address.Address, balance abi.TokenAmount, actorState runtime.CBORMarshaler) (vstate.Actor, address.Address, error) {
|
2020-03-25 06:21:22 +00:00
|
|
|
idAddr := addr
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
|
|
|
|
actHead, err := tree.Store.Put(context.Background(), actorState)
|
2020-02-27 22:17:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
2020-03-25 06:21:22 +00:00
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: code,
|
|
|
|
Head: actHead,
|
|
|
|
Balance: balance,
|
|
|
|
}}
|
|
|
|
|
|
|
|
idAddr, err = tree.RegisterNewAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, xerrors.Errorf("register new address for actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.SetActor(addr, &actr.Actor); err != nil {
|
|
|
|
return nil, address.Undef, xerrors.Errorf("setting new actor for actor: %w", err)
|
|
|
|
}
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
2020-03-25 06:21:22 +00:00
|
|
|
|
|
|
|
// store newState
|
|
|
|
head, err := tree.Store.Put(context.Background(), actorState)
|
2020-02-27 22:17:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
2020-03-25 06:21:22 +00:00
|
|
|
|
|
|
|
// create and store actor object
|
|
|
|
a := types.Actor{
|
2020-02-27 22:17:08 +00:00
|
|
|
Code: code,
|
2020-03-25 06:21:22 +00:00
|
|
|
Head: head,
|
2020-02-27 22:17:08 +00:00
|
|
|
Balance: balance,
|
|
|
|
}
|
2020-03-25 06:21:22 +00:00
|
|
|
if err := tree.SetActor(idAddr, &a); err != nil {
|
|
|
|
return nil, address.Undef, err
|
2020-03-03 23:01:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 06:21:22 +00:00
|
|
|
return &actorWrapper{a}, idAddr, s.flush(tree)
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flushes a state tree to storage and sets this state's root to that tree's root CID.
|
|
|
|
func (s *StateWrapper) flush(tree *state.StateTree) (err error) {
|
|
|
|
s.stateRoot, err = tree.Flush(context.TODO())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Actor Wrapper
|
|
|
|
//
|
|
|
|
|
|
|
|
type actorWrapper struct {
|
|
|
|
types.Actor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Code() cid.Cid {
|
|
|
|
return a.Actor.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Head() cid.Cid {
|
|
|
|
return a.Actor.Head
|
|
|
|
}
|
|
|
|
|
2020-03-23 06:39:42 +00:00
|
|
|
func (a *actorWrapper) CallSeqNum() uint64 {
|
|
|
|
return a.Actor.Nonce
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Balance() big.Int {
|
|
|
|
return a.Actor.Balance
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Storage
|
|
|
|
//
|
|
|
|
|
|
|
|
type contextStore struct {
|
|
|
|
cbor.IpldStore
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *contextStore) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|