Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 0517507

Browse files
committed
f Don't use rust-bitcoin types in non-model
1 parent 9491250 commit 0517507

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

integration_test/src/v28/raw_transactions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ macro_rules! impl_test_v28__submitpackage {
6565
let res = bitcoind
6666
.client
6767
.submit_package(&[tx_0, tx_1], None, None)
68+
.expect("failed to submit package")
69+
.into_model()
6870
.expect("failed to submit package");
6971
for (_, tx_result) in &res.tx_results {
7072
assert!(tx_result.error.is_some());

json/src/model/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub use self::{
3535
generating::{Generate, GenerateToAddress},
3636
network::{GetNetworkInfo, GetNetworkInfoAddress, GetNetworkInfoNetwork},
3737
raw_transactions::{
38-
SendRawTransaction, SubmitPackage, SubmitPackageTxResult, SubmitPackageTxResultFees,
38+
SendRawTransaction, SubmitPackage, SubmitPackageError, SubmitPackageTxResult,
39+
SubmitPackageTxResultFees,
3940
},
4041
wallet::{
4142
CreateWallet, GetBalance, GetBalances, GetBalancesMine, GetBalancesWatchOnly,

json/src/model/raw_transactions.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
use std::collections::HashMap;
99

10+
use bitcoin::amount::ParseAmountError;
11+
use bitcoin::hex::HexToArrayError;
1012
use bitcoin::{Amount, FeeRate, Txid, Wtxid};
1113

1214
/// Models the result of JSON-RPC method `sendrawtransaction`.
@@ -55,3 +57,16 @@ pub struct SubmitPackageTxResultFees {
5557
/// whose fees and vsizes are included in effective-feerate.
5658
pub effective_includes: Vec<Wtxid>,
5759
}
60+
61+
/// Error when converting a `SubmitPackageTxResultFees` type into the model type.
62+
#[derive(Debug)]
63+
pub enum SubmitPackageError {
64+
/// Conversion of a `Txid` failed.
65+
Txid(HexToArrayError),
66+
/// Conversion of a `Wtxid` failed.
67+
Wtxid(HexToArrayError),
68+
/// Conversion of the `base_fee` field failed.
69+
BaseFee(ParseAmountError),
70+
/// Conversion of the `vsize` field failed.
71+
Vsize,
72+
}

json/src/v28/raw_transactions.rs

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
//! Types for methods found under the `== Rawtransactions ==` section of the API docs.
66
77
use std::collections::HashMap;
8+
use std::str::FromStr;
89

910
use bitcoin::{Amount, FeeRate, Txid, Wtxid};
1011
use serde::{Deserialize, Serialize};
1112

12-
use crate::model;
13+
use crate::model::{self, SubmitPackageError};
1314

1415
/// Result of JSON-RPC method `submitpackage`.
1516
//submitpackage ["rawtx",...] ( maxfeerate maxburnamount )
@@ -70,20 +71,33 @@ pub struct SubmitPackage {
7071
pub package_msg: String,
7172
/// Transaction results keyed by [`Wtxid`].
7273
#[serde(rename = "tx-results")]
73-
pub tx_results: HashMap<Wtxid, SubmitPackageTxResult>,
74+
pub tx_results: HashMap<String, SubmitPackageTxResult>,
7475
/// List of txids of replaced transactions.
7576
#[serde(rename = "replaced-transactions")]
76-
pub replaced_transactions: Vec<Txid>,
77+
pub replaced_transactions: Vec<String>,
7778
}
7879

7980
impl SubmitPackage {
8081
/// Converts version specific type to a version in-specific, more strongly typed type.
81-
pub fn into_model(self) -> model::SubmitPackage {
82-
model::SubmitPackage {
83-
package_msg: self.package_msg,
84-
tx_results: self.tx_results.into_iter().map(|(k, v)| (k, v.into_model())).collect(),
85-
replaced_transactions: self.replaced_transactions,
82+
pub fn into_model(self) -> Result<model::SubmitPackage, model::SubmitPackageError> {
83+
let mut tx_results = HashMap::with_capacity(self.tx_results.len());
84+
for (k, v) in self.tx_results {
85+
let wtxid = Wtxid::from_str(&k).map_err(model::SubmitPackageError::Wtxid)?;
86+
let result = v.into_model()?;
87+
tx_results.insert(wtxid, result);
88+
}
89+
90+
let mut replaced_transactions = Vec::with_capacity(self.replaced_transactions.len());
91+
for t in self.replaced_transactions {
92+
let txid = Txid::from_str(&t).map_err(model::SubmitPackageError::Txid)?;
93+
replaced_transactions.push(txid);
8694
}
95+
96+
Ok(model::SubmitPackage {
97+
package_msg: self.package_msg,
98+
tx_results,
99+
replaced_transactions,
100+
})
87101
}
88102
}
89103

@@ -96,9 +110,9 @@ pub struct SubmitPackageTxResult {
96110
///
97111
/// If set, this means the submitted transaction was ignored.
98112
#[serde(rename = "other-wtxid")]
99-
pub other_wtxid: Option<Wtxid>,
113+
pub other_wtxid: Option<String>,
100114
/// Sigops-adjusted virtual transaction size.
101-
pub vsize: Option<usize>,
115+
pub vsize: Option<i64>,
102116
/// Transaction fees.
103117
pub fees: Option<SubmitPackageTxResultFees>,
104118
/// The transaction error string, if it was rejected by the mempool
@@ -107,14 +121,26 @@ pub struct SubmitPackageTxResult {
107121

108122
impl SubmitPackageTxResult {
109123
/// Converts version specific type to a version in-specific, more strongly typed type.
110-
pub fn into_model(self) -> model::SubmitPackageTxResult {
111-
model::SubmitPackageTxResult {
124+
pub fn into_model(self) -> Result<model::SubmitPackageTxResult, model::SubmitPackageError> {
125+
let other_wtxid = if let Some(other_wtxid) = self.other_wtxid {
126+
Some(Wtxid::from_str(&other_wtxid).map_err(SubmitPackageError::Wtxid)?)
127+
} else {
128+
None
129+
};
130+
131+
let vsize = if let Some(vsize) = self.vsize {
132+
Some(vsize.try_into().map_err(|_| SubmitPackageError::Vsize)?)
133+
} else {
134+
None
135+
};
136+
let fees = if let Some(fees) = self.fees { Some(fees.into_model()?) } else { None };
137+
Ok(model::SubmitPackageTxResult {
112138
txid: self.txid,
113-
other_wtxid: self.other_wtxid,
114-
vsize: self.vsize,
115-
fees: self.fees.map(move |f| f.into_model()),
139+
other_wtxid,
140+
vsize,
141+
fees,
116142
error: self.error,
117-
}
143+
})
118144
}
119145
}
120146

@@ -123,27 +149,41 @@ impl SubmitPackageTxResult {
123149
pub struct SubmitPackageTxResultFees {
124150
/// Transaction fee.
125151
#[serde(rename = "base")]
126-
pub base_fee: Amount,
152+
pub base_fee: f64,
127153
/// The effective feerate.
128154
///
129155
/// Will be `None` if the transaction was already in the mempool.
130156
///
131157
/// For example, the package feerate and/or feerate with modified fees from the `prioritisetransaction` JSON-RPC method.
132158
#[serde(rename = "effective-feerate")]
133-
pub effective_feerate: Option<FeeRate>,
159+
pub effective_feerate: Option<f64>,
134160
/// If [`Self::effective_feerate`] is provided, this holds the [`Wtxid`]s of the transactions
135161
/// whose fees and vsizes are included in effective-feerate.
136162
#[serde(rename = "effective-includes")]
137-
pub effective_includes: Vec<Wtxid>,
163+
pub effective_includes: Vec<String>,
138164
}
139165

140166
impl SubmitPackageTxResultFees {
141167
/// Converts version specific type to a version in-specific, more strongly typed type.
142-
pub fn into_model(self) -> model::SubmitPackageTxResultFees {
143-
model::SubmitPackageTxResultFees {
144-
base_fee: self.base_fee,
145-
effective_feerate: self.effective_feerate,
146-
effective_includes: self.effective_includes,
168+
pub fn into_model(self) -> Result<model::SubmitPackageTxResultFees, model::SubmitPackageError> {
169+
let mut effective_includes = Vec::with_capacity(self.effective_includes.len());
170+
for include in self.effective_includes {
171+
let wtxid = Wtxid::from_str(&include).map_err(SubmitPackageError::Wtxid)?;
172+
effective_includes.push(wtxid);
147173
}
174+
175+
let effective_feerate = self.effective_feerate.map(|rate_btc_kvb| {
176+
// Bitcoin Core gives us a feerate in BTC/KvB.
177+
// Thus, we multiply by 25_000_000 (10^8 / 4) to get satoshis/kwu.
178+
let fee_rate_sat_per_kwu = (rate_btc_kvb * 25_000_000.0).round() as u64;
179+
FeeRate::from_sat_per_kwu(fee_rate_sat_per_kwu)
180+
});
181+
182+
Ok(model::SubmitPackageTxResultFees {
183+
base_fee: Amount::from_btc(self.base_fee)
184+
.map_err(model::SubmitPackageError::BaseFee)?,
185+
effective_feerate,
186+
effective_includes,
187+
})
148188
}
149189
}

0 commit comments

Comments
 (0)