Merge pull request #1606 from filecoin-project/asr/internal

Remove SysErrInternal, some interop / spec changes
This commit is contained in:
Łukasz Magiera 2020-04-27 13:02:11 +02:00 committed by GitHub
commit 939d7a7a0b
5 changed files with 45 additions and 31 deletions

View File

@ -33,7 +33,7 @@ type Pricelist interface {
// OnDeleteActor returns the gas used for deleting an actor // OnDeleteActor returns the gas used for deleting an actor
OnDeleteActor() int64 OnDeleteActor() int64
OnVerifySignature(sigType crypto.SigType, planTextSize int) int64 OnVerifySignature(sigType crypto.SigType, planTextSize int) (int64, error)
OnHashing(dataSize int) int64 OnHashing(dataSize int) int64
OnComputeUnsealedSectorCid(proofType abi.RegisteredProof, pieces []abi.PieceInfo) int64 OnComputeUnsealedSectorCid(proofType abi.RegisteredProof, pieces []abi.PieceInfo) int64
OnVerifySeal(info abi.SealVerifyInfo) int64 OnVerifySeal(info abi.SealVerifyInfo) int64
@ -97,7 +97,11 @@ type pricedSyscalls struct {
// Verifies that a signature is valid for an address and plaintext. // Verifies that a signature is valid for an address and plaintext.
func (ps pricedSyscalls) VerifySignature(signature crypto.Signature, signer addr.Address, plaintext []byte) error { func (ps pricedSyscalls) VerifySignature(signature crypto.Signature, signer addr.Address, plaintext []byte) error {
ps.chargeGas(ps.pl.OnVerifySignature(signature.Type, len(plaintext))) c, err := ps.pl.OnVerifySignature(signature.Type, len(plaintext))
if err != nil {
return err
}
ps.chargeGas(c)
return ps.under.VerifySignature(signature, signer, plaintext) return ps.under.VerifySignature(signature, signer, plaintext)
} }

View File

@ -1,11 +1,10 @@
package vm package vm
import ( import (
"github.com/filecoin-project/lotus/chain/actors/aerrors" "fmt"
"github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/crypto" "github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
) )
type pricelistV0 struct { type pricelistV0 struct {
@ -127,12 +126,12 @@ func (pl *pricelistV0) OnDeleteActor() int64 {
} }
// OnVerifySignature // OnVerifySignature
func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) int64 { func (pl *pricelistV0) OnVerifySignature(sigType crypto.SigType, planTextSize int) (int64, error) {
costFn, ok := pl.verifySignature[sigType] costFn, ok := pl.verifySignature[sigType]
if !ok { if !ok {
panic(aerrors.Newf(exitcode.SysErrInternal, "Cost function for signature type %d not supported", sigType)) return 0, fmt.Errorf("cost function for signature type %d not supported", sigType)
} }
return costFn(int64(planTextSize)) return costFn(int64(planTextSize)), nil
} }
// OnHashing // OnHashing

View File

@ -13,7 +13,6 @@ import (
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/actors/aerrors" "github.com/filecoin-project/lotus/chain/actors/aerrors"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
) )
@ -33,25 +32,26 @@ var EmptyObjectCid cid.Cid
func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, aerrors.ActorError) { func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, aerrors.ActorError) {
addrID, err := rt.state.RegisterNewAddress(addr) addrID, err := rt.state.RegisterNewAddress(addr)
if err != nil { if err != nil {
return nil, aerrors.Absorb(err, exitcode.SysErrInternal, "registering actor address") return nil, aerrors.Escalate(err, "registering actor address")
} }
if err := rt.chargeGasSafe(PricelistByEpoch(rt.height).OnCreateActor()); err != nil { if err := rt.chargeGasSafe(PricelistByEpoch(rt.height).OnCreateActor()); err != nil {
return nil, err return nil, err
} }
act, aerr := makeActor(rt.state, addr) act, aerr := makeActor(addr)
if aerr != nil { if aerr != nil {
return nil, aerr return nil, aerr
} }
if err := rt.state.SetActor(addrID, act); err != nil { if err := rt.state.SetActor(addrID, act); err != nil {
return nil, aerrors.Absorb(err, exitcode.SysErrInternal, "creating new actor failed") return nil, aerrors.Escalate(err, "creating new actor failed")
} }
p, err := actors.SerializeParams(&addr) p, err := actors.SerializeParams(&addr)
if err != nil { if err != nil {
return nil, aerrors.Absorb(err, exitcode.SysErrInternal, "registering actor address") // TODO: Unsure whether this should be fatal
return nil, aerrors.Absorb(err, exitcode.SysErrInvalidParameters, "registering actor address")
} }
// call constructor on account // call constructor on account
@ -62,42 +62,42 @@ func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, aer
act, err = rt.state.GetActor(addrID) act, err = rt.state.GetActor(addrID)
if err != nil { if err != nil {
return nil, aerrors.Absorb(err, exitcode.SysErrInternal, "loading newly created actor failed") return nil, aerrors.Escalate(err, "loading newly created actor failed")
} }
return act, nil return act, nil
} }
func makeActor(st *state.StateTree, addr address.Address) (*types.Actor, aerrors.ActorError) { func makeActor(addr address.Address) (*types.Actor, aerrors.ActorError) {
switch addr.Protocol() { switch addr.Protocol() {
case address.BLS: case address.BLS:
return NewBLSAccountActor() return NewBLSAccountActor(), nil
case address.SECP256K1: case address.SECP256K1:
return NewSecp256k1AccountActor() return NewSecp256k1AccountActor(), nil
case address.ID: case address.ID:
return nil, aerrors.Newf(1, "no actor with given ID: %s", addr) return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no actor with given ID: %s", addr)
case address.Actor: case address.Actor:
return nil, aerrors.Newf(1, "no such actor: %s", addr) return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no such actor: %s", addr)
default: default:
return nil, aerrors.Newf(1, "address has unsupported protocol: %d", addr.Protocol()) return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "address has unsupported protocol: %d", addr.Protocol())
} }
} }
func NewBLSAccountActor() (*types.Actor, aerrors.ActorError) { func NewBLSAccountActor() *types.Actor {
nact := &types.Actor{ nact := &types.Actor{
Code: builtin.AccountActorCodeID, Code: builtin.AccountActorCodeID,
Balance: types.NewInt(0), Balance: types.NewInt(0),
Head: EmptyObjectCid, Head: EmptyObjectCid,
} }
return nact, nil return nact
} }
func NewSecp256k1AccountActor() (*types.Actor, aerrors.ActorError) { func NewSecp256k1AccountActor() *types.Actor {
nact := &types.Actor{ nact := &types.Actor{
Code: builtin.AccountActorCodeID, Code: builtin.AccountActorCodeID,
Balance: types.NewInt(0), Balance: types.NewInt(0),
Head: EmptyObjectCid, Head: EmptyObjectCid,
} }
return nact, nil return nact
} }

