void Apply(Block block) { logger.LogWarning($@"Applying Block {block.Height}:{ block.Id.ToString().Substring(0, 7)}."); var txIds = ( from tx in block.ParsedTransactions where !tx.ExecInfo.Coinbase select tx.Id).ToArray(); lock (InventoryManager.MemoryPool) { foreach (var txId in txIds) { InventoryManager.MemoryPool.Remove(txId); } } foreach (var tx in block.ParsedTransactions) { tx.ExecInfo.RedeemedOutputs.ForEach(x => Utxos.Remove(x)); tx.ExecInfo.GeneratedOutputs.ForEach(x => Utxos.Add(x)); } Latest = block; BlockExecuted(); }
void Revert(Block block) { logger.LogWarning($@"Reverting Block {block.Height}:{ block.Id.ToString().Substring(0, 7)}."); var txIds = ( from tx in block.ParsedTransactions where !tx.ExecInfo.Coinbase select new { Id = tx.Id, Body = tx }).ToArray(); lock (InventoryManager.MemoryPool) { foreach (var tx in txIds) { InventoryManager.MemoryPool.Add(tx.Id, tx.Body); } } foreach (var tx in block.ParsedTransactions) { tx.ExecInfo.RedeemedOutputs.ForEach(x => Utxos.Add(x)); tx.ExecInfo.GeneratedOutputs.ForEach(x => Utxos.Remove(x)); } Latest = Blocks[block.PreviousHash]; BlockExecuted(); }
//Generates and returns a new transaction from this wallet. public Transaction SendFunds(ECPublicKeyParameters recipient, decimal value, Chain chain) { if(GetBalance(chain) < value) { //gather balance and check funds. Console.WriteLine("Not Enough funds to send transaction. Transaction Discarded."); return null; } //create list of inputs - adding transaction inputs until we've got enough to satify the value List<TransactionInput> inputs = new List<TransactionInput>(); decimal total = 0; foreach(var utxo in this.Utxos.Values) { total += utxo.Value; inputs.Add(new TransactionInput(utxo.Id)); if(total > value) { break; } } // Create and sign a new transaction Transaction transaction = new Transaction(PublicKey, recipient , value, inputs); transaction.Sign(PrivateKey); // And finally clear down any utxos we've used foreach(TransactionInput ti in inputs) { Utxos.Remove(ti.TransactionOutputId); } return transaction; }