/
BaseStore.cs
302 lines (252 loc) · 11.2 KB
/
BaseStore.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Assets;
using Libplanet.Blocks;
using Libplanet.Tx;
using Serilog;
using FAV = Libplanet.Assets.FungibleAssetValue;
namespace Libplanet.Store
{
/// <summary>
/// Common code for several <see cref="IStore"/> implementations.
/// </summary>
public abstract class BaseStore : IStore
{
/// <inheritdoc />
public abstract IEnumerable<Guid> ListChainIds();
/// <inheritdoc />
public abstract Guid? GetCanonicalChainId();
/// <inheritdoc />
public abstract void SetCanonicalChainId(Guid chainId);
public abstract long CountIndex(Guid chainId);
/// <inheritdoc cref="IStore.IterateIndexes(Guid, int, int?)"/>
public abstract IEnumerable<BlockHash> IterateIndexes(Guid chainId, int offset, int? limit);
/// <inheritdoc cref="IStore.IndexBlockHash(Guid, long)"/>
public abstract BlockHash? IndexBlockHash(Guid chainId, long index);
/// <inheritdoc cref="IStore.AppendIndex(Guid, BlockHash)"/>
public abstract long AppendIndex(Guid chainId, BlockHash hash);
/// <inheritdoc cref="IStore.ForkBlockIndexes(Guid, Guid, BlockHash)"/>
public abstract void ForkBlockIndexes(
Guid sourceChainId,
Guid destinationChainId,
BlockHash branchpoint
);
public abstract IEnumerable<TxId> IterateTransactionIds();
public abstract Transaction<T> GetTransaction<T>(TxId txid)
where T : IAction, new();
public abstract void PutTransaction<T>(Transaction<T> tx)
where T : IAction, new();
public abstract bool DeleteTransaction(TxId txid);
/// <inheritdoc cref="IStore.IterateBlockHashes()"/>
public abstract IEnumerable<BlockHash> IterateBlockHashes();
/// <inheritdoc cref="IStore.GetBlock{T}"/>
public Block<T> GetBlock<T>(HashAlgorithmGetter hashAlgorithmGetter, BlockHash blockHash)
where T : IAction, new()
{
if (GetBlockDigest(blockHash) is BlockDigest blockDigest)
{
BlockHeader header = blockDigest.GetHeader(hashAlgorithmGetter);
(TxId TxId, Transaction<T> Tx)[] txs = blockDigest.TxIds
.Select(bytes => new TxId(bytes.ToArray()))
.Select(txid => (txid, GetTransaction<T>(txid)))
.ToArray();
TxId[] missingTxIds =
txs.Where(pair => pair.Tx is null).Select(pair => pair.TxId).ToArray();
if (missingTxIds.Any())
{
throw new InvalidOperationException(
$"Failed to find {missingTxIds.Length} tx(s) (out of {txs.Length}):\n " +
string.Join("\n ", missingTxIds)
);
}
return new Block<T>(header, txs.Select(pair => pair.Tx));
}
return null;
}
/// <inheritdoc cref="IStore.GetBlockIndex(BlockHash)"/>
public long? GetBlockIndex(BlockHash blockHash)
{
return GetBlockDigest(blockHash)?.Index;
}
/// <inheritdoc cref="IStore.GetBlockDigest(BlockHash)"/>
public abstract BlockDigest? GetBlockDigest(BlockHash blockHash);
/// <inheritdoc />
public abstract void PutBlock<T>(Block<T> block)
where T : IAction, new();
/// <inheritdoc cref="IStore.DeleteBlock(BlockHash)"/>
public abstract bool DeleteBlock(BlockHash blockHash);
/// <inheritdoc cref="IStore.ContainsBlock(BlockHash)"/>
public abstract bool ContainsBlock(BlockHash blockHash);
/// <inheritdoc cref="IStore.PutTxExecution(Libplanet.Tx.TxSuccess)"/>
public abstract void PutTxExecution(TxSuccess txSuccess);
/// <inheritdoc cref="IStore.PutTxExecution(Libplanet.Tx.TxFailure)"/>
public abstract void PutTxExecution(TxFailure txFailure);
/// <inheritdoc cref="IStore.GetTxExecution(BlockHash, TxId)"/>
public abstract TxExecution GetTxExecution(BlockHash blockHash, TxId txid);
/// <inheritdoc cref="IStore.PutTxIdBlockHashIndex(TxId, BlockHash)"/>
public abstract void PutTxIdBlockHashIndex(TxId txId, BlockHash blockHash);
public BlockHash? GetFirstTxIdBlockHashIndex(TxId txId)
{
BlockHash? blockHash;
try
{
blockHash = IterateTxIdBlockHashIndex(txId).First();
}
catch (InvalidOperationException)
{
blockHash = null;
}
return blockHash;
}
/// <inheritdoc cref="IStore.IterateTxIdBlockHashIndex(TxId)"/>
public abstract IEnumerable<BlockHash> IterateTxIdBlockHashIndex(TxId txId);
/// <inheritdoc cref="IStore.DeleteTxIdBlockHashIndex(TxId, BlockHash)"/>
public abstract void DeleteTxIdBlockHashIndex(TxId txId, BlockHash blockHash);
/// <inheritdoc cref="IStore.SetBlockPerceivedTime(BlockHash, DateTimeOffset)"/>
public abstract void SetBlockPerceivedTime(
BlockHash blockHash,
DateTimeOffset perceivedTime
);
/// <inheritdoc cref="IStore.GetBlockPerceivedTime(BlockHash)"/>
public abstract DateTimeOffset? GetBlockPerceivedTime(BlockHash blockHash);
/// <inheritdoc/>
public abstract IEnumerable<KeyValuePair<Address, long>> ListTxNonces(Guid chainId);
/// <inheritdoc/>
public abstract long GetTxNonce(Guid chainId, Address address);
/// <inheritdoc/>
public abstract void IncreaseTxNonce(Guid chainId, Address signer, long delta = 1);
public virtual long CountTransactions()
{
return IterateTransactionIds().LongCount();
}
public virtual long CountBlocks()
{
return IterateBlockHashes().LongCount();
}
/// <inheritdoc />
public abstract bool ContainsTransaction(TxId txId);
/// <inheritdoc/>
public abstract void DeleteChainId(Guid chainId);
/// <inheritdoc/>
public abstract void Dispose();
/// <inheritdoc/>
public abstract void ForkTxNonces(Guid sourceChainId, Guid destinationChainId);
protected static IValue SerializeTxExecution(TxSuccess txSuccess)
{
var sDelta = new Dictionary(
txSuccess.UpdatedStates.Select(kv =>
new KeyValuePair<IKey, IValue>(
new Binary(kv.Key.ByteArray),
kv.Value is { } v ? List.Empty.Add(v) : List.Empty
)
)
);
var favDelta = SerializeGroupedFAVs(txSuccess.FungibleAssetsDelta);
var updatedFAVs = SerializeGroupedFAVs(txSuccess.UpdatedFungibleAssets);
return (Dictionary)Dictionary.Empty
.Add("fail", false)
.Add("sDelta", sDelta)
.Add((IKey)(Text)"favDelta", new Dictionary(favDelta))
.Add((IKey)(Text)"updatedFAVs", new Dictionary(updatedFAVs));
}
protected static IValue SerializeTxExecution(TxFailure txFailure)
{
Dictionary d = Dictionary.Empty
.Add("fail", true)
.Add("exc", txFailure.ExceptionName);
return txFailure.ExceptionMetadata is { } v ? d.Add("excMeta", v) : d;
}
protected static TxExecution DeserializeTxExecution(
BlockHash blockHash,
TxId txid,
IValue decoded,
ILogger logger
)
{
if (!(decoded is Bencodex.Types.Dictionary d))
{
const string msg = nameof(TxExecution) +
" must be serialized as a Bencodex dictionary, not {ActualValue}.";
logger?.Error(msg, decoded.Inspection);
return null;
}
try
{
bool fail = d.GetValue<Bencodex.Types.Boolean>("fail");
if (fail)
{
string excName = d.GetValue<Text>("exc");
IValue excMetadata = d.ContainsKey("excMeta") ? d["excMeta"] : null;
return new TxFailure(blockHash, txid, excName, excMetadata);
}
ImmutableDictionary<Address, IValue> sDelta = d.GetValue<Dictionary>("sDelta")
.ToImmutableDictionary(
kv => new Address((Binary)kv.Key),
kv => kv.Value is List l && l.Any() ? l[0] : null
);
IImmutableDictionary<Address, IImmutableDictionary<Currency, FungibleAssetValue>>
favDelta = DeserializeGroupedFAVs(d.GetValue<Dictionary>("favDelta"));
IImmutableDictionary<Address, IImmutableDictionary<Currency, FungibleAssetValue>>
updatedFAVs = DeserializeGroupedFAVs(d.GetValue<Dictionary>("updatedFAVs"));
return new TxSuccess(
blockHash,
txid,
sDelta,
favDelta,
updatedFAVs
);
}
catch (Exception e)
{
const string msg =
"Uncaught exception during deserializing a " + nameof(TxExecution) +
": {Exception}";
logger?.Error(e, msg, e);
return null;
}
}
private static Bencodex.Types.Dictionary SerializeGroupedFAVs(
IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>> balanceDelta
) =>
new Dictionary(
balanceDelta.Select(pair =>
new KeyValuePair<IKey, IValue>(
new Binary(pair.Key.ByteArray),
SerializeFAVs(pair.Value)
)
)
);
private static IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>>
DeserializeGroupedFAVs(Bencodex.Types.Dictionary serialized) =>
serialized.ToImmutableDictionary(
kv => new Address((Binary)kv.Key),
kv => DeserializeFAVs((List)kv.Value)
);
private static Bencodex.Types.List SerializeFAVs(
IImmutableDictionary<Currency, FungibleAssetValue> favs
) =>
new List(
favs.Select(
kv => (IValue)List.Empty.Add(kv.Key.Serialize()).Add(kv.Value.RawValue)
)
);
private static IImmutableDictionary<Currency, FungibleAssetValue> DeserializeFAVs(
List serialized
) =>
serialized.Select(pList =>
{
var pair = (List)pList;
var currency = new Currency(pair[0]);
BigInteger rawValue = (Bencodex.Types.Integer)pair[1];
return new KeyValuePair<Currency, FAV>(
currency,
FAV.FromRawValue(currency, rawValue)
);
}).ToImmutableDictionary();
}
}