Skip to content

Commit c27c7a6

Browse files
committed
Merge #327: Add testmempoolaccept test and update structs
7401356 Run the formatter (jamillambert) 5f9d6f5 Add testmempoolaccept test and update structs (jamillambert) 60dbb35 Fix rounding error in btc_per_kb (Jamil Lambert, PhD) Pull request description: `testmempoolaccept` is untested and missing return fields added in versions after v17. The helper function `btc_per_kb` returned a `TooPreciseError` with 1e-5 `btc_per_kb` which should work. Due to the imprecise representation of 1e-5 in binary in f64, and when divided by 1000 and converting to sats results in 0.9999999999999998 not 1.0. Rounding is not possible since it will remove all occurances of `TooPreciseError`. - Update `btc_per_kb` to do the division by 1000 after creating the `Amount` in sats. - Add a test for `testmempoolaccept` and redefine the struct, model and into_model functions for changes in the RPC for all versions up to 29. - Run the formatter ACKs for top commit: tcharding: ACK 7401356 Tree-SHA512: 8d8c26bab040458e786620843e1305e19d79c612c930fd20afd307a68b750598d5142d44d696b46afd6ba94e6b877962a22faf393bf81a8a630d6cf62eaa4e5b
2 parents a1e9faf + 7401356 commit c27c7a6

File tree

30 files changed

+835
-79
lines changed

30 files changed

+835
-79
lines changed

integration_test/tests/raw_transactions.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,34 @@ fn raw_transactions__submit_package__modelled() {
382382
}
383383

384384
#[test]
385-
#[cfg(feature = "TODO")]
386-
fn raw_transactions__test_mempool_accept__modelled() {}
385+
fn raw_transactions__test_mempool_accept__modelled() {
386+
let node = Node::with_wallet(Wallet::Default, &[]);
387+
node.fund_wallet();
388+
let tx = create_a_raw_transaction(&node);
389+
390+
// Sign (but don't broadcast).
391+
let signed: SignRawTransaction = node
392+
.client
393+
.sign_raw_transaction_with_wallet(&tx)
394+
.expect("signrawtransactionwithwallet");
395+
let signed_model: mtype::SignRawTransaction = signed
396+
.into_model()
397+
.expect("SignRawTransaction into model");
398+
let signed_tx = signed_model.tx;
399+
400+
// Call testmempoolaccept with the valid (not yet broadcast) transaction.
401+
let json: TestMempoolAccept = node
402+
.client
403+
.test_mempool_accept(&[signed_tx.clone()])
404+
.expect("testmempoolaccept");
405+
let model: mtype::TestMempoolAccept = json
406+
.into_model()
407+
.expect("TestMempoolAccept into model");
408+
assert_eq!(model.results.len(), 1);
409+
let res = &model.results[0];
410+
assert_eq!(res.txid, signed_tx.compute_txid());
411+
assert!(res.allowed, "fresh signed tx should be allowed");
412+
}
387413

388414
#[test]
389415
#[cfg(not(feature = "v17"))] // utxoupdatepsbt was added in v0.18.

types/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ impl std::error::Error for NumericError {}
8989

9090
/// Converts `fee_rate` in BTC/kB to `FeeRate`.
9191
fn btc_per_kb(btc_per_kb: f64) -> Result<Option<FeeRate>, ParseAmountError> {
92-
let btc_per_byte = btc_per_kb / 1000_f64;
93-
let sats_per_byte = Amount::from_btc(btc_per_byte)?;
92+
let sats_per_kb = Amount::from_btc(btc_per_kb)?;
93+
let sats_per_byte = sats_per_kb.to_sat() / 1000;
9494

9595
// Virtual bytes equal bytes before segwit.
96-
let rate = FeeRate::from_sat_per_vb(sats_per_byte.to_sat());
96+
let rate = FeeRate::from_sat_per_vb(sats_per_byte);
9797

9898
Ok(rate)
9999
}

