Skip to content

Commit

Permalink
Add blockchain.block.tweaks RPC method
Browse files Browse the repository at this point in the history
  • Loading branch information
jlest01 committed Aug 25, 2024
1 parent 3dd7670 commit 6df1529
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ impl DBStore {
self.iter_cf(self.headers_cf(), opts, None)
}

pub(crate) fn read_tweaks(&self, height: u64) -> Vec<(Box<[u8]>, Box<[u8]>)> {
let mut opts = rocksdb::ReadOptions::default();
opts.set_iterate_lower_bound(height.to_be_bytes());
opts.fill_cache(false);
self.db
.iterator_cf_opt(self.tweak_cf(), opts, rocksdb::IteratorMode::Start)
.map(|row| row.expect("tweak iterator failed"))
.collect()
}

pub(crate) fn get_tip(&self) -> Option<Vec<u8>> {
self.db
.get_cf(self.headers_cf(), TIP_KEY)
Expand Down
7 changes: 7 additions & 0 deletions src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ impl Rpc {
Ok(json!({"count": count, "hex": String::from_iter(hex_headers), "max": max_count}))
}

fn sp_tweaks(&self, (start_height,): (usize,)) -> Result<Value> {
Ok(json!(self.tracker.get_tweaks(start_height)?))
}

fn estimate_fee(&self, (nblocks,): (u16,)) -> Result<Value> {
Ok(self
.daemon
Expand Down Expand Up @@ -554,6 +558,7 @@ impl Rpc {
Params::Banner => Ok(json!(self.banner)),
Params::BlockHeader(args) => self.block_header(*args),
Params::BlockHeaders(args) => self.block_headers(*args),
Params::SpTweaks(args) => self.sp_tweaks(*args),
Params::Donation => Ok(Value::Null),
Params::EstimateFee(args) => self.estimate_fee(*args),
Params::Features => self.features(),
Expand Down Expand Up @@ -583,6 +588,7 @@ enum Params {
Banner,
BlockHeader((usize,)),
BlockHeaders((usize, usize)),
SpTweaks((usize,)),
TransactionBroadcast((String,)),
Donation,
EstimateFee((u16,)),
Expand All @@ -608,6 +614,7 @@ impl Params {
Ok(match method {
"blockchain.block.header" => Params::BlockHeader(convert(params)?),
"blockchain.block.headers" => Params::BlockHeaders(convert(params)?),
"blockchain.block.tweaks" => Params::SpTweaks(convert(params)?),
"blockchain.estimatefee" => Params::EstimateFee(convert(params)?),
"blockchain.headers.subscribe" => Params::HeadersSubscribe,
"blockchain.relayfee" => Params::RelayFee,
Expand Down
21 changes: 21 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use bitcoin::consensus::{deserialize, Decodable, Encodable};
use bitcoin::hex::DisplayHex;
use bitcoin::secp256k1::PublicKey;
use bitcoin::hashes::Hash;
use bitcoin::{BlockHash, OutPoint, Txid, XOnlyPublicKey};
Expand Down Expand Up @@ -167,6 +168,26 @@ impl Index {
.map(|row| HashPrefixRow::from_db_row(row).height())
.filter_map(move |height| self.chain.get_block_hash(height))
}

pub(crate) fn get_tweaks(&self, height: u64) -> impl Iterator<Item = (u64, Vec<String>)> + '_ {
self.store
.read_tweaks(height)
.into_iter()
.filter_map(move |(block_height, tweaks)| {
assert!(tweaks.len() % 33 == 0 && tweaks.len() > 0);
assert!(block_height.len() == 8);

let tweak_row_block_height =
u64::from_be_bytes(block_height[..].try_into().unwrap());
let pks = tweaks
.chunks(33)
.map(|x| format!("{}", x.as_hex()))
.collect();

Some((tweak_row_block_height, pks))
})
}

pub(crate) fn silent_payments_sync(
&mut self,
daemon: &Daemon,
Expand Down
10 changes: 10 additions & 0 deletions src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bitcoin_slices::{
Error::VisitBreak,
Visit,
};
use std::collections::HashMap;

use crate::{
cache::Cache,
Expand Down Expand Up @@ -134,4 +135,13 @@ impl Tracker {
})?;
Ok(result)
}

pub(crate) fn get_tweaks(&self, height: usize) -> Result<HashMap<u64, Vec<String>>> {
let tweaks: Vec<(u64, Vec<String>)> = self.index.get_tweaks(height as u64).collect();
let mut res: HashMap<u64, Vec<String>> = HashMap::new();
for (height, tweaks) in tweaks {
res.entry(height).or_insert_with(Vec::new).extend(tweaks)
}
Ok(res)
}
}

0 comments on commit 6df1529

Please sign in to comment.