Skip to content

Commit ace6000

Browse files
flcl42kamilchodola
authored andcommitted
Return raw txs in mempool form if possible (#8330)
1 parent f9e7640 commit ace6000

8 files changed

Lines changed: 46 additions & 26 deletions

File tree

src/Nethermind/Nethermind.Blockchain.Test/Validators/BlockValidatorTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Nethermind.Core.Crypto;
77
using Nethermind.Core.Specs;
88
using Nethermind.Core.Test.Builders;
9-
using Nethermind.Crypto;
109
using Nethermind.Logging;
1110
using Nethermind.Specs;
1211
using Nethermind.Specs.Forks;
@@ -159,8 +158,6 @@ public void ValidateProcessedBlock_StateRootIsWrong_ErrorIsSet()
159158

160159
private static IEnumerable<TestCaseData> BadSuggestedBlocks()
161160
{
162-
KzgPolynomialCommitments.InitializeAsync().Wait();
163-
164161
yield return new TestCaseData(
165162
Build.A.Block.WithHeader(Build.A.BlockHeader.WithUnclesHash(Keccak.Zero).TestObject).TestObject,
166163
Substitute.For<ISpecProvider>(),

src/Nethermind/Nethermind.Blockchain.Test/Validators/TxValidatorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ private static IEnumerable<TestCaseData> ShardBlobTxIncorrectTransactions
687687
{
688688
get
689689
{
690-
KzgPolynomialCommitments.InitializeAsync().Wait();
691690
static TransactionBuilder<Transaction> MakeTestObject(int blobCount = 1) => Build.A.Transaction
692691
.WithChainId(TestBlockchainIds.ChainId)
693692
.WithTimestamp(ulong.MaxValue)

src/Nethermind/Nethermind.Core.Test/Builders/TransactionBuilder.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public TransactionBuilder<T> WithShardBlobTxTypeAndFields(int blobCount = 1, boo
183183
proofs: new byte[blobCount][]
184184
);
185185

186+
if (!KzgPolynomialCommitments.IsInitialized)
187+
{
188+
KzgPolynomialCommitments.InitializeAsync().Wait();
189+
}
190+
186191
for (int i = 0; i < blobCount; i++)
187192
{
188193
TestObjectInternal.BlobVersionedHashes[i] = new byte[32];
@@ -191,20 +196,11 @@ public TransactionBuilder<T> WithShardBlobTxTypeAndFields(int blobCount = 1, boo
191196
wrapper.Commitments[i] = new byte[Ckzg.Ckzg.BytesPerCommitment];
192197
wrapper.Proofs[i] = new byte[Ckzg.Ckzg.BytesPerProof];
193198

194-
if (KzgPolynomialCommitments.IsInitialized)
195-
{
196-
KzgPolynomialCommitments.KzgifyBlob(
197-
wrapper.Blobs[i],
198-
wrapper.Commitments[i],
199-
wrapper.Proofs[i],
200-
TestObjectInternal.BlobVersionedHashes[i].AsSpan());
201-
}
202-
else
203-
{
204-
TestObjectInternal.BlobVersionedHashes[i]![0] = KzgPolynomialCommitments.KzgBlobHashVersionV1;
205-
wrapper.Commitments[i][0] = (byte)(i % 256);
206-
wrapper.Proofs[i][0] = (byte)(i % 256);
207-
}
199+
KzgPolynomialCommitments.KzgifyBlob(
200+
wrapper.Blobs[i],
201+
wrapper.Commitments[i],
202+
wrapper.Proofs[i],
203+
TestObjectInternal.BlobVersionedHashes[i].AsSpan());
208204
}
209205

210206
TestObjectInternal.NetworkWrapper = wrapper;

src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using Nethermind.Facade.Eth.RpcTransaction;
2828
using Nethermind.Facade.Filters;
2929
using Nethermind.Int256;
30+
using Nethermind.JsonRpc.Client;
3031
using Nethermind.Serialization.Json;
3132
using Nethermind.Serialization.Rlp;
3233
using Nethermind.Specs;
@@ -104,6 +105,21 @@ public async Task Eth_get_raw_transaction_by_hash_for_typed_transaction()
104105
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"02f86c0180850165a0bc0085028fa6ae008252089400000000000000000000000000000000000000000180c080a063b08cc0a06c88fb1dd79f273736b3463af12c6754f9df764aa222d2693a5d43a0606b869eab1c9d01ff462f887826cb8f349ea8f1b59d0635ae77155b3b84ad86\",\"id\":67}"), serialized.Replace("\"", "\\\""));
105106
}
106107

108+
[Test]
109+
public async Task Eth_get_raw_transaction_by_hash_from_pool()
110+
{
111+
using Context ctx = await Context.CreateWithCancunEnabled();
112+
Transaction sent = Build.A.Transaction.WithShardBlobTxTypeAndFields().WithMaxPriorityFeePerGas(6.GWei()).WithMaxFeePerGas(11.GWei()).SignedAndResolved(TestItem.PrivateKeyC).TestObject;
113+
ctx.Test.TxPool.SubmitTx(sent, TxHandlingOptions.None);
114+
115+
string serialized = await ctx.Test.TestEthRpc("eth_getRawTransactionByHash", sent.Hash);
116+
byte[]? txBytes = new EthereumJsonSerializer().Deserialize<JsonRpcResponse<byte[]>>(serialized).Result;
117+
118+
Transaction tx = null!;
119+
Assert.DoesNotThrow(() => tx = TxDecoder.Instance.Decode(txBytes, RlpBehaviors.SkipTypedWrapping | RlpBehaviors.InMempoolForm));
120+
Assert.That(tx.IsInMempoolForm());
121+
}
122+
107123

108124
[Test]
109125
public async Task eth_maxPriorityFeePerGas_test()

src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/DebugRpcModule.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using Nethermind.JsonRpc.Modules.Eth;
2020
using Nethermind.Core.Specs;
2121
using Nethermind.Facade.Eth.RpcTransaction;
22+
using DotNetty.Buffers;
23+
using Nethermind.TxPool;
2224

2325
namespace Nethermind.JsonRpc.Modules.DebugModule;
2426

@@ -291,15 +293,21 @@ public ResultWrapper<bool> debug_resetHead(Hash256 blockHash)
291293
return ResultWrapper<bool>.Success(true);
292294
}
293295

294-
public ResultWrapper<byte[]> debug_getRawTransaction(Hash256 transactionHash)
296+
public ResultWrapper<string?> debug_getRawTransaction(Hash256 transactionHash)
295297
{
296-
var transaction = _debugBridge.GetTransactionFromHash(transactionHash);
298+
Transaction? transaction = _debugBridge.GetTransactionFromHash(transactionHash);
297299
if (transaction is null)
298300
{
299-
return ResultWrapper<byte[]>.Fail($"Transaction {transactionHash} was not found", ErrorCodes.ResourceNotFound);
301+
return ResultWrapper<string?>.Fail($"Transaction {transactionHash} was not found", ErrorCodes.ResourceNotFound);
300302
}
301-
var rlp = Rlp.Encode(transaction);
302-
return ResultWrapper<byte[]>.Success(rlp.Bytes);
303+
304+
RlpBehaviors encodingSettings = RlpBehaviors.SkipTypedWrapping | (transaction.IsInMempoolForm() ? RlpBehaviors.InMempoolForm : RlpBehaviors.None);
305+
306+
IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer(TxDecoder.Instance.GetLength(transaction, encodingSettings));
307+
using NettyRlpStream stream = new(buffer);
308+
TxDecoder.Instance.Encode(stream, transaction, encodingSettings);
309+
310+
return ResultWrapper<string?>.Success(buffer.AsSpan().ToHexString(false));
303311
}
304312

305313
public ResultWrapper<byte[][]> debug_getRawReceipts(BlockParameter blockParameter)

src/Nethermind/Nethermind.JsonRpc/Modules/DebugModule/IDebugRpcModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public interface IDebugRpcModule : IRpcModule
100100
ResultWrapper<byte[]> debug_getRawHeader(BlockParameter blockParameter);
101101

102102
[JsonRpcMethod(Description = "Get Raw Transaction format.")]
103-
ResultWrapper<byte[]> debug_getRawTransaction(Hash256 transactionHash);
103+
ResultWrapper<string> debug_getRawTransaction(Hash256 transactionHash);
104104

105105
[JsonRpcMethod(Description = "Retrives Nethermind Sync Stage, With extra Metadata")]
106106
Task<ResultWrapper<SyncReportSymmary>> debug_getSyncStage();

src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,11 @@ public ResultWrapper<BlockForRpc> eth_getBlockByNumber(BlockParameter blockParam
406406
return ResultWrapper<string?>.Success(null);
407407
}
408408

409-
IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer(TxDecoder.Instance.GetLength(transaction, RlpBehaviors.None));
409+
RlpBehaviors encodingSettings = RlpBehaviors.SkipTypedWrapping | (transaction.IsInMempoolForm() ? RlpBehaviors.InMempoolForm : RlpBehaviors.None);
410+
411+
IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer(TxDecoder.Instance.GetLength(transaction, encodingSettings));
410412
using NettyRlpStream stream = new(buffer);
411-
TxDecoder.Instance.Encode(stream, transaction, RlpBehaviors.SkipTypedWrapping);
413+
TxDecoder.Instance.Encode(stream, transaction, encodingSettings);
412414

413415
return ResultWrapper<string?>.Success(buffer.AsSpan().ToHexString(false));
414416
}

src/Nethermind/Nethermind.TxPool/TransactionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,7 @@ internal static bool IsOverflowWhenAddingTxCostToCumulative(this Transaction tx,
9191

9292
internal static bool IsOverflowInTxCostAndValue(this Transaction tx, out UInt256 txCost)
9393
=> IsOverflowWhenAddingTxCostToCumulative(tx, UInt256.Zero, out txCost);
94+
95+
public static bool IsInMempoolForm(this Transaction tx) => tx.NetworkWrapper is not null;
9496
}
9597
}

0 commit comments

Comments
 (0)