From 3f2f149249b4dfa25a3e56c82fc48622b13a0d54 Mon Sep 17 00:00:00 2001 From: Lukasz Rozmej Date: Mon, 11 Dec 2023 08:58:09 +0100 Subject: [PATCH] Unify js types, use typed arrays everywhere + long for slice parameters (#6319) --- .../Tracing/GethStyle/JavaScript/Context.cs | 24 +++++++++---------- .../Tracing/GethStyle/JavaScript/Engine.cs | 13 +++------- .../JavaScript/JavaScriptConverter.cs | 2 -- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Context.cs b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Context.cs index 2cc2d549e3a..a59ff244435 100644 --- a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Context.cs +++ b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Context.cs @@ -13,12 +13,12 @@ namespace Nethermind.Evm.Tracing.GethStyle.JavaScript; public class Context { - private IList? _blockHashConverted; - private IList? _txHashConverted; - private IList? _inputConverted; - private IList? _fromConverted; - private IList? _toConverted; - private IList? _outputConverted; + private ITypedArray? _blockHashConverted; + private ITypedArray? _txHashConverted; + private ITypedArray? _inputConverted; + private ITypedArray? _fromConverted; + private ITypedArray? _toConverted; + private ITypedArray? _outputConverted; private dynamic? _valueConverted; private dynamic? _gasPriceConverted; private Address? _from; @@ -111,17 +111,17 @@ public UInt256 GasPrice } public string type { get; set; } = null!; - public IList? from => _fromConverted ??= From?.Bytes.ToUnTypedScriptArray(); - public IList? to => _toConverted ??= To?.Bytes.ToUnTypedScriptArray(); - public IList? input => _inputConverted ??= Input.ToArray().ToUnTypedScriptArray(); + public ITypedArray? from => _fromConverted ??= From?.Bytes.ToTypedScriptArray(); + public ITypedArray? to => _toConverted ??= To?.Bytes.ToTypedScriptArray(); + public ITypedArray? input => _inputConverted ??= Input.ToArray().ToTypedScriptArray(); public long gas { get; set; } public long gasUsed { get; set; } public IJavaScriptObject gasPrice => _gasPriceConverted ??= GasPrice.ToBigInteger(); public IJavaScriptObject value => _valueConverted ??= Value.ToBigInteger(); public long block { get; set; } - public IList? output => _outputConverted ??= Output?.ToUnTypedScriptArray(); - public IList? blockHash => _blockHashConverted ??= BlockHash?.BytesToArray().ToUnTypedScriptArray(); + public ITypedArray? output => _outputConverted ??= Output?.ToTypedScriptArray(); + public ITypedArray? blockHash => _blockHashConverted ??= BlockHash?.BytesToArray().ToTypedScriptArray(); public int? txIndex { get; set; } - public IList? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToUnTypedScriptArray(); + public ITypedArray? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToTypedScriptArray(); public dynamic? error { get; set; } = Undefined.Value; } diff --git a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Engine.cs b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Engine.cs index 7499f7c4d95..300da421ef0 100644 --- a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Engine.cs +++ b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/Engine.cs @@ -33,7 +33,6 @@ public class Engine : IDisposable private readonly IReleaseSpec _spec; - private readonly dynamic _createUntypedArray; private dynamic _bigInteger; private dynamic _createUint8Array; @@ -89,7 +88,7 @@ public Engine(IReleaseSpec spec) Func toHex = ToHex; Func> toAddress = ToAddress; Func isPrecompiled = IsPrecompiled; - Func> slice = Slice; + Func> slice = Slice; Func> toContract = ToContract; Func> toContract2 = ToContract2; @@ -100,7 +99,6 @@ public Engine(IReleaseSpec spec) V8Engine.AddHostObject(nameof(slice), slice); V8Engine.AddHostObject(nameof(toContract), toContract); V8Engine.AddHostObject(nameof(toContract2), toContract2); - _createUntypedArray = V8Engine.Script.Array.from; if (!IsDebugging) { @@ -134,7 +132,7 @@ public Engine(IReleaseSpec spec) /// /// Returns a slice of input /// - private ITypedArray Slice(object input, int start, int end) + private ITypedArray Slice(object input, long start, long end) { if (input == null) { @@ -146,7 +144,7 @@ private ITypedArray Slice(object input, int start, int end) throw new ArgumentOutOfRangeException(nameof(start), $"tracer accessed out of bound memory: offset {start}, end {end}"); } - return input.ToBytes().Slice(start, end - start).ToTypedScriptArray(); + return input.ToBytes().Slice((int)start, (int)(end - start)).ToTypedScriptArray(); } /// @@ -171,11 +169,6 @@ public void Dispose() /// public ITypedArray CreateUint8Array(byte[] buffer) => _createUint8Array(buffer); - /// - /// Creates a JavaScript V8Engine untyped array with number values - /// - public IList CreateUntypedArray(byte[] buffer) => _createUntypedArray(buffer); - /// /// Creates a JavaScript BigInteger object /// diff --git a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/JavaScriptConverter.cs b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/JavaScriptConverter.cs index bd57c283434..2523f4764c6 100644 --- a/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/JavaScriptConverter.cs +++ b/src/Nethermind/Nethermind.Evm/Tracing/GethStyle/JavaScript/JavaScriptConverter.cs @@ -49,8 +49,6 @@ public static class JavaScriptConverter public static ITypedArray ToTypedScriptArray(this ReadOnlyMemory memory) => memory.ToArray().ToTypedScriptArray(); - public static IList ToUnTypedScriptArray(this byte[] array) => CurrentEngine.CreateUntypedArray(array); - public static IJavaScriptObject ToBigInteger(this BigInteger bigInteger) => CurrentEngine.CreateBigInteger(bigInteger); public static IJavaScriptObject ToBigInteger(this UInt256 bigInteger) => CurrentEngine.CreateBigInteger((BigInteger)bigInteger);