Exemple #1
0
        public async Task <Block> RequestMiningAsync(ConsensusRequestMiningDto requestMiningDto)
        {
            if (!ValidateBlockMiningTime(requestMiningDto.BlockTime, requestMiningDto.MiningDueTime,
                                         requestMiningDto.BlockExecutionTime))
            {
                return(null);
            }

            var blockExecutionDuration =
                CalculateBlockMiningDuration(requestMiningDto.BlockTime, requestMiningDto.BlockExecutionTime);

            var block = (await _minerService.MineAsync(requestMiningDto.PreviousBlockHash,
                                                       requestMiningDto.PreviousBlockHeight, requestMiningDto.BlockTime, blockExecutionDuration)).Block;

            return(block);
        }
Exemple #2
0
        public async Task RequestMining_Test()
        {
            var chain = await _blockchainService.GetChainAsync();

            var request = new ConsensusRequestMiningDto
            {
                BlockTime           = TimestampHelper.GetUtcNow(),
                BlockExecutionTime  = TimestampHelper.DurationFromMilliseconds(500),
                MiningDueTime       = TimestampHelper.GetUtcNow().AddMilliseconds(499),
                PreviousBlockHash   = chain.BestChainHash,
                PreviousBlockHeight = chain.BestChainHeight
            };

            var block = await _miningRequestService.RequestMiningAsync(request);

            block.ShouldBeNull();

            request = new ConsensusRequestMiningDto
            {
                BlockTime           = TimestampHelper.GetUtcNow().AddMilliseconds(-501),
                BlockExecutionTime  = TimestampHelper.DurationFromMilliseconds(500),
                MiningDueTime       = TimestampHelper.GetUtcNow().AddMilliseconds(350),
                PreviousBlockHash   = chain.BestChainHash,
                PreviousBlockHeight = chain.BestChainHeight
            };
            block = await _miningRequestService.RequestMiningAsync(request);

            block.ShouldBeNull();

            request = new ConsensusRequestMiningDto
            {
                BlockTime           = TimestampHelper.GetUtcNow().AddMilliseconds(-400),
                BlockExecutionTime  = TimestampHelper.DurationFromMilliseconds(500),
                MiningDueTime       = TimestampHelper.GetUtcNow().AddMilliseconds(350),
                PreviousBlockHash   = chain.BestChainHash,
                PreviousBlockHeight = chain.BestChainHeight
            };
            block = await _miningRequestService.RequestMiningAsync(request);

            block.ShouldNotBeNull();
            block.Header.PreviousBlockHash.ShouldBe(chain.BestChainHash);
            block.Header.Height.ShouldBe(chain.BestChainHeight + 1);
            block.Header.Time.ShouldBe(request.BlockTime);
        }