From 310f75163928f49151b668faee78f475a1da34ac Mon Sep 17 00:00:00 2001 From: Eduard S Date: Thu, 19 May 2022 14:35:30 +0200 Subject: [PATCH] eth/tracers/js: add memory.length method (#24887) --- eth/tracers/js/goja.go | 5 +++++ eth/tracers/js/tracer.go | 4 ++++ eth/tracers/js/tracer_test.go | 3 +++ 3 files changed, 12 insertions(+) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index 05611a611..e6d7cb53e 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -517,10 +517,15 @@ func (mo *memoryObj) GetUint(addr int64) goja.Value { return res } +func (mo *memoryObj) Length() int { + return mo.w.memory.Len() +} + func (m *memoryObj) setupObject() *goja.Object { o := m.vm.NewObject() o.Set("slice", m.vm.ToValue(m.Slice)) o.Set("getUint", m.vm.ToValue(m.GetUint)) + o.Set("length", m.vm.ToValue(m.Length)) return o } diff --git a/eth/tracers/js/tracer.go b/eth/tracers/js/tracer.go index 0ba8da476..66714497d 100644 --- a/eth/tracers/js/tracer.go +++ b/eth/tracers/js/tracer.go @@ -148,6 +148,10 @@ func (mw *memoryWrapper) getUint(addr int64) *big.Int { func (mw *memoryWrapper) pushObject(vm *duktape.Context) { obj := vm.PushObject() + // Generate the `length` method which returns the memory length + vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(mw.memory.Len()); return 1 }) + vm.PutPropString(obj, "length") + // Generate the `slice` method which takes two ints and returns a buffer vm.PushGoFunction(func(ctx *duktape.Context) int { blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1))) diff --git a/eth/tracers/js/tracer_test.go b/eth/tracers/js/tracer_test.go index 982cf7b37..49b598ce5 100644 --- a/eth/tracers/js/tracer_test.go +++ b/eth/tracers/js/tracer_test.go @@ -125,6 +125,9 @@ func testTracer(t *testing.T, newTracer tracerCtor) { }, { // tests that depth is reported correctly code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}", want: `[0,1,2]`, + }, { // tests memory length + code: "{lengths: [], step: function(log) { this.lengths.push(log.memory.length()); }, fault: function() {}, result: function() { return this.lengths; }}", + want: `[0,0,0]`, }, { // tests to-string of opcodes code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}", want: `["PUSH1","PUSH1","STOP"]`,