Skip to content

Commit

Permalink
Turn on null checker for Transaction.cs
Browse files Browse the repository at this point in the history
planetarium#458

[changelog skip]
  • Loading branch information
dahlia committed Aug 5, 2022
1 parent d86445e commit 4266b48
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions Libplanet/Tx/Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -38,7 +37,7 @@ public sealed class Transaction<T> : IEquatable<Transaction<T>>, ITxExcerpt
private TxId? _id;
private TxMetadata _metadata;
private byte[] _signature;
private byte[] _bytes;
private byte[]? _bytes;

/// <summary>
/// Creates a new <see cref="Transaction{T}"/> instance by copying data from a specified
Expand All @@ -59,8 +58,9 @@ public Transaction(ITxMetadata metadata, IEnumerable<T> actions, byte[] signatur
_metadata = new TxMetadata(metadata);
Actions = actions?.ToImmutableList()
?? throw new ArgumentNullException(nameof(actions));
Signature = signature
?? throw new ArgumentNullException(nameof(signature));
_signature =
new byte[(signature ?? throw new ArgumentNullException(nameof(signature))).Length];
signature.CopyTo(_signature, 0);
}

/// <summary>
Expand Down Expand Up @@ -126,8 +126,9 @@ public Transaction(
Timestamp = timestamp,
};

Signature = signature ??
throw new ArgumentNullException(nameof(signature));
_signature =
new byte[(signature ?? throw new ArgumentNullException(nameof(signature))).Length];
signature.CopyTo(_signature, 0);
Actions = actions?.ToImmutableList() ??
throw new ArgumentNullException(nameof(actions));
}
Expand Down Expand Up @@ -209,6 +210,8 @@ public TxId Id
/// <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>
/// <remarks>Although this cannot be <see langword="null"/>, it can be an empty
/// array if the transaction is not signed yet.</remarks>
public byte[] Signature
{
get
Expand Down Expand Up @@ -372,7 +375,7 @@ public static Transaction<T> Create(
PrivateKey privateKey,
BlockHash? genesisHash,
IEnumerable<T> actions,
IImmutableSet<Address> updatedAddresses = null,
IImmutableSet<Address>? updatedAddresses = null,
DateTimeOffset? timestamp = null
)
{
Expand Down Expand Up @@ -491,17 +494,13 @@ public static Transaction<T> CreateUnsigned(
PublicKey publicKey,
BlockHash? genesisHash,
IEnumerable<T> actions,
IImmutableSet<Address> updatedAddresses = null,
IImmutableSet<Address>? updatedAddresses = null,
DateTimeOffset? timestamp = null
)
{
var signer = new Address(publicKey);

if (ReferenceEquals(updatedAddresses, null))
{
updatedAddresses = ImmutableHashSet<Address>.Empty;
}

updatedAddresses ??= ImmutableHashSet<Address>.Empty;
DateTimeOffset ts = timestamp ?? DateTimeOffset.UtcNow;

ImmutableArray<T> actionsArray = actions.ToImmutableArray();
Expand Down Expand Up @@ -555,7 +554,7 @@ public byte[] Serialize(bool sign)
// 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)
if (_signature.Length > 0)
{
byte[] sigDict =
Codec.Encode(Dictionary.Empty.Add(TxMetadata.SignatureKey, _signature));
Expand Down Expand Up @@ -617,10 +616,10 @@ public void Validate()
}

/// <inheritdoc />
public bool Equals(Transaction<T> other) => Id.Equals(other?.Id);
public bool Equals(Transaction<T>? other) => Id.Equals(other?.Id);

/// <inheritdoc />
public override bool Equals(object obj) => obj is Transaction<T> other && Equals(other);
public override bool Equals(object? obj) => obj is Transaction<T> other && Equals(other);

/// <inheritdoc />
public override int GetHashCode() => Id.GetHashCode();
Expand Down

0 comments on commit 4266b48

Please sign in to comment.