Skip to content

Commit

Permalink
Update rust-elements to v0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Feb 16, 2024
1 parent a796e89 commit 85b8baf
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 151 deletions.
477 changes: 352 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bitcoin = { version = "0.31", features = [ "serde" ] }
clap = "2.33.3"
crossbeam-channel = "0.5.0"
dirs = "5.0.1"
elements = { version = "0.23.0", features = [ "serde" ], optional = true }
elements = { version = "0.24", features = [ "serde" ], optional = true }
error-chain = "0.12.4"
glob = "0.3"
hex = { package = "hex-conservative", version = "0.1.1" }
Expand Down Expand Up @@ -59,7 +59,7 @@ electrum-client = { version = "0.8", optional = true }

[dev-dependencies]
bitcoind = { version = "0.34", features = [ "25_0" ] }
elementsd = { version = "0.8.0", features = [ "22_1_1" ] }
elementsd = { version = "0.9", features = [ "22_1_1" ] }
electrumd = { version = "0.1.0", features = [ "4_1_5" ] }
ureq = { version = "2.9", default-features = false, features = [ "json" ] }
tempfile = "3.10"
Expand Down
11 changes: 1 addition & 10 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ pub enum Network {
LiquidRegtest,
}

#[cfg(feature = "liquid")]
pub const LIQUID_TESTNET_PARAMS: address::AddressParams = address::AddressParams {
p2pkh_prefix: 36,
p2sh_prefix: 19,
blinded_prefix: 23,
bech_hrp: "tex",
blech_hrp: "tlq",
};

impl Network {
#[cfg(not(feature = "liquid"))]
pub fn magic(self) -> u32 {
Expand Down Expand Up @@ -80,7 +71,7 @@ impl Network {
match self {
Network::Liquid => &address::AddressParams::LIQUID,
Network::LiquidRegtest => &address::AddressParams::ELEMENTS,
Network::LiquidTestnet => &LIQUID_TESTNET_PARAMS,
Network::LiquidTestnet => &address::AddressParams::LIQUID_TESTNET,
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,38 @@ impl From<&TxIn> for IssuanceValue {
}
}
}

// Traits to make rust-elements' types compatible with the changes made in rust-bitcoin v0.31
// Should hopefully eventually make its way into rust-elements itself.
pub mod ebcompact {
pub trait SizeMethod {
fn total_size(&self) -> usize;
}
impl SizeMethod for elements::Block {
fn total_size(&self) -> usize {
self.size()
}
}
impl SizeMethod for elements::Transaction {
fn total_size(&self) -> usize {
self.size()
}
}

pub trait ScriptMethods {
fn is_p2wpkh(&self) -> bool;
fn is_p2wsh(&self) -> bool;
fn is_p2tr(&self) -> bool;
}
impl ScriptMethods for elements::Script {
fn is_p2wpkh(&self) -> bool {
self.is_v0_p2wpkh()
}
fn is_p2wsh(&self) -> bool {
self.is_v0_p2wsh()
}
fn is_p2tr(&self) -> bool {
self.is_v1_p2tr()
}
}
}
1 change: 0 additions & 1 deletion src/elements/peg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl PegoutValue {
pub fn from_txout(txout: &TxOut, network: Network, parent_network: BNetwork) -> Option<Self> {
let pegoutdata = get_pegout_data(txout, network, parent_network)?;

// pending https://github.com/ElementsProject/rust-elements/pull/69 is merged
let scriptpubkey = pegoutdata.script_pubkey;
let address = bitcoin::Address::from_script(&scriptpubkey, parent_network).ok();

Expand Down
2 changes: 2 additions & 0 deletions src/new_index/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rayon::prelude::*;

#[cfg(feature = "liquid")]
use crate::elements::ebcompact::*;
#[cfg(not(feature = "liquid"))]
use bitcoin::consensus::encode::{deserialize, Decodable};
#[cfg(feature = "liquid")]
Expand Down
8 changes: 4 additions & 4 deletions src/new_index/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::daemon::Daemon;
use crate::errors::*;
use crate::metrics::{GaugeVec, HistogramOpts, HistogramVec, MetricOpts, Metrics};
use crate::new_index::{
compute_script_hash, schema::FullHash, ChainQuery, FundingInfo, ScriptStats, SpendingInfo,
SpendingInput, TxHistoryInfo, Utxo,
compute_script_hash, schema::FullHash, ChainQuery, FundingInfo, GetAmountVal, ScriptStats,
SpendingInfo, SpendingInput, TxHistoryInfo, Utxo,
};
use crate::util::fees::{make_fee_histogram, TxFeeInfo};
use crate::util::{extract_tx_prevouts, full_hash, has_prevout, is_spendable, Bytes};
Expand Down Expand Up @@ -380,7 +380,7 @@ impl Mempool {
vin: input_index as u16,
prev_txid: full_hash(&txi.previous_output.txid[..]),
prev_vout: txi.previous_output.vout as u16,
value: prevout.value.to_sat(),
value: prevout.value.amount_value(),
}),
)
});
Expand All @@ -399,7 +399,7 @@ impl Mempool {
TxHistoryInfo::Funding(FundingInfo {
txid: txid_bytes,
vout: index as u16,
value: txo.value.to_sat(),
value: txo.value.amount_value(),
}),
)
});
Expand Down
4 changes: 2 additions & 2 deletions src/new_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub use self::fetch::{BlockEntry, FetchFrom};
pub use self::mempool::Mempool;
pub use self::query::Query;
pub use self::schema::{
compute_script_hash, parse_hash, ChainQuery, FundingInfo, Indexer, ScriptStats, SpendingInfo,
SpendingInput, Store, TxHistoryInfo, TxHistoryKey, TxHistoryRow, Utxo,
compute_script_hash, parse_hash, ChainQuery, FundingInfo, GetAmountVal, Indexer, ScriptStats,
SpendingInfo, SpendingInput, Store, TxHistoryInfo, TxHistoryKey, TxHistoryRow, Utxo,
};
31 changes: 27 additions & 4 deletions src/new_index/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rayon::prelude::*;
use bitcoin::consensus::encode::{deserialize, serialize};
#[cfg(feature = "liquid")]
use elements::{
confidential,
encode::{deserialize, serialize},
AssetId,
};
Expand Down Expand Up @@ -112,9 +113,9 @@ pub struct Utxo {
pub value: Value,

#[cfg(feature = "liquid")]
pub asset: elements::confidential::Asset,
pub asset: confidential::Asset,
#[cfg(feature = "liquid")]
pub nonce: elements::confidential::Nonce,
pub nonce: confidential::Nonce,
#[cfg(feature = "liquid")]
pub witness: elements::TxOutWitness,
}
Expand Down Expand Up @@ -1104,7 +1105,7 @@ fn index_transaction(
TxHistoryInfo::Funding(FundingInfo {
txid,
vout: txo_index as u16,
value: txo.value.to_sat(),
value: txo.value.amount_value(),
}),
);
rows.push(history.into_row());
Expand Down Expand Up @@ -1132,7 +1133,7 @@ fn index_transaction(
vin: txi_index as u16,
prev_txid: full_hash(&txi.previous_output.txid[..]),
prev_vout: txi.previous_output.vout as u16,
value: prev_txo.value.to_sat(),
value: prev_txo.value.amount_value(),
}),
);
rows.push(history.into_row());
Expand Down Expand Up @@ -1642,3 +1643,25 @@ fn from_utxo_cache(utxos_cache: CachedUtxoMap, chain: &ChainQuery) -> UtxoMap {
})
.collect()
}

