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

Throw exception with network based on different genesis #746

Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ To be released.
continuous. [[#732]]
- `Swarm<T>` became to can process more requests at once by creating TURN
relaying proxy concurrently. [[#744]]
- `Swarm<T>` became to throw `InvalidGenesisBlockException` when receiving
block from the nodes that have a different genesis block. [[#746]]

### Bug fixes

Expand Down Expand Up @@ -160,6 +162,7 @@ To be released.
[#736]: https://github.com/planetarium/libplanet/pull/736
[#739]: https://github.com/planetarium/libplanet/pull/739
[#744]: https://github.com/planetarium/libplanet/pull/744
[#746]: https://github.com/planetarium/libplanet/pull/746


Version 0.7.0
Expand Down
33 changes: 33 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,39 @@ public async Task DoNotReceiveBlockFromNodeHavingDifferenceGenesisBlock()
}
}

[Fact(Timeout = Timeout)]
public async Task ThrowInvalidGenesisException()
{
var policy = new BlockPolicy<DumbAction>();
BlockChain<DumbAction> MakeBlockChain() => TestUtils.MakeBlockChain(
policy,
new DefaultStore(path: null),
null,
new PrivateKey());

var chainA = MakeBlockChain();
var chainB = MakeBlockChain();
var swarmA = CreateSwarm(chainA);
var swarmB = CreateSwarm(chainB);

await chainB.MineBlock(_fx1.Address1);

await StartAsync(swarmA);
await StartAsync(swarmB);

await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }, null);
Assert.NotEqual(chainA.Genesis, chainB.Genesis);
Task t = swarmA.PreloadAsync();
await Assert.ThrowsAsync<AggregateException>(async () => await t);
var exception = t.Exception.InnerException?.InnerException;
Assert.IsType<InvalidGenesisBlockException>(exception);

await StopAsync(swarmA);
await StopAsync(swarmB);
swarmA.Dispose();
swarmB.Dispose();
}

[Fact(Timeout = Timeout)]
public async Task FindSpecificPeerAsync()
{
Expand Down
4 changes: 3 additions & 1 deletion Libplanet/Blocks/InvalidGenesisBlockException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Libplanet.Blocks
/// <summary>
/// The exception that is thrown when the genesis block the <see cref="IStore"/> contains
/// mismatches to the genesis block the <see cref="BlockChain{T}"/> constructor (i.e., network)
/// expects.
/// expects or the first block of <see cref="BlockLocator"/> which the <see cref="IStore"/>
/// doesn't contain, because the block which <see cref="IStore"/> doesn't means
/// the genesis block in other network.
/// </summary>
public class InvalidGenesisBlockException : InvalidBlockException
{
Expand Down
10 changes: 7 additions & 3 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1910,11 +1910,15 @@ await GetBlockHashesAsync(peer, locator, stop, cancellationToken)
// same genesis block...
else if (!BlockChain.ContainsBlock(branchPoint))
{
_logger.Debug(
var msg =
$"Since the genesis block is fixed to {BlockChain.Genesis} " +
"protocol-wise, the blockchain which does not share " +
"any mutual block is not acceptable.");
break;
"any mutual block is not acceptable.";
_logger.Debug(msg);
throw new InvalidGenesisBlockException(
branchPoint,
workspace.Genesis.Hash,
msg);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Menees.Analyzers.Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<Menees.Analyzers.Settings>
<MaxLineColumns>100</MaxLineColumns>
<MaxMethodLines>200</MaxMethodLines>
<MaxFileLines>2800</MaxFileLines>
<MaxFileLines>2900</MaxFileLines>
</Menees.Analyzers.Settings>