Skip to content

Commit

Permalink
Fix null pointer in RecoverSignatures (#7564)
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 authored Oct 10, 2024
1 parent 8296752 commit 7ef9398
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
106 changes: 106 additions & 0 deletions src/Nethermind/Nethermind.Consensus.Test/RecoverSignaturesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Test.Builders;
using Nethermind.Core;
using NUnit.Framework;
using Nethermind.Consensus.Processing;
using NSubstitute;
using Nethermind.Crypto;
using Nethermind.Core.Specs;
using Nethermind.Logging;
using Nethermind.TxPool;
using System.Linq;
using Nethermind.Core.Crypto;

namespace Nethermind.Consensus.Test;
[TestFixture]
public class RecoverSignaturesTest
{
private static readonly IEthereumEcdsa _ecdsa = new EthereumEcdsa(BlockchainIds.GenericNonRealNetwork);

[Test]
public void RecoverData_SenderIsNotRecoveredAndNotInPool_SenderAndAuthorityIsRecovered()
{
PrivateKey signer = TestItem.PrivateKeyA;
PrivateKey authority = TestItem.PrivateKeyB;
Transaction tx = Build.A.Transaction
.WithType(TxType.SetCode)
.WithAuthorizationCode(_ecdsa.Sign(authority, 0, Address.Zero, 0))
.SignedAndResolved(signer)
.WithSenderAddress(null)
.TestObject;

Block block = Build.A.Block
.WithTransactions([tx])
.TestObject;

ISpecProvider specProvider = Substitute.For<ISpecProvider>();
IReleaseSpec releaseSpec = Substitute.For<IReleaseSpec>();
releaseSpec.IsAuthorizationListEnabled.Returns(true);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(releaseSpec);
RecoverSignatures sut = new(
_ecdsa,
NullTxPool.Instance,
specProvider,
Substitute.For<ILogManager>());

sut.RecoverData(block);

Assert.That(tx.SenderAddress, Is.EqualTo(signer.Address));
Assert.That(tx.AuthorizationList.First().Authority, Is.EqualTo(authority.Address));
}

[Test]
public void RecoverData_TxIsInPool_SenderAndAuthoritiesIsSetToSameAsInPool()
{
PrivateKey signer = TestItem.PrivateKeyA;
PrivateKey poolSender = TestItem.PrivateKeyB;
Transaction tx = Build.A.Transaction
.WithType(TxType.SetCode)
.WithAuthorizationCode
([
new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[65]), null),
new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[65]), null),
])
.SignedAndResolved(signer)
.WithSenderAddress(null)
.TestObject;

Block block = Build.A.Block
.WithTransactions([tx])
.TestObject;

Transaction txInPool = Build.A.Transaction
.WithType(TxType.SetCode)
.WithAuthorizationCode
([
new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[65]), poolSender.Address),
new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[65]), poolSender.Address),
])
.SignedAndResolved(poolSender)
.TestObject;
ITxPool txPool = Substitute.For<ITxPool>();
txPool
.TryGetPendingTransaction(Arg.Any<Hash256>(), out Arg.Any<Transaction>())
.Returns(x =>
{
x[1] = txInPool;
return true;
});
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
IReleaseSpec releaseSpec = Substitute.For<IReleaseSpec>();
releaseSpec.IsAuthorizationListEnabled.Returns(true);
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(releaseSpec);
RecoverSignatures sut = new(
_ecdsa,
txPool,
specProvider,
Substitute.For<ILogManager>());

sut.RecoverData(block);

Assert.That(tx.SenderAddress, Is.EqualTo(poolSender.Address));
Assert.That(tx.AuthorizationList.Select(a => a.Authority), Is.All.EqualTo(poolSender.Address));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public void RecoverData(Block block)
recoverFromEcdsa++;
}

if (tx.HasAuthorizationList)
if (poolTx is not null && tx.HasAuthorizationList)
{
for (int i = 0; i < tx.AuthorizationList.Length; i++)
{
if (poolTx.AuthorizationList[i].Authority is not null)
{
tx.AuthorizationList[i].Authority = poolTx.AuthorizationList[i].Authority;
}
else
else if (tx.AuthorizationList[i].Authority is null)
{
recoverFromEcdsa++;
}
Expand Down

0 comments on commit 7ef9398

Please sign in to comment.