Skip to content

Commit

Permalink
Merge #44: Add std feature
Browse files Browse the repository at this point in the history
3a72c19 Remove dependency on bitcoin-internals (Tobin C. Harding)
27dbc0c types: Add std feature (Tobin C. Harding)
c62cb3b Use BTreeMap instead of HashMap (Tobin C. Harding)

Pull request description:

  `master` is currently broken because the linter is warning because of a `std` feature (which is coming from our usage of `internals::write_err`).

  We can fix this breakage after observing that calling macros from other crates is error prone. Lets copy the macro. On the way add a `std` feature and stop using `HashMap`.

  This also allows the removal of the `internals` dependency. Sexy PR right here.

  Close: #12

ACKs for top commit:
  apoelstra:
    ACK 3a72c19; successfully ran local tests

Tree-SHA512: 0780be153283a723e0ebd4addf686304071aff8b1884e25270f5067e199840c8aff10a48a6b0bfb9f6a92b47374b9f7ac2231e6402ce58f94ce862eae03b4c18
  • Loading branch information
apoelstra committed Jan 22, 2025
2 parents 30bce38 + 3a72c19 commit 6d3be0a
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ name = "corepc-types"
version = "0.5.0"
dependencies = [
"bitcoin",
"bitcoin-internals",
"serde",
"serde_json",
]
Expand Down
1 change: 0 additions & 1 deletion Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ name = "corepc-types"
version = "0.5.0"
dependencies = [
"bitcoin",
"bitcoin-internals",
"serde",
"serde_json",
]
Expand Down
6 changes: 3 additions & 3 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ rust-version = "1.63.0"
exclude = ["tests", "contrib"]

[features]
default = []
default = ["std"]
std = ["bitcoin/std"]

[dependencies]
bitcoin = { version = "0.32.0", default-features = false, features = ["std", "serde", "base64"] }
internals = { package = "bitcoin-internals", version = "0.3.0", default-features = false, features = ["std"] }
bitcoin = { version = "0.32.0", default-features = false, features = ["serde", "base64"] }
serde = { version = "1.0.103", default-features = false, features = [ "derive", "alloc" ] }
serde_json = { version = "1.0.117" }

Expand Down
23 changes: 23 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: CC0-1.0

/// Formats error.
///
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
/// `e.source()` is only available in std builds, without this macro the error source is lost for
/// no-std builds.
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}
pub(crate) use write_err;
6 changes: 5 additions & 1 deletion types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/// Re-export the `rust-bitcoin` crate.
pub extern crate bitcoin;

extern crate alloc;

// TODO: Consider updating https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29 when this is complete.

// JSON types, for each specific version of `bitcoind`.
Expand All @@ -25,10 +27,11 @@ pub mod v26;
pub mod v27;
pub mod v28;

mod error;
// JSON types that model _all_ `bitcoind` versions.
pub mod model;

use std::fmt;
use core::fmt;

use bitcoin::amount::ParseAmountError;
use bitcoin::{Amount, FeeRate};
Expand Down Expand Up @@ -75,6 +78,7 @@ impl fmt::Display for NumericError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for NumericError {}

/// Converts `fee_rate` in BTC/kB to `FeeRate`.
Expand Down
2 changes: 1 addition & 1 deletion types/src/model/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! These structs model the types returned by the JSON-RPC API but have concrete types
//! and are not specific to a specific version of Bitcoin Core.
use std::collections::BTreeMap;
use alloc::collections::BTreeMap;

