Fix utxos in the wallet to be only latest

This commit is contained in:
Aditya Kulkarni
2019-09-13 16:02:26 -07:00
parent f532b70ca1
commit cb44b4a2a6
2 changed files with 44 additions and 39 deletions

View File

@@ -193,11 +193,10 @@ impl LightClient {
}); });
// Collect UTXOs // Collect UTXOs
let utxos = self.wallet.txs.read().unwrap().iter() let utxos = self.wallet.utxos.read().unwrap().iter()
.flat_map( |(_, wtx)| { .map(|utxo| {
wtx.utxos.iter().map(move |utxo| {
object!{ object!{
"created_in_block" => wtx.block, "created_in_block" => utxo.height,
"created_in_txid" => format!("{}", utxo.txid), "created_in_txid" => format!("{}", utxo.txid),
"value" => utxo.value, "value" => utxo.value,
"scriptkey" => hex::encode(utxo.script.clone()), "scriptkey" => hex::encode(utxo.script.clone()),
@@ -207,7 +206,6 @@ impl LightClient {
"unconfirmed_spent" => utxo.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)), "unconfirmed_spent" => utxo.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)),
} }
}) })
})
.collect::<Vec<JsonValue>>(); .collect::<Vec<JsonValue>>();
let mut res = object!{ let mut res = object!{
@@ -250,7 +248,7 @@ impl LightClient {
}); });
} }
// For each note that is not a change, add a Tx. // For each sapling note that is not a change, add a Tx.
txns.extend(v.notes.iter() txns.extend(v.notes.iter()
.filter( |nd| !nd.is_change ) .filter( |nd| !nd.is_change )
.map ( |nd| .map ( |nd|
@@ -271,6 +269,19 @@ impl LightClient {
}) })
); );
// Get the total transparent recieved
let total_transparent_received = v.utxos.iter().map(|u| u.value).sum::<u64>();
if total_transparent_received > v.total_transparent_value_spent {
// Create a input transaction for the transparent value as well.
txns.push(object!{
"block_height" => v.block,
"txid" => format!("{}", v.txid),
"amount" => total_transparent_received as i64 - v.total_transparent_value_spent as i64,
"address" => v.utxos.iter().map(|u| u.address.clone()).collect::<Vec<String>>().join(","),
"memo" => None::<String>
})
}
txns txns
}) })
.collect::<Vec<JsonValue>>(); .collect::<Vec<JsonValue>>();

View File

@@ -538,7 +538,7 @@ pub struct LightWallet {
pub tkeys: Vec<secp256k1::SecretKey>, pub tkeys: Vec<secp256k1::SecretKey>,
// Current UTXOs that can be spent // Current UTXOs that can be spent
utxos: Arc<RwLock<Vec<Utxo>>>, pub utxos: Arc<RwLock<Vec<Utxo>>>,
blocks: Arc<RwLock<Vec<BlockData>>>, blocks: Arc<RwLock<Vec<BlockData>>>,
pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>, pub txs: Arc<RwLock<HashMap<TxId, WalletTx>>>,
@@ -801,19 +801,14 @@ impl LightWallet {
} }
pub fn tbalance(&self, addr: Option<String>) -> u64 { pub fn tbalance(&self, addr: Option<String>) -> u64 {
self.txs.read().unwrap() self.utxos.read().unwrap().iter()
.values()
.map( |tx| {
tx.utxos.iter()
.filter(|utxo| { .filter(|utxo| {
match addr.clone() { match addr.clone() {
Some(a) => a == utxo.address, Some(a) => utxo.address == a,
None => true None => true,
} }
}) })
.map (|utxo| if utxo.spent.is_none() {utxo.value} else {0}) .map(|utxo| utxo.value )
.sum::<u64>()
})
.sum::<u64>() .sum::<u64>()
} }
@@ -1206,9 +1201,8 @@ impl LightWallet {
// Specifically, if you send an outgoing transaction that is sent to a shielded address, // Specifically, if you send an outgoing transaction that is sent to a shielded address,
// ZecWallet will add all your t-address funds into that transaction, and send them to your shielded // ZecWallet will add all your t-address funds into that transaction, and send them to your shielded
// address as change. // address as change.
let tinputs = self.txs.read().unwrap().iter() let tinputs = self.utxos.read().unwrap().iter()
.flat_map(|(_, wtx)| { .map(|utxo| {
wtx.utxos.iter().map(|utxo| {
let outpoint: OutPoint = utxo.to_outpoint(); let outpoint: OutPoint = utxo.to_outpoint();
let coin = TxOut { let coin = TxOut {
@@ -1218,7 +1212,7 @@ impl LightWallet {
(outpoint, coin) (outpoint, coin)
}) })
}).collect::<Vec<(OutPoint, TxOut)>>(); .collect::<Vec<(OutPoint, TxOut)>>();
if let Err(e) = match to { if let Err(e) = match to {
address::RecipientAddress::Shielded(_) => { address::RecipientAddress::Shielded(_) => {