Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit bf7f382

Browse files
Add refund to execution result in Go binding (#690)
This PR modifies the `Execute()` function from the Go bindings to return a `Result` struct instead of a list of result values to support the retrieval of the amount of refunded gas. This information is available in [evmc.h](https://github.com/ethereum/evmc/blob/v10.1.0/include/evmc/evmc.h)'s [evmc_result](https://github.com/ethereum/evmc/blob/v10.1.0/include/evmc/evmc.h#L404-L410), but was not exposed in the Go binding so far.
1 parent c3feb59 commit bf7f382

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

bindings/go/evmc/evmc.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,16 @@ func (vm *VM) SetOption(name string, value string) (err error) {
195195
return err
196196
}
197197

198+
type Result struct {
199+
Output []byte
200+
GasLeft int64
201+
GasRefund int64
202+
}
203+
198204
func (vm *VM) Execute(ctx HostContext, rev Revision,
199205
kind CallKind, static bool, depth int, gas int64,
200206
recipient Address, sender Address, input []byte, value Hash,
201-
code []byte) (output []byte, gasLeft int64, err error) {
207+
code []byte) (res Result, err error) {
202208

203209
flags := C.uint32_t(0)
204210
if static {
@@ -216,8 +222,9 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
216222
bytesPtr(code), C.size_t(len(code)))
217223
removeHostContext(ctxId)
218224

219-
output = C.GoBytes(unsafe.Pointer(result.output_data), C.int(result.output_size))
220-
gasLeft = int64(result.gas_left)
225+
res.Output = C.GoBytes(unsafe.Pointer(result.output_data), C.int(result.output_size))
226+
res.GasLeft = int64(result.gas_left)
227+
res.GasRefund = int64(result.gas_refund)
221228
if result.status_code != C.EVMC_SUCCESS {
222229
err = Error(result.status_code)
223230
}
@@ -226,7 +233,7 @@ func (vm *VM) Execute(ctx HostContext, rev Revision,
226233
C.evmc_release_result(&result)
227234
}
228235

229-
return output, gasLeft, err
236+
return res, err
230237
}
231238

232239
var (

bindings/go/evmc/evmc_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ func TestExecuteEmptyCode(t *testing.T) {
4747

4848
addr := Address{}
4949
h := Hash{}
50-
output, gasLeft, err := vm.Execute(nil, Byzantium, Call, false, 1, 999, addr, addr, nil, h, nil)
50+
result, err := vm.Execute(nil, Byzantium, Call, false, 1, 999, addr, addr, nil, h, nil)
5151

52-
if bytes.Compare(output, []byte("")) != 0 {
53-
t.Errorf("execution unexpected output: %x", output)
52+
if !bytes.Equal(result.Output, []byte("")) {
53+
t.Errorf("execution unexpected output: %x", result.Output)
5454
}
55-
if gasLeft != 999 {
56-
t.Errorf("execution gas left is incorrect: %d", gasLeft)
55+
if result.GasLeft != 999 {
56+
t.Errorf("execution gas left is incorrect: %d", result.GasLeft)
57+
}
58+
if result.GasRefund != 0 {
59+
t.Errorf("execution gas refund is incorrect: %d", result.GasRefund)
5760
}
5861
if err != nil {
5962
t.Errorf("execution returned unexpected error: %v", err)

bindings/go/evmc/host_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ func TestGetBlockNumberFromTxContext(t *testing.T) {
8282
host := &testHostContext{}
8383
addr := Address{}
8484
h := Hash{}
85-
output, gasLeft, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)
85+
result, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)
86+
output := result.Output
87+
gasLeft := result.GasLeft
8688

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

9193
// Should return value 42 (0x2a) as defined in GetTxContext().
9294
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")
93-
if bytes.Compare(output, expectedOutput) != 0 {
95+
if !bytes.Equal(output, expectedOutput) {
9496
t.Errorf("execution unexpected output: %x", output)
9597
}
9698
if gasLeft != 94 {
@@ -111,12 +113,14 @@ func TestCall(t *testing.T) {
111113
host := &testHostContext{}
112114
addr := Address{}
113115
h := Hash{}
114-
output, gasLeft, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)
116+
result, err := vm.Execute(host, Byzantium, Call, false, 1, 100, addr, addr, nil, h, code)
117+
output := result.Output
118+
gasLeft := result.GasLeft
115119

116120
if len(output) != 34 {
117121
t.Errorf("execution unexpected output length: %d", len(output))
118122
}
119-
if bytes.Compare(output, []byte("output from testHostContext.Call()")) != 0 {
123+
if !bytes.Equal(output, []byte("output from testHostContext.Call()")) {
120124
t.Errorf("execution unexpected output: %s", output)
121125
}
122126
if gasLeft != 89 {

0 commit comments

Comments
 (0)