db: record whether a coin was received on a change address
So we know what descriptor to use when spending it.
This commit is contained in:
6 files changed
+36
-11
No files matched your search
@@ -288,14 +288,15 @@ impl SqliteConn {
|
||||
for coin in coins {
|
||||
let deriv_index: u32 = coin.derivation_index.into();
|
||||
db_tx.execute(
|
||||
"INSERT INTO coins (wallet_id, txid, vout, amount_sat, derivation_index) \
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
"INSERT INTO coins (wallet_id, txid, vout, amount_sat, derivation_index, is_change) \
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
rusqlite::params![
|
||||
WALLET_ID,
|
||||
coin.outpoint.txid.to_vec(),
|
||||
coin.outpoint.vout,
|
||||
coin.amount.to_sat(),
|
||||
deriv_index,
|
||||
coin.is_change,
|
||||
],
|
||||
)?;
|
||||
}
|
||||
@@ -607,6 +608,7 @@ mod tests {
|
||||
block_time: None,
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(10).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: None,
|
||||
spend_block: None,
|
||||
};
|
||||
@@ -618,7 +620,7 @@ mod tests {
|
||||
assert_eq!(coins.len(), 1);
|
||||
assert_eq!(coins[0].outpoint, coin_a.outpoint);
|
||||
|
||||
// Add a second one, we'll get both.
|
||||
// Add a second one (this one is change), we'll get both.
|
||||
let coin_b = Coin {
|
||||
outpoint: bitcoin::OutPoint::from_str(
|
||||
"61db3e276b095e5b05f1849dd6bfffb4e7e5ec1c4a4210099b98fce01571936f:12",
|
||||
@@ -628,6 +630,7 @@ mod tests {
|
||||
block_time: None,
|
||||
amount: bitcoin::Amount::from_sat(1111),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(103).unwrap(),
|
||||
is_change: true,
|
||||
spend_txid: None,
|
||||
spend_block: None,
|
||||
};
|
||||
@@ -802,6 +805,7 @@ mod tests {
|
||||
block_time: None,
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(10).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: None,
|
||||
spend_block: None,
|
||||
},
|
||||
@@ -814,6 +818,7 @@ mod tests {
|
||||
block_time: Some(1_111_899),
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(100).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: None,
|
||||
spend_block: None,
|
||||
},
|
||||
@@ -826,6 +831,7 @@ mod tests {
|
||||
block_time: Some(1_121_899),
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(1000).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: Some(
|
||||
bitcoin::Txid::from_str(
|
||||
"0c62a990d20d54429e70859292e82374ba6b1b951a3ab60f26bb65fee5724ff7",
|
||||
@@ -846,6 +852,7 @@ mod tests {
|
||||
block_time: Some(1_131_899),
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(10000).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: None,
|
||||
spend_block: None,
|
||||
},
|
||||
@@ -858,6 +865,7 @@ mod tests {
|
||||
block_time: Some(1_134_899),
|
||||
amount: bitcoin::Amount::from_sat(98765),
|
||||
derivation_index: bip32::ChildNumber::from_normal_idx(100000).unwrap(),
|
||||
is_change: false,
|
||||
spend_txid: Some(
|
||||
bitcoin::Txid::from_str(
|
||||
"7477017f992cdc7ba08acafb77cb3b5bc0f42ac340d3e1e1da0785bdda20d5f6",
|
||||
|
||||
@@ -44,6 +44,7 @@ CREATE TABLE coins (
|
||||
vout INTEGER NOT NULL,
|
||||
amount_sat INTEGER NOT NULL,
|
||||
derivation_index INTEGER NOT NULL,
|
||||
is_change BOOLEAN NOT NULL CHECK (is_change IN (0,1)),
|
||||
spend_txid BLOB,
|
||||
spend_block_height INTEGER,
|
||||
spend_block_time INTEGER,
|
||||
@@ -146,6 +147,7 @@ pub struct DbCoin {
|
||||
pub block_time: Option<u32>,
|
||||
pub amount: bitcoin::Amount,
|
||||
pub derivation_index: bip32::ChildNumber,
|
||||
pub is_change: bool,
|
||||
pub spend_txid: Option<bitcoin::Txid>,
|
||||
pub spend_block: Option<DbSpendBlock>,
|
||||
}
|
||||
@@ -168,12 +170,13 @@ impl TryFrom<&rusqlite::Row<'_>> for DbCoin {
|
||||
let amount = bitcoin::Amount::from_sat(amount);
|
||||
let der_idx: u32 = row.get(7)?;
|
||||
let derivation_index = bip32::ChildNumber::from(der_idx);
|
||||
let is_change: bool = row.get(8)?;
|
||||
|
||||
let spend_txid: Option<Vec<u8>> = row.get(8)?;
|
||||
let spend_txid: Option<Vec<u8>> = row.get(9)?;
|
||||
let spend_txid =
|
||||
spend_txid.map(|txid| encode::deserialize(&txid).expect("We only store valid txids"));
|
||||
let spend_height: Option<i32> = row.get(9)?;
|
||||
let spend_time: Option<u32> = row.get(10)?;
|
||||
let spend_height: Option<i32> = row.get(10)?;
|
||||
let spend_time: Option<u32> = row.get(11)?;
|
||||
assert_eq!(spend_height.is_none(), spend_time.is_none());
|
||||
let spend_block = spend_height.map(|height| DbSpendBlock {
|
||||
height,
|
||||
@@ -188,6 +191,7 @@ impl TryFrom<&rusqlite::Row<'_>> for DbCoin {
|
||||
block_time,
|
||||
amount,
|
||||
derivation_index,
|
||||
is_change,
|
||||
spend_txid,
|
||||
spend_block,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user