-
Notifications
You must be signed in to change notification settings - Fork 139
/
TotalSupplyStateCompleters.cs
65 lines (60 loc) · 2.45 KB
/
TotalSupplyStateCompleters.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
using System;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Assets;
using Libplanet.Blocks;
namespace Libplanet.Blockchain
{
/// <summary>
/// Predefined built-in state completers that satisfy the
/// <see cref="TotalSupplyStateCompleter{T}"/> delegate.
/// </summary>
/// <typeparam name="T">An <see cref="IAction"/> type. It should match the
/// <see cref="BlockChain{T}"/>'s type parameter.</typeparam>
public static class TotalSupplyStateCompleters<T>
where T : IAction, new()
{
/// <summary>
/// See <see cref="StateCompleterSet{T}.Recalculate"/>.
/// </summary>
public static readonly TotalSupplyStateCompleter<T> Recalculate =
(blockChain, blockHash, currency) =>
{
blockChain.RecalculateBlockStates(blockHash);
return blockChain.GetTotalSupply(currency, blockHash);
};
/// <summary>
/// See <see cref="StateCompleterSet{T}.ComplementAll"/>.
/// </summary>
public static readonly TotalSupplyStateCompleter<T> ComplementAll =
(blockChain, blockHash, currency) =>
{
blockChain.ComplementAllBlockStates(blockHash);
return blockChain.GetTotalSupply(currency, blockHash);
};
/// <summary>
/// See <see cref="StateCompleterSet{T}.ComplementLatest"/>.
/// </summary>
public static readonly TotalSupplyStateCompleter<T> ComplementLatest =
(blockChain, blockHash, currency) =>
{
blockChain.ComplementLatestBlockStates(blockHash);
return blockChain.GetTotalSupply(currency, blockHash);
};
/// <summary>
/// Rejects to complement incomplete state and throws
/// an <see cref="IncompleteBlockStatesException"/>.
/// </summary>
public static readonly TotalSupplyStateCompleter<T> Reject =
(chain, blockHash, currency) =>
throw new IncompleteBlockStatesException(blockHash);
internal static Func<BlockChain<T>, BlockHash, IValue> ToRawStateCompleter(
TotalSupplyStateCompleter<T> stateCompleter,
Currency currency
) =>
(blockChain, hash) =>
stateCompleter(blockChain, hash, currency) is { } totalSupply
? (IValue)(Bencodex.Types.Integer)totalSupply.RawValue
: Bencodex.Types.Null.Value;
}
}