Exemple #1
0
        public bool Execute(CancellationToken token, out Block block)
        {
            block = null;
            var txs     = TransactionPool.GetPool();
            var time    = DateTime.UtcNow;
            var subsidy = BlockchainUtil.GetSubsidy(Blockchain.Chain.Count);

            var txList = txs.Where(tx =>
            {
                if (token.IsCancellationRequested || !Blockchain.VerifyTransaction(tx, time, false, out var txFee))
                {
                    return(false);
                }
                subsidy += txFee;
                return(true);
            }).ToList();

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

            if (!Blockchain.VerifyTransaction(coinbaseTx, time, true, out var txFee, subsidy))
            {
                return(false);
            }
            txList.Insert(0, coinbaseTx);

            var txIds     = txList.Select(x => x.Id.Bytes).ToList();
            var mineBlock = new Block()
            {
                Id = null,
                PreviousBlockHash = Blockchain.Chain.Last().Id,
                Transactions      = null,
                MerkleRootHash    = HashUtil.ComputeMerkleRootHash(txIds),
                Bits = Blockchain.GetDifficulty()
            };

            if (!Mine(mineBlock, token))
            {
                return(false);
            }

            mineBlock.Transactions = txList;
            block = mineBlock;
            return(true);
        }
Exemple #2
0
 public Miner(TransactionPool tp, Blockchain blockchain, byte[] minerKeyHash)
 {
     TransactionPool    = tp;
     Blockchain         = blockchain;
     MinerPublicKeyHash = minerKeyHash;
 }
Exemple #3
0
 public Blockchain(TransactionPool transactionPool)
 {
     TransactionPool = transactionPool;
     BlockVerify(BlockchainUtil.CreateGenesisBlock());
 }