Skip to content

Commit

Permalink
Scan for SP tweaks after initial sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jlest01 committed Aug 25, 2024
1 parent 4e65557 commit 3dd7670
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 16 deletions.
34 changes: 33 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use crate::types::{HashPrefix, SerializedHashPrefixRow, SerializedHeaderRow};
#[derive(Default)]
pub(crate) struct WriteBatch {
pub(crate) tip_row: [u8; 32],
pub(crate) sp_tip_row: [u8; 32],
pub(crate) header_rows: Vec<SerializedHeaderRow>,
pub(crate) funding_rows: Vec<SerializedHashPrefixRow>,
pub(crate) spending_rows: Vec<SerializedHashPrefixRow>,
pub(crate) txid_rows: Vec<SerializedHashPrefixRow>,
pub(crate) tweak_rows: Vec<Vec<u8>>,
}

impl WriteBatch {
Expand All @@ -21,6 +23,7 @@ impl WriteBatch {
self.funding_rows.sort_unstable();
self.spending_rows.sort_unstable();
self.txid_rows.sort_unstable();
self.tweak_rows.sort_unstable();
}
}

Expand All @@ -35,11 +38,13 @@ const HEADERS_CF: &str = "headers";
const TXID_CF: &str = "txid";
const FUNDING_CF: &str = "funding";
const SPENDING_CF: &str = "spending";
const TWEAK_CF: &str = "tweak";

const COLUMN_FAMILIES: &[&str] = &[CONFIG_CF, HEADERS_CF, TXID_CF, FUNDING_CF, SPENDING_CF];
const COLUMN_FAMILIES: &[&str] = &[CONFIG_CF, HEADERS_CF, TXID_CF, FUNDING_CF, SPENDING_CF, TWEAK_CF];

const CONFIG_KEY: &str = "C";
const TIP_KEY: &[u8] = b"T";
const SP_KEY: &[u8] = b"SP";

// Taken from https://github.com/facebook/rocksdb/blob/master/include/rocksdb/db.h#L654-L689
const DB_PROPERTIES: &[&str] = &[
Expand Down Expand Up @@ -218,6 +223,10 @@ impl DBStore {
self.db.cf_handle(HEADERS_CF).expect("missing HEADERS_CF")
}

fn tweak_cf(&self) -> &rocksdb::ColumnFamily {
self.db.cf_handle(TWEAK_CF).expect("missing TWEAK_CF")
}

pub(crate) fn iter_funding(
&self,
prefix: HashPrefix,
Expand Down Expand Up @@ -270,6 +279,12 @@ impl DBStore {
.expect("get_tip failed")
}

pub(crate) fn last_sp(&self) -> Option<Vec<u8>> {
self.db
.get_cf(self.headers_cf(), SP_KEY)
.expect("last_sp failed")
}

pub(crate) fn write(&self, batch: &WriteBatch) {
let mut db_batch = rocksdb::WriteBatch::default();
for key in &batch.funding_rows {
Expand All @@ -293,6 +308,23 @@ impl DBStore {
self.db.write_opt(db_batch, &opts).unwrap();
}

pub(crate) fn write_sp(&self, batch: &WriteBatch) {
let mut db_batch = rocksdb::WriteBatch::default();

for key in &batch.tweak_rows {
if key.len() > 8 {
db_batch.put_cf(self.tweak_cf(), &key[..8], &key[8..]);
}
}
db_batch.put_cf(self.headers_cf(), SP_KEY, batch.sp_tip_row);

let mut opts = rocksdb::WriteOptions::new();
let bulk_import = self.bulk_import.load(Ordering::Relaxed);
opts.set_sync(!bulk_import);
opts.disable_wal(bulk_import);
self.db.write_opt(db_batch, &opts).unwrap();
}

pub(crate) fn flush(&self) {
debug!("flushing DB column families");
let mut config = self.get_config().unwrap_or_default();
Expand Down
13 changes: 12 additions & 1 deletion src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,18 @@ impl Rpc {
Err(response) => return response, // params parsing may fail - the response contains request id
};
self.rpc_duration.observe_duration(&call.method, || {
if self.tracker.status().is_err() {
let is_sp_indexing = self.tracker.silent_payments_index && self.tracker.sp_status().is_err();

if is_sp_indexing {
match &call.params {
Params::SpTweaks(_) => {
return error_msg(&call.id, RpcError::UnavailableIndex)
}
_ => (),
};
}

if is_sp_indexing || self.tracker.status().is_err() {
// Allow only a few RPC (for sync status notification) not requiring index DB being compacted.
match &call.params {
Params::BlockHeader(_)
Expand Down
Loading

0 comments on commit 3dd7670

Please sign in to comment.