From 082fac87d7a336718c9d1a71a29ad5456cf718c3 Mon Sep 17 00:00:00 2001 From: Colibri Shin Date: Sat, 19 Nov 2022 02:53:51 +0900 Subject: [PATCH] test: regression --- .../BlockChainTest.ValidateNextBlock.cs | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs b/Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs index f86170daaa..2c540907fb 100644 --- a/Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs +++ b/Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs @@ -321,5 +321,118 @@ public void ValidateNextBlockLastCommitFailsDropExpectedValidator() Assert.Throws(() => _blockChain.Append(block2, TestUtils.CreateBlockCommit(block2))); } + + [Fact] + public void ValidateBlockCommitGenesis() + { + InvalidBlockCommitException ibcm = + _blockChain.ValidateBlockCommit(_fx.GenesisBlock, null); + + Assert.Null(ibcm); + + ibcm = _blockChain.ValidateBlockCommit( + _fx.GenesisBlock, + new BlockCommit( + 0, + 0, + _fx.GenesisBlock.Hash, + TestUtils.ValidatorPrivateKeys.Select(x => new VoteMetadata( + 0, + 0, + _fx.GenesisBlock.Hash, + DateTimeOffset.UtcNow, + x.PublicKey, + VoteFlag.PreCommit).Sign(x)).ToImmutableArray())); + + Assert.NotNull(ibcm); + } + + [Fact] + public void ValidateBlockCommitFailsDifferentBlockHash() + { + Block validNextBlock = new BlockContent( + new BlockMetadata( + index: 1L, + timestamp: _fx.GenesisBlock.Timestamp.AddDays(1), + publicKey: _fx.Miner.PublicKey, + previousHash: _fx.GenesisBlock.Hash, + txHash: null, + lastCommit: null)).Propose().Evaluate(_fx.Miner, _blockChain); + + Assert.Throws(() => + _blockChain.Append( + validNextBlock, + TestUtils.CreateBlockCommit( + new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size)), + 1, + 0))); + } + + [Fact] + public void ValidateBlockCommitFailsDifferentHeight() + { + Block validNextBlock = new BlockContent( + new BlockMetadata( + index: 1L, + timestamp: _fx.GenesisBlock.Timestamp.AddDays(1), + publicKey: _fx.Miner.PublicKey, + previousHash: _fx.GenesisBlock.Hash, + txHash: null, + lastCommit: null)).Propose().Evaluate(_fx.Miner, _blockChain); + + Assert.Throws(() => + _blockChain.Append( + validNextBlock, + TestUtils.CreateBlockCommit( + validNextBlock.Hash, + 2, + 0))); + } + + [Fact] + public void ValidateBlockCommitFailsDifferentValidatorSet() + { + Block validNextBlock = new BlockContent( + new BlockMetadata( + index: 1L, + timestamp: _fx.GenesisBlock.Timestamp.AddDays(1), + publicKey: _fx.Miner.PublicKey, + previousHash: _fx.GenesisBlock.Hash, + txHash: null, + lastCommit: null)).Propose().Evaluate(_fx.Miner, _blockChain); + + Assert.Throws(() => + _blockChain.Append( + validNextBlock, + new BlockCommit( + 1, + 0, + validNextBlock.Hash, + Enumerable.Range(0, TestUtils.ValidatorSet.TotalCount) + .Select(x => new PrivateKey()) + .Select(x => new VoteMetadata( + 1, + 0, + validNextBlock.Hash, + DateTimeOffset.UtcNow, + x.PublicKey, + VoteFlag.PreCommit).Sign(x)).ToImmutableArray()))); + } + + [Fact] + public void ValidateBlockCommitFailsNullBlockCommit() + { + Block validNextBlock = new BlockContent( + new BlockMetadata( + index: 1L, + timestamp: _fx.GenesisBlock.Timestamp.AddDays(1), + publicKey: _fx.Miner.PublicKey, + previousHash: _fx.GenesisBlock.Hash, + txHash: null, + lastCommit: null)).Propose().Evaluate(_fx.Miner, _blockChain); + + Assert.Throws(() => + _blockChain.Append(validNextBlock, null)); + } } }