Skip to content

Commit

Permalink
fix: revert reason for private enhanced tx (#1315)
Browse files Browse the repository at this point in the history
* fix: revert reason for private enhanced tx
* return revert reason in error message on rpc calls

Co-authored-by: Antony Denyer <[email protected]>
  • Loading branch information
baptiste-b-pegasys and antonydenyer authored Feb 2, 2022
1 parent 9d90642 commit 7fdc414
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction, pr

// runSimulation runs a simulation of the given transaction.
// It returns the EVM instance upon completion
func runSimulation(ctx context.Context, b Backend, from common.Address, tx *types.Transaction) (*vm.EVM, error) {
func runSimulation(ctx context.Context, b Backend, from common.Address, tx *types.Transaction) (*vm.EVM, []byte, error) {
defer func(start time.Time) {
log.Debug("Simulated Execution EVM call finished", "runtime", time.Since(start))
}(time.Now())
Expand Down Expand Up @@ -2074,11 +2074,11 @@ func runSimulation(ctx context.Context, b Backend, from common.Address, tx *type
blockNumber := b.CurrentBlock().Number().Uint64()
stateAtBlock, header, err := b.StateAndHeaderByNumber(ctx, rpc.BlockNumber(blockNumber))
if stateAtBlock == nil || err != nil {
return nil, err
return nil, nil, err
}
evm, _, err := b.GetEVM(ctx, msg, stateAtBlock, header)
if err != nil {
return nil, err
return nil, nil, err
}

// Wait for the context to be done and cancel the evm. Even if the
Expand All @@ -2093,7 +2093,8 @@ func runSimulation(ctx context.Context, b Backend, from common.Address, tx *type
// even the creation of a contract (init code) can invoke other contracts
if tx.To() != nil {
// removed contract availability checks as they are performed in checkAndHandlePrivateTransaction
_, _, err = evm.Call(vm.AccountRef(addr), *tx.To(), tx.Data(), tx.Gas(), tx.Value())
data, _, err := evm.Call(vm.AccountRef(addr), *tx.To(), tx.Data(), tx.Gas(), tx.Value())
return evm, data, err
} else {
_, contractAddr, _, err = evm.Create(vm.AccountRef(addr), tx.Data(), tx.Gas(), tx.Value())
//make sure that nonce is same in simulation as in actual block processing
Expand All @@ -2103,7 +2104,7 @@ func runSimulation(ctx context.Context, b Backend, from common.Address, tx *type
evm.StateDB.SetNonce(contractAddr, 1)
}
}
return evm, err
return evm, nil, err
}

// SendTransaction creates a transaction for the given argument, sign it and submit it to the
Expand Down Expand Up @@ -2965,7 +2966,7 @@ func simulateExecutionForPE(ctx context.Context, b Backend, from common.Address,
return nil, common.Hash{}, nil
}

evm, err := runSimulation(ctx, b, from, privateTx)
evm, data, err := runSimulation(ctx, b, from, privateTx)
if evm == nil {
log.Debug("TX Simulation setup failed", "error", err)
return nil, common.Hash{}, err
Expand All @@ -2976,6 +2977,19 @@ func simulateExecutionForPE(ctx context.Context, b Backend, from common.Address,
"Continuing to simulation checks.", "error", err)
} else {
log.Trace("Simulated execution", "error", err)
if len(data) > 0 && errors.Is(err, vm.ErrExecutionReverted) {
reason, errUnpack := abi.UnpackRevert(data)

reasonError := errors.New("execution reverted")
if errUnpack == nil {
reasonError = fmt.Errorf("execution reverted: %v", reason)
}
err = &revertError{
error: reasonError,
reason: hexutil.Encode(data),
}

}
return nil, common.Hash{}, err
}
}
Expand Down

0 comments on commit 7fdc414

Please sign in to comment.