コード例 #1
0
ファイル: Node.cs プロジェクト: svetoslavpetkov/BlockChain
        private Block BuildBlock()
        {
            var   lastBlock = BlockChain.Last().Value;
            Block tempBlock = Block.BuildBlockForMiner(lastBlock.Index + 1, PendingTransactions.ToList(), lastBlock.BlockHash, Difficulty);

            return(tempBlock);
        }
コード例 #2
0
ファイル: Node.cs プロジェクト: svetoslavpetkov/BlockChain
        public void NonceFound(string minerAddress, int nonce, string hash)
        {
            lock (_nonceFoundLock)
            {
                Block block = BlocksInProgress[minerAddress];
                if (block == null)
                {
                    return;
                }

                if (!CryptoUtil.IsAddressValid(minerAddress))
                {
                    throw new AddressNotValidException($"Miner address '{minerAddress}' is not valid");
                }

                if (block.Index != BlockChain.Last().Key + 1)
                {
                    throw new NonceUselessException();
                }

                ValidateBlockHash(block, nonce, hash);
                block.BlockMined(nonce, hash, minerAddress);

                int minedTransactionsCount = block.Transactions.Count;

                List <Transaction> notMinedTxs = new List <Transaction>();
                var allTx = PendingTransactions.ToList();

                for (int i = minedTransactionsCount; i < allTx.Count; i++)
                {
                    notMinedTxs.Add(allTx[i]);
                }

                PendingTransactions = new ConcurrentBag <Transaction>(notMinedTxs);

                BlockChain.TryAdd(block.Index, block);
                BlocksInProgress[minerAddress] = null;
                NodeSynchornizator.BroadcastBlock(block);
            }
        }