From 5741cd22fabcd68d385ea4acceab09785dd2bd42 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 12 Dec 2023 08:14:05 +0900 Subject: [PATCH] Expose `txPriority` parameter for customized transaction selection - 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. --- CHANGES.md | 5 +++++ Libplanet.Net/Consensus/ConsensusContext.cs | 16 ++++++++++++++-- Libplanet.Net/Consensus/Context.cs | 17 +++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d8f93c6c296..579a7598ad0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -104,6 +104,10 @@ To be released. for `StateQuery.validators` field. - Added `accountStateRootHash` of type `HashDigest?` argument for `StateQuery.validators` field. + - (Libplanet.Net) Added `txPriority` parameter to `ConsensusContext` + constructor. [[#3546]] + - (Libplanet.Net) Added `txPriority` parameter to `Context` constructor. + [[#3546]] ### Behavioral changes @@ -118,6 +122,7 @@ To be released. [#3512]: https://github.com/planetarium/libplanet/pull/3512 [#3524]: https://github.com/planetarium/libplanet/pull/3524 [#3540]: https://github.com/planetarium/libplanet/pull/3540 +[#3546]: https://github.com/planetarium/libplanet/pull/3546 Version 3.9.0 diff --git a/Libplanet.Net/Consensus/ConsensusContext.cs b/Libplanet.Net/Consensus/ConsensusContext.cs index 51535c78bc3..3fe0220a91c 100644 --- a/Libplanet.Net/Consensus/ConsensusContext.cs +++ b/Libplanet.Net/Consensus/ConsensusContext.cs @@ -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 @@ -26,9 +28,11 @@ public partial class ConsensusContext : IDisposable private readonly TimeSpan _newHeightDelay; private readonly ILogger _logger; private readonly Dictionary _contexts; + private readonly IComparer? _txPriority; private CancellationTokenSource? _newHeightCts; +#pragma warning disable MEN002 /// /// Initializes a new instance of the class. /// @@ -44,18 +48,25 @@ public partial class ConsensusContext : IDisposable /// /// A for /// configuring a timeout for each . + /// An optional comparer for give certain transactions to + /// priority to belong to the block. It will be passed as + /// + /// 's parameter. +#pragma warning restore MEN002 public ConsensusContext( IConsensusMessageCommunicator consensusMessageCommunicator, BlockChain blockChain, PrivateKey privateKey, TimeSpan newHeightDelay, - ContextTimeoutOption contextTimeoutOption) + ContextTimeoutOption contextTimeoutOption, + IComparer? txPriority = null) { _consensusMessageCommunicator = consensusMessageCommunicator; _blockChain = blockChain; _privateKey = privateKey; Height = -1; _newHeightDelay = newHeightDelay; + _txPriority = txPriority; _contextTimeoutOption = contextTimeoutOption; @@ -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; } diff --git a/Libplanet.Net/Consensus/Context.cs b/Libplanet.Net/Consensus/Context.cs index 008ad294281..6d0276b0e31 100644 --- a/Libplanet.Net/Consensus/Context.cs +++ b/Libplanet.Net/Consensus/Context.cs @@ -84,6 +84,7 @@ public partial class Context : IDisposable private readonly BlockChain _blockChain; private readonly Codec _codec; private readonly ValidatorSet _validatorSet; + private readonly IComparer? _txPriority; private readonly Channel _messageRequests; private readonly Channel _mutationRequests; private readonly HeightVoteSet _heightVoteSet; @@ -128,13 +129,18 @@ private readonly /// given . /// A for /// configuring a timeout for each . + /// An optional comparer for give certain transactions to + /// priority to belong to the block. It will be passed as + /// 's + /// parameter. public Context( IConsensusMessageCommunicator consensusMessageCommunicator, BlockChain blockChain, long height, PrivateKey privateKey, ValidatorSet validators, - ContextTimeoutOption contextTimeoutOptions) + ContextTimeoutOption contextTimeoutOptions, + IComparer? txPriority = null) : this( consensusMessageCommunicator, blockChain, @@ -144,7 +150,8 @@ public Context( ConsensusStep.Default, -1, 128, - contextTimeoutOptions) + contextTimeoutOptions, + txPriority) { } @@ -157,7 +164,8 @@ private Context( ConsensusStep consensusStep, int round = -1, int cacheSize = 128, - ContextTimeoutOption? contextTimeoutOptions = null) + ContextTimeoutOption? contextTimeoutOptions = null, + IComparer? txPriority = null) { if (height < 1) { @@ -191,6 +199,7 @@ private Context( _hasTwoThirdsPreVoteFlags = new HashSet(); _preCommitTimeoutFlags = new HashSet(); _validatorSet = validators; + _txPriority = txPriority; _cancellationTokenSource = new CancellationTokenSource(); _blockValidationCache = new LRUCache)>( @@ -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; }