Skip to content

Commit

Permalink
Merge pull request #5397 from onflow/ramtin/5268-add-control-over-out…
Browse files Browse the repository at this point in the history
…come-of-execution

[Flow EVM] Reporting results for evm.Run and evm.coa.Call
  • Loading branch information
ramtinms authored Feb 21, 2024
2 parents aa7161b + f6e3e16 commit 84e22a4
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 233 deletions.
9 changes: 7 additions & 2 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestEVMRun(t *testing.T) {
import EVM from %s
access(all)
fun main(tx: [UInt8], coinbaseBytes: [UInt8; 20]) {
fun main(tx: [UInt8], coinbaseBytes: [UInt8; 20]): EVM.Result {
let coinbase = EVM.EVMAddress(bytes: coinbaseBytes)
EVM.run(tx: tx, coinbase: coinbase)
return EVM.run(tx: tx, coinbase: coinbase)
}
`,
sc.EVMContract.Address.HexWithPrefix(),
Expand Down Expand Up @@ -79,6 +79,11 @@ func TestEVMRun(t *testing.T) {
snapshot)
require.NoError(t, err)
require.NoError(t, output.Err)

res, err := stdlib.ResultSummaryFromEVMResultValue(output.Value)
require.NoError(t, err)
require.Equal(t, types.StatusSuccessful, res.Status)
require.Equal(t, types.ErrCodeNoError, res.ErrorCode)
})
})
}
Expand Down
107 changes: 0 additions & 107 deletions fvm/evm/handler/codeFinder.go

This file was deleted.

47 changes: 16 additions & 31 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (h *ContractHandler) FlowTokenAddress() common.Address {
return h.flowTokenAddress
}

func (h *ContractHandler) EVMContractAddress() common.Address {
return common.Address(h.evmContractAddress)
}

var _ types.ContractHandler = &ContractHandler{}

func NewContractHandler(
Expand Down Expand Up @@ -105,34 +109,19 @@ func (h *ContractHandler) LastExecutedBlock() *types.Block {
return block
}

// Run runs an rlpencoded evm transaction and
// RunOrPanic runs an rlpencoded evm transaction and
// collects the gas fees and pay it to the coinbase address provided.
func (h *ContractHandler) Run(rlpEncodedTx []byte, coinbase types.Address) {
func (h *ContractHandler) RunOrPanic(rlpEncodedTx []byte, coinbase types.Address) {
_, err := h.run(rlpEncodedTx, coinbase)
panicOnAnyError(err)
}

// TryRun tries to run an rlpencoded evm transaction and
// Run tries to run an rlpencoded evm transaction and
// collects the gas fees and pay it to the coinbase address provided.
func (h *ContractHandler) TryRun(rlpEncodedTx []byte, coinbase types.Address) *types.ResultSummary {
func (h *ContractHandler) Run(rlpEncodedTx []byte, coinbase types.Address) *types.ResultSummary {
res, err := h.run(rlpEncodedTx, coinbase)
rs := &types.ResultSummary{
Status: types.StatusSuccessful,
}
if err != nil {
panicOnFatalOrBackendError(err)
// remaining errors are validation errors
rs.ErrorCode = ValidationErrorCode(err)
rs.Status = types.StatusInvalid
return rs
}
if res.VMError != nil {
rs.ErrorCode = ExecutionErrorCode(res.VMError)
rs.Status = types.StatusFailed
rs.GasConsumed = res.GasConsumed
}
rs.GasConsumed = res.GasConsumed
return rs
panicOnFatalOrBackendError(err)
return types.NewResultSummary(res, err)
}

func (h *ContractHandler) run(
Expand Down Expand Up @@ -569,13 +558,13 @@ func (a *Account) deploy(code types.Code, gaslimit types.GasLimit, balance types
// it would limit the gas used according to the limit provided
// given it doesn't goes beyond what Flow transaction allows.
// the balance would be deducted from the OFA account and would be transferred to the target address
func (a *Account) Call(to types.Address, data types.Data, gaslimit types.GasLimit, balance types.Balance) types.Data {
data, err := a.call(to, data, gaslimit, balance)
panicOnAnyError(err)
return data
func (a *Account) Call(to types.Address, data types.Data, gaslimit types.GasLimit, balance types.Balance) *types.ResultSummary {
res, err := a.call(to, data, gaslimit, balance)
panicOnFatalOrBackendError(err)
return types.NewResultSummary(res, err)
}

func (a *Account) call(to types.Address, data types.Data, gaslimit types.GasLimit, balance types.Balance) (types.Data, error) {
func (a *Account) call(to types.Address, data types.Data, gaslimit types.GasLimit, balance types.Balance) (*types.Result, error) {
ctx, err := a.precheck(true, gaslimit)
if err != nil {
return nil, err
Expand All @@ -589,11 +578,7 @@ func (a *Account) call(to types.Address, data types.Data, gaslimit types.GasLimi
a.Nonce(),
)

res, err := a.fch.executeAndHandleCall(ctx, call, nil, false)
if err != nil {
return nil, err
}
return res.ReturnedValue, nil
return a.fch.executeAndHandleCall(ctx, call, nil, false)
}

func (a *Account) precheck(authroized bool, gaslimit types.GasLimit) (types.BlockContext, error) {
Expand Down
Loading

0 comments on commit 84e22a4

Please sign in to comment.