Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify js types, use typed arrays everywhere + long for slice parameters #6319

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>? _blockHashConverted;
private ITypedArray<byte>? _txHashConverted;
private ITypedArray<byte>? _inputConverted;
private ITypedArray<byte>? _fromConverted;
private ITypedArray<byte>? _toConverted;
private ITypedArray<byte>? _outputConverted;
private dynamic? _valueConverted;
private dynamic? _gasPriceConverted;
private Address? _from;
Expand Down Expand Up @@ -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<byte>? from => _fromConverted ??= From?.Bytes.ToTypedScriptArray();
public ITypedArray<byte>? to => _toConverted ??= To?.Bytes.ToTypedScriptArray();
public ITypedArray<byte>? 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<byte>? output => _outputConverted ??= Output?.ToTypedScriptArray();
public ITypedArray<byte>? blockHash => _blockHashConverted ??= BlockHash?.BytesToArray().ToTypedScriptArray();
public int? txIndex { get; set; }
public IList? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToUnTypedScriptArray();
public ITypedArray<byte>? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToTypedScriptArray();
public dynamic? error { get; set; } = Undefined.Value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class Engine : IDisposable

private readonly IReleaseSpec _spec;

private readonly dynamic _createUntypedArray;
private dynamic _bigInteger;
private dynamic _createUint8Array;

Expand Down Expand Up @@ -89,7 +88,7 @@ public Engine(IReleaseSpec spec)
Func<object?, string> toHex = ToHex;
Func<object, ITypedArray<byte>> toAddress = ToAddress;
Func<object, bool> isPrecompiled = IsPrecompiled;
Func<object, int, int, ITypedArray<byte>> slice = Slice;
Func<object, long, long, ITypedArray<byte>> slice = Slice;
Func<object, ulong, ITypedArray<byte>> toContract = ToContract;
Func<object, string, object, ITypedArray<byte>> toContract2 = ToContract2;

Expand All @@ -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)
{
Expand Down Expand Up @@ -134,7 +132,7 @@ public Engine(IReleaseSpec spec)
/// <summary>
/// Returns a slice of input
/// </summary>
private ITypedArray<byte> Slice(object input, int start, int end)
private ITypedArray<byte> Slice(object input, long start, long end)
{
if (input == null)
{
Expand All @@ -146,7 +144,7 @@ private ITypedArray<byte> 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure there is no risk of overflows here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bit, but runtime doesn't support lists that long, so we would have to error out anyway

}

/// <summary>
Expand All @@ -171,11 +169,6 @@ public void Dispose()
/// </summary>
public ITypedArray<byte> CreateUint8Array(byte[] buffer) => _createUint8Array(buffer);

/// <summary>
/// Creates a JavaScript V8Engine untyped array with number values
/// </summary>
public IList CreateUntypedArray(byte[] buffer) => _createUntypedArray(buffer);

/// <summary>
/// Creates a JavaScript BigInteger object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public static class JavaScriptConverter

public static ITypedArray<byte> ToTypedScriptArray(this ReadOnlyMemory<byte> 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);

Expand Down
Loading