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 12, 2023
1 parent 85f1868 commit c92c558
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ To be released.
for `StateQuery.validators` field.
- Added `accountStateRootHash` of type `HashDigest<SHA256>?` 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

Expand All @@ -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
Expand Down
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
13 changes: 11 additions & 2 deletions Libplanet.Net/Consensus/ConsensusReactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,7 @@ public class ConsensusReactor : IReactor
private readonly BlockChain _blockChain;
private readonly ILogger _logger;

#pragma warning disable MEN002
/// <summary>
/// Initializes a new instance of the <see cref="ConsensusReactor"/> class.
/// </summary>
Expand All @@ -45,14 +47,20 @@ public class ConsensusReactor : IReactor
/// </param>
/// <param name="contextTimeoutOption">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="Consensus.ConsensusContext(IConsensusMessageCommunicator,BlockChain,PrivateKey,TimeSpan,ContextTimeoutOption,IComparer{Transaction})"/>
/// 's parameter.</param>
#pragma warning restore MEN002
public ConsensusReactor(
ITransport consensusTransport,
BlockChain blockChain,
PrivateKey privateKey,
ImmutableList<BoundPeer> validatorPeers,
ImmutableList<BoundPeer> seedPeers,
TimeSpan newHeightDelay,
ContextTimeoutOption contextTimeoutOption)
ContextTimeoutOption contextTimeoutOption,
IComparer<Transaction>? txPriority = null)
{
validatorPeers ??= ImmutableList<BoundPeer>.Empty;
seedPeers ??= ImmutableList<BoundPeer>.Empty;
Expand All @@ -71,7 +79,8 @@ public ConsensusReactor(
blockChain,
privateKey,
newHeightDelay,
contextTimeoutOption);
contextTimeoutOption,
txPriority);

_logger = Log
.ForContext("Tag", "Consensus")
Expand Down
9 changes: 9 additions & 0 deletions Libplanet.Net/Consensus/ConsensusReactorOption.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -45,5 +48,11 @@ public struct ConsensusReactorOption
/// A timeout second and multiplier value for used in <see cref="Context"/>.
/// </summary>
public ContextTimeoutOption ContextTimeoutOptions { get; set; }

/// <summary>
/// An optional comparer for give certain transactions to priority to belong to the block.
/// </summary>
/// <seealso cref="BlockChain.GatherTransactionsToPropose(long,IComparer{Transaction})"/>
public IComparer<Transaction>? TxPriorityComparer { get; set; }
}
}
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
3 changes: 2 additions & 1 deletion Libplanet.Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Swarm(
consensusReactorOption.ConsensusPeers,
consensusReactorOption.SeedPeers,
consensusReactorOption.TargetBlockInterval,
consensusReactorOption.ContextTimeoutOptions);
consensusReactorOption.ContextTimeoutOptions,
consensusReactorOption.TxPriorityComparer);
}
}

Expand Down

0 comments on commit c92c558

Please sign in to comment.