Skip to content

Commit 676d135

Browse files
committed
add wallet checkpoint for balance updates
1 parent 231cd36 commit 676d135

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/v2/src/main.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bdk_sp::{
1111
address::NetworkUnchecked,
1212
bip32,
1313
consensus::{deserialize, Decodable},
14+
hashes::Hash,
1415
hex::{DisplayHex, FromHex},
1516
key::Secp256k1,
1617
script::PushBytesBuf,
@@ -578,6 +579,10 @@ async fn main() -> anyhow::Result<()> {
578579
}
579580
}
580581
Commands::ScanFrigate { rpc_args, start } => {
582+
// The implementation done here differ from what mentionned in the section https://github.com/sparrowwallet/frigate/tree/master?tab=readme-ov-file#blockchainsilentpaymentssubscribe
583+
// We are doing a one time scanning only. So instead of calling `blockchain.scripthash.subscribe` on each script from the wallet,
584+
// we just subscribe and read the scanning result from the stream. On each result received we update the wallet state and once scanning progress reaches 1.0 (100%) we stop.
585+
581586
let mut client = FrigateClient::connect(&rpc_args.url).await.unwrap();
582587
let labels = wallet
583588
.indexer()
@@ -644,23 +649,34 @@ async fn main() -> anyhow::Result<()> {
644649
let mut block: Block =
645650
deserialize(&Vec::<u8>::from_hex(&raw_blk).unwrap()).unwrap();
646651

652+
let mut blockhash = BlockHash::all_zeros();
653+
647654
let mut txs: Vec<Transaction> = vec![coinbase];
648655
for key in secret.1.keys() {
649-
let raw_tx = client.get_transaction(key.to_string()).await.unwrap();
656+
let tx_result = client.get_transaction(key.to_string()).await.unwrap();
650657
let tx: Transaction =
651-
deserialize(&Vec::<u8>::from_hex(&raw_tx).unwrap()).unwrap();
658+
deserialize(&Vec::<u8>::from_hex(&tx_result.1).unwrap()).unwrap();
652659
txs.push(tx);
660+
661+
blockhash = BlockHash::from_str(&tx_result.0)?;
653662
}
654663

655664
block.txdata = txs;
656665
tracing::debug!("Final block {:?}", block);
657666
wallet.apply_block_relevant(&block, secret.1, secret.0);
667+
668+
tracing::debug!("Checkpoint hash {blockhash:?}");
669+
let checkpoint = wallet.chain().tip().insert(BlockId {
670+
height: secret.0,
671+
hash: blockhash,
672+
});
673+
wallet.update_chain(checkpoint);
658674
}
659675

660676
tracing::info!("Progress {progress}");
661677
// Check the progress
662678
if progress >= 1.0 {
663-
tracing::warn!("Scanning completed");
679+
tracing::info!("Scanning completed");
664680
break;
665681
}
666682
}

oracles/src/frigate/mod.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub struct RequestPayload {
8888
}
8989

9090
const SUBSCRIBE_RPC_METHOD: &str = "blockchain.silentpayments.subscribe";
91-
const _UNSUBSCRIBE_RPC_METHOD: &str = "blockchain.silentpayments.unsubscribe";
91+
const UNSUBSCRIBE_RPC_METHOD: &str = "blockchain.silentpayments.unsubscribe";
9292
const GET_RPC_METHOD: &str = "blockchain.transaction.get";
9393
const VERSION_RPC_METHOD: &str = "server.version";
9494
const _SUBSCRIBE_OWNED_OUTPUTS: &str = "blockchain.scripthash.subscribe";
@@ -109,7 +109,7 @@ impl FrigateClient {
109109
let mut buffer = vec![0; size];
110110
let n = self.client.read(&mut buffer).await?;
111111

112-
tracing::warn!("Read bytes from stream {n}");
112+
tracing::debug!("Read bytes from stream {n}");
113113
match n {
114114
0 => Err(FrigateError::Generic("Nothing read".to_string())),
115115
_ => {
@@ -121,6 +121,7 @@ impl FrigateClient {
121121
}
122122
}
123123
}
124+
124125
async fn send_request(&mut self, req_bytes: &[u8]) -> Result<Value, FrigateError> {
125126
self.client.write_all(req_bytes).await?;
126127

@@ -145,8 +146,11 @@ impl FrigateClient {
145146
Ok(String::from(res["result"].as_str().unwrap()))
146147
}
147148

148-
pub async fn get_transaction(&mut self, txid: String) -> Result<String, FrigateError> {
149-
let params = vec![txid];
149+
pub async fn get_transaction(
150+
&mut self,
151+
txid: String,
152+
) -> Result<(String, String), FrigateError> {
153+
let params = vec![txid, "true".to_string()];
150154
let req = RequestPayload {
151155
method: GET_RPC_METHOD.to_string(),
152156
params: serde_json::json!(params),
@@ -156,8 +160,10 @@ impl FrigateClient {
156160
let req_bytes = serde_json::to_vec(&req)?;
157161
let res = self.send_request(&req_bytes).await?;
158162

159-
tracing::debug!("[Get tx Request] Result {:?}", res);
160-
Ok(String::from(res["result"].as_str().unwrap()))
163+
tracing::debug!("[Get tx Request] Result {:#?}", res);
164+
let blockhash = String::from(res["result"]["blockhash"].as_str().unwrap());
165+
let hex = String::from(res["result"]["hex"].as_str().unwrap());
166+
Ok((blockhash, hex))
161167
}
162168

163169
pub async fn version(&mut self) -> Result<(), FrigateError> {
@@ -219,4 +225,23 @@ impl FrigateClient {
219225

220226
Ok(None)
221227
}
228+
229+
pub async fn unsubscribe(&mut self, req: &UnsubscribeRequest) -> Result<String, FrigateError> {
230+
let params: Vec<Value> = vec![
231+
serde_json::json!(req.scan_privkey),
232+
serde_json::json!(req.spend_pubkey),
233+
];
234+
235+
let req = RequestPayload {
236+
method: UNSUBSCRIBE_RPC_METHOD.to_string(),
237+
params: serde_json::json!(params),
238+
id: serde_json::Value::from(189),
239+
jsonrpc: "2.0".to_string(),
240+
};
241+
242+
let req_bytes = serde_json::to_vec(&req)?;
243+
let result = self.send_request(&req_bytes).await?;
244+
245+
Ok(result["result"].to_string())
246+
}
222247
}

0 commit comments

Comments
 (0)