Пример #1
0
        public async Task <BlockchainNodeContext> StartAsync(BlockchainNodeContextStartDto dto)
        {
            _defaultContractZeroCodeProvider.SetDefaultContractZeroRegistrationByType(dto.ZeroSmartContractType);

            var context = new BlockchainNodeContext
            {
                ChainId = dto.ChainId,
                TxHub   = _txHub,
            };
            var chain = await _blockchainService.GetChainAsync();

            chain = chain == null
                ? await _chainCreationService.CreateNewChainAsync(dto.Transactions)
                : await _blockchainService.ResetChainToLibAsync(chain);

            await _smartContractAddressUpdateService.UpdateSmartContractAddressesAsync(
                await _blockchainService.GetBlockHeaderByHashAsync(chain.BestChainHash));

            await _consensusService.TriggerConsensusAsync(new ChainContext
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            });

            return(context);
        }
Пример #2
0
        public async Task Start_Test()
        {
            var transactions = new List <Transaction>
            {
                _kernelTestHelper.GenerateTransaction(),
                _kernelTestHelper.GenerateTransaction()
            };

            var dto = new BlockchainNodeContextStartDto
            {
                ChainId = 1234,
                ZeroSmartContractType = typeof(IBlockchainNodeContextService),
                Transactions          = transactions.ToArray()
            };

            _kernelNodeTestContext.MockConsensusService.Verify(
                s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Never);

            var context = await _blockchainNodeContextService.StartAsync(dto);

            _kernelNodeTestContext.MockConsensusService.Verify(
                s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Once);

            context.ChainId.ShouldBe(dto.ChainId);
            var chain = await _blockchainService.GetChainAsync();

            chain.Id.ShouldBe(dto.ChainId);
            chain.BestChainHeight.ShouldBe(1);
            var block = await _blockchainService.GetBlockByHashAsync(chain.BestChainHash);

            block.Body.TransactionIds.Count.ShouldBe(2);
            block.Body.TransactionIds.ShouldContain(transactions[0].GetHash());
            block.Body.TransactionIds.ShouldContain(transactions[1].GetHash());

            var block2 = await _kernelTestHelper.AttachBlockToBestChain();

            var block3 = await _kernelTestHelper.AttachBlockToBestChain();

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHeight.ShouldBe(3);
            chain.BestChainHash.ShouldBe(block3.GetHash());

            await _blockchainService.SetIrreversibleBlockAsync(chain, block2.Height, block2.GetHash());

            chain = await _blockchainService.GetChainAsync();

            chain.LastIrreversibleBlockHeight.ShouldBe(2);

            context = await _blockchainNodeContextService.StartAsync(dto);

            chain = await _blockchainService.GetChainAsync();

            context.ChainId.ShouldBe(dto.ChainId);
            chain.BestChainHeight.ShouldBe(2);
            chain.BestChainHash.ShouldBe(block2.GetHash());

            _kernelNodeTestContext.MockConsensusService.Verify(
                s => s.TriggerConsensusAsync(It.IsAny <ChainContext>()), Times.Exactly(2));
        }