Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
<PackageVersion Include="Nethermind.Crypto.Bls" Version="1.0.5" />
<PackageVersion Include="Nethermind.Crypto.Pairings" Version="1.1.1" />
<PackageVersion Include="Nethermind.Crypto.SecP256k1" Version="1.4.0" />
<PackageVersion Include="Nethermind.Crypto.SecP256k1" Version="1.4.1" />
<PackageVersion Include="Nethermind.Crypto.SecP256r1" Version="1.0.0-preview.6" />
<PackageVersion Include="Nethermind.DotNetty.Buffers" Version="1.0.1" />
<PackageVersion Include="Nethermind.DotNetty.Handlers" Version="1.0.1" />
Expand Down Expand Up @@ -88,4 +88,4 @@
<PackageVersion Include="Websocket.Client" Version="5.2.0" />
<PackageVersion Include="ZstdSharp.Port" Version="0.8.5" />
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion scripts/build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for rid in "linux-x64" "linux-arm64" "win-x64" "osx-x64" "osx-arm64"; do
dotnet publish -c $build_config -r $rid -o $output_path/$rid --sc true \
-p:BuildTimestamp=$2 \
-p:Commit=$1 \
-p:DebugType=none \
-p:DebugType=embedded \
-p:IncludeAllContentForSelfExtract=true \
-p:PublishSingleFile=true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class RecoverSignaturesBenchmark
public void GlobalSetup()
{
_ethereumEcdsa = new(_specProvider.ChainId);
_sut = new(_ethereumEcdsa, NullTxPool.Instance, _specProvider, NullLogManager.Instance);
_sut = new(_ethereumEcdsa, _specProvider, NullLogManager.Instance);

var rnd = new Random();

Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Blockchain.Test/ReorgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public void Setup()
blockProcessor,
new RecoverSignatures(
ecdsa,
txPool,
specProvider,
LimboLogs.Instance),
stateReader,
Expand Down
54 changes: 0 additions & 54 deletions src/Nethermind/Nethermind.Consensus.Test/RecoverSignaturesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void RecoverData_SenderIsNotRecoveredAndNotInPool_SenderAndAuthorityIsRec
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(releaseSpec);
RecoverSignatures sut = new(
_ecdsa,
NullTxPool.Instance,
specProvider,
Substitute.For<ILogManager>());

Expand All @@ -50,57 +49,4 @@ public void RecoverData_SenderIsNotRecoveredAndNotInPool_SenderAndAuthorityIsRec
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 @@ -2,37 +2,29 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Threading;
using Nethermind.Crypto;
using Nethermind.Logging;
using Nethermind.Serialization.Rlp;
using Nethermind.TxPool;

namespace Nethermind.Consensus.Processing
{
public class RecoverSignatures : IBlockPreprocessorStep
{
private readonly IEthereumEcdsa _ecdsa;
private readonly ITxPool _txPool;
private readonly ISpecProvider _specProvider;
private readonly ILogger _logger;

/// <summary>
///
/// </summary>
/// <param name="ecdsa">Needed to recover an address from a signature.</param>
/// <param name="txPool">Finding transactions in mempool can speed up address recovery.</param>
/// <param name="specProvider">Spec Provider</param>
/// <param name="logManager">Logging</param>
public RecoverSignatures(IEthereumEcdsa? ecdsa, ITxPool? txPool, ISpecProvider? specProvider, ILogManager? logManager)
public RecoverSignatures(IEthereumEcdsa? ecdsa, ISpecProvider? specProvider, ILogManager? logManager)
{
_ecdsa = ecdsa ?? throw new ArgumentNullException(nameof(ecdsa));
_txPool = txPool ?? throw new ArgumentNullException(nameof(ecdsa));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
}
Expand All @@ -49,74 +41,9 @@ public void RecoverData(Block block)
// so we assume the rest of txs in the block are already recovered
return;

ParallelUnbalancedWork.For(
0,
txs.Length,
ParallelUnbalancedWork.DefaultOptions,
txs,
static (i, txs) =>
{
Transaction tx = txs[i];
if (!tx.IsHashCalculated)
{
tx.CalculateHashInternal();
}

return txs;
});


int recoverFromEcdsa = 0;
// Don't access txPool in Parallel loop as increases contention
foreach (Transaction tx in txs)
{
if (!ShouldRecoverSignatures(tx))
continue;

Transaction? poolTx = null;
try
{
_txPool.TryGetPendingTransaction(tx.Hash, out poolTx);
}
catch (Exception e)
{
if (_logger.IsError) _logger.Error($"An error occurred while getting a pending transaction from TxPool, Transaction: {tx}", e);
}

Address sender = poolTx?.SenderAddress;
if (sender is not null)
{
tx.SenderAddress = sender;

if (_logger.IsTrace) _logger.Trace($"Recovered {tx.SenderAddress} sender for {tx.Hash} (tx pool cached value: {sender})");
}
else
{
recoverFromEcdsa++;
}

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 if (tx.AuthorizationList[i].Authority is null)
{
recoverFromEcdsa++;
}
}
}
}

if (recoverFromEcdsa == 0)
return;

IReleaseSpec releaseSpec = _specProvider.GetSpec(block.Header);
bool useSignatureChainId = !releaseSpec.ValidateChainId;
if (recoverFromEcdsa > 3)
if (txs.Length > 3)
{
// Recover ecdsa in Parallel
ParallelUnbalancedWork.For(
Expand All @@ -127,7 +54,6 @@ public void RecoverData(Block block)
static (i, state) =>
{
Transaction tx = state.txs[i];
if (!ShouldRecoverSignatures(tx)) return state;

tx.SenderAddress ??= state.recover._ecdsa.RecoverAddress(tx, state.useSignatureChainId);
state.recover.RecoverAuthorities(tx, state.releaseSpec);
Expand All @@ -140,8 +66,6 @@ public void RecoverData(Block block)
{
foreach (Transaction tx in txs)
{
if (!ShouldRecoverSignatures(tx)) continue;

tx.SenderAddress ??= _ecdsa.RecoverAddress(tx, useSignatureChainId);
RecoverAuthorities(tx, releaseSpec);
if (_logger.IsTrace) _logger.Trace($"Recovered {tx.SenderAddress} sender for {tx.Hash}");
Expand All @@ -159,9 +83,10 @@ private void RecoverAuthorities(Transaction tx, IReleaseSpec releaseSpec)

if (tx.AuthorizationList.Length > 3)
{
Parallel.ForEach(tx.AuthorizationList.Where(t => t.Authority is null), (tuple) =>
ParallelUnbalancedWork.For(0, tx.AuthorizationList.Length, (i) =>
{
tuple.Authority = _ecdsa.RecoverAddress(tuple);
AuthorizationTuple tuple = tx.AuthorizationList[i];
tuple.Authority ??= _ecdsa.RecoverAddress(tuple);
});
}
else
Expand All @@ -172,9 +97,5 @@ private void RecoverAuthorities(Transaction tx, IReleaseSpec releaseSpec)
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool ShouldRecoverSignatures(Transaction tx)
=> tx.IsSigned && (tx.SenderAddress is null || (tx.HasAuthorizationList && tx.AuthorizationList.Any(static a => a.Authority is null)));
}
}
Loading
Loading