MCPcopy Create free account
hub / github.com/PacktPublishing/Rust-for-Blockchain-Application-Development / update

Method update

chapter 5/utxoSet.rs:98–130  ·  view source on GitHub ↗
(&self, block: &Block)

Source from the content-addressed store, hash-verified

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}

Callers 2

mainFunction · 0.45
sha256_digestFunction · 0.45

Calls 10

get_dbMethod · 0.45
get_transactionsMethod · 0.45
is_coinbaseMethod · 0.45
get_vinMethod · 0.45
getMethod · 0.45
get_txidMethod · 0.45
get_voutMethod · 0.45
lenMethod · 0.45
removeMethod · 0.45
get_idMethod · 0.45

Tested by

no test coverage detected