-
Notifications
You must be signed in to change notification settings - Fork 139
/
TxFailure.cs
92 lines (86 loc) · 3.34 KB
/
TxFailure.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
using System;
using System.Diagnostics.Contracts;
using System.Runtime.Serialization;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Blocks;
using Libplanet.Serialization;
namespace Libplanet.Tx
{
/// <summary>
/// Summarizes an execution result of a <see cref="Transaction{T}"/> with any exception-throwing
/// actions.
/// </summary>
[Serializable]
public sealed class TxFailure : TxExecution
{
/// <summary>
/// Creates a <see cref="TxFailure"/> 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="exceptionName">The name of the exception type,
/// e.g., <c>System.ArgumentException</c>.</param>
/// <param name="exceptionMetadata">Optional metadata about the exception.</param>
public TxFailure(
BlockHash blockHash,
TxId txId,
string exceptionName,
IValue? exceptionMetadata
)
: base(blockHash, txId)
{
ExceptionName = exceptionName;
ExceptionMetadata = exceptionMetadata;
}
/// <summary>
/// Creates a <see cref="TxFailure"/> 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="exception">The uncaught exception thrown by an action in the transaction.
/// </param>
public TxFailure(BlockHash blockHash, TxId txId, Exception exception)
: this(
blockHash,
txId,
exception.GetType().FullName ?? string.Empty,
exception.ExtractMetadata()
)
{
}
private TxFailure(SerializationInfo info, StreamingContext context)
: base(info, context)
{
ExceptionName = info.GetString(nameof(ExceptionName)) ?? string.Empty;
ExceptionMetadata
= info.GetValue<byte[]?>(nameof(ExceptionMetadata)) is { } bytes
? Codec.Decode(bytes)
: null;
}
/// <summary>
/// The name of the exception type, e.g., <c>System.ArgumentException</c>.
/// </summary>
[Pure]
public string ExceptionName { get; }
/// <summary>
/// Optional metadata about the exception.
/// </summary>
[Pure]
public IValue? ExceptionMetadata { get; }
/// <inheritdoc cref="ISerializable.GetObjectData(SerializationInfo, StreamingContext)"/>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(ExceptionName), ExceptionName);
info.AddValue(
nameof(ExceptionMetadata),
ExceptionMetadata is { } m ? Codec.Encode(m) : null
);
}
}
}