From 9944edb1dddab93972b1267f0901fac8bad4971f Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 23 Jun 2022 12:58:28 +0200 Subject: [PATCH 1/4] eth/tracers: optimize goja buffer conversion --- eth/tracers/js/goja.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index f0c78c084bd9..a1769b92d9cb 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -55,7 +55,7 @@ type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byt func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) { // bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS. - res, err := vm.New(bufType, vm.ToValue(val)) + res, err := vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val))) if err != nil { return nil, err } @@ -81,11 +81,11 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b if !obj.Get("constructor").SameAs(bufType) { break } - var b []byte - if err := vm.ExportTo(buf, &b); err != nil { + var b goja.ArrayBuffer + if err := vm.ExportTo(obj.Get("buffer"), &b); err != nil { return nil, err } - return b, nil + return b.Bytes(), nil } return nil, fmt.Errorf("invalid buffer type") } From 53474a8c956b199b88c992fe93d5ce37de0521e1 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Jun 2022 15:28:22 +0200 Subject: [PATCH 2/4] eth/tracers/js: tiny optimization in fromBuf --- eth/tracers/js/goja.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index a1769b92d9cb..f32efe27ee10 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -70,6 +70,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b break } return common.FromHex(obj.String()), nil + case "Array": var b []byte if err := vm.ExportTo(buf, &b); err != nil { @@ -81,11 +82,8 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b if !obj.Get("constructor").SameAs(bufType) { break } - var b goja.ArrayBuffer - if err := vm.ExportTo(obj.Get("buffer"), &b); err != nil { - return nil, err - } - return b.Bytes(), nil + b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes() + return b, nil } return nil, fmt.Errorf("invalid buffer type") } From 9ba345aa10e9e96636f9c5136dccf8f677dbdea5 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Jun 2022 15:29:13 +0200 Subject: [PATCH 3/4] eth/tracers/js: simplify toBuf --- eth/tracers/js/goja.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index f32efe27ee10..8c7505a22b13 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -55,11 +55,7 @@ type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byt func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) { // bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS. - res, err := vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val))) - if err != nil { - return nil, err - } - return vm.ToValue(res), nil + return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val))) } func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) { From e12e5bc349ed9f89bc786e079349f2c82476bf5d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Jun 2022 15:29:27 +0200 Subject: [PATCH 4/4] eth/tracers/js: return copy of calldata to JS in contract.getInput --- eth/tracers/js/goja.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index 8c7505a22b13..f54c8010494f 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -759,7 +759,7 @@ func (co *contractObj) GetValue() goja.Value { } func (co *contractObj) GetInput() goja.Value { - input := co.contract.Input + input := common.CopyBytes(co.contract.Input) res, err := co.toBuf(co.vm, input) if err != nil { co.vm.Interrupt(err)