Skip to content

Commit

Permalink
remove AccountStateJson, refactor BeaconBlockRootHandler (#7674)
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke26 authored Oct 28, 2024
1 parent 9f6cf3b commit 089d2c4
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/Nethermind/Ethereum.Test.Base/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Ethereum.Test.Base
{
public class AccountState
{
public byte[] Code { get; set; }
public byte[] Code { get; set; } = [];
public UInt256 Balance { get; set; }
public UInt256 Nonce { get; set; }
public Dictionary<UInt256, byte[]> Storage { get; set; }
public Dictionary<UInt256, byte[]> Storage { get; set; } = new();
}
}
15 changes: 0 additions & 15 deletions src/Nethermind/Ethereum.Test.Base/AccountStateJson.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Nethermind/Ethereum.Test.Base/BlockchainTestJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Collections.Generic;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Specs;

Expand All @@ -24,8 +25,8 @@ public class BlockchainTestJson
public TestBlockJson[]? Blocks { get; set; }
public TestBlockHeaderJson? GenesisBlockHeader { get; set; }

public Dictionary<string, AccountStateJson>? Pre { get; set; }
public Dictionary<string, AccountStateJson>? PostState { get; set; }
public Dictionary<Address, AccountState>? Pre { get; set; }
public Dictionary<Address, AccountState>? PostState { get; set; }

public Hash256? PostStateHash { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Ethereum.Test.Base/GeneralStateTestJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Nethermind.Core;

namespace Ethereum.Test.Base
{
Expand All @@ -12,7 +13,7 @@ public class GeneralStateTestJson
public GeneralStateTestInfoJson? Info { get; set; }
public GeneralStateTestEnvJson? Env { get; set; }
public Dictionary<string, PostStateJson[]>? Post { get; set; }
public Dictionary<string, AccountStateJson>? Pre { get; set; }
public Dictionary<Address, AccountState>? Pre { get; set; }
public string? SealEngine { get; set; }
public string? LoadFailure { get; set; }
public TransactionJson? Transaction { get; set; }
Expand Down
35 changes: 5 additions & 30 deletions src/Nethermind/Ethereum.Test.Base/JsonToEthereumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Ethereum.Test.Base
{
public static class JsonToEthereumTest
{
private static IReleaseSpec ParseSpec(string network)
public static IReleaseSpec ParseSpec(string network)
{
network = network.Replace("EIP150", "TangerineWhistle");
network = network.Replace("EIP158", "SpuriousDragon");
Expand Down Expand Up @@ -112,17 +112,6 @@ public static BlockHeader Convert(TestBlockHeaderJson? headerJson)
return header;
}

public static Block Convert(PostStateJson postStateJson, TestBlockJson testBlockJson)
{
BlockHeader? header = Convert(testBlockJson.BlockHeader);
BlockHeader[] uncles = testBlockJson.UncleHeaders?.Select(Convert).ToArray()
?? Array.Empty<BlockHeader>();
Transaction[] transactions = testBlockJson.Transactions?.Select(Convert).ToArray()
?? Array.Empty<Transaction>();
Block block = new(header, transactions, uncles);
return block;
}

public static Transaction Convert(PostStateJson postStateJson, TransactionJson transactionJson)
{
Transaction transaction = new();
Expand Down Expand Up @@ -216,7 +205,7 @@ public static Transaction Convert(PostStateJson postStateJson, TransactionJson t
return transaction;
}

private static void ProcessAccessList(AccessListItemJson[]? accessList, AccessList.Builder builder)
public static void ProcessAccessList(AccessListItemJson[]? accessList, AccessList.Builder builder)
{
foreach (AccessListItemJson accessListItemJson in accessList ?? Array.Empty<AccessListItemJson>())
{
Expand All @@ -242,20 +231,6 @@ public static Transaction Convert(LegacyTransactionJson transactionJson)
return transaction;
}

private static AccountState Convert(AccountStateJson accountStateJson)
{
AccountState state = new();
state.Balance = accountStateJson.Balance is not null ? Bytes.FromHexString(accountStateJson.Balance).ToUInt256() : 0;
state.Code = accountStateJson.Code is not null ? Bytes.FromHexString(accountStateJson.Code) : Array.Empty<byte>();
state.Nonce = accountStateJson.Nonce is not null ? Bytes.FromHexString(accountStateJson.Nonce).ToUInt256() : 0;
state.Storage = accountStateJson.Storage is not null
? accountStateJson.Storage.ToDictionary(
p => Bytes.FromHexString(p.Key).ToUInt256(),
p => Bytes.FromHexString(p.Value))
: new();
return state;
}

public static IEnumerable<GeneralStateTest> Convert(string name, GeneralStateTestJson testJson)
{
if (testJson.LoadFailure is not null)
Expand Down Expand Up @@ -294,7 +269,7 @@ public static IEnumerable<GeneralStateTest> Convert(string name, GeneralStateTes
test.ParentExcessBlobGas = testJson.Env.ParentExcessBlobGas;
test.PostReceiptsRoot = stateJson.Logs;
test.PostHash = stateJson.Hash;
test.Pre = testJson.Pre.ToDictionary(p => new Address(p.Key), p => Convert(p.Value));
test.Pre = testJson.Pre.ToDictionary(p => p.Key, p => p.Value);
test.Transaction = Convert(stateJson, testJson.Transaction);

blockchainTests.Add(test);
Expand All @@ -321,7 +296,7 @@ public static BlockchainTest Convert(string name, BlockchainTestJson testJson)
test.GenesisRlp = testJson.GenesisRlp is null ? null : new Rlp(Bytes.FromHexString(testJson.GenesisRlp));
test.GenesisBlockHeader = testJson.GenesisBlockHeader;
test.Blocks = testJson.Blocks;
test.Pre = testJson.Pre.ToDictionary(p => new Address(p.Key), p => Convert(p.Value));
test.Pre = testJson.Pre.ToDictionary(p => p.Key, p => p.Value);

HalfBlockchainTestJson half = testJson as HalfBlockchainTestJson;
if (half is not null)
Expand All @@ -330,7 +305,7 @@ public static BlockchainTest Convert(string name, BlockchainTestJson testJson)
}
else
{
test.PostState = testJson.PostState?.ToDictionary(p => new Address(p.Key), p => Convert(p.Value));
test.PostState = testJson.PostState?.ToDictionary(p => p.Key, p => p.Value);
test.PostStateRoot = testJson.PostStateHash;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Test_StoreBeaconRoot_AccessListIsNull()
BlockHeader header = Build.A.BlockHeader.TestObject;
Block block = Build.A.Block.WithHeader(header).TestObject;

_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance);
_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance, NullTxTracer.Instance);

_transactionProcessor.DidNotReceive().Execute(Arg.Any<Transaction>(), Arg.Any<BlockExecutionContext>(), Arg.Any<ITxTracer>());
}
Expand All @@ -136,7 +136,7 @@ public void Test_StoreBeaconRoot_AccessListNotNull()
Block block = Build.A.Block.WithHeader(header).TestObject;
_worldState.AccountExists(Arg.Any<Address>()).Returns(true);

_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance);
_beaconBlockRootHandler.StoreBeaconRoot(block, Cancun.Instance, NullTxTracer.Instance);

Transaction transaction = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BeaconBlockRootHandler(ITransactionProcessor processor, IWorldState
return (eip4788ContractAddress, builder.Build());
}

public void StoreBeaconRoot(Block block, IReleaseSpec spec)
public void StoreBeaconRoot(Block block, IReleaseSpec spec, ITxTracer tracer)
{
(Address? toAddress, AccessList? accessList) = BeaconRootsAccessList(block, spec, includeStorageCells: false);

Expand All @@ -62,7 +62,7 @@ public void StoreBeaconRoot(Block block, IReleaseSpec spec)

transaction.Hash = transaction.CalculateHash();

processor.Execute(transaction, header, NullTxTracer.Instance);
processor.Execute(transaction, header, tracer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using Nethermind.Core;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Specs;
using Nethermind.Evm.Tracing;
using Nethermind.State;

namespace Nethermind.Blockchain.BeaconBlockRoot;
public interface IBeaconBlockRootHandler
{
(Address? toAddress, AccessList? accessList) BeaconRootsAccessList(Block block, IReleaseSpec spec, bool includeStorageCells = true);
void StoreBeaconRoot(Block block, IReleaseSpec spec);
void StoreBeaconRoot(Block block, IReleaseSpec spec, ITxTracer tracer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private void StoreBeaconRoot(Block block, IReleaseSpec spec)
{
try
{
_beaconBlockRootHandler.StoreBeaconRoot(block, spec);
_beaconBlockRootHandler.StoreBeaconRoot(block, spec, NullTxTracer.Instance);
}
catch (Exception e)
{
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Microsoft.CodeAnalysis;
using Nethermind.Blockchain.BeaconBlockRoot;
using Nethermind.Core.Specs;
using Nethermind.Evm.Tracing;

namespace Nethermind.Merge.Plugin.Test
{
Expand Down Expand Up @@ -154,7 +155,7 @@ private static ExecutionPayloadV4 CreateBlockRequestV4(MergeTestBlockchain chain
blockRequestV4.TryGetBlock(out Block? block);

var beaconBlockRootHandler = new BeaconBlockRootHandler(chain.TxProcessor, chain.WorldStateManager.GlobalWorldState);
beaconBlockRootHandler.StoreBeaconRoot(block!, chain.SpecProvider.GetSpec(block!.Header));
beaconBlockRootHandler.StoreBeaconRoot(block!, chain.SpecProvider.GetSpec(block!.Header), NullTxTracer.Instance);
Snapshot before = chain.State.TakeSnapshot();
var blockHashStore = new BlockhashStore(chain.SpecProvider, chain.State);
blockHashStore.ApplyBlockhashStateChanges(block!.Header);
Expand Down

0 comments on commit 089d2c4

Please sign in to comment.