types/src/model/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ pub use self::{
4242
AnalyzePsbt, AnalyzePsbtInput, AnalyzePsbtInputMissing, CombinePsbt, CombineRawTransaction,
4343
ConvertToPsbt, CreatePsbt, CreateRawTransaction, DecodePsbt, DecodeRawTransaction,
4444
DecodeScript, DescriptorProcessPsbt, FinalizePsbt, FundRawTransaction, GetRawTransaction,
45-
GetRawTransactionVerbose, JoinPsbts, MempoolAcceptance, SendRawTransaction, SignFail,
46-
SignRawTransaction, SubmitPackage, SubmitPackageTxResult, SubmitPackageTxResultFees,
47-
TestMempoolAccept, UtxoUpdatePsbt,
45+
GetRawTransactionVerbose, JoinPsbts, MempoolAcceptance, MempoolAcceptanceFees,
46+
SendRawTransaction, SignFail, SignRawTransaction, SubmitPackage, SubmitPackageTxResult,
47+
SubmitPackageTxResultFees, TestMempoolAccept, UtxoUpdatePsbt,
4848
},
4949
util::{
5050
CreateMultisig, DeriveAddresses, DeriveAddressesMultipath, EstimateSmartFee,

types/src/model/raw_transactions.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,37 @@ pub struct TestMempoolAccept {
272272
pub results: Vec<MempoolAcceptance>,
273273
}
274274

275-
/// Represents a single mempool acceptance test result.
275+
/// Models a single mempool acceptance test result. Returned as part of `testmempoolaccept`.
276276
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
277277
#[serde(deny_unknown_fields)]
278278
pub struct MempoolAcceptance {
279279
/// The transaction ID.
280280
pub txid: Txid,
281+
/// The transaction witness hash in hex.
282+
pub wtxid: Option<Wtxid>,
281283
/// If the mempool allows this transaction to be inserted.
282284
pub allowed: bool,
285+
/// Virtual transaction size as defined in BIP 141 (only present when 'allowed' is true).
286+
pub vsize: Option<u32>,
287+
/// Transaction fee in BTC (only present if 'allowed' is true).
288+
pub fees: Option<MempoolAcceptanceFees>,
283289
/// Rejection string (only present when 'allowed' is false).
284290
pub reject_reason: Option<String>,
291+
/// Rejection details (only present when 'allowed' is false and rejection details exist)
292+
pub reject_details: Option<String>,
293+
}
294+
295+
/// Models the fees field. Returned as part of `testmempoolaccept`.
296+
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
297+
#[serde(deny_unknown_fields)]
298+
pub struct MempoolAcceptanceFees {
299+
/// Transaction fee in BTC.
300+
pub base: Amount,
301+
/// The effective feerate in BTC per KvB. May differ from the base feerate if, for example, there
302+
/// are modified fees from `prioritisetransaction` or a package feerate was used.
303+
pub effective_feerate: Option<FeeRate>,
304+
/// Transactions whose fees and vsizes are included in `effective_feerate`.
305+
pub effective_includes: Option<Vec<Wtxid>>,
285306
}
286307

287308
/// Models the result of JSON-RPC method `utxoupdatepsbt;`.

types/src/v17/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
//! | sendrawtransaction | version + model | |
129129
//! | signrawtransaction | version + model | |
130130
//! | signrawtransactionwithkey | version + model | |
131-
//! | testmempoolaccept | version + model | UNTESTED |
131+
//! | testmempoolaccept | version + model | |
132132
//!
133133
//! </details>
134134
//!

types/src/v17/raw_transactions/into.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl SignFail {
449449
impl TestMempoolAccept {
450450
/// Converts version specific type to a version nonspecific, more strongly typed type.
451451
pub fn into_model(self) -> Result<model::TestMempoolAccept, hex::HexToArrayError> {
452-
let results = self.results.into_iter().map(|r| r.into_model()).collect::<Result<_, _>>()?;
452+
let results = self.0.into_iter().map(|r| r.into_model()).collect::<Result<_, _>>()?;
453453

454454
Ok(model::TestMempoolAccept { results })
455455
}
@@ -462,8 +462,12 @@ impl MempoolAcceptance {
462462

463463
Ok(model::MempoolAcceptance {
464464
txid,
465+
wtxid: None, // v22 and later only.
465466
allowed: self.allowed,
467+
vsize: None, // v21 and later only.
468+
fees: None, // v21 and later only.
466469
reject_reason: self.reject_reason,
470+
reject_details: None, // v29 and later only.
467471
})
468472
}
469473
}

types/src/v17/raw_transactions/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,9 @@ pub struct SignFail {
451451
/// > 2. allowhighfees (boolean, optional, default=false) Allow high fees
452452
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
453453
#[serde(deny_unknown_fields)]
454-
pub struct TestMempoolAccept {
455-
/// Array of test results for each raw transaction in the input array.
456-
///
457-
/// Length is exactly one for now.
458-
pub results: Vec<MempoolAcceptance>,
459-
}
454+
pub struct TestMempoolAccept(pub Vec<MempoolAcceptance>);
460455

461-
/// Represents a single mempool acceptance test result.
456+
/// Represents a single mempool acceptance test result, returned as part of `testmempoolaccept`.
462457
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
463458
#[serde(deny_unknown_fields)]
464459
pub struct MempoolAcceptance {

types/src/v18/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
//! | joinpsbts | version + model | UNTESTED |
133133
//! | sendrawtransaction | version + model | |
134134
//! | signrawtransactionwithkey | version + model | |
135-
//! | testmempoolaccept | version + model | UNTESTED |
135+
//! | testmempoolaccept | version + model | |
136136
//! | utxoupdatepsbt | version + model | UNTESTED |
137137
//!
138138
//! </details>

types/src/v19/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
//! | joinpsbts | version + model | UNTESTED |
133133
//! | sendrawtransaction | version + model | |
134134
//! | signrawtransactionwithkey | version + model | |
135-
//! | testmempoolaccept | version + model | UNTESTED |
135+
//! | testmempoolaccept | version + model | |
136136
//! | utxoupdatepsbt | version + model | UNTESTED |
137137
//!
138138
//! </details>

types/src/v20/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
//! | joinpsbts | version + model | UNTESTED |
134134
//! | sendrawtransaction | version + model | |
135135
//! | signrawtransactionwithkey | version + model | |
136-
//! | testmempoolaccept | version + model | UNTESTED |
136+
//! | testmempoolaccept | version + model | |
137137
//! | utxoupdatepsbt | version + model | UNTESTED |
138138
//!
139139
//! </details>

0 commit comments

Comments
 (0)