Skip to content

Commit

Permalink
Merge pull request #195 from zoedberg/electrum_fix
Browse files Browse the repository at this point in the history
electrum: add safety margins for tx height check
  • Loading branch information
dr-orlovsky authored May 6, 2024
2 parents 88504da + c58e58d commit 7c91b28
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/resolvers/electrum_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl RgbResolver for Client {
Param::String(txid.to_string()),
Param::Bool(true),
]));
let forward = iter::from_fn(|| self.block_headers_pop().ok().flatten()).count();
let forward = iter::from_fn(|| self.block_headers_pop().ok().flatten()).count() as isize;

let Some(confirmations) = tx_details.get("confirmations") else {
return Ok(witness_anchor);
Expand All @@ -101,10 +101,16 @@ impl RgbResolver for Client {
);

let tip_height = u32::try_from(header.height).map_err(|_| s!("impossible height value"))?;
let height: usize = (tip_height + 1 - confirmations) as usize;
let get_merkle_res = (0..=forward)
.find_map(|offset| self.transaction_get_merkle(&txid, height + offset).ok())
let height: isize = (tip_height - confirmations) as isize;
const SAFETY_MARGIN: isize = 1;
// first check from expected min to max height
let get_merkle_res = (1..=forward + 1)
// we need this under assumption that electrum was lying due to "DB desynchronization"
// since this have a very low probability we do that after everything else
.chain((1..=SAFETY_MARGIN).flat_map(|i| [i + forward + 1, 1 - i]))
.find_map(|offset| self.transaction_get_merkle(&txid, (height + offset) as usize).ok())
.ok_or_else(|| s!("transaction can't be located in the blockchain"))?;

let tx_height = u32::try_from(get_merkle_res.block_height)
.map_err(|_| s!("impossible height value"))?;

Expand Down

0 comments on commit 7c91b28

Please sign in to comment.