Skip to content

Commit

Permalink
Merge pull request #1347 from greymistcube/refactor/header-caching
Browse files Browse the repository at this point in the history
Refactor/header caching
  • Loading branch information
greymistcube authored Jul 21, 2021
2 parents 57a8710 + eb8897d commit 63ff0c0
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 279 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ To be released.
if `OutOfMemoryException` is caught from `IAction.Execute()`.
[[#1320], [#1343]]
- Improved performance of broadcasting using `Swarm<T>`. [[#1334]]
- `Block<T>.Header` is now cached instead of creating a new instance every
call. [[#1347]]

### Bug fixes

Expand Down Expand Up @@ -364,6 +366,7 @@ To be released.
[#1341]: https://github.com/planetarium/libplanet/pull/1341
[#1342]: https://github.com/planetarium/libplanet/pull/1342
[#1343]: https://github.com/planetarium/libplanet/pull/1343
[#1347]: https://github.com/planetarium/libplanet/pull/1347
[#1348]: https://github.com/planetarium/libplanet/pull/1348
[#1349]: https://github.com/planetarium/libplanet/issues/1349
[#1350]: https://github.com/planetarium/libplanet/pull/1350
Expand Down
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,
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
40 changes: 12 additions & 28 deletions Libplanet.Tests/Blocks/BlockHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ public void ValidateTimestamp()
miner: ImmutableArray<byte>.Empty,
timestamp: future,
preEvaluationHash: TestUtils.GetRandomBytes(32).ToImmutableArray(),
stateRootHash: ImmutableArray<byte>.Empty
);
stateRootHash: TestUtils.GetRandomBytes(32).ToImmutableArray());

Assert.Throws<InvalidBlockTimestampException>(
() => { header.Validate(hashAlgorithm, now); });
Expand Down Expand Up @@ -376,33 +375,18 @@ private BlockHeader MakeBlockHeader(
ImmutableArray<byte> stateRootHash
)
{
ImmutableArray<byte> hash = hashAlgorithm.Digest(
BlockHeader.SerializeForHash(
protocolVersion,
index,
timestamp,
difficulty,
nonce,
miner,
previousHash,
txHash,
stateRootHash
)
).ToImmutableArray();
return new BlockHeader(
protocolVersion,
index,
timestamp,
nonce,
miner,
difficulty,
totalDifficulty,
previousHash,
txHash,
hash,
preEvaluationHash,
stateRootHash
);
protocolVersion: protocolVersion,
index: index,
timestamp: timestamp,
nonce: nonce,
miner: miner,
difficulty: difficulty,
totalDifficulty: totalDifficulty,
previousHash: previousHash,
txHash: txHash,
preEvaluationHash: preEvaluationHash,
stateRootHash: stateRootHash);
}
}
}
Loading

0 comments on commit 63ff0c0

Please sign in to comment.