Fix utxo selection

This commit is contained in:
Hanh 2022-12-06 15:55:24 +08:00
parent 7759cdff38
commit f55fc98ae0
1 changed files with 57 additions and 54 deletions

111
src/db.rs
View File

@ -637,61 +637,64 @@ impl DbAdapter {
) -> anyhow::Result<Vec<UTXO>> { ) -> anyhow::Result<Vec<UTXO>> {
let mut notes = vec![]; let mut notes = vec![];
let mut statement = self.connection.prepare( if !orchard {
"SELECT id_note, diversifier, value, rcm, witness FROM received_notes r, sapling_witnesses w WHERE spent IS NULL AND account = ?2 AND rho IS NULL let mut statement = self.connection.prepare(
AND (r.excluded IS NULL OR NOT r.excluded) AND w.height = ?1 AND r.orchard = ?3 "SELECT id_note, diversifier, value, rcm, witness FROM received_notes r, sapling_witnesses w WHERE spent IS NULL AND account = ?2 AND rho IS NULL
AND r.id_note = w.note")?; AND (r.excluded IS NULL OR NOT r.excluded) AND w.height = ?1
let rows = statement.query_map(params![checkpoint_height, account, orchard], |row| { AND r.id_note = w.note")?;
let id_note: u32 = row.get(0)?; let rows = statement.query_map(params![checkpoint_height, account], |row| {
let diversifier: Vec<u8> = row.get(1)?; let id_note: u32 = row.get(0)?;
let amount: i64 = row.get(2)?; let diversifier: Vec<u8> = row.get(1)?;
let rcm: Vec<u8> = row.get(3)?; let amount: i64 = row.get(2)?;
let witness: Vec<u8> = row.get(4)?; let rcm: Vec<u8> = row.get(3)?;
let source = Source::Sapling { let witness: Vec<u8> = row.get(4)?;
id_note, let source = Source::Sapling {
diversifier: diversifier.try_into().unwrap(), id_note,
rseed: rcm.try_into().unwrap(), diversifier: diversifier.try_into().unwrap(),
witness, rseed: rcm.try_into().unwrap(),
}; witness,
Ok(UTXO { };
id: id_note, Ok(UTXO {
source, id: id_note,
amount: amount as u64, source,
}) amount: amount as u64,
})?; })
for r in rows { })?;
let note = r?; for r in rows {
notes.push(note); let note = r?;
} notes.push(note);
}
let mut statement = self.connection.prepare(
"SELECT id_note, diversifier, value, rcm, rho, witness FROM received_notes r, orchard_witnesses w WHERE spent IS NULL AND account = ?2 AND rho IS NOT NULL
AND (r.excluded IS NULL OR NOT r.excluded) AND w.height = ?1
AND r.id_note = w.note")?;
let rows = statement.query_map(params![checkpoint_height, account], |row| {
let id_note: u32 = row.get(0)?;
let diversifier: Vec<u8> = row.get(1)?;
let amount: i64 = row.get(2)?;
let rcm: Vec<u8> = row.get(3)?;
let rho: Vec<u8> = row.get(4).unwrap();
let witness: Vec<u8> = row.get(5)?;
let source = Source::Orchard {
id_note,
diversifier: diversifier.try_into().unwrap(),
rseed: rcm.try_into().unwrap(),
rho: rho.try_into().unwrap(),
witness,
};
Ok(UTXO {
id: id_note,
source,
amount: amount as u64,
})
})?;
for r in rows {
let note = r?;
notes.push(note);
} }
else {
let mut statement = self.connection.prepare(
"SELECT id_note, diversifier, value, rcm, rho, witness FROM received_notes r, orchard_witnesses w WHERE spent IS NULL AND account = ?2 AND rho IS NOT NULL
AND (r.excluded IS NULL OR NOT r.excluded) AND w.height = ?1
AND r.id_note = w.note")?;
let rows = statement.query_map(params![checkpoint_height, account], |row| {
let id_note: u32 = row.get(0)?;
let diversifier: Vec<u8> = row.get(1)?;
let amount: i64 = row.get(2)?;
let rcm: Vec<u8> = row.get(3)?;
let rho: Vec<u8> = row.get(4).unwrap();
let witness: Vec<u8> = row.get(5)?;
let source = Source::Orchard {
id_note,
diversifier: diversifier.try_into().unwrap(),
rseed: rcm.try_into().unwrap(),
rho: rho.try_into().unwrap(),
witness,
};
Ok(UTXO {
id: id_note,
source,
amount: amount as u64,
})
})?;
for r in rows {
let note = r?;
notes.push(note);
}
};
Ok(notes) Ok(notes)
} }