Skip to content

Commit

Permalink
Write out IByteBuffer before it is released (#6459)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 5, 2024
1 parent 6c9309c commit 4550bfc
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.TxPool/BlobTxStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Add(Transaction transaction)
Span<byte> txHashPrefixed = stackalloc byte[64];
GetHashPrefixedByTimestamp(transaction.Timestamp, transaction.Hash, txHashPrefixed);

_fullBlobTxsDb.PutSpan(txHashPrefixed, EncodeTx(transaction));
EncodeAndSaveTx(transaction, _fullBlobTxsDb, txHashPrefixed);
_lightBlobTxsDb.Set(transaction.Hash, LightTxDecoder.Encode(transaction));
}

Expand All @@ -85,7 +85,7 @@ public void AddBlobTransactionsFromBlock(long blockNumber, IList<Transaction> bl
return;
}

_processedBlobTxsDb.Set(blockNumber, EncodeTxs(blockBlobTransactions));
EncodeAndSaveTxs(blockBlobTransactions, _processedBlobTxsDb, blockNumber);
}

public bool TryGetBlobTransactionsFromBlock(long blockNumber, out Transaction[]? blockBlobTransactions)
Expand Down Expand Up @@ -138,17 +138,17 @@ private static void GetHashPrefixedByTimestamp(UInt256 timestamp, ValueHash256 h
hash.Bytes.CopyTo(txHashPrefixed[32..]);
}

private Span<byte> EncodeTx(Transaction transaction)
private void EncodeAndSaveTx(Transaction transaction, IDb db, Span<byte> txHashPrefixed)
{
int length = _txDecoder.GetLength(transaction, RlpBehaviors.InMempoolForm);
IByteBuffer byteBuffer = PooledByteBufferAllocator.Default.Buffer(length);
using NettyRlpStream rlpStream = new(byteBuffer);
rlpStream.Encode(transaction, RlpBehaviors.InMempoolForm);

return byteBuffer.AsSpan();
db.PutSpan(txHashPrefixed, byteBuffer.AsSpan());
}

private byte[] EncodeTxs(IList<Transaction> blockBlobTransactions)
private void EncodeAndSaveTxs(IList<Transaction> blockBlobTransactions, IDb db, long blockNumber)
{
int contentLength = GetLength(blockBlobTransactions);

Expand All @@ -160,7 +160,7 @@ private byte[] EncodeTxs(IList<Transaction> blockBlobTransactions)
_txDecoder.Encode(rlpStream, transaction, RlpBehaviors.InMempoolForm);
}

return byteBuffer.Array;
db.Set(blockNumber, byteBuffer.Array);
}

private int GetLength(IList<Transaction> blockBlobTransactions)
Expand Down

0 comments on commit 4550bfc

Please sign in to comment.