Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade bitcoin rpc dependency #490

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ light-client = []

[dependencies]
thiserror = "1.0"
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "bde02d7fbf031df7d3a49946ec0e7f1abde34e58" }
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "7bd815f1e1ae721404719ee8e6867064b7c68494" }
hex = "0.4.2"
async-trait = "0.1.40"
tokio = { version = "1.0", features = ["full"] }
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/electrs/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitcoincore_rpc::bitcoin::{
consensus::encode::Error as BitcoinEncodeError, hashes::hex::Error as HexError,
util::address::Error as BitcoinAddressError,
address::Error as BitcoinAddressError, consensus::encode::Error as BitcoinEncodeError,
hashes::hex::Error as HexError,
};
use reqwest::{Error as ReqwestError, StatusCode};
use serde_json::Error as SerdeJsonError;
Expand Down
15 changes: 8 additions & 7 deletions bitcoin/src/electrs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
mod error;
mod types;

use bitcoincore_rpc::bitcoin::ScriptBuf;
pub use error::Error;
pub use types::*;

use crate::{
deserialize, opcodes, serialize, Address, Block, BlockHash, BlockHeader, Builder as ScriptBuilder, FromHex,
Network, OutPoint, Script, SignedAmount, ToHex, Transaction, Txid, H256,
Network, OutPoint, SignedAmount, Transaction, Txid, H256,
};
use futures::future::{join_all, try_join};
use reqwest::{Client, Url};
Expand Down Expand Up @@ -185,7 +186,7 @@ impl ElectrsClient {
.collect::<Result<Vec<_>, Error>>()
}

pub(crate) async fn get_script_pubkey(&self, outpoint: OutPoint) -> Result<Script, Error> {
pub(crate) async fn get_script_pubkey(&self, outpoint: OutPoint) -> Result<ScriptBuf, Error> {
let tx: TransactionValue = self
.get_and_decode(&format!("/tx/{txid}", txid = outpoint.txid))
.await?;
Expand Down Expand Up @@ -215,7 +216,7 @@ impl ElectrsClient {
let txid = self
.cli
.post(url)
.body(serialize(&tx).to_hex())
.body(hex::encode(serialize(&tx)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to ToHex?

Copy link
Member Author

@sander2 sander2 Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was giving me errors, I think because upstream made some changes, see rust-bitcoin/rust-bitcoincore-rpc#88

.send()
.await?
.error_for_status()?
Expand All @@ -232,7 +233,7 @@ impl ElectrsClient {
let mut transactions: Vec<TransactionValue> = self
.get_and_decode(&format!(
"/scripthash/{scripthash}/txs/chain/{last_seen_txid}",
scripthash = script_hash.to_hex()
scripthash = hex::encode(&script_hash)
))
.await?;
let page_size = transactions.len();
Expand All @@ -257,7 +258,7 @@ impl ElectrsClient {
) -> Result<Option<Txid>, Error> {
let script = ScriptBuilder::new()
.push_opcode(opcodes::OP_RETURN)
.push_slice(data.as_bytes())
.push_slice(&data.as_fixed_bytes())
.into_script();

let script_hash = {
Expand Down Expand Up @@ -302,11 +303,11 @@ mod tests {
async fn test_electrs(url: &str, script_hex: &str, expected_txid: &str) {
let script_bytes = Vec::from_hex(script_hex).unwrap();
let script_hash = Sha256Hash::hash(&script_bytes);
let expected_txid = Txid::from_hex(expected_txid).unwrap();
let expected_txid = Txid::from_str(expected_txid).unwrap();

let electrs_client = ElectrsClient::new(Some(url.to_owned()), Network::Bitcoin).unwrap();
let txs = electrs_client
.get_txs_by_scripthash(script_hash.to_vec())
.get_txs_by_scripthash(script_hash.to_byte_array().to_vec())
.await
.unwrap();
assert!(txs.iter().any(|tx| tx.txid.eq(&expected_txid)));
Expand Down
7 changes: 4 additions & 3 deletions bitcoin/src/electrs/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{BlockHash, Script, Txid};
use crate::{BlockHash, Txid};
use bitcoincore_rpc::bitcoin::ScriptBuf;
use serde::Deserialize;

// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/util/transaction.rs#L17-L26
Expand All @@ -19,7 +20,7 @@ pub struct TxInValue {
pub txid: Txid,
pub vout: u32,
pub prevout: Option<TxOutValue>,
pub scriptsig: Script,
pub scriptsig: ScriptBuf,
pub scriptsig_asm: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub witness: Option<Vec<String>>,
Expand All @@ -34,7 +35,7 @@ pub struct TxInValue {
// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/rest.rs#L239-L270
#[derive(Deserialize)]
pub struct TxOutValue {
pub scriptpubkey: Script,
pub scriptpubkey: ScriptBuf,
pub scriptpubkey_asm: String,
pub scriptpubkey_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{BitcoinError, BitcoinLightError, ElectrsError};
use bitcoincore_rpc::{
bitcoin::{
address::Error as AddressError,
consensus::encode::Error as BitcoinEncodeError,
hashes::{hex::Error as HashHexError, Error as HashesError},
key::Error as KeyError,
secp256k1::Error as Secp256k1Error,
util::{address::Error as AddressError, key::Error as KeyError},
},
jsonrpc::{error::RpcError, Error as JsonRpcError},
};
Expand Down Expand Up @@ -74,6 +75,8 @@ pub enum Error {
MissingBitcoinFeeInfo,
#[error("FailedToConstructWalletName")]
FailedToConstructWalletName,
#[error("AddressError: {0}")]
AddressError(#[from] AddressError),
}

impl Error {
Expand Down
10 changes: 5 additions & 5 deletions bitcoin/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ async fn get_best_block_info(rpc: &DynBitcoinCoreApi) -> Result<(u32, BlockHash)
mod tests {
use super::*;
use crate::*;
use bitcoincore_rpc::bitcoin::PackedLockTime;
pub use bitcoincore_rpc::bitcoin::{Address, Amount, Network, PublicKey, TxMerkleNode};
use bitcoincore_rpc::bitcoin::{absolute::Height, block::Version, locktime::absolute::LockTime, CompactTarget};
pub use bitcoincore_rpc::bitcoin::{Address, Amount, Network, PublicKey};
use sp_core::H256;

mockall::mock! {
Expand Down Expand Up @@ -282,7 +282,7 @@ mod tests {
fn dummy_tx(value: i32) -> Transaction {
Transaction {
version: value,
lock_time: PackedLockTime(1),
lock_time: LockTime::Blocks(Height::ZERO),
input: vec![],
output: vec![],
}
Expand All @@ -292,8 +292,8 @@ mod tests {
Block {
txdata: transactions.into_iter().map(dummy_tx).collect(),
header: BlockHeader {
version: 4,
bits: 0,
version: Version::from_consensus(4),
bits: CompactTarget::from_consensus(0),
nonce: 0,
time: 0,
prev_blockhash: next_hash,
Expand Down
Loading