Skip to content

Commit

Permalink
Merge pull request #118 from zoedberg/fix_electrum_resolver
Browse files Browse the repository at this point in the history
fix electrum Resolver
  • Loading branch information
dr-orlovsky authored Jan 30, 2024
2 parents d94d3e8 + bc4a561 commit 3d30537
Showing 1 changed file with 47 additions and 35 deletions.
82 changes: 47 additions & 35 deletions src/resolvers/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,56 @@ impl ResolveHeight for Resolver {
}

let last_block_height_min = get_block_height(&self.electrum_client)?;
let tx_details = self.electrum_client.raw_call(
"blockchain.transaction.get",
vec![Param::String(txid.to_string()), Param::Bool(true)],
)?;
let witness_ord = if let Some(confirmations) = tx_details.get("confirmations") {
let confirmations = confirmations
.as_u64()
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?;
let last_block_height_max = get_block_height(&self.electrum_client)?;
let skew = confirmations - 1;
let mut tx_height: u32 = 0;
for height in (last_block_height_min - skew)..=(last_block_height_max - skew) {
if let Ok(h) = self.electrum_client.transaction_get_merkle(
&BitcoinTxid::from_byte_array(txid.to_byte_array()),
height
.try_into()
.map_err(|_| AnchorResolverError::ImpossibleConversion)?,
) {
tx_height = h
.block_height
.try_into()
.map_err(|_| AnchorResolverError::ImpossibleConversion)?;
break;
let witness_ord = match self
.electrum_client
.raw_call("blockchain.transaction.get", vec![
Param::String(txid.to_string()),
Param::Bool(true),
]) {
Ok(tx_details) => {
if let Some(confirmations) = tx_details.get("confirmations") {
let confirmations = confirmations
.as_u64()
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?;
let last_block_height_max = get_block_height(&self.electrum_client)?;
let skew = confirmations - 1;
let mut tx_height: u32 = 0;
for height in (last_block_height_min - skew)..=(last_block_height_max - skew) {
if let Ok(h) = self.electrum_client.transaction_get_merkle(
&BitcoinTxid::from_byte_array(txid.to_byte_array()),
height
.try_into()
.map_err(|_| AnchorResolverError::ImpossibleConversion)?,
) {
tx_height = h
.block_height
.try_into()
.map_err(|_| AnchorResolverError::ImpossibleConversion)?;
break;
} else {
continue;
}
}
let block_time = tx_details
.get("blocktime")
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?
.as_i64()
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?;
WitnessOrd::OnChain(
WitnessPos::new(tx_height, block_time)
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?,
)
} else {
continue;
WitnessOrd::OffChain
}
}
let block_time = tx_details
.get("blocktime")
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?
.as_i64()
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?;
WitnessOrd::OnChain(
WitnessPos::new(tx_height, block_time)
.ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?,
)
} else {
WitnessOrd::OffChain
Err(e)
if e.to_string()
.contains("No such mempool or blockchain transaction") =>
{
WitnessOrd::OffChain
}
Err(e) => return Err(e.into()),
};

Ok(WitnessAnchor {
Expand Down

0 comments on commit 3d30537

Please sign in to comment.