(&self, block: &Block)
| 96 | |
| 97 | |
| 98 | pub fn update(&self, block: &Block) { |
| 99 | let db = self.blockchain.get_db(); |
| 100 | let utxo_tree = db.open_tree(UTXO_TREE).unwrap(); |
| 101 | for tx in block.get_transactions() { |
| 102 | if tx.is_coinbase() == false { |
| 103 | for vin in tx.get_vin() { |
| 104 | let mut updated_outs = vec![]; |
| 105 | let outs_bytes = utxo_tree.get(vin.get_txid()).unwrap().unwrap(); |
| 106 | let outs: Vec<TXOutput> = bincode::deserialize(outs_bytes.as_ref()) |
| 107 | .expect("unable to deserialize TXOutput"); |
| 108 | for (idx, out) in outs.iter().enumerate() { |
| 109 | if idx != vin.get_vout() { |
| 110 | updated_outs.push(out.clone()) |
| 111 | } |
| 112 | } |
| 113 | if updated_outs.len() == 0 { |
| 114 | let _ = utxo_tree.remove(vin.get_txid()).unwrap(); |
| 115 | } else { |
| 116 | let outs_bytes = bincode::serialize(&updated_outs) |
| 117 | .expect("unable to serialize TXOutput"); |
| 118 | utxo_tree.insert(vin.get_txid(), outs_bytes).unwrap(); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | let mut new_outputs = vec![]; |
| 123 | for out in tx.get_vout() { |
| 124 | new_outputs.push(out.clone()) |
| 125 | } |
| 126 | let outs_bytes = |
| 127 | bincode::serialize(&new_outputs).expect("unable to serialize TXOutput"); |
| 128 | let _ = utxo_tree.insert(tx.get_id(), outs_bytes).unwrap(); |
| 129 | } |
| 130 | } |
| 131 | } |
no test coverage detected