/
Transaction.cs
731 lines (682 loc) · 31.3 KB
/
Transaction.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Blocks;
using Libplanet.Crypto;
namespace Libplanet.Tx
{
/// <summary>
/// Consists of <see cref="IAction"/> and is signed to be included in
/// a <see cref="Blocks.Block{T}"/> and transmitted over the network.
/// </summary>
/// <typeparam name="T">A class implementing <see cref="IAction"/> to
/// include.
/// Each game usually defines its own concrete class which implements
/// <see cref="IAction"/>, and uses it for this type parameter.
/// This type parameter is aligned with <see cref="Blocks.Block{T}"/>'s
/// and <see cref="Blockchain.BlockChain{T}"/>'s type parameters.
/// </typeparam>
/// <seealso cref="IAction"/>
/// <seealso cref="PolymorphicAction{T}"/>
public sealed class Transaction<T> : IEquatable<Transaction<T>>
where T : IAction, new()
{
private const string TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ffffffZ";
// If a tx is longer than 50 KiB don't cache its bytes representation to _bytes.
private const int BytesCacheThreshold = 50 * 1024;
private TxId? _id;
private byte[] _signature;
private byte[] _bytes;
private int _bytesLength;
/// <summary>
/// Creates a new <see cref="Transaction{T}"/>.
/// <para>This constructor takes all required and only required values
/// for a <see cref="Transaction{T}"/>, so gives you full control of
/// creating a <see cref="Transaction{T}"/>, and in other words,
/// this constructor is only useful when all details of
/// a <see cref="Transaction{T}"/> need to be manually adjusted.
/// For the most cases, the façade factory <see
/// cref="Create(long, PrivateKey, BlockHash?, IEnumerable{T},
/// IImmutableSet{Address}, DateTimeOffset?)"/> is more useful.</para>
/// </summary>
/// <param name="nonce">The number of previous
/// <see cref="Transaction{T}"/>s committed by the <see cref="Signer"/>
/// of this transaction. This goes to the
/// <see cref="Transaction{T}.Nonce"/> property.</param>
/// <param name="signer">An <see cref="Address"/> of the account
/// who signs this transaction. If this is not derived from <paramref
/// name="publicKey"/> <see cref="InvalidTxPublicKeyException"/> is
/// thrown. This goes to the <see cref="Signer"/> property.</param>
/// <param name="publicKey">A <see cref="PublicKey"/> of the account
/// who signs this transaction. If this does not match to <paramref
/// name="signer"/> address <see cref="InvalidTxPublicKeyException"/>
/// is thrown. This cannot be <c>null</c>. This goes to
/// the <see cref="PublicKey"/> property.</param>
/// <param name="genesisHash">A <see cref="HashDigest{SHA256}"/> value
/// of the genesis which this <see cref="Transaction{T}"/> is made from.
/// This can be <c>null</c> iff the transaction is contained
/// in the genesis block.
/// </param>
/// <param name="updatedAddresses"><see cref="Address"/>es whose
/// states affected by <paramref name="actions"/>. This goes to
/// the <see cref="UpdatedAddresses"/> property.</param>
/// <param name="timestamp">The time this <see cref="Transaction{T}"/>
/// is created and signed. This goes to the <see cref="Timestamp"/>
/// property.</param>
/// <param name="actions">A list of <see cref="IAction"/>s. This
/// can be empty, but cannot be <c>null</c>. This goes to
/// the <see cref="Actions"/> property.</param>
/// <param name="signature">A digital signature of the content of
/// this <see cref="Transaction{T}"/>. This has to be signed by
/// the account who corresponds to <paramref name="publicKey"/>,
/// or it will throw <see cref="InvalidTxSignatureException"/>.
/// This is copied and then assigned to the <see cref="Signature"/>
/// property.</param>
/// <exception cref="ArgumentNullException">Thrown when <c>null</c>
/// is passed to <paramref name="signature"/>,
/// <paramref name="actions"/>, or <paramref name="publicKey"/>.
/// </exception>
public Transaction(
long nonce,
Address signer,
PublicKey publicKey,
BlockHash? genesisHash,
IImmutableSet<Address> updatedAddresses,
DateTimeOffset timestamp,
IEnumerable<T> actions,
byte[] signature)
{
Nonce = nonce;
Signer = signer;
GenesisHash = genesisHash;
UpdatedAddresses = updatedAddresses ??
throw new ArgumentNullException(nameof(updatedAddresses));
Signature = signature ??
throw new ArgumentNullException(nameof(signature));
Timestamp = timestamp;
Actions = actions?.ToImmutableList() ??
throw new ArgumentNullException(nameof(actions));
PublicKey = publicKey ??
throw new ArgumentNullException(nameof(publicKey));
}
/// <summary>
/// Creates a <see cref="Transaction{T}"/> instance from its serialization.
/// </summary>
/// <param name="dict">The <see cref="Bencodex.Types.Dictionary"/>
/// representation of <see cref="Transaction{T}"/> instance.
/// </param>
public Transaction(Bencodex.Types.Dictionary dict)
: this(new RawTransaction(dict))
{
}
#pragma warning disable SA1118 // Parameter spans multiple line
internal Transaction(RawTransaction rawTx)
: this(
rawTx.Nonce,
new Address(rawTx.Signer),
new PublicKey(rawTx.PublicKey.ToArray()),
rawTx.GenesisHash != ImmutableArray<byte>.Empty
? new BlockHash(rawTx.GenesisHash.ToArray())
: (BlockHash?)null,
rawTx.UpdatedAddresses.Select(
a => new Address(a)
).ToImmutableHashSet(),
DateTimeOffset.ParseExact(
rawTx.Timestamp,
TimestampFormat,
CultureInfo.InvariantCulture).ToUniversalTime(),
rawTx.Actions.Select(ToAction).ToImmutableList(),
rawTx.Signature.ToArray())
#pragma warning restore SA1118 // Parameter spans multiple line
{
}
private Transaction(
long nonce,
Address signer,
PublicKey publicKey,
BlockHash? genesisHash,
IImmutableSet<Address> updatedAddresses,
DateTimeOffset timestamp,
IEnumerable<T> actions)
: this(
nonce,
signer,
publicKey,
genesisHash,
updatedAddresses,
timestamp,
actions.ToImmutableList(),
new byte[0])
{
}
/// <summary>
/// A unique identifier derived from this <see cref="Transaction{T}"/>'s
/// content.
/// <para>For more characteristics, see <see cref="TxId"/> type.</para>
/// </summary>
/// <seealso cref="TxId"/>
public TxId Id
{
get
{
if (!(_id is { } nonNull))
{
using var hasher = SHA256.Create();
byte[] payload = Serialize(true);
_id = nonNull = new TxId(hasher.ComputeHash(payload));
}
return nonNull;
}
}
/// <summary>
/// The number of previous <see cref="Transaction{T}"/>s committed by
/// the <see cref="Signer"/> of this transaction.
/// </summary>
public long Nonce { get; }
/// <summary>
/// A <see cref="PublicKey"/> of the account who signs this transaction.
/// This is derived from the <see cref="PublicKey"/>.
/// </summary>
public Address Signer { get; }
/// <summary>
/// <see cref="Address"/>es whose states affected by
/// <see cref="Actions"/>.
/// </summary>
// TODO: We should remove this property.
// See also https://github.com/planetarium/libplanet/issues/368
public IImmutableSet<Address> UpdatedAddresses { get; }
/// <summary>
/// A digital signature of the content of this
/// <see cref="Transaction{T}"/>. This is signed by the account
/// who corresponds to <see cref="PublicKey"/>.
/// This cannot be <c>null</c>.
/// </summary>
/// <returns>A new <see cref="byte"/> array of this transaction's
/// signature. Changing a returned array does not affect the internal
/// state of this <see cref="Transaction{T}"/> object.</returns>
public byte[] Signature
{
get
{
var sig = new byte[_signature.Length];
Array.Copy(_signature, sig, _signature.Length);
return sig;
}
private set
{
_signature = new byte[value.Length];
Array.Copy(value, _signature, value.Length);
}
}
/// <summary>
/// A list of <see cref="IAction"/>s. These are executed in the order.
/// This can be empty, but cannot be <c>null</c>.
/// </summary>
public IImmutableList<T> Actions { get; }
/// <summary>
/// The time this <see cref="Transaction{T}"/> is created and signed.
/// </summary>
public DateTimeOffset Timestamp { get; }
/// <summary>
/// A <see cref="PublicKey"/> of the account who signs this
/// <see cref="Transaction{T}"/>.
/// The <see cref="Signer"/> address is always corresponding to this
/// for each transaction. This cannot be <c>null</c>.
/// </summary>
public PublicKey PublicKey { get; }
/// <summary>
/// A <see cref="HashDigest{SHA256}"/> value of the genesis which this
/// <see cref="Transaction{T}"/> is made from.
/// This can be <c>null</c> iff the transaction is contained
/// in the genesis block.
/// </summary>
public BlockHash? GenesisHash { get; }
/// <summary>
/// The bytes length in its serialized format.
/// </summary>
public int BytesLength
{
get
{
// Note that Serialize() by itself caches _byteLength, so that this ByteLength
// property never invokes Serialize() more than once.
return _bytesLength > 0 ? _bytesLength : Serialize(true).Length;
}
}
/// <summary>
/// Decodes a <see cref="Transaction{T}"/>'s
/// <a href="https://bencodex.org/">Bencodex</a> representation.
/// </summary>
/// <param name="bytes">A <a href="https://bencodex.org/">Bencodex</a>
/// representation of a <see cref="Transaction{T}"/>.</param>
/// <param name="validate">Whether to validate the transaction.</param>
/// <returns>A decoded <see cref="Transaction{T}"/> object.</returns>
/// <exception cref="InvalidTxSignatureException">Thrown when its
/// <see cref="Signature"/> is invalid or not signed by
/// the account who corresponds to <see cref="PublicKey"/>.
/// </exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when its
/// <see cref="Signer"/> is not derived from its
/// <see cref="PublicKey"/>.</exception>
/// <seealso cref="Serialize(bool)"/>
public static Transaction<T> Deserialize(byte[] bytes, bool validate = true)
{
IValue value = new Codec().Decode(bytes);
if (!(value is Bencodex.Types.Dictionary dict))
{
throw new DecodingException(
$"Expected {typeof(Bencodex.Types.Dictionary)} but " +
$"{value.GetType()}");
}
var tx = new Transaction<T>(dict);
if (validate)
{
tx.Validate();
}
if (bytes.Length < BytesCacheThreshold)
{
tx._bytes = bytes;
}
tx._bytesLength = bytes.Length;
return tx;
}
/// <summary>
/// A façade factory to create a new <see cref="Transaction{T}"/>.
/// Unlike the <see cref="Transaction(long, Address, PublicKey, BlockHash?,
/// IImmutableSet{Address}, DateTimeOffset, IEnumerable{T}, byte[])"/>
/// constructor, it automatically fills the following values from:
/// <list type="table">
/// <listheader>
/// <term>Property</term>
/// <description>Parameter the filled value derived from</description>
/// </listheader>
/// <item>
/// <term><see cref="Signer"/></term>
/// <description><paramref name="privateKey"/></description>
/// </item>
/// <item>
/// <term><see cref="PublicKey"/></term>
/// <description><paramref name="privateKey"/></description>
/// </item>
/// <item>
/// <term><see cref="UpdatedAddresses"/></term>
/// <description><paramref name="actions"/> and
/// <paramref name="updatedAddresses"/></description>
/// </item>
/// </list>
/// <para>Note that the <paramref name="privateKey"/> in itself is not
/// included in the created <see cref="Transaction{T}"/>.</para>
/// </summary>
/// <remarks>
/// This factory method tries its best to fill the <see
/// cref="UpdatedAddresses"/> property by actually evaluating
/// the given <paramref name="actions"/> (we call it “rehearsal
/// mode”), but remember that its result
/// is approximated in some degree, because the result of
/// <paramref name="actions"/> are not deterministic until
/// the <see cref="Transaction{T}"/> belongs to a <see
/// cref="Libplanet.Blocks.Block{T}"/>.
/// <para>If an <see cref="IAction"/> depends on previous states or
/// some randomness to determine what <see cref="Address"/> to update,
/// the automatically filled <see cref="UpdatedAddresses"/> became
/// mismatched from the <see cref="Address"/>es
/// <paramref name="actions"/> actually update after
/// a <see cref="Libplanet.Blocks.Block{T}"/> is mined.
/// Although such case would be rare, a programmer could manually give
/// the <paramref name="updatedAddresses"/> parameter
/// the <see cref="Address"/>es they predict to be updated.</para>
/// <para>If an <see cref="IAction"/> oversimplifies the assumption
/// about the <see cref="Libplanet.Blocks.Block{T}"/> it belongs to,
/// runtime exceptions could be thrown from this factory method.
/// The best solution to that is not to oversimplify things,
/// there is an option to check <see cref="IActionContext"/>'s
/// <see cref="IActionContext.Rehearsal"/> is <c>true</c> and
/// a conditional logic for the case.</para>
/// </remarks>
/// <param name="nonce">The number of previous
/// <see cref="Transaction{T}"/>s committed by the <see cref="Signer"/>
/// of this transaction. This goes to the
/// <see cref="Transaction{T}.Nonce"/> property.</param>
/// <param name="privateKey">A <see cref="PrivateKey"/> of the account
/// who creates and signs a new transaction. This key is used to fill
/// the <see cref="Signer"/>, <see cref="PublicKey"/>, and
/// <see cref="Signature"/> properties, but this in itself is not
/// included in the transaction.</param>
/// <param name="genesisHash">A <see cref="HashDigest{SHA256}"/> value
/// of the genesis which this <see cref="Transaction{T}"/> is made from.
/// This can be <c>null</c> iff the transaction is contained
/// in the genesis block.
/// </param>
/// <param name="actions">A list of <see cref="IAction"/>s. This
/// can be empty, but cannot be <c>null</c>. This goes to
/// the <see cref="Actions"/> property, and <see cref="IAction"/>s
/// are evaluated before a <see cref="Transaction{T}"/> is created
/// in order to fill the <see cref="UpdatedAddresses"/>. See also
/// <em>Remarks</em> section.</param>
/// <param name="updatedAddresses"><see cref="Address"/>es whose
/// states affected by <paramref name="actions"/>.
/// These <see cref="Address"/>es are also included in
/// the <see cref="UpdatedAddresses"/> property, besides
/// <see cref="Address"/>es projected by evaluating
/// <paramref name="actions"/>. See also <em>Remarks</em> section.
/// </param>
/// <param name="timestamp">The time this <see cref="Transaction{T}"/>
/// is created and signed. This goes to the <see cref="Timestamp"/>
/// property. If <c>null</c> (which is default) is passed this will
/// be the current time.</param>
/// <returns>A created new <see cref="Transaction{T}"/> signed by
/// the given <paramref name="privateKey"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <c>null</c>
/// is passed to <paramref name="privateKey"/> or
/// <paramref name="actions"/>.
/// </exception>
public static Transaction<T> Create(
long nonce,
PrivateKey privateKey,
BlockHash? genesisHash,
IEnumerable<T> actions,
IImmutableSet<Address> updatedAddresses = null,
DateTimeOffset? timestamp = null
)
{
if (ReferenceEquals(privateKey, null))
{
throw new ArgumentNullException(nameof(privateKey));
}
Transaction<T> unsignedTransaction = CreateUnsigned(
nonce,
privateKey.PublicKey,
genesisHash,
actions,
updatedAddresses,
timestamp);
byte[] payload = unsignedTransaction.Serialize(false);
byte[] sig = privateKey.Sign(payload);
return new Transaction<T>(
unsignedTransaction.Nonce,
unsignedTransaction.Signer,
unsignedTransaction.PublicKey,
unsignedTransaction.GenesisHash,
unsignedTransaction.UpdatedAddresses,
unsignedTransaction.Timestamp,
unsignedTransaction.Actions,
sig);
}
/// <summary>
/// A façade factory to create a new <see cref="Transaction{T}"/>.
/// Unlike the <see cref="Transaction(long, Address, PublicKey, BlockHash?,
/// IImmutableSet{Address}, DateTimeOffset, IEnumerable{T}, byte[])"/>
/// constructor, it automatically fills the following values from:
/// <list type="table">
/// <listheader>
/// <term>Property</term>
/// <description>Parameter the filled value derived from</description>
/// </listheader>
/// <item>
/// <term><see cref="Signer"/></term>
/// <description><paramref name="publicKey"/></description>
/// </item>
/// <item>
/// <term><see cref="PublicKey"/></term>
/// <description><paramref name="publicKey"/></description>
/// </item>
/// <item>
/// <term><see cref="UpdatedAddresses"/></term>
/// <description><paramref name="actions"/> and
/// <paramref name="updatedAddresses"/></description>
/// </item>
/// </list>
/// </summary>
/// <remarks>
/// This factory method tries its best to fill the <see
/// cref="UpdatedAddresses"/> property by actually evaluating
/// the given <paramref name="actions"/> (we call it “rehearsal
/// mode”), but remember that its result
/// is approximated in some degree, because the result of
/// <paramref name="actions"/> are not deterministic until
/// the <see cref="Transaction{T}"/> belongs to a <see
/// cref="Libplanet.Blocks.Block{T}"/>.
/// <para>If an <see cref="IAction"/> depends on previous states or
/// some randomness to determine what <see cref="Address"/> to update,
/// the automatically filled <see cref="UpdatedAddresses"/> became
/// mismatched from the <see cref="Address"/>es
/// <paramref name="actions"/> actually update after
/// a <see cref="Libplanet.Blocks.Block{T}"/> is mined.
/// Although such case would be rare, a programmer could manually give
/// the <paramref name="updatedAddresses"/> parameter
/// the <see cref="Address"/>es they predict to be updated.</para>
/// <para>If an <see cref="IAction"/> oversimplifies the assumption
/// about the <see cref="Libplanet.Blocks.Block{T}"/> it belongs to,
/// runtime exceptions could be thrown from this factory method.
/// The best solution to that is not to oversimplify things,
/// there is an option to check <see cref="IActionContext"/>'s
/// <see cref="IActionContext.Rehearsal"/> is <c>true</c> and
/// a conditional logic for the case.</para>
/// </remarks>
/// <param name="nonce">The number of previous
/// <see cref="Transaction{T}"/>s committed by the <see cref="Signer"/>
/// of this transaction. This goes to the
/// <see cref="Transaction{T}.Nonce"/> property.</param>
/// <param name="publicKey">A <see cref="PublicKey"/> of the account
/// who creates a new transaction. This key is used to fill
/// the <see cref="Signer"/> and <see cref="PublicKey"/> properties,
/// but this in itself is not included in the transaction.</param>
/// <param name="genesisHash">A <see cref="HashDigest{SHA256}"/> value
/// of the genesis which this <see cref="Transaction{T}"/> is made from.
/// This can be <c>null</c> iff the transaction is contained
/// in the genesis block.
/// </param>
/// <param name="actions">A list of <see cref="IAction"/>s. This
/// can be empty, but cannot be <c>null</c>. This goes to
/// the <see cref="Actions"/> property, and <see cref="IAction"/>s
/// are evaluated before a <see cref="Transaction{T}"/> is created
/// in order to fill the <see cref="UpdatedAddresses"/>. See also
/// <em>Remarks</em> section.</param>
/// <param name="updatedAddresses"><see cref="Address"/>es whose
/// states affected by <paramref name="actions"/>.
/// These <see cref="Address"/>es are also included in
/// the <see cref="UpdatedAddresses"/> property, besides
/// <see cref="Address"/>es projected by evaluating
/// <paramref name="actions"/>. See also <em>Remarks</em> section.
/// </param>
/// <param name="timestamp">The time this <see cref="Transaction{T}"/>
/// is created. This goes to the <see cref="Timestamp"/>
/// property. If <c>null</c> (which is default) is passed this will
/// be the current time.</param>
/// <returns>A created new <see cref="Transaction{T}"/> unsigned.</returns>
/// <exception cref="ArgumentNullException">Thrown when <c>null</c>
/// is passed to <paramref name="actions"/>.
/// </exception>
public static Transaction<T> CreateUnsigned(
long nonce,
PublicKey publicKey,
BlockHash? genesisHash,
IEnumerable<T> actions,
IImmutableSet<Address> updatedAddresses = null,
DateTimeOffset? timestamp = null
)
{
var signer = new Address(publicKey);
if (ReferenceEquals(updatedAddresses, null))
{
updatedAddresses = ImmutableHashSet<Address>.Empty;
}
DateTimeOffset ts = timestamp ?? DateTimeOffset.UtcNow;
ImmutableArray<T> actionsArray = actions.ToImmutableArray();
if (!actionsArray.IsEmpty)
{
// FIXME: Although we are assuming all block hashes are SHA256 digest, we should
// parametrize this in the future.
BlockHash emptyBlockHash = BlockHash.FromHashDigest(default(HashDigest<SHA256>));
var evalUpdatedAddresses = ActionEvaluator<T>.GetUpdatedAddresses(
new Transaction<T>(
nonce,
signer,
publicKey,
genesisHash,
updatedAddresses,
ts,
actionsArray));
if (!updatedAddresses.IsSupersetOf(evalUpdatedAddresses))
{
updatedAddresses = updatedAddresses.Union(evalUpdatedAddresses);
}
}
return new Transaction<T>(
nonce,
signer,
publicKey,
genesisHash,
updatedAddresses,
ts,
actionsArray);
}
/// <summary>
/// Encodes this <see cref="Transaction{T}"/> into a <see cref="byte"/> array.
/// </summary>
/// <param name="sign">Whether to include its <see cref="Signature"/>.
/// </param>
/// <returns>A <a href="https://bencodex.org/">Bencodex</a>
/// representation of this <see cref="Transaction{T}"/>.</returns>
public byte[] Serialize(bool sign)
{
Codec codec = null;
if (_bytes is { })
{
if (sign)
{
return _bytes;
}
// Poor man's way to optimize serialization without signature...
// FIXME: We need to rather reorganize the serialization layout
// & optimize Bencodex.Codec in general.
if (_signature is { } && _signature.Length > 0)
{
codec = new Codec();
byte[] sigDict =
codec.Encode(Dictionary.Empty.Add(RawTransaction.SignatureKey, _signature));
var sigField = new byte[sigDict.Length - 1];
Array.Copy(sigDict, 1, sigField, 0, sigField.Length);
int sigOffset = _bytes.IndexOf(sigField);
if (sigOffset > 0)
{
int sigEnd = sigOffset + _signature.Length;
var buffer = new byte[_bytes.Length - sigField.Length];
Array.Copy(_bytes, buffer, sigOffset);
Array.Copy(_bytes, sigEnd, buffer, sigOffset, _bytesLength - sigEnd);
return buffer;
}
}
}
codec ??= new Codec();
byte[] serialized = codec.Encode(ToBencodex(sign));
if (sign)
{
if (serialized.Length < BytesCacheThreshold)
{
_bytes = serialized;
}
_bytesLength = serialized.Length;
}
return serialized;
}
/// <summary>
/// Encodes this <see cref="Transaction{T}"/> into a <see cref="IValue"/>.
/// </summary>
/// <param name="sign">Whether to include its <see cref="Signature"/>.
/// Note that an encoding without signature cannot be decoded.
/// </param>
/// <returns>A <see cref="Bencodex.Types.Dictionary"/> typed
/// <a href="https://bencodex.org/">Bencodex</a>
/// representation of this <see cref="Transaction{T}"/>.</returns>
public Bencodex.Types.Dictionary ToBencodex(bool sign) =>
ToRawTransaction(sign).ToBencodex();
/// <summary>
/// Validates this <see cref="Transaction{T}"/> and throws an appropriate exception
/// if not valid.
/// </summary>
/// <exception cref="InvalidTxSignatureException">Thrown when its
/// <see cref="Transaction{T}.Signature"/> is invalid or not signed by
/// the account who corresponds to its <see cref="PublicKey"/>.
/// </exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when its
/// <see cref="Signer"/> is not derived from its
/// <see cref="Transaction{T}.PublicKey"/>.</exception>
public void Validate()
{
if (Signature.Length == 0 || !PublicKey.Verify(Serialize(false), Signature))
{
string message =
$"The signature ({ByteUtil.Hex(Signature)}) is failed " +
"to verify.";
throw new InvalidTxSignatureException(Id, message);
}
if (!new Address(PublicKey).Equals(Signer))
{
string message =
$"The public key ({ByteUtil.Hex(PublicKey.Format(true))} " +
$"is not matched to the address ({Signer}).";
throw new InvalidTxPublicKeyException(Id, message);
}
}
/// <inheritdoc />
public bool Equals(Transaction<T> other)
{
return Id.Equals(other.Id);
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is Transaction<T> other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
return Id.GetHashCode();
}
internal RawTransaction ToRawTransaction(bool includeSign)
{
ImmutableArray<byte> genesisHash =
GenesisHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray<byte>.Empty;
var rawTx = new RawTransaction(
nonce: Nonce,
signer: Signer.ByteArray,
genesisHash: genesisHash,
updatedAddresses: UpdatedAddresses.Select(a =>
a.ByteArray).ToImmutableArray(),
publicKey: PublicKey.Format(false).ToImmutableArray(),
timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture),
actions: Actions.Select(a => a.PlainValue).ToImmutableArray()
);
if (includeSign)
{
rawTx = rawTx.AddSignature(Signature);
}
return rawTx;
}
private static T ToAction(IValue value)
{
var action = new T();
action.LoadPlainValue(value);
return action;
}
private readonly struct TransactionSerializationContext
{
internal TransactionSerializationContext(bool includeSignature)
{
IncludeSignature = includeSignature;
}
internal bool IncludeSignature { get; }
}
}
}