From 187e386740c52f1ec044ecfa14c1f8e5466cc51b Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Wed, 6 May 2020 16:53:32 -0400 Subject: [PATCH] Bugfix: Correctly transfer gas when failing to charge for retValue --- chain/vm/vm.go | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 7aa22fee2..fc98a5dc3 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -360,29 +360,27 @@ func (vm *VM) ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (*ApplyRet, return nil, xerrors.Errorf("[from=%s,to=%s,n=%d,m=%d,h=%d] fatal error: %w", msg.From, msg.To, msg.Nonce, msg.Method, vm.blockHeight, actorErr) } - { - if rt == nil { - return nil, xerrors.Errorf("send returned nil runtime, send error was: %s", actorErr) - } - actorErr2 := rt.chargeGasSafe(rt.Pricelist().OnChainReturnValue(len(ret))) - if actorErr2 != nil { - return &ApplyRet{ - MessageReceipt: types.MessageReceipt{ - ExitCode: aerrors.RetCode(actorErr2), - GasUsed: rt.gasUsed, - }, - ActorErr: actorErr2, - Penalty: types.NewInt(0), - Duration: time.Since(start), - }, nil - - } - } - if actorErr != nil { log.Warnw("Send actor error", "from", msg.From, "to", msg.To, "nonce", msg.Nonce, "method", msg.Method, "height", vm.blockHeight, "error", fmt.Sprintf("%+v", actorErr)) } + if actorErr != nil && len(ret) != 0 { + // This should not happen, something is wonky + return nil, xerrors.Errorf("message invocation errored, but had a return value anyway: %w", actorErr) + } + + if rt == nil { + return nil, xerrors.Errorf("send returned nil runtime, send error was: %s", actorErr) + } + + if len(ret) != 0 { + // safely override actorErr since it must be nil + actorErr = rt.chargeGasSafe(rt.Pricelist().OnChainReturnValue(len(ret))) + if actorErr != nil { + ret = nil + } + } + var errcode exitcode.ExitCode var gasUsed int64