From 021792d5b6003fd0f1ee26b2d38682f0f57f1e10 Mon Sep 17 00:00:00 2001 From: ak88 Date: Thu, 22 Aug 2024 13:55:32 +0200 Subject: [PATCH] unit tests --- .../CodeInfoRepositoryTests.cs | 70 +++++++++++++++---- .../Nethermind.Evm/CodeInfoRepository.cs | 4 +- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs b/src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs index 7e399647478..6434004c0cb 100644 --- a/src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs +++ b/src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs @@ -20,6 +20,7 @@ using Nethermind.Core.Extensions; using Nethermind.Db; using Nethermind.Trie.Pruning; +using System.Diagnostics.Tracing; namespace Nethermind.Evm.Test; @@ -44,26 +45,35 @@ public static IEnumerable 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(), tuples, Substitute.For()); + CodeInsertResult result = sut.InsertFromAuthorizations(stateProvider, [tuple], Substitute.For()); - result.Addresses.Count().Should().Be(expectedCount); + Assert.That(stateProvider.HasCode(result.Addresses.First()), Is.EqualTo(shouldInsert)); } [Test] @@ -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(), tuples, Substitute.For()); + + 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()); + + result.Refunds.Should().Be(1); + } + private static AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, UInt256? nonce) { AuthorizationTupleDecoder decoder = new(); diff --git a/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs b/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs index 87f6f25b9c6..633974fc4ff 100644 --- a/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs +++ b/src/Nethermind/Nethermind.Evm/CodeInfoRepository.cs @@ -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);