-
Notifications
You must be signed in to change notification settings - Fork 514
/
Copy pathDb.cs
40 lines (29 loc) · 1.38 KB
/
Db.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using Microsoft.ClearScript.JavaScript;
using Nethermind.Core;
using Nethermind.Core.Buffers;
using Nethermind.State;
namespace Nethermind.Evm.Tracing.GethStyle.Custom.JavaScript;
public class Db
{
public IWorldState WorldState { get; }
public Db(IWorldState worldState) => WorldState = worldState;
public IJavaScriptObject getBalance(object address) => WorldState.GetBalance(address.ToAddress()).ToBigInteger();
public ulong getNonce(object address) => (ulong)WorldState.GetNonce(address.ToAddress());
public ITypedArray<byte> getCode(object address) => WorldState.GetCode(address.ToAddress()).ToTypedScriptArray();
public ITypedArray<byte> getState(object address, object hash)
{
using var handle = ArrayPoolDisposableReturn.Rent(32, out byte[] array);
StorageValue v = WorldState.Get(new StorageCell(address.ToAddress(), hash.GetHash()));
var bytes = v.BytesWithNoLeadingZeroes;
if (bytes.Length < array.Length)
{
Array.Clear(array);
}
bytes.CopyTo(array.AsSpan(array.Length - bytes.Length));
return array.ToTypedScriptArray();
}
public bool exists(object address) => WorldState.TryGetAccount(address.ToAddress(), out AccountStruct account) && !account.IsTotallyEmpty;
}