private Block BuildBlock() { var lastBlock = BlockChain.Last().Value; Block tempBlock = Block.BuildBlockForMiner(lastBlock.Index + 1, PendingTransactions.ToList(), lastBlock.BlockHash, Difficulty); return(tempBlock); }
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); } }