Skip to content

Commit

Permalink
refactor: rename guard and abortf
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Sep 8, 2020
1 parent 6d540a0 commit b9381df
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
12 changes: 6 additions & 6 deletions chain/actors/aerrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type ActorError interface {
error
IsFatal() bool
RetCode() exitcode.ExitCode
// guard is a sentinel to prevent code outside if the aerrors package
// from implementing the ActorError interface. It ensures actors
// use Runtime.Abortf and do not panic with an ActorError directly
// and illegally using a system error code.
guard() guard
// _internal is a sentinel to prevent code outside if the aerrors package
// from implementing the ActorError interface. It ensures actors use
// Runtime.Abortf and do not panic with an ActorError directly and
// illegally using a system error code.
_internal() guard
}

type actorError struct {
Expand Down Expand Up @@ -73,7 +73,7 @@ func (e *actorError) Unwrap() error {
return e.err
}

func (e *actorError) guard() guard {
func (e *actorError) _internal() guard {
return guard{}
}

Expand Down
6 changes: 3 additions & 3 deletions chain/vm/invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func (b basicContract) Exports() []interface{} {
}

func (basicContract) InvokeSomething0(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
abortf(exitcode.ExitCode(params.B), "params.B")
vmabortf(exitcode.ExitCode(params.B), "params.B")
return nil
}

func (basicContract) BadParam(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
abortf(255, "bad params")
vmabortf(255, "bad params")
return nil
}

func (basicContract) InvokeSomething10(rt runtime.Runtime, params *basicParams) *adt.EmptyValue {
abortf(exitcode.ExitCode(params.B+10), "params.B")
vmabortf(exitcode.ExitCode(params.B+10), "params.B")
return nil
}

Expand Down
34 changes: 17 additions & 17 deletions chain/vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (rt *Runtime) shimCall(f func() interface{}) (rval []byte, aerr aerrors.Act
ret := f()

if !rt.callerValidated {
abortf(exitcode.SysErrorIllegalActor, "Caller MUST be validated during method execution")
vmabortf(exitcode.SysErrorIllegalActor, "Caller MUST be validated during method execution")
}

switch ret := ret.(type) {
Expand Down Expand Up @@ -159,7 +159,7 @@ func (rt *Runtime) ValidateImmediateCallerAcceptAny() {
func (rt *Runtime) CurrentBalance() abi.TokenAmount {
b, err := rt.GetBalance(rt.Message().Receiver())
if err != nil {
abortf(err.RetCode(), "get current balance: %v", err)
vmabortf(err.RetCode(), "get current balance: %v", err)
}
return b
}
Expand Down Expand Up @@ -221,16 +221,16 @@ func (rt *Runtime) NewActorAddress() address.Address {

func (rt *Runtime) CreateActor(codeID cid.Cid, address address.Address) {
if !builtin.IsBuiltinActor(codeID) {
abortf(exitcode.SysErrorIllegalArgument, "Can only create built-in actors.")
vmabortf(exitcode.SysErrorIllegalArgument, "Can only create built-in actors.")
}

if builtin.IsSingletonActor(codeID) {
abortf(exitcode.SysErrorIllegalArgument, "Can only have one instance of singleton actors.")
vmabortf(exitcode.SysErrorIllegalArgument, "Can only have one instance of singleton actors.")
}

_, err := rt.state.GetActor(address)
if err == nil {
abortf(exitcode.SysErrorIllegalArgument, "Actor address already exists")
vmabortf(exitcode.SysErrorIllegalArgument, "Actor address already exists")
}

rt.chargeGas(rt.Pricelist().OnCreateActor())
Expand All @@ -256,7 +256,7 @@ func (rt *Runtime) DeleteActor(beneficiary address.Address) {
act, err := rt.state.GetActor(rt.Message().Receiver())
if err != nil {
if xerrors.Is(err, types.ErrActorNotFound) {
abortf(exitcode.SysErrorIllegalActor, "failed to load actor in delete actor: %s", err)
vmabortf(exitcode.SysErrorIllegalActor, "failed to load actor in delete actor: %s", err)
}
panic(aerrors.Fatalf("failed to get actor: %s", err))
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func (rt *Runtime) ValidateImmediateCallerIs(as ...address.Address) {
return
}
}
abortf(exitcode.SysErrForbidden, "caller %s is not one of %s", rt.Message().Caller(), as)
vmabortf(exitcode.SysErrForbidden, "caller %s is not one of %s", rt.Message().Caller(), as)
}

func (rt *Runtime) Context() context.Context {
Expand All @@ -308,10 +308,10 @@ func (rt *Runtime) Abortf(code exitcode.ExitCode, msg string, args ...interface{
panic(aerrors.NewfSkip(2, code, msg, args...))
}

// abortf should be called by the runtime/vm to abort execution with ANY exit
// vmabortf should be called by the runtime/vm to abort execution with ANY exit
// code, including system codes.
func abortf(code exitcode.ExitCode, msg string, args ...interface{}) {
log.Warnf("abortf: " + fmt.Sprintf(msg, args...))
func vmabortf(code exitcode.ExitCode, msg string, args ...interface{}) {
log.Warnf("vmabortf: " + fmt.Sprintf(msg, args...))
panic(aerrors.NewfSkip(2, code, msg, args...))
}

Expand All @@ -330,7 +330,7 @@ func (rt *Runtime) ValidateImmediateCallerType(ts ...cid.Cid) {
return
}
}
abortf(exitcode.SysErrForbidden, "caller cid type %q was not one of %v", callerCid, ts)
vmabortf(exitcode.SysErrForbidden, "caller cid type %q was not one of %v", callerCid, ts)
}

func (rt *Runtime) CurrEpoch() abi.ChainEpoch {
Expand All @@ -347,13 +347,13 @@ func (dwt *dumbWrapperType) Into(um vmr.CBORUnmarshaler) error {

func (rt *Runtime) Send(to address.Address, method abi.MethodNum, m vmr.CBORMarshaler, value abi.TokenAmount) (vmr.SendReturn, exitcode.ExitCode) {
if !rt.allowInternal {
abortf(exitcode.SysErrorIllegalActor, "runtime.Send() is currently disallowed")
vmabortf(exitcode.SysErrorIllegalActor, "runtime.Send() is currently disallowed")
}
var params []byte
if m != nil {
buf := new(bytes.Buffer)
if err := m.MarshalCBOR(buf); err != nil {
abortf(exitcode.SysErrInvalidParameters, "failed to marshal input parameters: %s", err)
vmabortf(exitcode.SysErrInvalidParameters, "failed to marshal input parameters: %s", err)
}
params = buf.Bytes()
}
Expand Down Expand Up @@ -430,19 +430,19 @@ func (ssh *shimStateHandle) Create(obj vmr.CBORMarshaler) {
func (ssh *shimStateHandle) Readonly(obj vmr.CBORUnmarshaler) {
act, err := ssh.rt.state.GetActor(ssh.rt.Message().Receiver())
if err != nil {
abortf(exitcode.SysErrorIllegalArgument, "failed to get actor for Readonly state: %s", err)
vmabortf(exitcode.SysErrorIllegalArgument, "failed to get actor for Readonly state: %s", err)
}
ssh.rt.Get(act.Head, obj)
}

func (ssh *shimStateHandle) Transaction(obj vmr.CBORer, f func()) {
if obj == nil {
abortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
vmabortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
}

act, err := ssh.rt.state.GetActor(ssh.rt.Message().Receiver())
if err != nil {
abortf(exitcode.SysErrorIllegalActor, "failed to get actor for Transaction: %s", err)
vmabortf(exitcode.SysErrorIllegalActor, "failed to get actor for Transaction: %s", err)
}
baseState := act.Head
ssh.rt.Get(baseState, obj)
Expand Down Expand Up @@ -574,7 +574,7 @@ func (rt *Runtime) incrementNumActorsCreated() {

func (rt *Runtime) abortIfAlreadyValidated() {
if rt.callerValidated {
abortf(exitcode.SysErrorIllegalActor, "Method must validate caller identity exactly once")
vmabortf(exitcode.SysErrorIllegalActor, "Method must validate caller identity exactly once")
}
rt.callerValidated = true
}
Expand Down
2 changes: 1 addition & 1 deletion chain/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (vm *VM) makeRuntime(ctx context.Context, msg *types.Message, origin addres
vmm := *msg
resF, ok := rt.ResolveAddress(msg.From)
if !ok {
abortf(exitcode.SysErrInvalidReceiver, "resolve msg.From address failed")
vmabortf(exitcode.SysErrInvalidReceiver, "resolve msg.From address failed")
}
vmm.From = resF
rt.vmsg = &vmm
Expand Down

0 comments on commit b9381df

Please sign in to comment.