Skip to content

Commit

Permalink
fx unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Aug 20, 2024
1 parent 3d94703 commit 7dbfcc3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
26 changes: 24 additions & 2 deletions src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Nethermind.State;
using Nethermind.Core.Specs;
using Nethermind.Evm.CodeAnalysis;
using Nethermind.Core.Extensions;

namespace Nethermind.Evm.Test;

Expand Down Expand Up @@ -70,17 +71,38 @@ public void InsertFromAuthorizations_AuthorityHasCode_NoCodeIsInserted()
Address codeSource = TestItem.AddressB;
IWorldState mockWorldState = Substitute.For<IWorldState>();
mockWorldState.HasCode(authority.Address).Returns(true);
mockWorldState.GetCode(authority.Address).Returns(new byte[32]);
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, codeSource, (UInt256)0),
};

IEnumerable<Address> result = sut.InsertFromAuthorizations(mockWorldState, tuples, Substitute.For<IReleaseSpec>());
sut.InsertFromAuthorizations(mockWorldState, tuples, Substitute.For<IReleaseSpec>());

result.Count().Should().Be(0);
mockWorldState.DidNotReceive().InsertCode(Arg.Any<Address>(), Arg.Any<ReadOnlyMemory<byte>>(), Arg.Any<IReleaseSpec>());
}

[Test]
public void InsertFromAuthorizations_AuthorityHasDelegatedCode_CodeIsInserted()
{
PrivateKey authority = TestItem.PrivateKeyA;
Address codeSource = TestItem.AddressB;
IWorldState mockWorldState = Substitute.For<IWorldState>();
mockWorldState.HasCode(authority.Address).Returns(true);
byte[] code = new byte[23];
Eip7702Constants.DelegationHeader.CopyTo(code);
mockWorldState.GetCode(authority.Address).Returns(code);
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, codeSource, (UInt256)0),
};

sut.InsertFromAuthorizations(mockWorldState, tuples, Substitute.For<IReleaseSpec>());

mockWorldState.Received().InsertCode(authority.Address, Arg.Any<Hash256>(), Arg.Any<ReadOnlyMemory<byte>>(), Arg.Any<IReleaseSpec>(), Arg.Any<bool>());
}

private static AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, UInt256? nonce)
{
Expand Down
15 changes: 6 additions & 9 deletions src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public void InsertCode(IWorldState state, ReadOnlyMemory<byte> code, Address cod
_codeCache.Set(codeHash, codeInfo);
}


/// <summary>
/// Insert code delegations from transaction authorization_list authorized by signature,
/// and return all authority addresses that was accessed.
Expand All @@ -200,10 +199,10 @@ public IEnumerable<Address> InsertFromAuthorizations(
result.Add(authTuple.Authority);
string? error;
if (!IsValidForExecution(authTuple, worldState, _ethereumEcdsa.ChainId, spec, out error))
{
continue;
}

InsertAuthorizedCode(worldState, authTuple.CodeAddress, authTuple.Authority, spec);
worldState.IncrementNonce(authTuple.Authority);
}
return result;

Expand All @@ -214,7 +213,6 @@ void InsertAuthorizedCode(IWorldState state, Address codeSource, Address authori
Hash256 codeHash = Keccak.Compute(authorizedBuffer);
state.InsertCode(authority, codeHash, authorizedBuffer.AsMemory(), spec);
_codeCache.Set(codeHash, new CodeInfo(authorizedBuffer));
state.IncrementNonce(authority);
}
}

Expand All @@ -239,7 +237,7 @@ private bool IsValidForExecution(
return false;
}
if (stateProvider.HasCode(authorizationTuple.Authority)
&& HasDelegatedCode(stateProvider, authorizationTuple.Authority, spec))
&& !HasDelegatedCode(stateProvider, authorizationTuple.Authority))
{
error = $"Authority ({authorizationTuple.Authority}) has code deployed.";
return false;
Expand All @@ -255,19 +253,18 @@ private bool IsValidForExecution(
return true;
}

private bool HasDelegatedCode(IWorldState worldState, Address source, IReleaseSpec spec)
private bool HasDelegatedCode(IWorldState worldState, Address source)
{
CodeInfo codeInfo = GetCachedCodeInfo(worldState, source, spec);
return
HasDelegatedCode(codeInfo.MachineCode.Span);
HasDelegatedCode(worldState.GetCode(source));
}

private static bool HasDelegatedCode(ReadOnlySpan<byte> code)
{
return
code.Length >= Eip7702Constants.DelegationHeader.Length
&& Eip7702Constants.DelegationHeader.SequenceEqual(
code.Slice(Eip7702Constants.DelegationHeader.Length));
code.Slice(0, Eip7702Constants.DelegationHeader.Length));
}

private static Address ParseDelegatedAddress(byte[] code)
Expand Down

0 comments on commit 7dbfcc3

Please sign in to comment.