Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Aug 22, 2024
1 parent 0cc3cfe commit 021792d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
70 changes: 58 additions & 12 deletions src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.Core.Extensions;
using Nethermind.Db;
using Nethermind.Trie.Pruning;
using System.Diagnostics.Tracing;

namespace Nethermind.Evm.Test;

Expand All @@ -44,26 +45,35 @@ public static IEnumerable<object[]> AuthorizationCases()
{
yield return new object[]
{
new[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressB, (UInt256)0),
//Wrong chain id
CreateAuthorizationTuple(TestItem.PrivateKeyA, 0, TestItem.AddressB, (UInt256)0),
//wrong nonce
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressB, (UInt256)1),
}
, 1
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressB, (UInt256)0),
true
};
yield return new object[]
{
//Wrong chain id
CreateAuthorizationTuple(TestItem.PrivateKeyB, 2, TestItem.AddressB, (UInt256)0),
false
};
yield return new object[]
{
//wrong nonce
CreateAuthorizationTuple(TestItem.PrivateKeyC, 1, TestItem.AddressB, (UInt256)1),
false
};
}

[TestCaseSource(nameof(AuthorizationCases))]
public void InsertFromAuthorizations_MixOfCorrectAndWrongChainIdAndNonce_InsertsExpectedCount(AuthorizationTuple[] tuples, int expectedCount)
public void InsertFromAuthorizations_MixOfCorrectAndWrongChainIdAndNonce_InsertsIfExpected(AuthorizationTuple tuple, bool shouldInsert)
{
IDb stateDb = new MemDb();
IDb codeDb = new MemDb();
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
CodeInfoRepository sut = new(1);

CodeInsertResult result = sut.InsertFromAuthorizations(Substitute.For<IWorldState>(), tuples, Substitute.For<IReleaseSpec>());
CodeInsertResult result = sut.InsertFromAuthorizations(stateProvider, [tuple], Substitute.For<IReleaseSpec>());

result.Addresses.Count().Should().Be(expectedCount);
Assert.That(stateProvider.HasCode(result.Addresses.First()), Is.EqualTo(shouldInsert));
}

[Test]
Expand Down Expand Up @@ -127,6 +137,42 @@ public void InsertFromAuthorizations_AuthorityHasZeroNonce_NonceIsIncrementedByO
Assert.That(stateProvider.GetNonce(authority.Address), Is.EqualTo((UInt256)1));
}

[Test]
public void InsertFromAuthorizations_FourAuthorizationInTotalButTwoAreInvalid_ResultContainsAllFour()
{
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyC, 2, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyD, 1, TestItem.AddressF, (UInt256)1),
};
CodeInsertResult result = sut.InsertFromAuthorizations(Substitute.For<IWorldState>(), tuples, Substitute.For<IReleaseSpec>());

result.Addresses.Should().BeEquivalentTo([TestItem.AddressA, TestItem.AddressB, TestItem.AddressC, TestItem.AddressD]);
}

[Test]
public void InsertFromAuthorizations_AuthorizationsHasOneExistingAccount_ResultHaveOneRefund()
{
IDb stateDb = new MemDb();
IDb codeDb = new MemDb();
TrieStore trieStore = new(stateDb, LimboLogs.Instance);
IWorldState stateProvider = new WorldState(trieStore, codeDb, LimboLogs.Instance);
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, (UInt256)0),
};
stateProvider.CreateAccount(TestItem.AddressA, 0);

CodeInsertResult result = sut.InsertFromAuthorizations(stateProvider, tuples, Substitute.For<IReleaseSpec>());

result.Refunds.Should().Be(1);
}

private static AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, UInt256? nonce)
{
AuthorizationTupleDecoder decoder = new();
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ public CodeInsertResult InsertFromAuthorizations(
if (!IsValidForExecution(authTuple, worldState, _ethereumEcdsa.ChainId, spec, out error))
continue;

InsertAuthorizedCode(worldState, authTuple.CodeAddress, authTuple.Authority, spec);

if (!worldState.AccountExists(authTuple.Authority))
worldState.CreateAccount(authTuple.Authority, 0);
else
refunds++;

InsertAuthorizedCode(worldState, authTuple.CodeAddress, authTuple.Authority, spec);

worldState.IncrementNonce(authTuple.Authority);
}
return new CodeInsertResult(result, refunds);
Expand Down

0 comments on commit 021792d

Please sign in to comment.