Skip to content

Commit 6df1529

Browse files
committed
Add blockchain.block.tweaks RPC method
1 parent 3dd7670 commit 6df1529

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/db.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ impl DBStore {
273273
self.iter_cf(self.headers_cf(), opts, None)
274274
}
275275

276+
pub(crate) fn read_tweaks(&self, height: u64) -> Vec<(Box<[u8]>, Box<[u8]>)> {
277+
let mut opts = rocksdb::ReadOptions::default();
278+
opts.set_iterate_lower_bound(height.to_be_bytes());
279+
opts.fill_cache(false);
280+
self.db
281+
.iterator_cf_opt(self.tweak_cf(), opts, rocksdb::IteratorMode::Start)
282+
.map(|row| row.expect("tweak iterator failed"))
283+
.collect()
284+
}
285+
276286
pub(crate) fn get_tip(&self) -> Option<Vec<u8>> {
277287
self.db
278288
.get_cf(self.headers_cf(), TIP_KEY)

src/electrum.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ impl Rpc {
235235
Ok(json!({"count": count, "hex": String::from_iter(hex_headers), "max": max_count}))
236236
}
237237

238+
fn sp_tweaks(&self, (start_height,): (usize,)) -> Result<Value> {
239+
Ok(json!(self.tracker.get_tweaks(start_height)?))
240+
}
241+
238242
fn estimate_fee(&self, (nblocks,): (u16,)) -> Result<Value> {
239243
Ok(self
240244
.daemon
@@ -554,6 +558,7 @@ impl Rpc {
554558
Params::Banner => Ok(json!(self.banner)),
555559
Params::BlockHeader(args) => self.block_header(*args),
556560
Params::BlockHeaders(args) => self.block_headers(*args),
561+
Params::SpTweaks(args) => self.sp_tweaks(*args),
557562
Params::Donation => Ok(Value::Null),
558563
Params::EstimateFee(args) => self.estimate_fee(*args),
559564
Params::Features => self.features(),
@@ -583,6 +588,7 @@ enum Params {
583588
Banner,
584589
BlockHeader((usize,)),
585590
BlockHeaders((usize, usize)),
591+
SpTweaks((usize,)),
586592
TransactionBroadcast((String,)),
587593
Donation,
588594
EstimateFee((u16,)),
@@ -608,6 +614,7 @@ impl Params {
608614
Ok(match method {
609615
"blockchain.block.header" => Params::BlockHeader(convert(params)?),
610616
"blockchain.block.headers" => Params::BlockHeaders(convert(params)?),
617+
"blockchain.block.tweaks" => Params::SpTweaks(convert(params)?),
611618
"blockchain.estimatefee" => Params::EstimateFee(convert(params)?),
612619
"blockchain.headers.subscribe" => Params::HeadersSubscribe,
613620
"blockchain.relayfee" => Params::RelayFee,

src/index.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{Context, Result};
22
use bitcoin::consensus::{deserialize, Decodable, Encodable};
3+
use bitcoin::hex::DisplayHex;
34
use bitcoin::secp256k1::PublicKey;
45
use bitcoin::hashes::Hash;
56
use bitcoin::{BlockHash, OutPoint, Txid, XOnlyPublicKey};
@@ -167,6 +168,26 @@ impl Index {
167168
.map(|row| HashPrefixRow::from_db_row(row).height())
168169
.filter_map(move |height| self.chain.get_block_hash(height))
169170
}
171+
172+
pub(crate) fn get_tweaks(&self, height: u64) -> impl Iterator<Item = (u64, Vec<String>)> + '_ {
173+
self.store
174+
.read_tweaks(height)
175+
.into_iter()
176+
.filter_map(move |(block_height, tweaks)| {
177+
assert!(tweaks.len() % 33 == 0 && tweaks.len() > 0);
178+
assert!(block_height.len() == 8);
179+
180+
let tweak_row_block_height =
181+
u64::from_be_bytes(block_height[..].try_into().unwrap());
182+
let pks = tweaks
183+
.chunks(33)
184+
.map(|x| format!("{}", x.as_hex()))
185+
.collect();
186+
187+
Some((tweak_row_block_height, pks))
188+
})
189+
}
190+
170191
pub(crate) fn silent_payments_sync(
171192
&mut self,
172193
daemon: &Daemon,

src/tracker.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bitcoin_slices::{
55
Error::VisitBreak,
66
Visit,
77
};
8+
use std::collections::HashMap;
89

910
use crate::{
1011
cache::Cache,
@@ -134,4 +135,13 @@ impl Tracker {
134135
})?;
135136
Ok(result)
136137
}
138+
139+
pub(crate) fn get_tweaks(&self, height: usize) -> Result<HashMap<u64, Vec<String>>> {
140+
let tweaks: Vec<(u64, Vec<String>)> = self.index.get_tweaks(height as u64).collect();
141+
let mut res: HashMap<u64, Vec<String>> = HashMap::new();
142+
for (height, tweaks) in tweaks {
143+
res.entry(height).or_insert_with(Vec::new).extend(tweaks)
144+
}
145+
Ok(res)
146+
}
137147
}

0 commit comments

Comments
 (0)