Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert reason for private enhanced tx #1315

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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