Skip to content

Commit

Permalink
Expose txPriority parameter for customized transaction selection
Browse files Browse the repository at this point in the history
- Added `txPriority` as an argument in the public constructor API.
- This allows external callers to specify the priority for including
  transactions in the block to propose.
  • Loading branch information
moreal committed Dec 11, 2023
1 parent 85f1868 commit 6f5bdd0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 14 additions & 2 deletions Libplanet.Net/Consensus/ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Libplanet.Crypto;
using Libplanet.Net.Messages;
using Libplanet.Types.Blocks;
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Serilog;

namespace Libplanet.Net.Consensus
Expand All @@ -26,9 +28,11 @@ public partial class ConsensusContext : IDisposable
private readonly TimeSpan _newHeightDelay;
private readonly ILogger _logger;
private readonly Dictionary<long, Context> _contexts;
private readonly IComparer<Transaction>? _txPriority;

private CancellationTokenSource? _newHeightCts;

#pragma warning disable MEN002
/// <summary>
/// Initializes a new instance of the <see cref="ConsensusContext"/> class.
/// </summary>
Expand All @@ -44,18 +48,25 @@ public partial class ConsensusContext : IDisposable
/// </param>
/// <param name="contextTimeoutOption">A <see cref="ContextTimeoutOption"/> for
/// configuring a timeout for each <see cref="Step"/>.</param>
/// <param name="txPriority">An optional comparer for give certain transactions to
/// priority to belong to the block. It will be passed as
/// <see cref="Context(IConsensusMessageCommunicator,BlockChain,long,PrivateKey,ValidatorSet,ContextTimeoutOption,IComparer{Transaction})"/>
/// 's parameter.</param>
#pragma warning restore MEN002
public ConsensusContext(
IConsensusMessageCommunicator consensusMessageCommunicator,
BlockChain blockChain,
PrivateKey privateKey,
TimeSpan newHeightDelay,
ContextTimeoutOption contextTimeoutOption)
ContextTimeoutOption contextTimeoutOption,
IComparer<Transaction>? txPriority = null)
{
_consensusMessageCommunicator = consensusMessageCommunicator;
_blockChain = blockChain;
_privateKey = privateKey;
Height = -1;
_newHeightDelay = newHeightDelay;
_txPriority = txPriority;

_contextTimeoutOption = contextTimeoutOption;

Expand Down Expand Up @@ -439,7 +450,8 @@ private Context CreateContext(long height)
height,
_privateKey,
_blockChain.GetValidatorSet(_blockChain[Height - 1].Hash),
contextTimeoutOptions: _contextTimeoutOption);
contextTimeoutOptions: _contextTimeoutOption,
txPriority: _txPriority);
AttachEventHandlers(context);
return context;
}
Expand Down
17 changes: 13 additions & 4 deletions Libplanet.Net/Consensus/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public partial class Context : IDisposable
private readonly BlockChain _blockChain;
private readonly Codec _codec;
private readonly ValidatorSet _validatorSet;
private readonly IComparer<Transaction>? _txPriority;
private readonly Channel<ConsensusMsg> _messageRequests;
private readonly Channel<System.Action> _mutationRequests;
private readonly HeightVoteSet _heightVoteSet;
Expand Down Expand Up @@ -128,13 +129,18 @@ private readonly
/// given <paramref name="height"/>.</param>
/// <param name="contextTimeoutOptions">A <see cref="ContextTimeoutOption"/> for
/// configuring a timeout for each <see cref="ConsensusStep"/>.</param>
/// <param name="txPriority">An optional comparer for give certain transactions to
/// priority to belong to the block. It will be passed as
/// <see cref="BlockChain.GatherTransactionsToPropose(long,IComparer{Transaction})"/>'s
/// parameter.</param>
public Context(
IConsensusMessageCommunicator consensusMessageCommunicator,
BlockChain blockChain,
long height,
PrivateKey privateKey,
ValidatorSet validators,
ContextTimeoutOption contextTimeoutOptions)
ContextTimeoutOption contextTimeoutOptions,
IComparer<Transaction>? txPriority = null)
: this(
consensusMessageCommunicator,
blockChain,
Expand All @@ -144,7 +150,8 @@ public Context(
ConsensusStep.Default,
-1,
128,
contextTimeoutOptions)
contextTimeoutOptions,
txPriority)
{
}

Expand All @@ -157,7 +164,8 @@ private Context(
ConsensusStep consensusStep,
int round = -1,
int cacheSize = 128,
ContextTimeoutOption? contextTimeoutOptions = null)
ContextTimeoutOption? contextTimeoutOptions = null,
IComparer<Transaction>? txPriority = null)
{
if (height < 1)
{
Expand Down Expand Up @@ -191,6 +199,7 @@ private Context(
_hasTwoThirdsPreVoteFlags = new HashSet<int>();
_preCommitTimeoutFlags = new HashSet<int>();
_validatorSet = validators;
_txPriority = txPriority;
_cancellationTokenSource = new CancellationTokenSource();
_blockValidationCache =
new LRUCache<BlockHash, (bool, IReadOnlyList<ICommittedActionEvaluation>)>(
Expand Down Expand Up @@ -418,7 +427,7 @@ private TimeSpan TimeoutPropose(long round)
{
try
{
Block block = _blockChain.ProposeBlock(_privateKey, _lastCommit);
Block block = _blockChain.ProposeBlock(_privateKey, _lastCommit, _txPriority);
_blockChain.Store.PutBlock(block);
return block;
}
Expand Down

0 comments on commit 6f5bdd0

Please sign in to comment.