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

Verkle/spec #49

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageVersion Include="Nethermind.DotNetty.Transport" Version="1.0.1" />
<PackageVersion Include="Nethermind.Gmp" Version="1.0.1" />
<PackageVersion Include="Nethermind.Numerics.Int256" Version="1.1.0" />
<PackageVersion Include="Nethermind.Verkle" Version="0.1.3-alpha" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nito.Collections.Deque" Version="1.1.1" />
<PackageVersion Include="NLog" Version="5.2.2" />
Expand Down Expand Up @@ -81,4 +82,4 @@
<PackageVersion Include="Websocket.Client" Version="4.6.1" />
<PackageVersion Include="YamlDotNet" Version="13.1.1" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void SetAccountStates(IEnumerable<Address> missingAddresses)

SetAccountStates(testCase.MissingAddresses);

BlockReceiptsTracer receiptsTracer = new();
BlockReceiptsTracer receiptsTracer = new(true, false);
receiptsTracer.StartNewBlockTrace(blockToProduce);

txExecutor.ProcessTransactions(blockToProduce, ProcessingOptions.ProducingBlock, receiptsTracer, spec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BlockProcessor(
_blockTransactionsExecutor = blockTransactionsExecutor ?? throw new ArgumentNullException(nameof(blockTransactionsExecutor));


_receiptsTracer = new BlockReceiptsTracer();
_receiptsTracer = new BlockReceiptsTracer(true, false);
}

public event EventHandler<BlockProcessedEventArgs> BlockProcessed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ void DeleteInvalidBlocks(in ProcessingBranch processingBranch, Keccak invalidBlo
TraceFailingBranch(
processingBranch,
options,
new BlockReceiptsTracer(),
new BlockReceiptsTracer(true, false),
DumpOptions.Receipts);

TraceFailingBranch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AccountBuilder WithNonce(UInt256 nonce)

public AccountBuilder WithCode(byte[] code)
{
TestObjectInternal = TestObjectInternal.WithChangedCodeHash(Keccak.Compute(code));
TestObjectInternal = TestObjectInternal.WithChangedCodeHash(Keccak.Compute(code), code);
return this;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Nethermind/Nethermind.Core.Test/Builders/BlockBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Linq;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Core.Verkle;
using Nethermind.Crypto;
using Nethermind.Int256;
using Nethermind.State.Proofs;
Expand Down Expand Up @@ -277,5 +277,12 @@ public BlockBuilder WithWithdrawals(params Withdrawal[]? withdrawals)

return this;
}

public BlockBuilder WithExecutionWitness(ExecutionWitness? witness)
{
TestObjectInternal = TestObjectInternal
.WithReplacedBody(TestObjectInternal.Body.WithChangedExecutionWitness(witness));
return this;
}
}
}
61 changes: 50 additions & 11 deletions src/Nethermind/Nethermind.Core.Test/Builders/TestItem.Tree.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only


using System;
using System.Net;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Int256;
using Nethermind.Db.Rocks;
using Nethermind.Logging;
using Nethermind.Serialization.Rlp;
using Nethermind.State;
using Nethermind.State.Snap;
using Nethermind.Trie.Pruning;
using Nethermind.Verkle.Tree;
using Nethermind.Verkle.Tree.Interfaces;
using Nethermind.Verkle.Tree.Sync;

