Never delete spent utxos from the database
A `is_spent` field is added to LocalUtxo; when a txo is spent we set this field to true instead of deleting the entire utxo from the database. This allows us to create txs double-spending txs already in blockchain. Listunspent won't return spent utxos, effectively excluding them from the coin selection and balance calculation
This commit is contained in:
@@ -569,6 +569,7 @@ mod test {
|
||||
script_pubkey: Script::new(),
|
||||
},
|
||||
keychain: KeychainKind::External,
|
||||
is_spent: false,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -596,6 +597,7 @@ mod test {
|
||||
script_pubkey: Script::new(),
|
||||
},
|
||||
keychain: KeychainKind::External,
|
||||
is_spent: false,
|
||||
}),
|
||||
});
|
||||
}
|
||||
@@ -615,6 +617,7 @@ mod test {
|
||||
script_pubkey: Script::new(),
|
||||
},
|
||||
keychain: KeychainKind::External,
|
||||
is_spent: false,
|
||||
}),
|
||||
};
|
||||
vec![utxo; utxos_number]
|
||||
|
||||
@@ -387,7 +387,13 @@ where
|
||||
/// Note that this method only operates on the internal database, which first needs to be
|
||||
/// [`Wallet::sync`] manually.
|
||||
pub fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
|
||||
self.database.borrow().iter_utxos()
|
||||
Ok(self
|
||||
.database
|
||||
.borrow()
|
||||
.iter_utxos()?
|
||||
.into_iter()
|
||||
.filter(|l| !l.is_spent)
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Returns the `UTXO` owned by this wallet corresponding to `outpoint` if it exists in the
|
||||
@@ -879,6 +885,7 @@ where
|
||||
outpoint: txin.previous_output,
|
||||
txout,
|
||||
keychain,
|
||||
is_spent: true,
|
||||
};
|
||||
|
||||
Ok(WeightedUtxo {
|
||||
|
||||
@@ -838,6 +838,7 @@ mod test {
|
||||
},
|
||||
txout: Default::default(),
|
||||
keychain: KeychainKind::External,
|
||||
is_spent: false,
|
||||
},
|
||||
LocalUtxo {
|
||||
outpoint: OutPoint {
|
||||
@@ -846,6 +847,7 @@ mod test {
|
||||
},
|
||||
txout: Default::default(),
|
||||
keychain: KeychainKind::Internal,
|
||||
is_spent: false,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user