use bitcoin::address::NetworkUnchecked;
use bitcoin::{
Expand Down
2 changes: 1 addition & 1 deletion types/src/model/raw_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! These structs model the types returned by the JSON-RPC API but have concrete types
//! and are not specific to a specific version of Bitcoin Core.
use std::collections::BTreeMap;
use alloc::collections::BTreeMap;

use bitcoin::{Amount, FeeRate, Txid, Wtxid};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion types/src/model/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! These structs model the types returned by the JSON-RPC API but have concrete types
//! and are not specific to a specific version of Bitcoin Core.
use std::collections::BTreeMap;
use alloc::collections::BTreeMap;

use bitcoin::address::NetworkUnchecked;
use bitcoin::hashes::hash160;
Expand Down
15 changes: 14 additions & 1 deletion types/src/v17/blockchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use bitcoin::amount::{self, ParseAmountError};
use bitcoin::consensus::encode;
use bitcoin::error::UnprefixedHexError;
use bitcoin::{address, hex, network};
use internals::write_err;

use crate::error::write_err;
use crate::NumericError;

/// Error when converting a `GetBlockVerbosityOne` type into the model type.
Expand Down Expand Up @@ -47,6 +47,7 @@ impl fmt::Display for GetBlockVerbosityOneError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetBlockVerbosityOneError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetBlockVerbosityOneError::*;
Expand Down Expand Up @@ -94,6 +95,7 @@ impl fmt::Display for GetBlockchainInfoError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetBlockchainInfoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetBlockchainInfoError::*;
Expand Down Expand Up @@ -131,6 +133,7 @@ impl fmt::Display for GetBlockHeaderError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetBlockHeaderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetBlockHeaderError::*;
Expand Down Expand Up @@ -179,6 +182,7 @@ impl fmt::Display for GetBlockHeaderVerboseError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetBlockHeaderVerboseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetBlockHeaderVerboseError::*;
Expand Down Expand Up @@ -219,6 +223,7 @@ impl fmt::Display for GetBlockStatsError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetBlockStatsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetBlockStatsError::*;
Expand Down Expand Up @@ -254,6 +259,7 @@ impl fmt::Display for ChainTipsError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for ChainTipsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use ChainTipsError::*;
Expand Down Expand Up @@ -290,6 +296,7 @@ impl fmt::Display for GetChainTxStatsError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetChainTxStatsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetChainTxStatsError::*;
Expand Down Expand Up @@ -325,6 +332,7 @@ impl fmt::Display for MapMempoolEntryError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for MapMempoolEntryError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use MapMempoolEntryError as E;
Expand Down Expand Up @@ -369,6 +377,7 @@ impl fmt::Display for MempoolEntryError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for MempoolEntryError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use MempoolEntryError as E;
Expand Down Expand Up @@ -409,6 +418,7 @@ impl fmt::Display for MempoolEntryFeesError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for MempoolEntryFeesError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use MempoolEntryFeesError as E;
Expand Down Expand Up @@ -442,6 +452,7 @@ impl fmt::Display for GetMempoolInfoError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetMempoolInfoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetMempoolInfoError as E;
Expand Down Expand Up @@ -491,6 +502,7 @@ impl fmt::Display for GetTxOutError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetTxOutError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetTxOutError::*;
Expand Down Expand Up @@ -532,6 +544,7 @@ impl fmt::Display for GetTxOutSetInfoError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetTxOutSetInfoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetTxOutSetInfoError::*;
Expand Down
2 changes: 1 addition & 1 deletion types/src/v17/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mod error;
mod into;

use std::collections::BTreeMap;
use alloc::collections::BTreeMap;

use bitcoin::hex::FromHex;
use bitcoin::{Address, Amount, FeeRate, Network, ScriptBuf, TxMerkleNode, TxOut, Wtxid};
Expand Down
4 changes: 2 additions & 2 deletions types/src/v17/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! Types for methods found under the `== Control ==` section of the API docs.
use std::collections::HashMap;
use alloc::collections::BTreeMap;

use serde::{Deserialize, Serialize};

Expand All @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
// This just mimics the map returned by my instance of Core `v0.17`, I don't know how
// to handle other map values or if they exist?
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetMemoryInfoStats(pub HashMap<String, Locked>);
pub struct GetMemoryInfoStats(pub BTreeMap<String, Locked>);

/// Information about locked memory manager.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
Expand Down
4 changes: 3 additions & 1 deletion types/src/v17/network/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use core::fmt;

use bitcoin::amount::ParseAmountError;
use internals::write_err;

use crate::error::write_err;

/// Error when converting a `GetTransaction` type into the model type.
#[derive(Debug)]
Expand All @@ -26,6 +27,7 @@ impl fmt::Display for GetNetworkInfoError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for GetNetworkInfoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use GetNetworkInfoError as E;
Expand Down
2 changes: 1 addition & 1 deletion types/src/v17/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
mod error;
mod into;

use std::collections::BTreeMap;
use alloc::collections::BTreeMap;

use serde::{Deserialize, Serialize};

Expand Down
Loading

0 comments on commit 6d3be0a

Please sign in to comment.