Skip to content

Commit f6f3f4a

Browse files
committed
poller: update the DB tip after a new block
1 parent 113690b commit f6f3f4a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/database/mod.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod schema;
22

33
use revault_tx::{
4-
bitcoin::{secp256k1, util::bip32, Amount, Network, OutPoint},
4+
bitcoin::{secp256k1, util::bip32, Amount, BlockHash, Network, OutPoint},
55
scripts::{CpfpDescriptor, DepositDescriptor, UnvaultDescriptor},
66
};
77
use schema::{DbInstance, DbSignature, DbVault, SigTxType, SCHEMA};
@@ -134,6 +134,23 @@ pub fn db_instance(db_path: &path::Path) -> Result<DbInstance, DatabaseError> {
134134
Ok(rows.pop().expect("No row in instances table?"))
135135
}
136136

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+
137154
/// Register a new vault to be watched. Atomically inserts the vault and the Emergency signatures.
138155
pub fn db_new_vault(
139156
db_path: &path::Path,
@@ -941,4 +958,30 @@ mod tests {
941958
// Cleanup
942959
fs::remove_file(&db_path).unwrap();
943960
}
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+
}
944987
}

src/poller.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
config::Config,
77
database::{
88
db_cancel_signatures, db_canceling_vaults, db_del_vault, db_delegated_vaults, db_instance,
9-
db_revoc_confirmed, db_should_cancel_vault, db_should_not_cancel_vault, schema::DbVault,
10-
DatabaseError,
9+
db_revoc_confirmed, db_should_cancel_vault, db_should_not_cancel_vault, db_update_tip,
10+
schema::DbVault, DatabaseError,
1111
},
1212
};
1313
use revault_tx::{
@@ -392,7 +392,7 @@ pub fn main_loop(
392392
}
393393

394394
new_block(db_path, secp, config, bitcoind, &bitcoind_tip)?;
395-
// TODO: update tip in db
395+
db_update_tip(db_path, bitcoind_tip.height, bitcoind_tip.hash)?;
396396
} else if bitcoind_tip.hash != db_instance.tip_blockhash {
397397
panic!("No reorg handling yet");
398398
}

0 commit comments

Comments
 (0)