View File

@ -52,6 +52,7 @@ type Runtime struct {
internalExecutions []*types.ExecutionResult internalExecutions []*types.ExecutionResult
numActorsCreated uint64 numActorsCreated uint64
allowInternal bool
} }
func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount { func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount {
@ -315,6 +316,9 @@ func (dwt *dumbWrapperType) Into(um vmr.CBORUnmarshaler) error {
} }
func (rs *Runtime) Send(to address.Address, method abi.MethodNum, m vmr.CBORMarshaler, value abi.TokenAmount) (vmr.SendReturn, exitcode.ExitCode) { func (rs *Runtime) Send(to address.Address, method abi.MethodNum, m vmr.CBORMarshaler, value abi.TokenAmount) (vmr.SendReturn, exitcode.ExitCode) {
if !rs.allowInternal {
rs.Abortf(exitcode.SysErrorIllegalActor, "runtime.Send() is currently disallowed")
}
var params []byte var params []byte
if m != nil { if m != nil {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@ -407,20 +411,26 @@ func (ssh *shimStateHandle) Create(obj vmr.CBORMarshaler) {
func (ssh *shimStateHandle) Readonly(obj vmr.CBORUnmarshaler) { func (ssh *shimStateHandle) Readonly(obj vmr.CBORUnmarshaler) {
act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver()) act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver())
if err != nil { if err != nil {
ssh.rs.Abortf(exitcode.SysErrInternal, "failed to get actor for Readonly state: %s", err) ssh.rs.Abortf(exitcode.SysErrorIllegalArgument, "failed to get actor for Readonly state: %s", err)
} }
ssh.rs.Get(act.Head, obj) ssh.rs.Get(act.Head, obj)
} }
func (ssh *shimStateHandle) Transaction(obj vmr.CBORer, f func() interface{}) interface{} { func (ssh *shimStateHandle) Transaction(obj vmr.CBORer, f func() interface{}) interface{} {
if obj == nil {
ssh.rs.Abortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
}
act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver()) act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver())
if err != nil { if err != nil {
ssh.rs.Abortf(exitcode.SysErrInternal, "failed to get actor for Readonly state: %s", err) ssh.rs.Abortf(exitcode.SysErrorIllegalActor, "failed to get actor for Transaction: %s", err)
} }
baseState := act.Head baseState := act.Head
ssh.rs.Get(baseState, obj) ssh.rs.Get(baseState, obj)
ssh.rs.allowInternal = false
out := f() out := f()
ssh.rs.allowInternal = true
c := ssh.rs.Put(obj) c := ssh.rs.Put(obj)
@ -445,17 +455,17 @@ func (rt *Runtime) stateCommit(oldh, newh cid.Cid) aerrors.ActorError {
// TODO: we can make this more efficient in the future... // TODO: we can make this more efficient in the future...
act, err := rt.state.GetActor(rt.Message().Receiver()) act, err := rt.state.GetActor(rt.Message().Receiver())
if err != nil { if err != nil {
rt.Abortf(exitcode.SysErrInternal, "failed to get actor to commit state: %s", err) return aerrors.Escalate(err, "failed to get actor to commit state")
} }
if act.Head != oldh { if act.Head != oldh {
rt.Abortf(exitcode.ErrIllegalState, "failed to update, inconsistent base reference") return aerrors.Fatal("failed to update, inconsistent base reference")
} }
act.Head = newh act.Head = newh
if err := rt.state.SetActor(rt.Message().Receiver(), act); err != nil { if err := rt.state.SetActor(rt.Message().Receiver(), act); err != nil {
rt.Abortf(exitcode.SysErrInternal, "failed to set actor in commit state: %s", err) return aerrors.Fatalf("failed to set actor in commit state: %s", err)
} }
return nil return nil

View File

@ -44,16 +44,16 @@ func ResolveToKeyAddr(state types.StateTree, cst cbor.IpldStore, addr address.Ad
act, err := state.GetActor(addr) act, err := state.GetActor(addr)
if err != nil { if err != nil {
return address.Undef, aerrors.Newf(exitcode.SysErrInternal, "failed to find actor: %s", addr) return address.Undef, aerrors.Newf(exitcode.SysErrInvalidParameters, "failed to find actor: %s", addr)
} }
if act.Code != builtin.AccountActorCodeID { if act.Code != builtin.AccountActorCodeID {
return address.Undef, aerrors.Fatalf("address %s was not for an account actor", addr) return address.Undef, aerrors.Newf(exitcode.SysErrInvalidParameters, "address %s was not for an account actor", addr)
} }
var aast account.State var aast account.State
if err := cst.Get(context.TODO(), act.Head, &aast); err != nil { if err := cst.Get(context.TODO(), act.Head, &aast); err != nil {
return address.Undef, aerrors.Escalate(err, fmt.Sprintf("failed to get account actor state for %s", addr)) return address.Undef, aerrors.Absorb(err, exitcode.SysErrInvalidParameters, fmt.Sprintf("failed to get account actor state for %s", addr))
} }
return aast.Address, nil return aast.Address, nil
@ -100,6 +100,7 @@ func (vm *VM) makeRuntime(ctx context.Context, msg *types.Message, origin addres
gasAvailable: msg.GasLimit, gasAvailable: msg.GasLimit,
numActorsCreated: nac, numActorsCreated: nac,
pricelist: PricelistByEpoch(vm.blockHeight), pricelist: PricelistByEpoch(vm.blockHeight),
allowInternal: true,
} }
rt.cst = &cbor.BasicIpldStore{ rt.cst = &cbor.BasicIpldStore{