Skip to content

Commit

Permalink
Merge Execute and Execute2 into a single version with a struct parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertJordan committed May 11, 2023
1 parent 0801f88 commit 153f1b0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
42 changes: 23 additions & 19 deletions bindings/go/evmc/evmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,18 @@ func (vm *VM) SetOption(name string, value string) (err error) {
return err
}

func (vm *VM) Execute(ctx HostContext, rev Revision,
kind CallKind, static bool, depth int, gas int64,
recipient Address, sender Address, input []byte, value Hash,
code []byte) (output []byte, gasLeft int64, err error) {
result, err := vm.Execute2(ctx, rev, kind, static, depth, gas, recipient, sender, input, value, code)
return result.Output, result.GasLeft, err
type Parameters struct {
Context HostContext
Revision Revision
Kind CallKind
Static bool
Depth int
Gas int64
Recipient Address
Sender Address
Input []byte
Value Hash
Code []byte
}

type Result struct {
Expand All @@ -209,25 +215,23 @@ type Result struct {
GasRefund int64
}

func (vm *VM) Execute2(ctx HostContext, rev Revision,
kind CallKind, static bool, depth int, gas int64,
recipient Address, sender Address, input []byte, value Hash,
code []byte) (res Result, err error) {
func (vm *VM) Execute(params Parameters) (res Result, err error) {

flags := C.uint32_t(0)
if static {
if params.Static {
flags |= C.EVMC_STATIC
}

ctxId := addHostContext(ctx)
ctxId := addHostContext(params.Context)
// FIXME: Clarify passing by pointer vs passing by value.
evmcRecipient := evmcAddress(recipient)
evmcSender := evmcAddress(sender)
evmcValue := evmcBytes32(value)
result := C.execute_wrapper(vm.handle, C.uintptr_t(ctxId), uint32(rev),
C.enum_evmc_call_kind(kind), flags, C.int32_t(depth), C.int64_t(gas),
&evmcRecipient, &evmcSender, bytesPtr(input), C.size_t(len(input)), &evmcValue,
bytesPtr(code), C.size_t(len(code)))
evmcRecipient := evmcAddress(params.Recipient)
evmcSender := evmcAddress(params.Sender)
evmcValue := evmcBytes32(params.Value)
result := C.execute_wrapper(vm.handle, C.uintptr_t(ctxId),
uint32(params.Revision), C.enum_evmc_call_kind(params.Kind), flags,
C.int32_t(params.Depth), C.int64_t(params.Gas), &evmcRecipient,
&evmcSender, bytesPtr(params.Input), C.size_t(len(params.Input)),
&evmcValue, bytesPtr(params.Code), C.size_t(len(params.Code)))
removeHostContext(ctxId)

res.Output = C.GoBytes(unsafe.Pointer(result.output_data), C.int(result.output_size))
Expand Down
28 changes: 6 additions & 22 deletions bindings/go/evmc/evmc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,12 @@ func TestExecuteEmptyCode(t *testing.T) {
vm, _ := Load(modulePath)
defer vm.Destroy()

addr := Address{}
h := Hash{}
output, gasLeft, err := vm.Execute(nil, Byzantium, Call, false, 1, 999, addr, addr, nil, h, nil)

if !bytes.Equal(output, []byte("")) {
t.Errorf("execution unexpected output: %x", output)
}
if gasLeft != 999 {
t.Errorf("execution gas left is incorrect: %d", gasLeft)
}
if err != nil {
t.Errorf("execution returned unexpected error: %v", err)
}
}

func TestExecuteEmptyCode2(t *testing.T) {
vm, _ := Load(modulePath)
defer vm.Destroy()

addr := Address{}
h := Hash{}
result, err := vm.Execute2(nil, Byzantium, Call, false, 1, 999, addr, addr, nil, h, nil)
result, err := vm.Execute(Parameters{
Revision: Byzantium,
Kind: Call,
Depth: 1,
Gas: 999,
})

if !bytes.Equal(result.Output, []byte("")) {
t.Errorf("execution unexpected output: %x", result.Output)
Expand Down
31 changes: 23 additions & 8 deletions bindings/go/evmc/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,24 @@ func TestGetBlockNumberFromTxContext(t *testing.T) {
defer vm.Destroy()

host := &testHostContext{}
addr := Address{}
h := Hash{}
output, gasLeft, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)
result, err := vm.Execute(Parameters{
Context: host,
Revision: Byzantium,
Kind: Call,
Depth: 1,
Gas: 100,
Code: code,
})
output := result.Output
gasLeft := result.GasLeft

if len(output) != 32 {
t.Errorf("unexpected output size: %d", len(output))
}

// Should return value 42 (0x2a) as defined in GetTxContext().
expectedOutput := []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2a")
if bytes.Compare(output, expectedOutput) != 0 {
if !bytes.Equal(output, expectedOutput) {
t.Errorf("execution unexpected output: %x", output)
}
if gasLeft != 94 {
Expand All @@ -109,14 +116,22 @@ func TestCall(t *testing.T) {
defer vm.Destroy()

host := &testHostContext{}
addr := Address{}
h := Hash{}
output, gasLeft, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)

result, err := vm.Execute(Parameters{
Context: host,
Revision: Byzantium,
Kind: Call,
Depth: 1,
Gas: 100,
Code: code,
})
output := result.Output
gasLeft := result.GasLeft

if len(output) != 34 {
t.Errorf("execution unexpected output length: %d", len(output))
}
if bytes.Compare(output, []byte("output from testHostContext.Call()")) != 0 {
if !bytes.Equal(output, []byte("output from testHostContext.Call()")) {
t.Errorf("execution unexpected output: %s", output)
}
if gasLeft != 89 {
Expand Down

0 comments on commit 153f1b0

Please sign in to comment.