Skip to content
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
3 changes: 0 additions & 3 deletions scripts/known-failing-hive-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ eth_simulateV1/ethSimulate-overflow-nonce-validation (nethermind)
eth_simulateV1/ethSimulate-override-address-twice (nethermind)
eth_simulateV1/ethSimulate-run-gas-spending (nethermind)
eth_simulateV1/ethSimulate-run-out-of-gas-in-block-38015 (nethermind)
eth_simulateV1/ethSimulate-send-eth-and-delegate-call (nethermind)
eth_simulateV1/ethSimulate-send-eth-and-delegate-call-to-eoa (nethermind)
eth_simulateV1/ethSimulate-send-eth-and-delegate-call-to-payble-contract (nethermind)
eth_simulateV1/ethSimulate-simple-more-params-validate (nethermind)
eth_simulateV1/ethSimulate-simple-no-funds (nethermind)
eth_simulateV1/ethSimulate-simple-no-funds-with-balance-querying (nethermind)
Expand Down
22 changes: 3 additions & 19 deletions src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ICodeInfo GetCachedCodeInfo(Address codeSource, bool followDelegation, IR

ICodeInfo cachedCodeInfo = InternalGetCachedCode(_worldState, codeSource, vmSpec);

if (!cachedCodeInfo.IsEmpty && TryGetDelegatedAddress(cachedCodeInfo.CodeSpan, out delegationAddress))
if (!cachedCodeInfo.IsEmpty && ICodeInfoRepository.TryGetDelegatedAddress(cachedCodeInfo.CodeSpan, out delegationAddress))
{
if (followDelegation)
cachedCodeInfo = InternalGetCachedCode(_worldState, delegationAddress, vmSpec);
Expand Down Expand Up @@ -144,27 +144,11 @@ public ValueHash256 GetExecutableCodeHash(Address address, IReleaseSpec spec)
: codeHash;
}

/// <remarks>
/// Parses delegation code to extract the contained address.
/// <b>Assumes </b><paramref name="code"/> <b>is delegation code!</b>
/// </remarks>
private static bool TryGetDelegatedAddress(ReadOnlySpan<byte> code, [NotNullWhen(true)] out Address? address)
{
if (Eip7702Constants.IsDelegatedCode(code))
{
address = new Address(code[Eip7702Constants.DelegationHeader.Length..].ToArray());
return true;
}

address = null;
return false;
}

public bool TryGetDelegation(Address address, IReleaseSpec spec, [NotNullWhen(true)] out Address? delegatedAddress) =>
TryGetDelegatedAddress(InternalGetCachedCode(_worldState, address, spec).CodeSpan, out delegatedAddress);
ICodeInfoRepository.TryGetDelegatedAddress(InternalGetCachedCode(_worldState, address, spec).CodeSpan, out delegatedAddress);

public bool TryGetDelegation(in ValueHash256 codeHash, IReleaseSpec spec, [NotNullWhen(true)] out Address? delegatedAddress) =>
TryGetDelegatedAddress(InternalGetCachedCode(_worldState, in codeHash, spec).CodeSpan, out delegatedAddress);
ICodeInfoRepository.TryGetDelegatedAddress(InternalGetCachedCode(_worldState, in codeHash, spec).CodeSpan, out delegatedAddress);

private sealed class CodeLruCache
{
Expand Down
17 changes: 17 additions & 0 deletions src/Nethermind/Nethermind.Evm/ICodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ public interface ICodeInfoRepository
void InsertCode(ReadOnlyMemory<byte> code, Address codeOwner, IReleaseSpec spec);
void SetDelegation(Address codeSource, Address authority, IReleaseSpec spec);
bool TryGetDelegation(Address address, IReleaseSpec spec, [NotNullWhen(true)] out Address? delegatedAddress);

/// <remarks>
/// Parses delegation code to extract the contained address.
/// <b>Assumes </b><paramref name="code"/> <b>is delegation code!</b>
/// </remarks>
static bool TryGetDelegatedAddress(ReadOnlySpan<byte> code, [NotNullWhen(true)] out Address? address)
{
if (Eip7702Constants.IsDelegatedCode(code))
{
address = new Address(code[Eip7702Constants.DelegationHeader.Length..].ToArray());
return true;
}

address = null;
return false;
}

}

public static class CodeInfoRepositoryExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private void PrepareState(
BlockStateCall<TransactionWithSourceDetails> blockStateCall,
IWorldState stateProvider,
IOverridableCodeInfoRepository codeInfoRepository,
long blockNumber,
IReleaseSpec releaseSpec)
{
stateProvider.ApplyStateOverridesNoCommit(codeInfoRepository, blockStateCall.StateOverrides, releaseSpec);
Expand All @@ -49,7 +50,8 @@ private void PrepareState(
stateProvider.CreateAccountIfNotExists(address, 0, 0);
}

stateProvider.Commit(releaseSpec, commitRoots: false);
stateProvider.Commit(releaseSpec, commitRoots: true);
stateProvider.CommitTree(blockNumber);
}

public SimulateOutput<TTrace> TrySimulate<TTrace>(
Expand Down Expand Up @@ -121,7 +123,7 @@ private bool TrySimulate<TTrace>(BlockHeader parent,
BlockBody body = AssembleBody(calls, stateProvider, nonceCache, spec);
Block callBlock = new Block(callHeader, body);

PrepareState(blockCall, env.WorldState, env.CodeInfoRepository, spec);
PrepareState(blockCall, env.WorldState, env.CodeInfoRepository, callHeader.Number, spec);

ProcessingOptions processingFlags = payload.Validation
? SimulateProcessingOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public SimulateTxTracer(bool isTracingTransfers, Transaction tx, ulong currentBl
public override void ReportAction(long gas, UInt256 value, Address from, Address to, ReadOnlyMemory<byte> input, ExecutionType callType, bool isPrecompileCall = false)
{
base.ReportAction(gas, value, from, to, input, callType, isPrecompileCall);
if (callType == ExecutionType.DELEGATECALL) return;
if (value > UInt256.Zero)
{
var data = AbiEncoder.Instance.Encode(AbiEncodingStyle.Packed, AbiTransferSignature, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ namespace Nethermind.State.OverridableEnv;

public class OverridableCodeInfoRepository(ICodeInfoRepository codeInfoRepository, IWorldState worldState) : IOverridableCodeInfoRepository
{
private readonly Dictionary<Address, ICodeInfo> _codeOverrides = new();
private readonly Dictionary<Address, (ICodeInfo codeInfo, ValueHash256 codeHash)> _codeOverrides = new();
private readonly Dictionary<Address, (ICodeInfo codeInfo, Address initialAddr)> _precompileOverrides = new();

public ICodeInfo GetCachedCodeInfo(Address codeSource, bool followDelegation, IReleaseSpec vmSpec, out Address? delegationAddress)
{
delegationAddress = null;
if (_precompileOverrides.TryGetValue(codeSource, out var precompile)) return precompile.codeInfo;

return _codeOverrides.TryGetValue(codeSource, out ICodeInfo result)
? result
: codeInfoRepository.GetCachedCodeInfo(codeSource, followDelegation, vmSpec, out delegationAddress);
if (_codeOverrides.TryGetValue(codeSource, out var result))
{
return !result.codeInfo.IsEmpty &&
ICodeInfoRepository.TryGetDelegatedAddress(result.codeInfo.CodeSpan, out delegationAddress) &&
followDelegation
? GetCachedCodeInfo(delegationAddress, false, vmSpec, out Address? _)
: result.codeInfo;
}

return codeInfoRepository.GetCachedCodeInfo(codeSource, followDelegation, vmSpec, out delegationAddress);
}

public void InsertCode(ReadOnlyMemory<byte> code, Address codeOwner, IReleaseSpec spec) =>
Expand All @@ -36,23 +43,31 @@ public void SetCodeOverride(
Address key,
ICodeInfo value)
{
_codeOverrides[key] = value;
_codeOverrides[key] = (value, ValueKeccak.Compute(value.CodeSpan));
}

public void MovePrecompile(IReleaseSpec vmSpec, Address precompileAddr, Address targetAddr)
{
_precompileOverrides[targetAddr] = (this.GetCachedCodeInfo(precompileAddr, vmSpec), precompileAddr);
_codeOverrides[precompileAddr] = new CodeInfo(worldState.GetCode(precompileAddr));
_codeOverrides[precompileAddr] = (new CodeInfo(worldState.GetCode(precompileAddr)), worldState.GetCodeHash(precompileAddr));
}

public void SetDelegation(Address codeSource, Address authority, IReleaseSpec spec) =>
codeInfoRepository.SetDelegation(codeSource, authority, spec);

public bool TryGetDelegation(Address address, IReleaseSpec vmSpec, [NotNullWhen(true)] out Address? delegatedAddress) =>
codeInfoRepository.TryGetDelegation(address, vmSpec, out delegatedAddress);
public bool TryGetDelegation(Address address, IReleaseSpec vmSpec,
[NotNullWhen(true)] out Address? delegatedAddress)
{
delegatedAddress = null;
return _codeOverrides.TryGetValue(address, out var result)
? ICodeInfoRepository.TryGetDelegatedAddress(result.codeInfo.CodeSpan, out delegatedAddress)
: codeInfoRepository.TryGetDelegation(address, vmSpec, out delegatedAddress);
}


public ValueHash256 GetExecutableCodeHash(Address address, IReleaseSpec spec) =>
codeInfoRepository.GetExecutableCodeHash(address, spec);
public ValueHash256 GetExecutableCodeHash(Address address, IReleaseSpec spec) => _codeOverrides.TryGetValue(address, out var result)
? result.codeHash
: codeInfoRepository.GetExecutableCodeHash(address, spec);

public void ResetOverrides()
{
Expand Down
Loading