namespace Nethermind.Core.Test.Builders
{
Expand All @@ -23,12 +22,12 @@ public static class Tree
{
public static Keccak AccountAddress0 = new Keccak("0000000000000000000000000000000000000000000000000000000001101234");

private static readonly Account _account0 = Build.An.Account.WithBalance(0).TestObject;
private static readonly Account _account1 = Build.An.Account.WithBalance(1).TestObject;
private static readonly Account _account2 = Build.An.Account.WithBalance(2).TestObject;
private static readonly Account _account3 = Build.An.Account.WithBalance(3).TestObject;
private static readonly Account _account4 = Build.An.Account.WithBalance(4).TestObject;
private static readonly Account _account5 = Build.An.Account.WithBalance(5).TestObject;
public static readonly Account _account0 = Build.An.Account.WithBalance(0).TestObject;
public static readonly Account _account1 = Build.An.Account.WithBalance(1).TestObject;
public static readonly Account _account2 = Build.An.Account.WithBalance(2).TestObject;
public static readonly Account _account3 = Build.An.Account.WithBalance(3).TestObject;
public static readonly Account _account4 = Build.An.Account.WithBalance(4).TestObject;
public static readonly Account _account5 = Build.An.Account.WithBalance(5).TestObject;

public static PathWithAccount[] AccountsWithPaths = new PathWithAccount[]
{
Expand Down Expand Up @@ -61,6 +60,17 @@ public static StateTree GetStateTree(ITrieStore? store)
return stateTree;
}

public static VerkleStateTree GetVerkleStateTree(IVerkleTrieStore? store)
{
store ??= new VerkleStateStore(VerkleDbFactory.InitDatabase(DbMode.MemDb, null), LimboLogs.Instance);

var stateTree = new VerkleStateTree(store, LimboLogs.Instance);

// FillStateTreeWithTestAccounts(stateTree);

return stateTree;
}

public static void FillStateTreeWithTestAccounts(StateTree stateTree)
{
stateTree.Set(AccountsWithPaths[0].Path, AccountsWithPaths[0].Account);
Expand All @@ -72,6 +82,35 @@ public static void FillStateTreeWithTestAccounts(StateTree stateTree)
stateTree.Commit(0);
}

public static byte[] stem0 = new Keccak("0000000000000000000000000000000000000000000000000000000001101234").Bytes[1..].ToArray();
public static byte[] stem1 = new Keccak("0000000000000000000000000000000000000000000000000000000001112345").Bytes[1..].ToArray();
public static byte[] stem2 = new Keccak("0000000000000000000000000000000000000000000000000000000001113456").Bytes[1..].ToArray();
public static byte[] stem3 = new Keccak("0000000000000000000000000000000000000000000000000000000001114567").Bytes[1..].ToArray();
public static byte[] stem4 = new Keccak("0000000000000000000000000000000000000000000000000000000001123456").Bytes[1..].ToArray();
public static byte[] stem5 = new Keccak("0000000000000000000000000000000000000000000000000000000001123457").Bytes[1..].ToArray();

public static PathWithSubTree[] SubTreesWithPaths = new PathWithSubTree[]
{
new PathWithSubTree(stem0, _account0.ToVerkleDict()),
new PathWithSubTree(stem1, _account1.ToVerkleDict()),
new PathWithSubTree(stem2, _account2.ToVerkleDict()),
new PathWithSubTree(stem3, _account3.ToVerkleDict()),
new PathWithSubTree(stem4, _account4.ToVerkleDict()),
new PathWithSubTree(stem5, _account5.ToVerkleDict()),
};

public static void FillStateTreeWithTestAccounts(VerkleStateTree stateTree)
{
stateTree.InsertStemBatch(stem0, _account0.ToVerkleDict());
stateTree.InsertStemBatch(stem1, _account1.ToVerkleDict());
stateTree.InsertStemBatch(stem2, _account2.ToVerkleDict());
stateTree.InsertStemBatch(stem3, _account3.ToVerkleDict());
stateTree.InsertStemBatch(stem4, _account4.ToVerkleDict());
stateTree.InsertStemBatch(stem5, _account5.ToVerkleDict());
stateTree.Commit();
stateTree.CommitTree(0);
}

public static (StateTree stateTree, StorageTree storageTree) GetTrees(ITrieStore? store)
{
store ??= new TrieStore(new MemDb(), LimboLogs.Instance);
Expand Down
151 changes: 151 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/Builders/TestItem.VerkleTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using Nethermind.Core.Extensions;
using Nethermind.Core.Verkle;
using Nethermind.Db;
using Nethermind.Db.Rocks;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.Verkle.Tree;
using Nethermind.Verkle.Tree.Interfaces;
using Nethermind.Verkle.Tree.Sync;
using Nethermind.Verkle.Tree.Utils;
using NUnit.Framework;

namespace Nethermind.Core.Test.Builders;

public static partial class TestItem
{
public static readonly Stem Stem0 = new Stem("00000000000000000000000000000000000000000000000000000001101234");
public static readonly Stem Stem1 = new Stem("00000000000000000000000000000000000000000000000000000001112345");
public static readonly Stem Stem2 = new Stem("00000000000000000000000000000000000000000000000000000001113456");
public static readonly Stem Stem3 = new Stem("00000000000000000000000000000000000000000000000000000001114567");
public static readonly Stem Stem4 = new Stem("00000000000000000000000000000000000000000000000000000001123456");
public static readonly Stem Stem5 = new Stem("00000000000000000000000000000000000000000000000000000001123457");

public static readonly Account _account0 = Build.An.Account.WithBalance(0).TestObject;
public static readonly Account _account1 = Build.An.Account.WithBalance(1).TestObject;
public static readonly Account _account2 = Build.An.Account.WithBalance(2).TestObject;
public static readonly Account _account3 = Build.An.Account.WithBalance(3).TestObject;
public static readonly Account _account4 = Build.An.Account.WithBalance(4).TestObject;
public static readonly Account _account5 = Build.An.Account.WithBalance(5).TestObject;

public static PathWithSubTree[] SubTreesWithPaths = new PathWithSubTree[]
{
new PathWithSubTree(Stem0, _account0.ToVerkleDict()),
new PathWithSubTree(Stem1, _account1.ToVerkleDict()),
new PathWithSubTree(Stem2, _account2.ToVerkleDict()),
new PathWithSubTree(Stem3, _account3.ToVerkleDict()),
new PathWithSubTree(Stem4, _account4.ToVerkleDict()),
new PathWithSubTree(Stem5, _account5.ToVerkleDict()),
};

private static string GetDbPathForTest()
{
string tempDir = Path.GetTempPath();
string dbname = "VerkleTrie_TestID_" + TestContext.CurrentContext.Test.ID;
return Path.Combine(tempDir, dbname);
}

public static IVerkleTrieStore GetVerkleStore(DbMode dbMode, int history = 128)
{
IDbProvider provider;
switch (dbMode)
{
case DbMode.MemDb:
provider = VerkleDbFactory.InitDatabase(dbMode, null);
break;
case DbMode.PersistantDb:
provider = VerkleDbFactory.InitDatabase(dbMode, GetDbPathForTest());
break;
case DbMode.ReadOnlyDb:
default:
throw new ArgumentOutOfRangeException(nameof(dbMode), dbMode, null);
}

return new VerkleStateStore(provider, LimboLogs.Instance,maxNumberOfBlocksInCache: history);
}

public static VerkleStateTree GetVerkleStateTree(IVerkleTrieStore? store)
{
store ??= GetVerkleStore(DbMode.MemDb);
VerkleStateTree stateTree = new VerkleStateTree(store, LimboLogs.Instance);
FillStateTreeWithTestAccounts(stateTree);
return stateTree;
}

public static void FillStateTreeWithTestAccounts(VerkleStateTree stateTree)
{
stateTree.InsertStemBatch(Stem0, _account0.ToVerkleDict());
stateTree.InsertStemBatch(Stem1, _account1.ToVerkleDict());
stateTree.InsertStemBatch(Stem2, _account2.ToVerkleDict());
stateTree.InsertStemBatch(Stem3, _account3.ToVerkleDict());
stateTree.InsertStemBatch(Stem4, _account4.ToVerkleDict());
stateTree.InsertStemBatch(Stem5, _account5.ToVerkleDict());
stateTree.Commit();
stateTree.CommitTree(0);
}

public static void InsertBigVerkleTree(VerkleTree tree, int numBlocks, long leafPerBlock, long pathPoolCount, out SortedDictionary<Pedersen, byte[]> leafs)
{
Pedersen[] pathPool = new Pedersen[pathPoolCount];
leafs = new();

for (int i = 0; i < pathPoolCount; i++)
{
byte[] key = new byte[32];
((UInt256)i).ToBigEndian(key);
Pedersen keccak = new Pedersen(key);
pathPool[i] = keccak;
}


for (int leafIndex = 0; leafIndex < 10000; leafIndex++)
{
byte[] value = new byte[32];
Random.NextBytes(value);
Pedersen path = pathPool[Random.Next(pathPool.Length - 1)];
tree.Insert(path, value);
leafs[path] = value;
}

tree.Commit();
tree.CommitTree(0);

for (int blockNumber = 1; blockNumber <= numBlocks; blockNumber++)
{
for (int accountIndex = 0; accountIndex < leafPerBlock; accountIndex++)
{
byte[] leafValue = new byte[32];



Random.NextBytes(leafValue);
Pedersen path = pathPool[Random.Next(pathPool.Length - 1)];

if (leafs.ContainsKey(path))
{
if (!(Random.NextSingle() > 0.5)) continue;
Console.WriteLine($"blockNumber:{blockNumber} uKey:{path} uValue:{leafValue.ToHexString()}");
tree.Insert(path, leafValue);
leafs[path] = leafValue;
Console.WriteLine("new values");
}
else
{
Console.WriteLine($"blockNumber:{blockNumber} nKey:{path} nValue:{leafValue.ToHexString()}");
tree.Insert(path, leafValue);
leafs[path] = leafValue;
}
}

tree.Commit();
tree.CommitTree(blockNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\Nethermind.Blockchain\Nethermind.Blockchain.csproj" />
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
<ProjectReference Include="..\Nethermind.Crypto\Nethermind.Crypto.csproj" />
<ProjectReference Include="..\Nethermind.Db.Rocks\Nethermind.Db.Rocks.csproj" />
<ProjectReference Include="..\Nethermind.Facade\Nethermind.Facade.csproj" />
<ProjectReference Include="..\Nethermind.Network\Nethermind.Network.csproj" />
<ProjectReference Include="..\Nethermind.Specs.Test\Nethermind.Specs.Test.csproj" />
Expand Down
Loading