@@ -35,7 +35,8 @@ static MIGRATIONS: &[&str] = &[
3535 "CREATE UNIQUE INDEX idx_indices_keychain ON last_derivation_indices(keychain);" ,
3636 "CREATE TABLE checksums (keychain TEXT, checksum BLOB);" ,
3737 "CREATE INDEX idx_checksums_keychain ON checksums(keychain);" ,
38- "CREATE TABLE sync_time (id INTEGER PRIMARY KEY, height INTEGER, timestamp INTEGER);"
38+ "CREATE TABLE sync_time (id INTEGER PRIMARY KEY, height INTEGER, timestamp INTEGER);" ,
39+ "ALTER TABLE utxos ADD COLUMN is_spent_unconfirmed;" ,
3940] ;
4041
4142/// Sqlite database stored on filesystem
@@ -79,14 +80,16 @@ impl SqliteDatabase {
7980 vout : u32 ,
8081 txid : & [ u8 ] ,
8182 script : & [ u8 ] ,
83+ is_spent_unconfirmed : bool ,
8284 ) -> Result < i64 , Error > {
83- let mut statement = self . connection . prepare_cached ( "INSERT INTO utxos (value, keychain, vout, txid, script) VALUES (:value, :keychain, :vout, :txid, :script)" ) ?;
85+ let mut statement = self . connection . prepare_cached ( "INSERT INTO utxos (value, keychain, vout, txid, script, is_spent_unconfirmed ) VALUES (:value, :keychain, :vout, :txid, :script, :is_spent_unconfirmed )" ) ?;
8486 statement. execute ( named_params ! {
8587 ":value" : value,
8688 ":keychain" : keychain,
8789 ":vout" : vout,
8890 ":txid" : txid,
89- ":script" : script
91+ ":script" : script,
92+ ":is_spent_unconfirmed" : is_spent_unconfirmed,
9093 } ) ?;
9194
9295 Ok ( self . connection . last_insert_rowid ( ) )
@@ -287,9 +290,9 @@ impl SqliteDatabase {
287290 }
288291
289292 fn select_utxos ( & self ) -> Result < Vec < LocalUtxo > , Error > {
290- let mut statement = self
291- . connection
292- . prepare_cached ( "SELECT value, keychain, vout, txid, script FROM utxos" ) ?;
293+ let mut statement = self . connection . prepare_cached (
294+ "SELECT value, keychain, vout, txid, script, is_spent_unconfirmed FROM utxos" ,
295+ ) ?;
293296 let mut utxos: Vec < LocalUtxo > = vec ! [ ] ;
294297 let mut rows = statement. query ( [ ] ) ?;
295298 while let Some ( row) = rows. next ( ) ? {
@@ -298,6 +301,7 @@ impl SqliteDatabase {
298301 let vout = row. get ( 2 ) ?;
299302 let txid: Vec < u8 > = row. get ( 3 ) ?;
300303 let script: Vec < u8 > = row. get ( 4 ) ?;
304+ let is_spent_unconfirmed: bool = row. get ( 5 ) ?;
301305
302306 let keychain: KeychainKind = serde_json:: from_str ( & keychain) ?;
303307
@@ -308,19 +312,16 @@ impl SqliteDatabase {
308312 script_pubkey : script. into ( ) ,
309313 } ,
310314 keychain,
315+ is_spent_unconfirmed,
311316 } )
312317 }
313318
314319 Ok ( utxos)
315320 }
316321
317- fn select_utxo_by_outpoint (
318- & self ,
319- txid : & [ u8 ] ,
320- vout : u32 ,
321- ) -> Result < Option < ( u64 , KeychainKind , Script ) > , Error > {
322+ fn select_utxo_by_outpoint ( & self , txid : & [ u8 ] , vout : u32 ) -> Result < Option < LocalUtxo > , Error > {
322323 let mut statement = self . connection . prepare_cached (
323- "SELECT value, keychain, script FROM utxos WHERE txid=:txid AND vout=:vout" ,
324+ "SELECT value, keychain, script, is_spent_unconfirmed FROM utxos WHERE txid=:txid AND vout=:vout" ,
324325 ) ?;
325326 let mut rows = statement. query ( named_params ! { ":txid" : txid, ":vout" : vout} ) ?;
326327 match rows. next ( ) ? {
@@ -329,9 +330,18 @@ impl SqliteDatabase {
329330 let keychain: String = row. get ( 1 ) ?;
330331 let keychain: KeychainKind = serde_json:: from_str ( & keychain) ?;
331332 let script: Vec < u8 > = row. get ( 2 ) ?;
332- let script: Script = script. into ( ) ;
333+ let script_pubkey: Script = script. into ( ) ;
334+ let is_spent_unconfirmed: bool = row. get ( 3 ) ?;
333335
334- Ok ( Some ( ( value, keychain, script) ) )
336+ Ok ( Some ( LocalUtxo {
337+ outpoint : OutPoint :: new ( deserialize ( & txid) ?, vout) ,
338+ txout : TxOut {
339+ value,
340+ script_pubkey,
341+ } ,
342+ keychain,
343+ is_spent_unconfirmed,
344+ } ) )
335345 }
336346 None => Ok ( None ) ,
337347 }
@@ -624,6 +634,7 @@ impl BatchOperations for SqliteDatabase {
624634 utxo. outpoint . vout ,
625635 & utxo. outpoint . txid ,
626636 utxo. txout . script_pubkey . as_bytes ( ) ,
637+ utxo. is_spent_unconfirmed ,
627638 ) ?;
628639 Ok ( ( ) )
629640 }
@@ -698,16 +709,9 @@ impl BatchOperations for SqliteDatabase {
698709
699710 fn del_utxo ( & mut self , outpoint : & OutPoint ) -> Result < Option < LocalUtxo > , Error > {
700711 match self . select_utxo_by_outpoint ( & outpoint. txid , outpoint. vout ) ? {
701- Some ( ( value , keychain , script_pubkey ) ) => {
712+ Some ( local_utxo ) => {
702713 self . delete_utxo_by_outpoint ( & outpoint. txid , outpoint. vout ) ?;
703- Ok ( Some ( LocalUtxo {
704- outpoint : * outpoint,
705- txout : TxOut {
706- value,
707- script_pubkey,
708- } ,
709- keychain,
710- } ) )
714+ Ok ( Some ( local_utxo) )
711715 }
712716 None => Ok ( None ) ,
713717 }
@@ -836,17 +840,7 @@ impl Database for SqliteDatabase {
836840 }
837841
838842 fn get_utxo ( & self , outpoint : & OutPoint ) -> Result < Option < LocalUtxo > , Error > {
839- match self . select_utxo_by_outpoint ( & outpoint. txid , outpoint. vout ) ? {
840- Some ( ( value, keychain, script_pubkey) ) => Ok ( Some ( LocalUtxo {
841- outpoint : * outpoint,
842- txout : TxOut {
843- value,
844- script_pubkey,
845- } ,
846- keychain,
847- } ) ) ,
848- None => Ok ( None ) ,
849- }
843+ self . select_utxo_by_outpoint ( & outpoint. txid , outpoint. vout )
850844 }
851845
852846 fn get_raw_tx ( & self , txid : & Txid ) -> Result < Option < Transaction > , Error > {
0 commit comments