예제 #1
0
        public async Task <(NBitcoin.Block, string)> MineNextBlockAsync(IEnumerable <NBitcoin.Transaction> transactions, bool throwOnError, NBitcoin.Block parentBlock, long parentBlockHeight)
        {
            var newBlock = parentBlock.CreateNextBlockWithCoinbase(new Key().PubKey, parentBlockHeight, NBitcoin.Altcoins.BCash.Instance.Regtest.Consensus.ConsensusFactory);

            newBlock.Transactions.AddRange(transactions);
            newBlock.Header.Bits      = parentBlock.Header.Bits; // assume same difficulty target
            newBlock.Header.BlockTime = parentBlock.Header.BlockTime.AddSeconds(1);
            newBlock.UpdateMerkleRoot();

            // Try to solve the block
            bool found = false;

            for (int i = 0; !found && i < 10000; i++)
            {
                newBlock.Header.Nonce = (uint)i;
                found = newBlock.Header.CheckProofOfWork();
            }

            if (!found)
            {
                throw new Exception("Bad luck - unable to find nonce that matches required difficulty");
            }

            var submitResult = await rpcClient0.SubmitBlock(newBlock.ToBytes());

            if (!string.IsNullOrEmpty(submitResult) && throwOnError)
            {
                throw new Exception($"Error while submitting new block - submitBlock returned {submitResult}");
            }

            return(newBlock, submitResult);
        }