Skip to content

Commit

Permalink
unittest fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Sep 4, 2024
1 parent d18d4ea commit bf78353
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ [new AuthorizationTuple(
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
],
GasCostOf.PerAuthBaseCost);
GasCostOf.NewAccount);
yield return (
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
Expand All @@ -147,7 +147,7 @@ [new AuthorizationTuple(
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
],
GasCostOf.PerAuthBaseCost * 2);
GasCostOf.NewAccount * 2);
yield return (
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
Expand All @@ -171,7 +171,7 @@ [new AuthorizationTuple(
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
],
GasCostOf.PerAuthBaseCost * 3);
GasCostOf.NewAccount * 3);
}
[TestCaseSource(nameof(AuthorizationListTestCaseSource))]
public void Calculate_TxHasAuthorizationList_ReturnsExpectedCostOfTx((AuthorizationTuple[] AuthorizationList, long ExpectedCost) testCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Execute_TxHasAuthorizationWithCodeThatSavesCallerAddress_ExpectedAdd
Transaction tx = Build.A.Transaction
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithGasLimit(100_000)
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, 0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Expand Down Expand Up @@ -116,13 +116,13 @@ public void Execute_TxHasAuthorizationCodeButAuthorityHasCode_NoAuthorizedCodeIs

public static IEnumerable<object[]> SenderSignerCases()
{
yield return new object[] { TestItem.PrivateKeyA, TestItem.PrivateKeyB };
yield return new object[] { TestItem.PrivateKeyA, TestItem.PrivateKeyA };
yield return new object[] { TestItem.PrivateKeyA, TestItem.PrivateKeyB, 0ul };
yield return new object[] { TestItem.PrivateKeyA, TestItem.PrivateKeyA, 1ul };
}
[TestCaseSource(nameof(SenderSignerCases))]
public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_SenderAddressIsSaved(PrivateKey sender, PrivateKey signer)
public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_SenderAddressIsSaved(PrivateKey sender, PrivateKey signer, ulong nonce)
{
Address codeSource = TestItem.AddressB;
Address codeSource = TestItem.AddressC;
_stateProvider.CreateAccount(sender.Address, 1.Ether());
//Save caller in storage slot 0
byte[] code = Prepare.EvmCode
Expand All @@ -136,7 +136,7 @@ public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, 0))
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, nonce))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand All @@ -148,18 +148,16 @@ public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_

ReadOnlySpan<byte> cell = _stateProvider.Get(new StorageCell(signer.Address, 0));

Assert.That(new Address(cell.ToArray()), Is.EqualTo(sender.Address));
Assert.That(sender.Address.Bytes, Is.EqualTo(cell.ToArray()));
}
public static IEnumerable<object[]> DifferentCommitValues()
{
//Base case
yield return new object[] { 1ul, 0, TestItem.AddressA.Bytes };
yield return new object[] { 1ul, 0ul, TestItem.AddressA.Bytes };
//Wrong nonce
yield return new object[] { 1ul, 1, new[] { (byte)0x0 } };
//Null nonce means it should be ignored
yield return new object[] { 1ul, 0, TestItem.AddressA.Bytes };
yield return new object[] { 1ul, 1ul, new[] { (byte)0x0 } };
//Wrong chain id
yield return new object[] { 2ul, 0, new[] { (byte)0x0 } };
yield return new object[] { 2ul, 0ul, new[] { (byte)0x0 } };
}

[TestCaseSource(nameof(DifferentCommitValues))]
Expand Down Expand Up @@ -207,7 +205,7 @@ public void Execute_TxHasDifferentAmountOfAuthorizedCode_UsedGasIsExpected(int c
Transaction tx = Build.A.Transaction
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(GasCostOf.Transaction + GasCostOf.PerAuthBaseCost * count)
.WithGasLimit(GasCostOf.Transaction + GasCostOf.NewAccount * count)
.WithAuthorizationCode(Enumerable.Range(0, count)
.Select(i => CreateAuthorizationTuple(
signer,
Expand All @@ -226,7 +224,7 @@ public void Execute_TxHasDifferentAmountOfAuthorizedCode_UsedGasIsExpected(int c

_transactionProcessor.Execute(tx, block.Header, tracer);

Assert.That(tracer.GasSpent, Is.EqualTo(GasCostOf.Transaction + GasCostOf.PerAuthBaseCost * count));
Assert.That(tracer.GasSpent, Is.EqualTo(GasCostOf.Transaction + GasCostOf.NewAccount * count));
}

[Test]
Expand Down Expand Up @@ -265,15 +263,15 @@ public void Execute_TxAuthorizationListWithBALANCE_WarmAccountReadGasIsCharged()
_transactionProcessor.Execute(tx, block.Header, tracer);
//Tx should only be charged for warm state read
Assert.That(tracer.GasSpent, Is.EqualTo(GasCostOf.Transaction
+ GasCostOf.PerAuthBaseCost
+ GasCostOf.NewAccount
+ Prague.Instance.GetBalanceCost()
+ GasCostOf.WarmStateRead
+ GasCostOf.VeryLow));
}

[TestCase(false, 1)]
[TestCase(true, 2)]
public void Execute_AuthorizationListHasSameAuthorityButDifferentCode_OnlyFirstInstanceIsUsed(bool reverseOrder, int expectedStoredValue)
[TestCase(2)]
[TestCase(1)]
public void Execute_AuthorizationListHasSameAuthorityButDifferentCode_OnlyLastInstanceIsUsed(int expectedStoredValue)
{
PrivateKey sender = TestItem.PrivateKeyA;
PrivateKey signer = TestItem.PrivateKeyB;
Expand All @@ -282,14 +280,14 @@ public void Execute_AuthorizationListHasSameAuthorityButDifferentCode_OnlyFirstI
_stateProvider.CreateAccount(sender.Address, 1.Ether());

byte[] firstCode = Prepare.EvmCode
.PushData(1)
.PushData(0)
.Op(Instruction.PUSH0)
.Op(Instruction.SSTORE)
.Done;
DeployCode(firstCodeSource, firstCode);

byte[] secondCode = Prepare.EvmCode
.PushData(2)
.PushData(expectedStoredValue)
.Op(Instruction.PUSH0)
.Op(Instruction.SSTORE)
.Done;
Expand All @@ -305,16 +303,12 @@ public void Execute_AuthorizationListHasSameAuthorityButDifferentCode_OnlyFirstI
signer,
_specProvider.ChainId,
secondCodeSource,
0),
1),
];
if (reverseOrder)
{
authList = authList.Reverse();
}
Transaction tx = Build.A.Transaction
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithGasLimit(100_000)
.WithAuthorizationCode(authList)
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Int256;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace Nethermind.Serialization.Rlp;

Expand Down

0 comments on commit bf78353

Please sign in to comment.