Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes/improvements to tx broadcasting #5619

Merged
merged 5 commits into from
Apr 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ public virtual Task<byte[][]> GetNodeData(IReadOnlyList<Keccak> hashes, Cancella

public abstract void NotifyOfNewBlock(Block block, SendBlockMode mode);

private bool ShouldNotifyTransaction(Keccak? hash) => hash is not null && NotifiedTransactions.Set(hash);

public void SendNewTransaction(Transaction tx)
{
if (tx.Hash != null && NotifiedTransactions.Set(tx.Hash))
if (ShouldNotifyTransaction(tx.Hash))
{
SendNewTransactionCore(tx);
}
}

protected virtual void SendNewTransactionCore(Transaction tx)
{
if (tx.Type != TxType.Blob) //additional protection from sending full blob-type txs
if (!tx.SupportsBlobs) //additional protection from sending full tx with blob
{
SendMessage(new[] { tx });
}
Expand All @@ -205,7 +207,7 @@ private IEnumerable<Transaction> TxsToSendAndMarkAsNotified(IEnumerable<Transact
{
foreach (Transaction tx in txs)
{
if (sendFullTx || (tx.Hash != null && NotifiedTransactions.Set(tx.Hash)))
if (sendFullTx || ShouldNotifyTransaction(tx.Hash))
{
yield return tx;
}
Expand All @@ -228,7 +230,7 @@ protected virtual void SendNewTransactionsCore(IEnumerable<Transaction> txs, boo
packetSizeLeft = TransactionsMessage.MaxPacketSize;
}

if (tx.Hash is not null && tx.Type != TxType.Blob) //additional protection from sending full blob-type txs
if (tx.Hash is not null && !tx.SupportsBlobs) //additional protection from sending full tx with blob
{
txsToSend.Add(tx);
packetSizeLeft -= txSize;
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.TxPool.Test/TxBroadcasterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ public void should_broadcast_hash_of_blob_local_tx_to_eth68_peers_immediately_af
}

[TestCase(1_000, true)]
[TestCase(128_000, true)]
[TestCase(128_001, false)]
[TestCase(128 * 1024, true)]
[TestCase(128 * 1024 + 1, false)]
[TestCase(1_000_000, false)]
public void should_broadcast_full_local_tx_up_to_max_size_and_only_announce_if_larger(int txSize, bool shouldBroadcastFullTx)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.TxPool/TransactionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.CompilerServices;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Int256;
using Nethermind.Serialization.Rlp;

Expand All @@ -12,15 +13,15 @@ namespace Nethermind.TxPool
{
public static class TransactionExtensions
{
private static readonly int MaxSizeOfTxForBroadcast = 128_000; //128KB, as proposed in https://eips.ethereum.org/EIPS/eip-5793
private static readonly long MaxSizeOfTxForBroadcast = 128.KiB(); //128KB, as proposed in https://eips.ethereum.org/EIPS/eip-5793
private static readonly ITransactionSizeCalculator _transactionSizeCalculator = new TxDecoder();

public static int GetLength(this Transaction tx)
{
return tx.GetLength(_transactionSizeCalculator);
}

public static bool CanBeBroadcast(this Transaction tx) => tx.Type != TxType.Blob && tx.GetLength() <= MaxSizeOfTxForBroadcast;
public static bool CanBeBroadcast(this Transaction tx) => !tx.SupportsBlobs && tx.GetLength() <= MaxSizeOfTxForBroadcast;

internal static UInt256 CalculateGasPrice(this Transaction tx, bool eip1559Enabled, in UInt256 baseFee)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Nethermind/Nethermind.TxPool/TxBroadcaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ public void BroadcastOnce(ITxPoolPeer peer, Transaction[] txs)

public void BroadcastPersistentTxs()
{
if (_persistentTxs.Count == 0)
{
if (_logger.IsTrace) _logger.Trace($"There is nothing to broadcast - collection of persistent txs is empty");
return;
}

DateTimeOffset now = DateTimeOffset.Now;
if (_lastPersistedTxBroadcast + _minTimeBetweenPersistedTxBroadcast > now)
{
Expand Down