Esempio n. 1
0
        public void Execute(CancellationToken token)
        {
            var txs     = BlockchainManager.GetPool();
            var time    = DateTime.UtcNow;
            var subsidy = BlockchainManager.GetSubsidy(BlockchainManager.Chain.Count);

            var txList = txs.Where(tx =>
            {
                try
                {
                    token.ThrowIfCancellationRequested();
                    BlockchainManager.VerifyTransaction(tx, time);
                    subsidy += tx.TransactionFee;
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }).ToList();

            var cbOut = new Output()
            {
                Amount        = subsidy,
                PublicKeyHash = MinerKeyHash.Bytes
            };
            var tb = new TransactionBuilder(new List <Output>()
            {
                cbOut
            }, new List <Input>());
            var coinbaseTx = tb.ToTransaction(time);

            BlockchainManager.VerifyTransaction(coinbaseTx, time, subsidy);
            txList.Insert(0, coinbaseTx);

            var txIds = txList.Select(x => x.Id).ToList();

            var block = new Block()
            {
                PreviousBlockHash = BlockchainManager.Chain.Last().Id,
                Transactions      = txList,
                MerkleRootHash    = HashUtil.ComputeMerkleRootHash(txIds),
                Bits = Difficulty.DifficultyBits
            };

            if (!Mining(block, token))
            {
                return;
            }

            _logger.LogInformation($"Mined");
            _logger.LogInformation($"{JsonSerializer.PrettyPrint(JsonSerializer.Serialize(block))}");

            //Broadcast Block
        }