Skip to content

Commit

Permalink
Tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jul 20, 2021
1 parent 09f54a9 commit fe88cd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
9 changes: 5 additions & 4 deletions Libplanet.Explorer.UnitTests/GraphTypes/BlockTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public async void Query()
1,
1,
1,
new Nonce(new byte[] {0x01, 0x23, 0x45, 0x56}),
new Nonce(new byte[] { 0x01, 0x23, 0x45, 0x56 }),
new Address(TestUtils.GetRandomBytes(Address.Size)),
new BlockHash(TestUtils.GetRandomBytes(HashDigest<SHA256>.Size)),
DateTimeOffset.UtcNow,
ImmutableArray<Transaction<NoOpAction>>.Empty,
hashAlgorithm: HashAlgorithmType.Of<SHA256>(),
stateRootHash: new HashDigest<SHA256>(
TestUtils.GetRandomBytes(HashDigest<SHA256>.Size)));
hashAlgorithm: HashAlgorithmType.Of<SHA256>());
block = new Block<NoOpAction>(
block,
new HashDigest<SHA256>(TestUtils.GetRandomBytes(HashDigest<SHA256>.Size)));
var query =
@"{
index
Expand Down
31 changes: 13 additions & 18 deletions Libplanet/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public Block(
// hashAlgorithm, preEvaluationHash, and stateRootHash respectively.
// Despite its summary, currently accepted only use cases are:
// - (true, false, false): Called when preEvaluationHash needs to be calculated.
// - (false, true, false): Allowed purely for testing. Normal code path should not
// lead to this combination.
// - (false, true, true): Called when stateRootHash needs to be attached.
// All other combinations are rejected.
if (!(hashAlgorithm is { } ^ preEvaluationHash is { }))
Expand All @@ -102,11 +104,11 @@ public Block(
$"Exactly one of {nameof(hashAlgorithm)} " +
$"and {nameof(preEvaluationHash)} should be provided as non-null.");
}
else if (preEvaluationHash is { } ^ stateRootHash is { })
else if (!(preEvaluationHash is { }) && stateRootHash is { })
{
throw new ArgumentException(
$"Either both {nameof(preEvaluationHash)} and {nameof(stateRootHash)} " +
$"should be null or both should be non-null.");
$"Parameter {nameof(stateRootHash)} cannot be non-null while" +
$"{nameof(preEvaluationHash)} is null.");
}

ProtocolVersion = protocolVersion;
Expand Down Expand Up @@ -148,9 +150,6 @@ public Block(
ImmutableArray<byte> peh = preEvaluationHash
?? throw new NullReferenceException(
$"Parameter {nameof(preEvaluationHash)} cannot be null.");
HashDigest<SHA256> srh = stateRootHash
?? throw new NullReferenceException(
$"Parameter {nameof(stateRootHash)} cannot be null.");

_header = new BlockHeader(
protocolVersion: ProtocolVersion,
Expand All @@ -167,7 +166,8 @@ public Block(
txHash: TxHash?.ToByteArray().ToImmutableArray()
?? ImmutableArray<byte>.Empty,
preEvaluationHash: peh,
stateRootHash: srh.ToByteArray().ToImmutableArray());
stateRootHash: stateRootHash?.ToByteArray().ToImmutableArray()
?? ImmutableArray<byte>.Empty);

_preEvaluationHash = Header.PreEvaluationHash;
StateRootHash = stateRootHash;
Expand Down Expand Up @@ -283,13 +283,14 @@ private Block(RawBlock rawBlock)
: throw new ArgumentException(
$"PreEvaluationHash of {nameof(rawBlock.Header)} cannot be empty.");

// FIXME: we should convert `StateRootHash`'s type to `HashDisgest<SHA256>` after
// FIXME: We should convert `StateRootHash`'s type to `HashDisgest<SHA256>` after
// removing `IBlockStateStore`.
// See also <https://github.com/planetarium/libplanet/pull/1116#discussion_r535836480>.
// FIXME: Normal path should not lead to StateRootHash being null. Should be
// refactored to throw an exception.
StateRootHash = rawBlock.Header.StateRootHash.Any()
? new HashDigest<SHA256>(rawBlock.Header.StateRootHash)
: throw new ArgumentException(
$"StateRootHash of {nameof(rawBlock.Header)} cannot be empty.");
: (HashDigest<SHA256>?)null;
_hash = new BlockHash(rawBlock.Header.Hash);
}

Expand All @@ -305,14 +306,8 @@ private Block(RawBlock rawBlock)
/// </summary>
/// <seealso cref="PreEvaluationHash"/>
/// <seealso cref="StateRootHash"/>
public BlockHash Hash
{
get
{
return _hash
?? throw new InvalidOperationException("Hash has not been set.");
}
}
public BlockHash Hash => _hash
?? throw new InvalidOperationException("Hash has not been set.");

/// <summary>
/// The hash derived from the block <em>except of</em>
Expand Down
14 changes: 7 additions & 7 deletions Libplanet/Blocks/BlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ public BlockHeader(Bencodex.Types.Dictionary dict)
? dict.GetValue<Binary>(TxHashKey).ToImmutableArray()
: ImmutableArray<byte>.Empty;

Hash = dict.ContainsKey((IKey)(Binary)HashKey)
? dict.GetValue<Binary>(HashKey).ToImmutableArray()
: ImmutableArray<byte>.Empty;

PreEvaluationHash = dict.ContainsKey((IKey)(Binary)PreEvaluationHashKey)
? dict.GetValue<Binary>(PreEvaluationHashKey).ToImmutableArray()
: ImmutableArray<byte>.Empty;

StateRootHash = dict.ContainsKey((IKey)(Binary)StateRootHashKey)
? dict.GetValue<Binary>(StateRootHashKey).ToImmutableArray()
: ImmutableArray<byte>.Empty;

Hash = dict.ContainsKey((IKey)(Binary)HashKey)
? dict.GetValue<Binary>(HashKey).ToImmutableArray()
: ImmutableArray<byte>.Empty;
}

/// <summary>
Expand Down Expand Up @@ -192,10 +192,10 @@ internal BlockHeader(
PreviousHash = previousHash;
TxHash = txHash;

PreEvaluationHash =
hashAlgorithm.Digest(SerializeForPreEvaluationHash()).ToImmutableArray();
byte[] serialized = SerializeForPreEvaluationHash();
PreEvaluationHash = hashAlgorithm.Digest(serialized).ToImmutableArray();
StateRootHash = ImmutableArray<byte>.Empty;
Hash = hashAlgorithm.Digest(SerializeForHash()).ToImmutableArray();
Hash = BlockHash.DeriveFrom(serialized).ByteArray;
}

/// <summary>
Expand Down

0 comments on commit fe88cd9

Please sign in to comment.