From 0287666b7d9beffb93f5a7c6500d242d00dbd085 Mon Sep 17 00:00:00 2001 From: wellna Date: Wed, 21 May 2025 13:20:36 +0100 Subject: [PATCH] eth/tracers: Improve test coverage for toWord (#31846) --- eth/tracers/js/goja.go | 1 - eth/tracers/js/tracer_test.go | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index d1e65bf7f4..7ec737f4e4 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -480,7 +480,6 @@ func (t *jsTracer) setBuiltinFunctions() { return hexutil.Encode(b) }) vm.Set("toWord", func(v goja.Value) goja.Value { - // TODO: add test with []byte len < 32 or > 32 b, err := t.fromBuf(vm, v, true) if err != nil { vm.Interrupt(err) diff --git a/eth/tracers/js/tracer_test.go b/eth/tracers/js/tracer_test.go index dbfc7308f7..a12b990a93 100644 --- a/eth/tracers/js/tracer_test.go +++ b/eth/tracers/js/tracer_test.go @@ -122,9 +122,15 @@ func TestTracer(t *testing.T) { }, { // tests gasUsed code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed; }}", want: `"100000.21006"`, - }, { + }, { // tests toWord with byte array length < 32 code: "{res: null, step: function(log) {}, fault: function() {}, result: function() { return toWord('0xffaa') }}", want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":255,"31":170}`, + }, { // tests toWord with byte array length = 32 + code: "{step: function() {}, fault: function() {}, result: function() { return toWord('0x1234567890123456789012345678901234567890123456789012345678901234'); }}", + want: `{"0":18,"1":52,"2":86,"3":120,"4":144,"5":18,"6":52,"7":86,"8":120,"9":144,"10":18,"11":52,"12":86,"13":120,"14":144,"15":18,"16":52,"17":86,"18":120,"19":144,"20":18,"21":52,"22":86,"23":120,"24":144,"25":18,"26":52,"27":86,"28":120,"29":144,"30":18,"31":52}`, + }, { // tests toWord with byte array length > 32 + code: "{step: function() {}, fault: function() {}, result: function() { return toWord('0x1234567890123456789012345678901234567890123456789012345678901234567890'); }}", + want: `{"0":120,"1":144,"2":18,"3":52,"4":86,"5":120,"6":144,"7":18,"8":52,"9":86,"10":120,"11":144,"12":18,"13":52,"14":86,"15":120,"16":144,"17":18,"18":52,"19":86,"20":120,"21":144,"22":18,"23":52,"24":86,"25":120,"26":144,"27":18,"28":52,"29":86,"30":120,"31":144}`, }, { // test feeding a buffer back into go code: "{res: null, step: function(log) { var address = log.contract.getAddress(); this.res = toAddress(address); }, fault: function() {}, result: function() { return this.res }}", want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,