Example #1
0
        /// <summary>
        /// Cancels the current mining progress without checking for any blocks.
        /// </summary>
        public void CancelMining()
        {
            if (_currentMiningProgress == null)
            {
                throw new InvalidOperationException("No mining is progress");
            }

            _currentMiningProgress.Dispose();
            _currentMiningProgress = null;
        }
Example #2
0
        /// <summary>
        /// Creates a block containing suitable pending transactions and a mining helper object.
        /// </summary>
        /// <param name="minerAddress">Wallet address of the miner to be added to coinbase transaction.</param>
        /// <param name="nonceRange">Range of nonce values to try in each call to <see cref="BlockMining.Attempt"/>.</param>
        /// <returns>The mining helper.</returns>
        public BlockMining PrepareMining(string minerAddress, int nonceRange = 100_000)
        {
            if (_currentMiningProgress != null)
            {
                throw new InvalidOperationException("Mining in progress");
            }

            var block = new Block
            {
                Index             = LatestBlock.Index + 1,
                Timestamp         = TimeUtils.MsSinceEpochToUtcNow(),
                PreviousBlockHash = LatestBlock.Hash,
            };

            transactionDb.CollectPendingTransactionsForBlock(block, minerAddress);

            _currentMiningProgress = new BlockMining(block, nonceRange);
            return(_currentMiningProgress);
        }
Example #3
0
        /// <summary>
        /// Finishes the current mining progress.
        /// If a valid block has been mined, adds it to the chain.
        /// </summary>
        /// <returns>True if a valid block has been mined and added.</returns>
        public bool FinishMining()
        {
            if (_currentMiningProgress == null)
            {
                throw new InvalidOperationException("No mining is progress");
            }

            var  receivedTxMap  = new Dictionary <string, Transaction>();
            var  spentTxOutputs = new HashSet <TxOutPointer>();
            bool isValid        = validateBlock(_currentMiningProgress.Block, LatestBlock, transactionDb, receivedTxMap, spentTxOutputs);

            if (isValid is false)
            {
                _currentMiningProgress.Dispose();
                _currentMiningProgress = null;
                return(false);
            }

            addNewBlock(_currentMiningProgress.Block);
            _currentMiningProgress.Dispose();
            _currentMiningProgress = null;

            return(true);
        }