2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-12-06 00:27:32 +00:00
|
|
|
"github.com/minio/blake2b-simd"
|
2019-11-26 21:58:43 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-12-06 00:27:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-26 21:58:43 +00:00
|
|
|
|
|
|
|
vstate "github.com/filecoin-project/chain-validation/pkg/state"
|
|
|
|
vactors "github.com/filecoin-project/chain-validation/pkg/state/actors"
|
|
|
|
vaddress "github.com/filecoin-project/chain-validation/pkg/state/address"
|
|
|
|
vtypes "github.com/filecoin-project/chain-validation/pkg/state/types"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-12-19 20:15:14 +00:00
|
|
|
"github.com/filecoin-project/go-crypto"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-02-11 20:48:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen/genesis"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-11-26 21:58:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StateWrapper struct {
|
|
|
|
// The blockstore underlying the state tree and storage.
|
|
|
|
bs blockstore.Blockstore
|
|
|
|
// 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
|
|
|
// A store for encryption keys.
|
|
|
|
keys *keyStore
|
|
|
|
|
|
|
|
// CID of the root of the state tree.
|
|
|
|
stateRoot cid.Cid
|
|
|
|
// The root node of the state tree, essentially a cache of LoadStateTree(cst, stateRoot)
|
|
|
|
//tree *state.StateTree
|
|
|
|
// A look-through storage implementation.
|
|
|
|
storage *directStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vstate.Wrapper = &StateWrapper{}
|
|
|
|
|
|
|
|
func NewState() *StateWrapper {
|
|
|
|
bs := blockstore.NewBlockstore(datastore.NewMapDatastore())
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(bs)
|
2019-10-17 04:53:25 +00:00
|
|
|
// 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.
|
|
|
|
}
|
2020-01-22 20:56:40 +00:00
|
|
|
root, err := treeImpl.Flush(context.TODO())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
storageImpl := &directStorage{cst}
|
|
|
|
return &StateWrapper{bs, cst, newKeyStore(), root, storageImpl}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Cid() cid.Cid {
|
|
|
|
return s.stateRoot
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) Actor(addr vaddress.Address) (vstate.Actor, error) {
|
|
|
|
vaddr, err := address.NewFromBytes(addr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fcActor, err := tree.GetActor(vaddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &actorWrapper{*fcActor}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) Storage(addr vaddress.Address) (vstate.Storage, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
return s.storage, nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) NewAccountAddress() (vaddress.Address, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
return s.keys.NewAddress()
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) SetActor(addr vaddress.Address, code vactors.ActorCodeID, balance vtypes.BigInt) (vstate.Actor, vstate.Storage, error) {
|
|
|
|
addrInt, err := address.NewFromBytes(addr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: fromActorCode(code),
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}}
|
|
|
|
// The ID-based address is dropped here, but should be reported back to the caller.
|
|
|
|
_, err = tree.RegisterNewAddress(addrInt, &actr.Actor)
|
|
|
|
if err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("register new address for actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return actr, s.storage, s.flush(tree)
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) SetSingletonActor(addr vactors.SingletonActorID, balance vtypes.BigInt) (vstate.Actor, vstate.Storage, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
vaddr := fromSingletonAddress(addr)
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
|
|
|
|
lotusAddr, err := address.NewFromBytes(vaddr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
switch lotusAddr {
|
|
|
|
case actors.InitAddress:
|
2020-02-11 20:48:03 +00:00
|
|
|
initact, err := genesis.SetupInitActor(s.bs, nil)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.InitAddress, initact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set init actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &actorWrapper{*initact}, s.storage, s.flush(tree)
|
|
|
|
case actors.StorageMarketAddress:
|
2020-02-11 20:48:03 +00:00
|
|
|
nsroot, err := genesis.SetupStorageMarketActor(s.bs, s.stateRoot, nil)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-29 03:38:18 +00:00
|
|
|
s.stateRoot = nsroot
|
|
|
|
|
|
|
|
tree, err = state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
smact, err := tree.GetActor(actors.StorageMarketAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*smact}, s.storage, s.flush(tree)
|
|
|
|
case actors.StoragePowerAddress:
|
2020-02-11 20:48:03 +00:00
|
|
|
spact, err := genesis.SetupStoragePowerActor(s.bs)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.StoragePowerAddress, spact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network storage market actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*spact}, s.storage, s.flush(tree)
|
|
|
|
case actors.NetworkAddress:
|
|
|
|
ntwkact := &types.Actor{
|
|
|
|
Code: actors.AccountCodeCid,
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.NetworkAddress, ntwkact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*ntwkact}, s.storage, s.flush(tree)
|
|
|
|
case actors.BurntFundsAddress:
|
|
|
|
ntwkact := &types.Actor{
|
|
|
|
Code: actors.AccountCodeCid,
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.BurntFundsAddress, ntwkact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*ntwkact}, s.storage, s.flush(tree)
|
|
|
|
default:
|
2019-11-22 18:31:15 +00:00
|
|
|
return nil, nil, xerrors.Errorf("%v is not a singleton actor address", addr)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) Sign(ctx context.Context, addr vaddress.Address, data []byte) (*vtypes.Signature, error) {
|
|
|
|
sig, err := s.keys.Sign(ctx, addr, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &vtypes.Signature{
|
|
|
|
Type: sig.Type,
|
|
|
|
Data: sig.Data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
func (s *StateWrapper) Signer() *keyStore {
|
|
|
|
return s.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-01-22 20:56:40 +00:00
|
|
|
s.stateRoot, err = tree.Flush(context.TODO())
|
2019-10-17 04:53:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Key store
|
|
|
|
//
|
|
|
|
type keyStore struct {
|
|
|
|
// Private keys by address
|
2019-11-26 21:58:43 +00:00
|
|
|
keys map[vaddress.Address]vtypes.KeyInfo
|
2019-10-17 04:53:25 +00:00
|
|
|
// Seed for deterministic key generation.
|
|
|
|
seed int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func newKeyStore() *keyStore {
|
|
|
|
return &keyStore{
|
2019-11-26 21:58:43 +00:00
|
|
|
keys: make(map[vaddress.Address]vtypes.KeyInfo),
|
2019-10-17 04:53:25 +00:00
|
|
|
seed: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *keyStore) NewAddress() (vaddress.Address, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
randSrc := rand.New(rand.NewSource(s.seed))
|
|
|
|
prv, err := crypto.GenerateKeyFromSeed(randSrc)
|
|
|
|
if err != nil {
|
2019-11-26 21:58:43 +00:00
|
|
|
return vaddress.Undef, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
vki := vtypes.KeyInfo{
|
2019-10-17 04:53:25 +00:00
|
|
|
PrivateKey: prv,
|
|
|
|
Type: types.KTSecp256k1,
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
key, err := wallet.NewKey(types.KeyInfo{
|
|
|
|
Type: vki.Type,
|
|
|
|
PrivateKey: vki.PrivateKey,
|
|
|
|
})
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
2019-11-26 21:58:43 +00:00
|
|
|
return vaddress.Undef, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
vaddr, err := vaddress.NewFromBytes(key.Address.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return vaddress.Undef, err
|
|
|
|
}
|
|
|
|
s.keys[vaddr] = vki
|
2019-10-17 04:53:25 +00:00
|
|
|
s.seed++
|
2019-11-26 21:58:43 +00:00
|
|
|
return vaddress.NewFromBytes(key.Address.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *keyStore) Sign(ctx context.Context, addr vaddress.Address, data []byte) (*types.Signature, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
ki, ok := s.keys[addr]
|
|
|
|
if !ok {
|
|
|
|
return &types.Signature{}, fmt.Errorf("unknown address %v", addr)
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
b2sum := blake2b.Sum256(data)
|
|
|
|
digest, err := crypto.Sign(ki.PrivateKey, b2sum[:])
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return &types.Signature{}, err
|
|
|
|
}
|
|
|
|
return &types.Signature{
|
|
|
|
Type: types.KTSecp256k1,
|
|
|
|
Data: digest,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Nonce() uint64 {
|
|
|
|
return a.Actor.Nonce
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (a *actorWrapper) Balance() vtypes.BigInt {
|
|
|
|
return vtypes.NewInt(a.Actor.Balance.Uint64())
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Storage
|
|
|
|
//
|
|
|
|
|
|
|
|
type directStorage struct {
|
2020-02-04 22:19:05 +00:00
|
|
|
cst cbor.IpldStore
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (d *directStorage) Get(c cid.Cid, out interface{}) error {
|
|
|
|
if err := d.cst.Get(context.TODO(), c, out.(cbg.CBORUnmarshaler)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func fromActorCode(code vactors.ActorCodeID) cid.Cid {
|
2019-10-17 04:53:25 +00:00
|
|
|
switch code {
|
2019-11-26 21:58:43 +00:00
|
|
|
case vactors.AccountActorCodeCid:
|
2019-10-17 04:53:25 +00:00
|
|
|
return actors.AccountCodeCid
|
2019-11-26 21:58:43 +00:00
|
|
|
case vactors.StorageMinerCodeCid:
|
2019-10-17 04:53:25 +00:00
|
|
|
return actors.StorageMinerCodeCid
|
2019-11-26 21:58:43 +00:00
|
|
|
case vactors.MultisigActorCodeCid:
|
2019-10-17 04:53:25 +00:00
|
|
|
return actors.MultisigCodeCid
|
2019-11-26 21:58:43 +00:00
|
|
|
case vactors.PaymentChannelActorCodeCid:
|
2019-10-17 04:53:25 +00:00
|
|
|
return actors.PaymentChannelCodeCid
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown actor code: %v", code))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
func fromSingletonAddress(addr vactors.SingletonActorID) vaddress.Address {
|
2019-10-17 04:53:25 +00:00
|
|
|
switch addr {
|
2019-11-26 21:58:43 +00:00
|
|
|
case vactors.InitAddress:
|
|
|
|
out, err := vaddress.NewFromBytes(actors.InitAddress.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
case vactors.NetworkAddress:
|
2019-12-06 00:27:32 +00:00
|
|
|
out, err := vaddress.NewFromBytes(actors.NetworkAddress.Bytes())
|
2019-11-26 21:58:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
case vactors.StorageMarketAddress:
|
2019-12-06 00:27:32 +00:00
|
|
|
out, err := vaddress.NewFromBytes(actors.StorageMarketAddress.Bytes())
|
2019-11-26 21:58:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
case vactors.BurntFundsAddress:
|
2019-12-06 00:27:32 +00:00
|
|
|
out, err := vaddress.NewFromBytes(actors.BurntFundsAddress.Bytes())
|
2019-11-26 21:58:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
case vactors.StoragePowerAddress:
|
2019-12-06 00:27:32 +00:00
|
|
|
out, err := vaddress.NewFromBytes(actors.StoragePowerAddress.Bytes())
|
2019-11-26 21:58:43 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out
|
2019-10-17 04:53:25 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown singleton actor address: %v", addr))
|
|
|
|
}
|
|
|
|
}
|