// Get the amount value as gets stored in the DB and mempool tracker.
// For bitcoin it is the Amount's inner u64, for elements it is the confidential::Value itself.
pub trait GetAmountVal {
#[cfg(not(feature = "liquid"))]
fn amount_value(self) -> u64;
#[cfg(feature = "liquid")]
fn amount_value(self) -> confidential::Value;
}

#[cfg(not(feature = "liquid"))]
impl GetAmountVal for bitcoin::Amount {
fn amount_value(self) -> u64 {
self.to_sat()
}
}
#[cfg(feature = "liquid")]
impl GetAmountVal for confidential::Value {
fn amount_value(self) -> confidential::Value {
self
}
}
10 changes: 7 additions & 3 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::str::FromStr;

#[cfg(feature = "liquid")]
use {
crate::elements::{peg::PegoutValue, AssetSorting, IssuanceValue},
crate::elements::{ebcompact::*, peg::PegoutValue, AssetSorting, IssuanceValue},
elements::{encode, secp256k1_zkp as zkp, AssetId},
};

Expand Down Expand Up @@ -159,7 +159,10 @@ impl TransactionValue {

TransactionValue {
txid: tx.txid(),
#[cfg(not(feature = "liquid"))]
version: tx.version.0 as u32,
#[cfg(feature = "liquid")]
version: tx.version as u32,
locktime: tx.lock_time.to_consensus_u32(),
vin: vins,
vout: vouts,
Expand Down Expand Up @@ -284,7 +287,7 @@ struct TxOutValue {
impl TxOutValue {
fn new(txout: &TxOut, config: &Config) -> Self {
#[cfg(not(feature = "liquid"))]
let value = txout.value;
let value = txout.value.to_sat();
#[cfg(feature = "liquid")]
let value = txout.value.explicit();

Expand Down Expand Up @@ -330,7 +333,7 @@ impl TxOutValue {
scriptpubkey_asm: script_asm,
scriptpubkey_address: script_addr,
scriptpubkey_type: script_type.to_string(),
value: value.to_sat(),
value,
#[cfg(feature = "liquid")]
valuecommitment: txout.value.commitment(),
#[cfg(feature = "liquid")]
Expand Down Expand Up @@ -1296,6 +1299,7 @@ impl From<std::string::FromUtf8Error> for HttpError {
}
}

#[cfg(not(feature = "liquid"))]
impl From<address::ParseError> for HttpError {
fn from(e: address::ParseError) -> Self {
HttpError::from(e.to_string())
Expand Down
2 changes: 2 additions & 0 deletions src/util/script.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "liquid")]
use crate::elements::ebcompact::*;
#[cfg(feature = "liquid")]
use elements::address as elements_address;

use crate::chain::{script, Network, Script, TxIn, TxOut};
Expand Down

0 comments on commit 85b8baf

Please sign in to comment.