From c92c558bb7b1c94abde5a32a3c195737ef0e6998 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 | 7 +++++++ Libplanet.Net/Consensus/ConsensusContext.cs | 16 ++++++++++++++-- Libplanet.Net/Consensus/ConsensusReactor.cs | 13 +++++++++++-- .../Consensus/ConsensusReactorOption.cs | 9 +++++++++ Libplanet.Net/Consensus/Context.cs | 17 +++++++++++++---- Libplanet.Net/Swarm.cs | 3 ++- 6 files changed, 56 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d8f93c6c296..fbe3a6544d4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -104,6 +104,12 @@ 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]] + - (Libplanet.Net) Added `TxPriority` property to `ConsensusReactorOption` + class. [[#3546]] ### Behavioral changes @@ -118,6 +124,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/ConsensusReactor.cs b/Libplanet.Net/Consensus/ConsensusReactor.cs index 6beab8853fc..0a944f367e7 100644 --- a/Libplanet.Net/Consensus/ConsensusReactor.cs +++ b/Libplanet.Net/Consensus/ConsensusReactor.cs @@ -10,6 +10,7 @@ using Libplanet.Crypto; using Libplanet.Net.Messages; using Libplanet.Net.Transports; +using Libplanet.Types.Tx; using Serilog; namespace Libplanet.Net.Consensus @@ -25,6 +26,7 @@ public class ConsensusReactor : IReactor private readonly BlockChain _blockChain; private readonly ILogger _logger; +#pragma warning disable MEN002 /// /// Initializes a new instance of the class. /// @@ -45,6 +47,11 @@ public class ConsensusReactor : IReactor /// /// 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 ConsensusReactor( ITransport consensusTransport, BlockChain blockChain, @@ -52,7 +59,8 @@ public ConsensusReactor( ImmutableList validatorPeers, ImmutableList seedPeers, TimeSpan newHeightDelay, - ContextTimeoutOption contextTimeoutOption) + ContextTimeoutOption contextTimeoutOption, + IComparer? txPriority = null) { validatorPeers ??= ImmutableList.Empty; seedPeers ??= ImmutableList.Empty; @@ -71,7 +79,8 @@ public ConsensusReactor( blockChain, privateKey, newHeightDelay, - contextTimeoutOption); + contextTimeoutOption, + txPriority); _logger = Log .ForContext("Tag", "Consensus") diff --git a/Libplanet.Net/Consensus/ConsensusReactorOption.cs b/Libplanet.Net/Consensus/ConsensusReactorOption.cs index f39aa792e34..5c33ca6d67e 100644 --- a/Libplanet.Net/Consensus/ConsensusReactorOption.cs +++ b/Libplanet.Net/Consensus/ConsensusReactorOption.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; +using Libplanet.Blockchain; using Libplanet.Crypto; using Libplanet.Net.Messages; using Libplanet.Net.Transports; +using Libplanet.Types.Tx; namespace Libplanet.Net.Consensus { @@ -45,5 +48,11 @@ public struct ConsensusReactorOption /// A timeout second and multiplier value for used in . /// public ContextTimeoutOption ContextTimeoutOptions { get; set; } + + /// + /// An optional comparer for give certain transactions to priority to belong to the block. + /// + /// + public IComparer? TxPriorityComparer { get; set; } } } 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; } diff --git a/Libplanet.Net/Swarm.cs b/Libplanet.Net/Swarm.cs index 9d4d8317cff..5a3e8d2fa87 100644 --- a/Libplanet.Net/Swarm.cs +++ b/Libplanet.Net/Swarm.cs @@ -117,7 +117,8 @@ public Swarm( consensusReactorOption.ConsensusPeers, consensusReactorOption.SeedPeers, consensusReactorOption.TargetBlockInterval, - consensusReactorOption.ContextTimeoutOptions); + consensusReactorOption.ContextTimeoutOptions, + consensusReactorOption.TxPriorityComparer); } }