Skip to content

Commit

Permalink
Merge pull request fedimint#6314 from tvolk131/bitcoin32_internal
Browse files Browse the repository at this point in the history
chore(deps): bump electrum-client to v0.21
  • Loading branch information
dpc authored Nov 8, 2024
2 parents 9b11b56 + 2e4446b commit 972c34e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 154 deletions.
31 changes: 5 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ clap = { version = "4.5.20", features = ["derive", "env"] }
cln-rpc = "0.2.0"
criterion = "0.5.1"
devimint = { path = "./devimint", version = "=0.5.0-alpha" }
electrum-client = { version = "0.18.0", features = ["use-rustls"] }
electrum-client = { version = "0.21.0", default-features = false, features = [
"proxy",
"use-rustls-ring",
] }
erased-serde = "0.4"
esplora-client = { version = "0.10.0", default-features = false, features = [
"async-https-rustls",
Expand Down
45 changes: 14 additions & 31 deletions fedimint-bitcoind/src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use anyhow::{anyhow as format_err, bail};
use bitcoin::{BlockHash, Network, ScriptBuf, Transaction, Txid};
use electrum_client::ElectrumApi;
use electrum_client::Error::Protocol;
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_block_hash, bitcoin30_to_bitcoin32_tx,
bitcoin32_to_bitcoin30_script_buf, bitcoin32_to_bitcoin30_txid,
};
use fedimint_core::envs::BitcoinRpcConfig;
use fedimint_core::runtime::block_in_place;
use fedimint_core::task::TaskHandle;
Expand Down Expand Up @@ -75,13 +71,11 @@ impl IBitcoindRpc for ElectrumClient {
async fn get_block_hash(&self, height: u64) -> anyhow::Result<BlockHash> {
let height = usize::try_from(height)?;
let result = block_in_place(|| self.client.block_headers(height, 1))?;
Ok(bitcoin30_to_bitcoin32_block_hash(
&result
.headers
.first()
.ok_or_else(|| format_err!("empty block headers response"))?
.block_hash(),
))
Ok(result
.headers
.first()
.ok_or_else(|| format_err!("empty block headers response"))?
.block_hash())
}

async fn get_fee_rate(&self, confirmation_target: u16) -> anyhow::Result<Option<Feerate>> {
Expand All @@ -107,11 +101,8 @@ impl IBitcoindRpc for ElectrumClient {
}

async fn get_tx_block_height(&self, txid: &Txid) -> anyhow::Result<Option<u64>> {
let tx = block_in_place(|| {
self.client
.transaction_get(&bitcoin32_to_bitcoin30_txid(txid))
})
.map_err(|error| info!(?error, "Unable to get raw transaction"));
let tx = block_in_place(|| self.client.transaction_get(txid))
.map_err(|error| info!(?error, "Unable to get raw transaction"));
match tx.ok() {
None => Ok(None),
Some(tx) => {
Expand All @@ -132,11 +123,8 @@ impl IBitcoindRpc for ElectrumClient {
block_hash: &BlockHash,
block_height: u64,
) -> anyhow::Result<bool> {
let tx = block_in_place(|| {
self.client
.transaction_get(&bitcoin32_to_bitcoin30_txid(txid))
})
.map_err(|error| info!(?error, "Unable to get raw transaction"));
let tx = block_in_place(|| self.client.transaction_get(txid))
.map_err(|error| info!(?error, "Unable to get raw transaction"));

match tx.ok() {
None => Ok(false),
Expand All @@ -149,10 +137,8 @@ impl IBitcoindRpc for ElectrumClient {

match block_in_place(|| self.client.script_get_history(&output.script_pubkey))?
.iter()
.find(|tx| {
tx.tx_hash == bitcoin32_to_bitcoin30_txid(txid)
&& tx.height as u64 == block_height
}) {
.find(|tx| &tx.tx_hash == txid && tx.height as u64 == block_height)
{
Some(tx) => {
let sanity_block_hash = self.get_block_hash(tx.height as u64).await?;
anyhow::ensure!(
Expand All @@ -178,14 +164,11 @@ impl IBitcoindRpc for ElectrumClient {
script: &ScriptBuf,
) -> anyhow::Result<Vec<bitcoin::Transaction>> {
let mut results = vec![];
let transactions = block_in_place(|| {
self.client
.script_get_history(&bitcoin32_to_bitcoin30_script_buf(script))
})?;
let transactions = block_in_place(|| self.client.script_get_history(script))?;
for history in transactions {
results.push(bitcoin30_to_bitcoin32_tx(&block_in_place(|| {
results.push(block_in_place(|| {
self.client.transaction_get(&history.tx_hash)
})?));
})?);
}
Ok(results)
}
Expand Down
96 changes: 0 additions & 96 deletions fedimint-core/src/bitcoin_migration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::str::FromStr;

use bitcoin::consensus::Decodable;
use bitcoin30::consensus::Encodable;
use bitcoin30::hashes::Hash;

pub fn bitcoin29_to_bitcoin32_psbt(
Expand Down Expand Up @@ -80,77 +78,6 @@ pub fn bitcoin32_to_bitcoin30_address(address: &bitcoin::Address) -> bitcoin30::
.assume_checked()
}

fn bitcoin30_to_bitcoin32_witness(witness: &bitcoin30::Witness) -> bitcoin::Witness {
let mut bytes = vec![];
witness
.consensus_encode(&mut bytes)
.expect("Failed to consensus-encode bitcoin30 witness");
let mut cursor = bitcoin::io::Cursor::new(bytes);
bitcoin::Witness::consensus_decode(&mut cursor)
.expect("Failed to convert bitcoin30 witness to bitcoin32 witness")
}

fn bitcoin30_to_bitcoin32_txin(txin: &bitcoin30::TxIn) -> bitcoin::TxIn {
bitcoin::TxIn {
previous_output: bitcoin30_to_bitcoin32_outpoint(&txin.previous_output),
script_sig: bitcoin30_to_bitcoin32_script_buf(&txin.script_sig),
sequence: bitcoin::Sequence(txin.sequence.0),
witness: bitcoin30_to_bitcoin32_witness(&txin.witness),
}
}

fn bitcoin30_to_bitcoin32_txout(txout: &bitcoin30::TxOut) -> bitcoin::TxOut {
bitcoin::TxOut {
value: bitcoin::Amount::from_sat(txout.value),
script_pubkey: bitcoin30_to_bitcoin32_script_buf(&txout.script_pubkey),
}
}

fn bitcoin30_to_bitcoin32_locktime(
locktime: bitcoin30::blockdata::locktime::absolute::LockTime,
) -> bitcoin::blockdata::locktime::absolute::LockTime {
match locktime {
bitcoin30::blockdata::locktime::absolute::LockTime::Blocks(height) => {
bitcoin::blockdata::locktime::absolute::LockTime::Blocks(
bitcoin::blockdata::locktime::absolute::Height::from_consensus(
height.to_consensus_u32(),
)
.expect("Failed to convert bitcoin30 block height locktime to bitcoin32 block height locktime"),
)
}
bitcoin30::blockdata::locktime::absolute::LockTime::Seconds(time) => {
bitcoin::blockdata::locktime::absolute::LockTime::Seconds(
bitcoin::blockdata::locktime::absolute::Time::from_consensus(time.to_consensus_u32()).expect("Failed to convert bitcoin30 timestamp locktime to bitcoin32 timestamp locktime"),
)
}
}
}

pub fn bitcoin30_to_bitcoin32_tx(tx: &bitcoin30::Transaction) -> bitcoin::Transaction {
bitcoin::Transaction {
version: bitcoin::blockdata::transaction::Version(tx.version),
lock_time: bitcoin30_to_bitcoin32_locktime(tx.lock_time),
input: tx.input.iter().map(bitcoin30_to_bitcoin32_txin).collect(),
output: tx.output.iter().map(bitcoin30_to_bitcoin32_txout).collect(),
}
}

fn bitcoin30_to_bitcoin32_script_buf(script: &bitcoin30::ScriptBuf) -> bitcoin::ScriptBuf {
bitcoin::ScriptBuf::from(script.as_bytes().to_vec())
}

pub fn bitcoin32_to_bitcoin30_script_buf(script: &bitcoin::ScriptBuf) -> bitcoin30::ScriptBuf {
bitcoin30::ScriptBuf::from(script.as_bytes().to_vec())
}

pub fn bitcoin30_to_bitcoin32_block_hash(
hash: &bitcoin30::block::BlockHash,
) -> bitcoin::block::BlockHash {
bitcoin::block::BlockHash::from_raw_hash(bitcoin30_to_bitcoin32_sha256d_hash(
&hash.to_raw_hash(),
))
}

pub fn bitcoin32_to_bitcoin30_block_hash(
hash: &bitcoin::block::BlockHash,
) -> bitcoin30::block::BlockHash {
Expand Down Expand Up @@ -188,23 +115,6 @@ pub fn bitcoin32_to_bitcoin30_network(network: &bitcoin::Network) -> bitcoin30::
}
}

fn bitcoin30_to_bitcoin32_txid(txid: &bitcoin30::Txid) -> bitcoin::Txid {
bitcoin::Txid::from_str(&txid.to_string())
.expect("Failed to convert bitcoin30 txid to bitcoin32 txid")
}

pub fn bitcoin32_to_bitcoin30_txid(txid: &bitcoin::Txid) -> bitcoin30::Txid {
bitcoin30::Txid::from_str(&txid.to_string())
.expect("Failed to convert bitcoin32 txid to bitcoin30 txid")
}

fn bitcoin30_to_bitcoin32_outpoint(outpoint: &bitcoin30::OutPoint) -> bitcoin::OutPoint {
bitcoin::OutPoint {
txid: bitcoin30_to_bitcoin32_txid(&outpoint.txid),
vout: outpoint.vout,
}
}

pub fn bitcoin30_to_bitcoin32_payment_preimage(
preimage: &lightning::ln::PaymentPreimage,
) -> lightning_types::payment::PaymentPreimage {
Expand All @@ -223,12 +133,6 @@ pub fn bitcoin32_to_bitcoin30_sha256_hash(
bitcoin30::hashes::sha256::Hash::from_slice(hash.as_ref()).expect("Invalid hash length")
}

fn bitcoin30_to_bitcoin32_sha256d_hash(
hash: &bitcoin30::hashes::sha256d::Hash,
) -> bitcoin::hashes::sha256d::Hash {
*bitcoin::hashes::sha256d::Hash::from_bytes_ref(hash.as_ref())
}

fn bitcoin32_to_bitcoin30_sha256d_hash(
hash: &bitcoin::hashes::sha256d::Hash,
) -> bitcoin30::hashes::sha256d::Hash {
Expand Down

0 comments on commit 972c34e

Please sign in to comment.