Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total difficulty property to Block<T> #917

Merged
merged 6 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ To be released.
[[#796], [#878]]
- Added `render` optional parameter to `BlockChain<T>()` constructor.
[[#883]]
- Added `BigInteger`-typed `totalDifficulty` parameter to `Block<T>()`
constructor. [[#666], [#917]]
- Added `BigInteger`-typed `previousTotalDifficulty` parameter to
`Block<T>.Mine()` static method. [[#666], [#917]]

### Backward-incompatible network protocol changes

Expand All @@ -36,10 +40,12 @@ To be released.
- The existing `RecentStates` message type (with the type number `0x0f`) was
replaced by a new `RecentStates` message type
(with the type number `0x13`). [[#912]]
- Added `BlockHeader.TotalDifficulty` property. [[#666], [#917]]

### Backward-incompatible storage format changes

- Added `RawTransaction<T>.GenesisHash` property. [[#796], [#878]]
- Added `BlockHeader.TotalDifficulty` property. [[#666], [#917]]

### Added APIs

Expand All @@ -51,6 +57,7 @@ To be released.
- Added `CurrencyPermissionException` class. [[#861], [#900]]
- Added `InsufficientBalanceException` class. [[#861], [#900]]
- Added `BlockChain<T>.GetBalance()` method. [[#861], [#900]]
- Added `Block<T>.TotalDifficulty` property. [[#666], [#917]]

### Behavioral changes

Expand All @@ -69,6 +76,8 @@ To be released.
- `Swarm<T>` became to ignore invalid `BlockHeader`s immediately. [[#898]]
- `Swarm<T>.PreloadAsync()` became to clean up only temporary chains.
[[#902]]
- `BlockPolicy<T>` became to validate `Block<T>.TotalDifficulty` property
of a `Block<T>`. [[#666], [#917]]

### Bug fixes

Expand All @@ -88,6 +97,7 @@ To be released.
### CLI tools

[#404]: https://github.com/planetarium/libplanet/issues/404
[#666]: https://github.com/planetarium/libplanet/issues/666
[#756]: https://github.com/planetarium/libplanet/issues/756
[#796]: https://github.com/planetarium/libplanet/issues/796
[#858]: https://github.com/planetarium/libplanet/issues/858
Expand All @@ -108,6 +118,7 @@ To be released.
[#912]: https://github.com/planetarium/libplanet/pull/912
[#913]: https://github.com/planetarium/libplanet/pull/913
[#916]: https://github.com/planetarium/libplanet/pull/916
[#917]: https://github.com/planetarium/libplanet/pull/917
[sleep mode]: https://en.wikipedia.org/wiki/Sleep_mode


Expand Down
25 changes: 25 additions & 0 deletions Libplanet.Tests/Blockchain/Policies/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public BlockPolicyTest(ITestOutputHelper output)
_validNext = Block<DumbAction>.Mine(
1,
1024,
_genesis.TotalDifficulty,
_genesis.Miner.Value,
_genesis.Hash,
_genesis.Timestamp.AddSeconds(1),
Expand Down Expand Up @@ -162,6 +163,7 @@ public void ValidateNextBlock()
var validNextBlock = Block<DumbAction>.Mine(
1,
1,
_genesis.TotalDifficulty,
_genesis.Miner.Value,
_genesis.Hash,
_genesis.Timestamp.AddDays(1),
Expand All @@ -177,6 +179,7 @@ public void ValidateNextBlockInvalidIndex()
var invalidIndexBlock = Block<DumbAction>.Mine(
1,
1,
_genesis.TotalDifficulty,
_genesis.Miner.Value,
_validNext.Hash,
_validNext.Timestamp.AddSeconds(1),
Expand All @@ -193,6 +196,7 @@ public void ValidateNextBlockInvalidDifficulty()
var invalidDifficultyBlock = Block<DumbAction>.Mine(
2,
1,
_validNext.TotalDifficulty,
_genesis.Miner.Value,
_validNext.Hash,
_validNext.Timestamp.AddSeconds(1),
Expand All @@ -203,6 +207,25 @@ public void ValidateNextBlockInvalidDifficulty()
invalidDifficultyBlock));
}

[Fact]
public void ValidateNextBlockInvalidTotalDifficulty()
{
_chain.Append(_validNext);

var invalidTotalDifficultyBlock = Block<DumbAction>.Mine(
2,
_policy.GetNextBlockDifficulty(_chain),
_validNext.TotalDifficulty - 1,
_genesis.Miner.Value,
_validNext.Hash,
_validNext.Timestamp.AddSeconds(1),
_emptyTransaction);
Assert.IsType<InvalidBlockTotalDifficultyException>(
_policy.ValidateNextBlock(
_chain,
invalidTotalDifficultyBlock));
}

[Fact]
public void ValidateNextBlockInvalidPreviousHash()
{
Expand All @@ -211,6 +234,7 @@ public void ValidateNextBlockInvalidPreviousHash()
var invalidPreviousHashBlock = Block<DumbAction>.Mine(
2,
1032,
_validNext.TotalDifficulty,
_genesis.Miner.Value,
new HashDigest<SHA256>(new byte[32]),
_validNext.Timestamp.AddSeconds(1),
Expand All @@ -229,6 +253,7 @@ public void ValidateNextBlockInvalidTimestamp()
var invalidPreviousTimestamp = Block<DumbAction>.Mine(
2,
1032,
_validNext.TotalDifficulty,
_genesis.Miner.Value,
_validNext.Hash,
_validNext.Timestamp.Subtract(TimeSpan.FromSeconds(1)),
Expand Down
26 changes: 24 additions & 2 deletions Libplanet.Tests/Blocks/BlockHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void ValidateTimestamp()
var header = new BlockHeader(
index: 0,
difficulty: 0,
totalDifficulty: 0,
nonce: ImmutableArray<byte>.Empty,
previousHash: ImmutableArray<byte>.Empty,
txHash: ImmutableArray<byte>.Empty,
Expand All @@ -42,6 +43,7 @@ public void ValidateNonce()
var header = new BlockHeader(
index: _fx.Next.Index,
difficulty: long.MaxValue,
totalDifficulty: _fx.Genesis.TotalDifficulty + long.MaxValue,
nonce: _fx.Next.Nonce.ByteArray,
miner: _fx.Next.Miner?.ByteArray ?? ImmutableArray<byte>.Empty,
hash: _fx.Next.Hash.ByteArray,
Expand All @@ -63,6 +65,7 @@ public void ValidateIndex()
var header = new BlockHeader(
index: -1,
difficulty: _fx.Next.Difficulty,
totalDifficulty: _fx.Next.TotalDifficulty,
nonce: _fx.Next.Nonce.ByteArray,
miner: _fx.Next.Miner?.ByteArray ?? ImmutableArray<byte>.Empty,
hash: _fx.Next.Hash.ByteArray,
Expand All @@ -85,6 +88,7 @@ public void ValidateDifficulty()
var genesisHeader = new BlockHeader(
index: 0,
difficulty: 1000,
totalDifficulty: 1000,
nonce: ImmutableArray<byte>.Empty,
previousHash: ImmutableArray<byte>.Empty,
txHash: ImmutableArray<byte>.Empty,
Expand All @@ -96,9 +100,10 @@ public void ValidateDifficulty()
Assert.Throws<InvalidBlockDifficultyException>(() =>
genesisHeader.Validate(DateTimeOffset.UtcNow));

var header = new BlockHeader(
var header1 = new BlockHeader(
index: 10,
difficulty: 0,
totalDifficulty: 1000,
nonce: ImmutableArray<byte>.Empty,
previousHash: ImmutableArray<byte>.Empty,
txHash: ImmutableArray<byte>.Empty,
Expand All @@ -108,7 +113,22 @@ public void ValidateDifficulty()
);

Assert.Throws<InvalidBlockDifficultyException>(() =>
genesisHeader.Validate(DateTimeOffset.UtcNow));
header1.Validate(DateTimeOffset.UtcNow));

var header2 = new BlockHeader(
index: 10,
difficulty: 1000,
totalDifficulty: 10,
nonce: ImmutableArray<byte>.Empty,
previousHash: ImmutableArray<byte>.Empty,
txHash: ImmutableArray<byte>.Empty,
hash: TestUtils.GetRandomBytes(32).ToImmutableArray(),
miner: ImmutableArray<byte>.Empty,
timestamp: now.ToString(BlockHeader.TimestampFormat, CultureInfo.InvariantCulture)
);

Assert.Throws<InvalidBlockTotalDifficultyException>(() =>
header2.Validate(DateTimeOffset.UtcNow));
}

[Fact]
Expand All @@ -118,6 +138,7 @@ public void ValidatePreviousHash()
var genesisHeader = new BlockHeader(
index: 0,
difficulty: 0,
totalDifficulty: 0,
nonce: ImmutableArray<byte>.Empty,
previousHash: TestUtils.GetRandomBytes(32).ToImmutableArray(),
txHash: ImmutableArray<byte>.Empty,
Expand All @@ -132,6 +153,7 @@ public void ValidatePreviousHash()
var header = new BlockHeader(
index: 10,
difficulty: 1000,
totalDifficulty: 1500,
nonce: ImmutableArray<byte>.Empty,
previousHash: ImmutableArray<byte>.Empty,
txHash: ImmutableArray<byte>.Empty,
Expand Down
Loading