|
1 | 1 | pub mod schema; |
2 | 2 |
|
3 | 3 | use revault_tx::{ |
4 | | - bitcoin::{secp256k1, util::bip32, Amount, Network, OutPoint}, |
| 4 | + bitcoin::{secp256k1, util::bip32, Amount, BlockHash, Network, OutPoint}, |
5 | 5 | scripts::{CpfpDescriptor, DepositDescriptor, UnvaultDescriptor}, |
6 | 6 | }; |
7 | 7 | use schema::{DbInstance, DbSignature, DbVault, SigTxType, SCHEMA}; |
@@ -134,6 +134,23 @@ pub fn db_instance(db_path: &path::Path) -> Result<DbInstance, DatabaseError> { |
134 | 134 | Ok(rows.pop().expect("No row in instances table?")) |
135 | 135 | } |
136 | 136 |
|
| 137 | +/// Set the new current block chain tip |
| 138 | +pub fn db_update_tip( |
| 139 | + db_path: &path::Path, |
| 140 | + height: i32, |
| 141 | + hash: BlockHash, |
| 142 | +) -> Result<(), DatabaseError> { |
| 143 | + let instance_id = db_instance(db_path)?.id; |
| 144 | + |
| 145 | + db_exec(db_path, |db_tx| { |
| 146 | + db_tx.execute( |
| 147 | + "UPDATE instances SET tip_blockheight = (?1), tip_blockhash = (?2) WHERE id = (?3)", |
| 148 | + params![height, hash.to_vec(), instance_id], |
| 149 | + )?; |
| 150 | + Ok(()) |
| 151 | + }) |
| 152 | +} |
| 153 | + |
137 | 154 | /// Register a new vault to be watched. Atomically inserts the vault and the Emergency signatures. |
138 | 155 | pub fn db_new_vault( |
139 | 156 | db_path: &path::Path, |
@@ -941,4 +958,30 @@ mod tests { |
941 | 958 | // Cleanup |
942 | 959 | fs::remove_file(&db_path).unwrap(); |
943 | 960 | } |
| 961 | + |
| 962 | + #[test] |
| 963 | + fn db_tip_update() { |
| 964 | + let db_path = get_db(); |
| 965 | + |
| 966 | + let height = 21; |
| 967 | + let hash = |
| 968 | + BlockHash::from_str("000000000000000000018dc30378a7d580c45ae3de35e046a16fec8c357a0e81") |
| 969 | + .unwrap(); |
| 970 | + db_update_tip(&db_path, height, hash).unwrap(); |
| 971 | + let instance = db_instance(&db_path).unwrap(); |
| 972 | + assert_eq!(instance.tip_blockheight, height); |
| 973 | + assert_eq!(instance.tip_blockhash, hash); |
| 974 | + |
| 975 | + let height = 22; |
| 976 | + let hash = |
| 977 | + BlockHash::from_str("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") |
| 978 | + .unwrap(); |
| 979 | + db_update_tip(&db_path, height, hash).unwrap(); |
| 980 | + let instance = db_instance(&db_path).unwrap(); |
| 981 | + assert_eq!(instance.tip_blockheight, height); |
| 982 | + assert_eq!(instance.tip_blockhash, hash); |
| 983 | + |
| 984 | + // Cleanup |
| 985 | + fs::remove_file(&db_path).unwrap(); |
| 986 | + } |
944 | 987 | } |
0 commit comments