-
Notifications
You must be signed in to change notification settings - Fork 139
/
TxSuccess.cs
164 lines (155 loc) · 7.07 KB
/
TxSuccess.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Runtime.Serialization;
using Bencodex.Types;
using Libplanet.Assets;
using Libplanet.Blocks;
using Libplanet.Serialization;
using FAV = Libplanet.Assets.FungibleAssetValue;
namespace Libplanet.Tx
{
/// <summary>
/// Summarizes an execution result of a successful <see cref="Transaction{T}"/>.
/// </summary>
[Serializable]
public sealed class TxSuccess : TxExecution, ISerializable
{
/// <summary>
/// Creates a <see cref="TxSuccess"/> instance.
/// </summary>
/// <param name="blockHash">The <see cref="Block{T}.Hash"/> of the <see cref="Block{T}"/>
/// that the <see cref="Transaction{T}"/> is executed within.</param>
/// <param name="txId">The executed <see cref="Transaction{T}"/>'s <see
/// cref="Transaction{T}.Id"/>.</param>
/// <param name="updatedStates">The states delta made by the actions in
/// the transaction within the block.</param>
/// <param name="fungibleAssetsDelta"><see cref="Address"/>es and sets of
/// <see cref="Currency"/> whose fungible assets have been updated by the actions in
/// the transaction within the block. Included <see cref="FungibleAssetValue"/>s are
/// the delta values (plus or minus) that the transaction makes.</param>
/// <param name="updatedFungibleAssets"><see cref="Address"/>es and sets of
/// <see cref="Currency"/> whose fungible assets have been updated by the actions in
/// the transaction within the block. Included <see cref="FungibleAssetValue"/>s are
/// the final balances right after the transaction is executed.</param>
public TxSuccess(
BlockHash blockHash,
TxId txId,
IImmutableDictionary<Address, IValue?> updatedStates,
IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>> fungibleAssetsDelta,
IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>> updatedFungibleAssets
)
: base(blockHash, txId)
{
UpdatedStates = updatedStates;
FungibleAssetsDelta = fungibleAssetsDelta;
UpdatedFungibleAssets = updatedFungibleAssets;
}
private TxSuccess(SerializationInfo info, StreamingContext context)
: base(info, context)
{
var updatedStates =
(Dictionary)Codec.Decode(info.GetValue<byte[]>(nameof(UpdatedStates)));
UpdatedStates = updatedStates.ToImmutableDictionary(
kv => new Address((Binary)kv.Key),
kv => kv.Value is List l && l.Any() ? l[0] : null
);
FungibleAssetsDelta = DecodeFungibleAssetGroups(
info.GetValue<byte[]>(nameof(FungibleAssetsDelta))
);
UpdatedFungibleAssets = DecodeFungibleAssetGroups(
info.GetValue<byte[]>(nameof(UpdatedFungibleAssets))
);
}
/// <summary>
/// The states delta made by the actions in the transaction within the block.
/// </summary>
[Pure]
public IImmutableDictionary<Address, IValue?> UpdatedStates { get; }
/// <summary>
/// <see cref="Address"/>es and sets of
/// <see cref="Currency"/> whose fungible assets have been updated by the actions in
/// the transaction within the block. Included <see cref="FungibleAssetValue"/>s are
/// the delta values (plus or minus) that the transaction makes.
/// </summary>
[Pure]
public IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>>
FungibleAssetsDelta { get; }
/// <summary>
/// <see cref="Address"/>es and sets of <see cref="Currency"/> whose fungible assets have
/// been updated by the actions in the transaction within the block. Included
/// <see cref="FungibleAssetValue"/>s are the final balances right after the transaction is
/// executed.
/// </summary>
[Pure]
public IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>>
UpdatedFungibleAssets { get; }
/// <summary>
/// All <seealso cref="Address"/>es of the accounts that have been updated by the actions
/// in the transaction within the block.
/// </summary>
[Pure]
public IImmutableSet<Address> UpdatedAddresses =>
UpdatedStates.Keys.ToImmutableHashSet().Union(UpdatedFungibleAssets.Keys);
/// <inheritdoc cref="ISerializable.GetObjectData(SerializationInfo, StreamingContext)"/>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(
nameof(UpdatedStates),
Codec.Encode(
new Dictionary(
UpdatedStates.Select(kv =>
new KeyValuePair<IKey, IValue>(
new Binary(kv.Key.ToByteArray()),
kv.Value is { } v ? List.Empty.Add(v) : List.Empty
)
)
)
)
);
info.AddValue(
nameof(FungibleAssetsDelta),
EncodeFungibleAssetGroups(FungibleAssetsDelta)
);
info.AddValue(
nameof(UpdatedFungibleAssets),
EncodeFungibleAssetGroups(UpdatedFungibleAssets)
);
}
private static byte[] EncodeFungibleAssetGroups(
IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>> g
) =>
Codec.Encode(
new Dictionary(
g.Select(kv =>
new KeyValuePair<IKey, IValue>(
(Binary)kv.Key.ByteArray,
new List(
kv.Value.Select(fav =>
(IValue)List.Empty
.Add(fav.Key.Serialize())
.Add(fav.Value.RawValue)
)
)
)
)
)
);
private static IImmutableDictionary<Address, IImmutableDictionary<Currency, FAV>>
DecodeFungibleAssetGroups(byte[] encoded) =>
((Dictionary)Codec.Decode(encoded)).ToImmutableDictionary(
kv => new Address((Binary)kv.Key),
kv => (IImmutableDictionary<Currency, FAV>)((List)kv.Value)
.Cast<List>()
.Select(pair => (new Currency(pair[0]), (Bencodex.Types.Integer)pair[1]))
.ToImmutableDictionary(
pair => pair.Item1,
pair => FungibleAssetValue.FromRawValue(pair.Item1, (BigInteger)pair.Item2)
)
);
}
}