Пример #1
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));
        }
        public async Task <OsBlockchainNodeContext> StartAsync(OsBlockchainNodeContextStartDto dto)
        {
            var transactions = new List <Transaction>();

            transactions.Add(GetTransactionForDeployment(dto.ZeroSmartContract,
                                                         ZeroSmartContractAddressNameProvider.Name,
                                                         dto.SmartContractRunnerCategory));

            transactions.AddRange(dto.InitializationSmartContracts
                                  .Select(p => GetTransactionForDeployment(p.Code, p.SystemSmartContractName,
                                                                           dto.SmartContractRunnerCategory,
                                                                           p.ContractInitializationMethodCallList)));

            if (dto.InitializationTransactions != null)
            {
                transactions.AddRange(dto.InitializationTransactions);
            }

            // Add transaction for initialization
            transactions.Add(GetTransactionForGenesisOwnerInitialization(dto));

            var blockchainNodeContextStartDto = new BlockchainNodeContextStartDto()
            {
                ChainId = dto.ChainId,
                ZeroSmartContractType = dto.ZeroSmartContract,
                Transactions          = transactions.ToArray()
            };

            var context = new OsBlockchainNodeContext
            {
                BlockchainNodeContext =
                    await _blockchainNodeContextService.StartAsync(blockchainNodeContextStartDto),
                AElfNetworkServer = _networkServer
            };

            await _networkServer.StartAsync();

            foreach (var nodePlugin in _nodePlugins)
            {
                await nodePlugin.StartAsync(dto.ChainId);
            }

            return(context);
        }