public async Task finaliseBlock_should_succeed() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); ResultWrapper <Result> resultWrapper = await rpc.consensus_finaliseBlock(TestItem.KeccakE); resultWrapper.Data.Should().Be(Result.OK); }
public async Task setHead_to_unknown_block_fails() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); ResultWrapper <Result> setHeadResult = await rpc.consensus_setHead(TestItem.KeccakF); setHeadResult.Data.success.Should().BeFalse(); }
public async Task assembleBlock_should_not_create_block_with_unknown_parent() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Keccak notExistingHash = TestItem.KeccakH; AssembleBlockRequest assembleBlockRequest = new() { ParentHash = notExistingHash }; ResultWrapper <BlockRequestResult?> response = await rpc.consensus_assembleBlock(assembleBlockRequest); response.Data.Should().BeNull(); }
public async Task can_progress_chain_one_by_one(int count) { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Keccak lastHash = (await ProduceBranch(rpc, chain.BlockTree, count, chain.BlockTree.HeadHash, true)).Last().BlockHash; chain.BlockTree.HeadHash.Should().Be(lastHash); Block?last = RunForAllBlocksInBranch(chain.BlockTree, chain.BlockTree.HeadHash, b => b.IsGenesis, true); last.Should().NotBeNull(); last !.IsGenesis.Should().BeTrue(); }
public async Task newBlock_accepts_first_block() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); BlockRequestResult blockRequestResult = CreateBlockRequest( CreateParentBlockRequestOnHead(chain.BlockTree), TestItem.AddressD); ResultWrapper <NewBlockResult> resultWrapper = await rpc.consensus_newBlock(blockRequestResult); resultWrapper.Data.Valid.Should().BeTrue(); new BlockRequestResult(chain.BlockTree.BestSuggestedBody).Should().BeEquivalentTo(blockRequestResult); }
public async Task setHead_no_common_branch_fails() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); BlockHeader parent = Build.A.BlockHeader.WithNumber(1).WithHash(TestItem.KeccakA).TestObject; Block block = Build.A.Block.WithNumber(2).WithParent(parent).TestObject; chain.BlockTree.SuggestBlock(block); ResultWrapper <Result> setHeadResult = await rpc.consensus_setHead(block.Hash !); setHeadResult.Data.success.Should().BeFalse(); }
public async Task newBlock_accepts_already_known_block() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Block block = Build.A.Block.WithNumber(1).WithParent(chain.BlockTree.Head).TestObject; block.Header.Hash = new Keccak("0xdc3419cbd81455372f3e576f930560b35ec828cd6cdfbd4958499e43c68effdf"); chain.BlockTree.SuggestBlock(block); ResultWrapper <NewBlockResult> newBlockResult = await rpc.consensus_newBlock(new BlockRequestResult(block)); newBlockResult.Data.Valid.Should().BeTrue(); }
public async Task setHead_can_reorganize_to_any_block() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); async Task CanReorganizeToBlock(BlockRequestResult block, MergeTestBlockchain testChain) { ResultWrapper <Result> result = await rpc.consensus_setHead(block.BlockHash); result.Data.Should().Be(Result.OK); testChain.BlockTree.HeadHash.Should().Be(block.BlockHash); testChain.BlockTree.Head !.Number.Should().Be(block.Number); testChain.State.StateRoot.Should().Be(testChain.BlockTree.Head !.StateRoot !); } async Task CanReorganizeToAnyBlock(MergeTestBlockchain testChain, params IReadOnlyList <BlockRequestResult>[] branches)
public async Task newBlock_rejects_incorrect_input(Action <BlockRequestResult> breakerAction) { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); BlockRequestResult assembleBlockResult = await GetAssembleBlockResult(chain, rpc); Keccak blockHash = assembleBlockResult.BlockHash; breakerAction(assembleBlockResult); if (blockHash == assembleBlockResult.BlockHash && TryCalculateHash(assembleBlockResult, out var hash)) { assembleBlockResult.BlockHash = hash; } ResultWrapper <NewBlockResult> newBlockResult = await rpc.consensus_newBlock(assembleBlockResult); newBlockResult.Data.Valid.Should().BeFalse(); }
public async Task assembleBlock_should_create_block_on_top_of_genesis() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Keccak startingHead = chain.BlockTree.HeadHash; UInt256 timestamp = Timestamper.UnixTime.Seconds; AssembleBlockRequest assembleBlockRequest = new() { ParentHash = startingHead, Timestamp = timestamp }; ResultWrapper <BlockRequestResult?> response = await rpc.consensus_assembleBlock(assembleBlockRequest); BlockRequestResult expected = CreateParentBlockRequestOnHead(chain.BlockTree); expected.GasLimit = 4000000L; expected.BlockHash = new Keccak("0xfe37027d377e75ffb161f11733d8880083378fe6236270c7a2ee1fc7efe71cfd"); expected.LogsBloom = Bloom.Empty; expected.Miner = chain.MinerAddress; expected.Number = 1; expected.ParentHash = startingHead; expected.SetTransactions(Array.Empty <Transaction>()); expected.Timestamp = timestamp; response.Data.Should().BeEquivalentTo(expected); }
public async Task newBlock_accepts_previously_assembled_block_multiple_times([Values(1, 3)] int times) { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Keccak startingHead = chain.BlockTree.HeadHash; BlockHeader startingBestSuggestedHeader = chain.BlockTree.BestSuggestedHeader !; AssembleBlockRequest assembleBlockRequest = new() { ParentHash = startingHead }; ResultWrapper <BlockRequestResult?> assembleBlockResult = await rpc.consensus_assembleBlock(assembleBlockRequest); assembleBlockResult.Data !.ParentHash.Should().Be(startingHead); for (int i = 0; i < times; i++) { ResultWrapper <NewBlockResult> newBlockResult = await rpc.consensus_newBlock(assembleBlockResult.Data !); newBlockResult.Data.Valid.Should().BeTrue(); } Keccak bestSuggestedHeaderHash = chain.BlockTree.BestSuggestedHeader !.Hash !; bestSuggestedHeaderHash.Should().Be(assembleBlockResult.Data !.BlockHash); bestSuggestedHeaderHash.Should().NotBe(startingBestSuggestedHeader !.Hash !); }
public async Task setHead_should_change_head() { using MergeTestBlockchain chain = await CreateBlockChain(); IConsensusRpcModule rpc = CreateConsensusModule(chain); Keccak startingHead = chain.BlockTree.HeadHash; BlockRequestResult blockRequestResult = CreateBlockRequest( CreateParentBlockRequestOnHead(chain.BlockTree), TestItem.AddressD); ResultWrapper <NewBlockResult> newBlockResult = await rpc.consensus_newBlock(blockRequestResult); newBlockResult.Data.Valid.Should().BeTrue(); Keccak newHeadHash = blockRequestResult.BlockHash; ResultWrapper <Result> setHeadResult = await rpc.consensus_setHead(newHeadHash !); setHeadResult.Data.Should().Be(Result.OK); Keccak actualHead = chain.BlockTree.HeadHash; actualHead.Should().NotBe(startingHead); actualHead.Should().Be(newHeadHash); }
public void consensus_assembleBlock_should_return_expected_results() { IConsensusRpcModule consensusRpcModule = CreateConsensusModule(); Assert.Throws <NotImplementedException> (() => consensusRpcModule.consensus_assembleBlock(new AssembleBlockRequest())); }