/
StateCompleters.cs
59 lines (54 loc) · 2.12 KB
/
StateCompleters.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
#nullable enable
using System;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Blocks;
namespace Libplanet.Blockchain
{
/// <summary>
/// Predefined built-in state completers that satisfy <see cref="StateCompleter{T}"/> delegate.
/// </summary>
/// <typeparam name="T">An <see cref="IAction"/> type. It should match to
/// <see cref="BlockChain{T}"/>'s type parameter.</typeparam>
public static class StateCompleters<T>
where T : IAction, new()
{
/// <summary>
/// See <see cref="StateCompleterSet{T}.Recalculate"/>.
/// </summary>
public static readonly StateCompleter<T> Recalculate = (blockChain, blockHash, address) =>
{
blockChain.RecalculateBlockStates(blockHash);
return blockChain.GetState(address, blockHash);
};
/// <summary>
/// See <see cref="StateCompleterSet{T}.ComplementAll"/>.
/// </summary>
public static readonly StateCompleter<T> ComplementAll =
(blockChain, blockHash, address) =>
{
blockChain.ComplementAllBlockStates(blockHash);
return blockChain.GetState(address, blockHash);
};
/// <summary>
/// See <see cref="StateCompleterSet{T}.ComplementLatest"/>.
/// </summary>
public static readonly StateCompleter<T> ComplementLatest =
(blockChain, blockHash, address) =>
{
blockChain.ComplementLatestBlockStates(blockHash);
return blockChain.GetState(address, blockHash);
};
/// <summary>
/// Rejects to complement incomplete state and throws
/// an <see cref="IncompleteBlockStatesException"/>.
/// </summary>
public static readonly StateCompleter<T> Reject = (chain, blockHash, address) =>
throw new IncompleteBlockStatesException(blockHash);
internal static Func<BlockChain<T>, BlockHash, IValue> ToRawStateCompleter(
StateCompleter<T> stateCompleter,
Address address
) =>
(blockChain, hash) => stateCompleter(blockChain, hash, address);
}
}