From b519c8d185a328cff5c81c032d4fad6746704760 Mon Sep 17 00:00:00 2001 From: jp1ac4 <121959000+jp1ac4@users.noreply.github.com> Date: Tue, 4 Apr 2023 09:27:03 +0100 Subject: [PATCH] lib: rename and use existing structs `(Db)SpendBlock` --- src/commands/mod.rs | 8 ++------ src/database/mod.rs | 21 +++------------------ src/database/sqlite/mod.rs | 10 +++++----- src/database/sqlite/schema.rs | 10 ++-------- src/testutils.rs | 4 ++-- 5 files changed, 14 insertions(+), 39 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index be7324e7..7a0664f6 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -868,11 +868,7 @@ pub struct CreateRecoveryResult { #[cfg(test)] mod tests { use super::*; - use crate::{ - bitcoin::Block, - database::{BlockInfo, SpendBlock}, - testutils::*, - }; + use crate::{bitcoin::Block, database::BlockInfo, testutils::*}; use bitcoin::{ blockdata::transaction::{TxIn, TxOut}, @@ -1296,7 +1292,7 @@ mod tests { vout: 0, }, block_info: Some(BlockInfo { height: 1, time: 1 }), - spend_block: Some(SpendBlock { time: 3, height: 3 }), + spend_block: Some(BlockInfo { time: 3, height: 3 }), derivation_index: ChildNumber::from(0), amount: bitcoin::Amount::from_sat(100_000_000), spend_txid: Some(spend_tx.txid()), diff --git a/src/database/mod.rs b/src/database/mod.rs index 070ed7e7..857041c1 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -6,7 +6,7 @@ pub mod sqlite; use crate::{ bitcoin::BlockChainTip, database::sqlite::{ - schema::{DbBlockInfo, DbCoin, DbSpendBlock, DbTip}, + schema::{DbBlockInfo, DbCoin, DbTip}, SqliteConn, SqliteDb, }, }; @@ -261,21 +261,6 @@ impl DatabaseConnection for SqliteConn { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct SpendBlock { - pub height: i32, - pub time: u32, -} - -impl From for SpendBlock { - fn from(b: DbSpendBlock) -> SpendBlock { - SpendBlock { - height: b.height, - time: b.time, - } - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct BlockInfo { pub height: i32, @@ -299,7 +284,7 @@ pub struct Coin { pub derivation_index: bip32::ChildNumber, pub is_change: bool, pub spend_txid: Option, - pub spend_block: Option, + pub spend_block: Option, } impl std::convert::From for Coin { @@ -321,7 +306,7 @@ impl std::convert::From for Coin { derivation_index, is_change, spend_txid, - spend_block: spend_block.map(SpendBlock::from), + spend_block: spend_block.map(BlockInfo::from), } } } diff --git a/src/database/sqlite/mod.rs b/src/database/sqlite/mod.rs index 1c979ad2..60eca4e0 100644 --- a/src/database/sqlite/mod.rs +++ b/src/database/sqlite/mod.rs @@ -585,7 +585,7 @@ impl SqliteConn { #[cfg(test)] mod tests { use super::*; - use crate::database::{BlockInfo, DbBlockInfo, SpendBlock}; + use crate::database::{BlockInfo, DbBlockInfo}; use crate::testutils::*; use std::{ collections::{HashMap, HashSet}, @@ -1005,7 +1005,7 @@ mod tests { ) .unwrap(), ), - spend_block: Some(SpendBlock { + spend_block: Some(BlockInfo { height: 101_199, time: 1_231_678, }), @@ -1043,7 +1043,7 @@ mod tests { ) .unwrap(), ), - spend_block: Some(SpendBlock { + spend_block: Some(BlockInfo { height: 101_105, time: 1_201_678, }), @@ -1218,7 +1218,7 @@ mod tests { ) .unwrap(), ), - spend_block: Some(SpendBlock { + spend_block: Some(BlockInfo { height: 101_199, time: 1_123_000, }), @@ -1256,7 +1256,7 @@ mod tests { ) .unwrap(), ), - spend_block: Some(SpendBlock { + spend_block: Some(BlockInfo { height: 101_105, time: 1_126_000, }), diff --git a/src/database/sqlite/schema.rs b/src/database/sqlite/schema.rs index 01a4c8c9..9a5d6354 100644 --- a/src/database/sqlite/schema.rs +++ b/src/database/sqlite/schema.rs @@ -147,12 +147,6 @@ impl TryFrom<&rusqlite::Row<'_>> for DbWallet { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct DbSpendBlock { - pub height: i32, - pub time: u32, -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct DbBlockInfo { pub height: i32, @@ -169,7 +163,7 @@ pub struct DbCoin { pub derivation_index: bip32::ChildNumber, pub is_change: bool, pub spend_txid: Option, - pub spend_block: Option, + pub spend_block: Option, } impl TryFrom<&rusqlite::Row<'_>> for DbCoin { @@ -203,7 +197,7 @@ impl TryFrom<&rusqlite::Row<'_>> for DbCoin { let spend_height: Option = row.get(10)?; let spend_time: Option = row.get(11)?; assert_eq!(spend_height.is_none(), spend_time.is_none()); - let spend_block = spend_height.map(|height| DbSpendBlock { + let spend_block = spend_height.map(|height| DbBlockInfo { height, time: spend_time.expect("Must be there if height is"), }); diff --git a/src/testutils.rs b/src/testutils.rs index 82cedd85..d5d61c2b 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -1,7 +1,7 @@ use crate::{ bitcoin::{BitcoinInterface, Block, BlockChainTip, UTxO}, config::{BitcoinConfig, Config}, - database::{BlockInfo, Coin, CoinType, DatabaseConnection, DatabaseInterface, SpendBlock}, + database::{BlockInfo, Coin, CoinType, DatabaseConnection, DatabaseInterface}, descriptors, DaemonHandle, }; @@ -258,7 +258,7 @@ impl DatabaseConnection for DummyDatabase { assert!(spent.spend_txid.is_some()); assert!(spent.spend_block.is_none()); spent.spend_txid = Some(*spend_txid); - spent.spend_block = Some(SpendBlock { + spent.spend_block = Some(BlockInfo { height: *height, time: *time, });