-
Notifications
You must be signed in to change notification settings - Fork 139
/
VolatileStagePolicy.cs
279 lines (257 loc) · 8.8 KB
/
VolatileStagePolicy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using Libplanet.Action;
using Libplanet.Tx;
using Serilog;
namespace Libplanet.Blockchain.Policies
{
/// <summary>
/// <para>
/// An in memory implementation of the <see cref="IStagePolicy{T}"/>.
/// </para>
/// <para>
/// This implementation holds on to every unconfirmed <see cref="Transaction{T}"/> except
/// for the following reasons:
/// <list type="bullet">
/// <item>
/// <description>A <see cref="Transaction{T}"/> has been specifically marked to
/// be ignored due to <see cref="Transaction{T}"/> not being valid.</description>
/// </item>
/// <item>
/// <description>A <see cref="Transaction{T}"/> has expired due to its staleness.
/// </description>
/// </item>
/// </list>
/// </para>
/// <para>
/// Additionally, any <see cref="Transaction{T}"/> with a lower nonce than that of returned by
/// the <see cref="BlockChain{T}"/> is masked and filtered by default.
/// </para>
/// </summary>
/// <typeparam name="T">An <see cref="IAction"/> type. It should match
/// the <see cref="BlockChain{T}"/>'s type parameter.</typeparam>
public class VolatileStagePolicy<T> : IStagePolicy<T>
where T : IAction, new()
{
private readonly ConcurrentDictionary<TxId, Transaction<T>> _staged;
private readonly ConcurrentBag<TxId> _ignored;
private readonly ReaderWriterLockSlim _lock;
private readonly ILogger _logger;
/// <summary>
/// Creates a new <see cref="VolatileStagePolicy{T}"/> instance.
/// By default, <see cref="Lifetime"/> is configured to 3 hours.
/// </summary>
public VolatileStagePolicy()
: this(TimeSpan.FromHours(3))
{
}
/// <summary>
/// Creates a new <see cref="VolatileStagePolicy{T}"/> instance.
/// </summary>
/// <param name="lifetime">Volatilizes staged transactions older than this
/// <see cref="TimeSpan"/>. See also <see cref="Lifetime"/>.</param>
public VolatileStagePolicy(TimeSpan lifetime)
{
_logger = Log.ForContext<VolatileStagePolicy<T>>();
Lifetime = lifetime;
_staged = new ConcurrentDictionary<TxId, Transaction<T>>();
_ignored = new ConcurrentBag<TxId>();
_lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}
/// <summary>
/// Lifespan for <see cref="Transaction{T}"/>s. Any <see cref="Transaction{T}"/> older
/// than this <see cref="TimeSpan"/> will be considered expired.
/// </summary>
/// <remarks>
/// Expired <see cref="Transaction{T}"/>s cannot be staged.
/// </remarks>
public TimeSpan Lifetime { get; }
/// <inheritdoc/>
public bool Stage(BlockChain<T> blockChain, Transaction<T> transaction)
{
if (Exipred(transaction))
{
return false;
}
bool result;
_lock.EnterUpgradeableReadLock();
try
{
if (_ignored.Contains(transaction.Id))
{
return false;
}
else
{
_lock.EnterWriteLock();
try
{
result = _staged.TryAdd(transaction.Id, transaction);
if (result)
{
const string TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ffffffZ";
_logger
.ForContext("Tag", "Metric")
.ForContext("Subtag", "TxStageTimestamp")
.Debug(
"Transaction {TxId} by {Signer} " +
"with timestamp {TxTimestamp} staged at {StagedTimestamp}.",
transaction.Id,
transaction.Signer,
transaction.Timestamp.ToString(
TimestampFormat, CultureInfo.InvariantCulture),
DateTimeOffset.UtcNow.ToString(
TimestampFormat, CultureInfo.InvariantCulture));
}
}
finally
{
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
return result;
}
/// <inheritdoc/>
public bool Unstage(BlockChain<T> blockChain, TxId id)
{
bool result;
_lock.EnterWriteLock();
try
{
result = _staged.TryRemove(id, out _);
}
finally
{
_lock.ExitWriteLock();
}
return result;
}
/// <inheritdoc/>
public void Ignore(BlockChain<T> blockChain, TxId id)
{
_lock.EnterUpgradeableReadLock();
try
{
if (_ignored.Contains(id))
{
return;
}
_lock.EnterWriteLock();
try
{
_ignored.Add(id);
}
finally
{
_lock.ExitWriteLock();
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
}
/// <inheritdoc/>
public bool Ignores(BlockChain<T> blockChain, TxId id)
{
_lock.EnterReadLock();
try
{
return _ignored.Contains(id);
}
finally
{
_lock.ExitReadLock();
}
}
/// <inheritdoc/>
public Transaction<T>? Get(BlockChain<T> blockChain, TxId id, bool filtered = true)
{
Transaction<T>? transaction = null;
_lock.EnterWriteLock();
try
{
_staged.TryGetValue(id, out transaction);
if (transaction is Transaction<T> tx)
{
if (Exipred(tx) || _ignored.Contains(tx.Id))
{
_staged.TryRemove(id, out _);
return null;
}
else if (filtered)
{
return blockChain.Store.GetTxNonce(blockChain.Id, tx.Signer) <= tx.Nonce
? tx
: null;
}
else
{
return tx;
}
}
else
{
return null;
}
}
finally
{
_lock.ExitWriteLock();
}
}
/// <inheritdoc/>
public IEnumerable<Transaction<T>> Iterate(BlockChain<T> blockChain, bool filtered = true)
{
List<Transaction<T>> transactions = new List<Transaction<T>>();
_lock.EnterUpgradeableReadLock();
try
{
List<TxId> txIds = _staged.Keys.ToList();
foreach (TxId txId in txIds)
{
if (Get(blockChain, txId, filtered) is Transaction<T> tx)
{
transactions.Add(tx);
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
return transactions;
}
/// <inheritdoc/>
public long GetNextTxNonce(BlockChain<T> blockChain, Address address)
{
long nonce = blockChain.Store.GetTxNonce(blockChain.Id, address);
IEnumerable<Transaction<T>> orderedTxs = Iterate(blockChain, filtered: true)
.Where(tx => tx.Signer.Equals(address))
.OrderBy(tx => tx.Nonce);
foreach (Transaction<T> tx in orderedTxs)
{
if (nonce < tx.Nonce)
{
break;
}
else if (nonce == tx.Nonce)
{
nonce++;
}
}
return nonce;
}
private bool Exipred(Transaction<T> transaction) =>
transaction.Timestamp + Lifetime < DateTimeOffset.UtcNow;
}
}