Skip to content

Commit 2c1eb42

Browse files
committed
Unify ListSinceBlock and ListTransactions item
Both RPCs return the same shape sub type. Including the changes in versions between 17 and 29. ListSinceBlockTransaction has all the changes implemented up to v29. Use ListSinceBlockTransaction for both RPCs including the error and into functions. Rename it to TransactionItem to be more general. Remove ListTransactionsItem and associated error and into functions.
1 parent 2a40f7e commit 2c1eb42

File tree

31 files changed

+136
-343
lines changed

31 files changed

+136
-343
lines changed

integration_test/tests/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ fn wallet__list_since_block__modelled() {
606606
let model: Result<mtype::ListSinceBlock, ListSinceBlockError> = json.into_model();
607607
let model = model.unwrap();
608608

609-
let first_tx: mtype::ListSinceBlockTransaction = model.transactions[0].clone();
609+
let first_tx: mtype::TransactionItem = model.transactions[0].clone();
610610
assert_eq!(first_tx.txid.unwrap().to_string().len(), 64);
611611
}
612612

types/src/model/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ pub use self::{
5858
GetTransactionDetail, GetUnconfirmedBalance, GetWalletInfo, GetWalletInfoScanning, HdKey,
5959
HdKeyDescriptor, LastProcessedBlock, ListAddressGroupings, ListAddressGroupingsItem,
6060
ListLockUnspent, ListLockUnspentItem, ListReceivedByAddress, ListReceivedByAddressItem,
61-
ListReceivedByLabel, ListReceivedByLabelItem, ListSinceBlock, ListSinceBlockTransaction,
62-
ListTransactions, ListTransactionsItem, ListUnspent, ListUnspentItem, ListWallets,
63-
LoadWallet, PsbtBumpFee, RescanBlockchain, ScriptType, Send, SendAll, SendMany,
64-
SendManyVerbose, SendToAddress, SignMessage, SimulateRawTransaction, TransactionCategory,
65-
UnloadWallet, WalletCreateFundedPsbt, WalletDisplayAddress, WalletProcessPsbt,
61+
ListReceivedByLabel, ListReceivedByLabelItem, ListSinceBlock, ListTransactions,
62+
ListUnspent, ListUnspentItem, ListWallets, LoadWallet, PsbtBumpFee, RescanBlockchain,
63+
ScriptType, Send, SendAll, SendMany, SendManyVerbose, SendToAddress, SignMessage,
64+
SimulateRawTransaction, TransactionCategory, TransactionItem, UnloadWallet,
65+
WalletCreateFundedPsbt, WalletDisplayAddress, WalletProcessPsbt,
6666
},
6767
};

types/src/model/wallet.rs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,12 @@ pub struct ListReceivedByLabelItem {
590590
#[serde(deny_unknown_fields)]
591591
pub struct ListSinceBlock {
592592
/// All the transactions.
593-
pub transactions: Vec<ListSinceBlockTransaction>,
593+
pub transactions: Vec<TransactionItem>,
594594
/// Only present if `include_removed=true`.
595595
///
596596
/// Note: transactions that were re-added in the active chain will appear as-is in this array,
597597
/// and may thus have a positive confirmation count.
598-
pub removed: Vec<ListSinceBlockTransaction>,
598+
pub removed: Vec<TransactionItem>,
599599
/// The hash of the block (target_confirmations-1) from the best block on the main chain.
600600
///
601601
/// This is typically used to feed back into listsinceblock the next time you call it. So you
@@ -604,11 +604,10 @@ pub struct ListSinceBlock {
604604
pub last_block: BlockHash,
605605
}
606606

607-
/// Transaction list item, part of `ListSinceBlock`.
608-
// https://github.com/rust-bitcoin/rust-bitcoin/issues/3516
607+
/// Transaction item, part of `listsinceblock` and `listtransactions`.
609608
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
610609
#[serde(deny_unknown_fields)]
611-
pub struct ListSinceBlockTransaction {
610+
pub struct TransactionItem {
612611
/// Only returns true if imported addresses were involved in transaction.
613612
pub involves_watch_only: Option<bool>,
614613
/// The bitcoin address of the transaction.
@@ -690,58 +689,7 @@ pub struct ListSinceBlockTransaction {
690689
/// Models the result of JSON-RPC method `listtransactions`.
691690
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
692691
#[serde(deny_unknown_fields)]
693-
pub struct ListTransactions(pub Vec<ListTransactionsItem>);
694-
695-
/// Transaction list item, part of `ListTransactions`.
696-
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
697-
#[serde(deny_unknown_fields)]
698-
pub struct ListTransactionsItem {
699-
/// The bitcoin address of the transaction.
700-
pub address: Address<NetworkUnchecked>,
701-
/// The transaction category.
702-
pub category: TransactionCategory,
703-
/// The amount.
704-
///
705-
/// This is negative for the 'send' category, and is positive for the 'receive' category.
706-
#[serde(default, with = "bitcoin::amount::serde::as_btc")]
707-
pub amount: SignedAmount,
708-
/// A comment for the address/transaction, if any.
709-
pub label: Option<String>,
710-
/// The vout value.
711-
pub vout: u32,
712-
/// The amount of the fee in BTC.
713-
///
714-
/// This is negative and only available for the 'send' category of transactions.
715-
#[serde(default, with = "bitcoin::amount::serde::as_btc")]
716-
pub fee: SignedAmount,
717-
/// The number of confirmations for the transaction.
718-
///
719-
/// Negative confirmations indicate the transaction conflicts with the block chain.
720-
pub confirmations: i64,
721-
/// Whether we consider the outputs of this unconfirmed transaction safe to spend.
722-
pub trusted: bool,
723-
/// The block hash containing the transaction.
724-
pub block_hash: BlockHash,
725-
/// The index of the transaction in the block that includes it.
726-
pub block_index: u32,
727-
/// The block time in seconds since epoch (1 Jan 1970 GMT).
728-
pub block_time: u32,
729-
/// The transaction id.
730-
pub txid: Txid,
731-
/// The transaction time in seconds since epoch (Jan 1 1970 GMT).
732-
pub time: u32,
733-
/// The time received in seconds since epoch (Jan 1 1970 GMT).
734-
pub time_received: u32,
735-
/// If a comment is associated with the transaction.
736-
pub comment: Option<String>,
737-
/// Whether this transaction could be replaced due to BIP125 (replace-by-fee);
738-
/// may be unknown for unconfirmed transactions not in the mempool
739-
pub bip125_replaceable: Bip125Replaceable,
740-
/// If the transaction has been abandoned (inputs are respendable).
741-
///
742-
/// Only available for the 'send' category of transactions.
743-
pub abandoned: Option<bool>,
744-
}
692+
pub struct ListTransactions(pub Vec<TransactionItem>);
745693

746694
/// Models the result of JSON-RPC method `listunspent`.
747695
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]

types/src/v17/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ pub use self::{
281281
ListAddressGroupingsError, ListAddressGroupingsItem, ListLabels, ListLockUnspent,
282282
ListLockUnspentItem, ListLockUnspentItemError, ListReceivedByAddress,
283283
ListReceivedByAddressError, ListReceivedByAddressItem, ListSinceBlock, ListSinceBlockError,
284-
ListSinceBlockTransaction, ListSinceBlockTransactionError, ListTransactions,
285-
ListTransactionsItem, ListTransactionsItemError, ListUnspent, ListUnspentItem,
284+
ListTransactions,
285+
TransactionItem, TransactionItemError, ListUnspent, ListUnspentItem,
286286
ListUnspentItemError, ListWallets, LoadWallet, LockUnspent, RescanBlockchain, ScriptType,
287287
SendMany, SendToAddress, SetTxFee, SignMessage, TransactionCategory,
288288
WalletCreateFundedPsbt, WalletCreateFundedPsbtError, WalletProcessPsbt,

types/src/v17/wallet/error.rs

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ impl std::error::Error for ListReceivedByAddressError {
496496
#[derive(Debug)]
497497
pub enum ListSinceBlockError {
498498
/// Conversion of item in `transactions` list failed.
499-
Transactions(ListSinceBlockTransactionError),
499+
Transactions(TransactionItemError),
500500
/// Conversion of item in `removed` list failed.
501-
Removed(ListSinceBlockTransactionError),
501+
Removed(TransactionItemError),
502502
/// Conversion of the `last_block` field failed.
503503
LastBlock(hex::HexToArrayError),
504504
}
@@ -529,9 +529,9 @@ impl std::error::Error for ListSinceBlockError {
529529
}
530530
}
531531

532-
/// Error when converting a `ListSinceBlockTransaction` type into the model type.
532+
/// Error when converting a `TransactionItem` type into the model type.
533533
#[derive(Debug)]
534-
pub enum ListSinceBlockTransactionError {
534+
pub enum TransactionItemError {
535535
/// Conversion of numeric type to expected type failed.
536536
Numeric(NumericError),
537537
/// Conversion of the `address` field failed.
@@ -546,9 +546,9 @@ pub enum ListSinceBlockTransactionError {
546546
Txid(hex::HexToArrayError),
547547
}
548548

549-
impl fmt::Display for ListSinceBlockTransactionError {
549+
impl fmt::Display for TransactionItemError {
550550
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
551-
use ListSinceBlockTransactionError as E;
551+
use TransactionItemError as E;
552552

553553
match *self {
554554
E::Numeric(ref e) => write_err!(f, "numeric"; e),
@@ -562,9 +562,9 @@ impl fmt::Display for ListSinceBlockTransactionError {
562562
}
563563

564564
#[cfg(feature = "std")]
565-
impl std::error::Error for ListSinceBlockTransactionError {
565+
impl std::error::Error for TransactionItemError {
566566
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
567-
use ListSinceBlockTransactionError as E;
567+
use TransactionItemError as E;
568568

569569
match *self {
570570
E::Numeric(ref e) => Some(e),
@@ -577,59 +577,7 @@ impl std::error::Error for ListSinceBlockTransactionError {
577577
}
578578
}
579579

580-
impl From<NumericError> for ListSinceBlockTransactionError {
581-
fn from(e: NumericError) -> Self { Self::Numeric(e) }
582-
}
583-
584-
/// Error when converting a `ListTransactionsItem` type into the model type.
585-
#[derive(Debug)]
586-
pub enum ListTransactionsItemError {
587-
/// Conversion of numeric type to expected type failed.
588-
Numeric(NumericError),
589-
/// Conversion of the `address` field failed.
590-
Address(address::ParseError),
591-
/// Conversion of the `amount` field failed.
592-
Amount(ParseAmountError),
593-
/// Conversion of the `fee` field failed.
594-
Fee(ParseAmountError),
595-
/// Conversion of the `block_hash` field failed.
596-
BlockHash(hex::HexToArrayError),
597-
/// Conversion of the `txid` field failed.
598-
Txid(hex::HexToArrayError),
599-
}
600-
601-
impl fmt::Display for ListTransactionsItemError {
602-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
603-
use ListTransactionsItemError as E;
604-
605-
match *self {
606-
E::Numeric(ref e) => write_err!(f, "numeric"; e),
607-
E::Address(ref e) => write_err!(f, "conversion of the `address` field failed"; e),
608-
E::Amount(ref e) => write_err!(f, "conversion of the `amount` field failed"; e),
609-
E::Fee(ref e) => write_err!(f, "conversion of the `fee` field failed"; e),
610-
E::BlockHash(ref e) => write_err!(f, "conversion of the `block_hash` field failed"; e),
611-
E::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
612-
}
613-
}
614-
}
615-
616-
#[cfg(feature = "std")]
617-
impl std::error::Error for ListTransactionsItemError {
618-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
619-
use ListTransactionsItemError as E;
620-
621-
match *self {
622-
E::Numeric(ref e) => Some(e),
623-
E::Address(ref e) => Some(e),
624-
E::Amount(ref e) => Some(e),
625-
E::Fee(ref e) => Some(e),
626-
E::BlockHash(ref e) => Some(e),
627-
E::Txid(ref e) => Some(e),
628-
}
629-
}
630-
}
631-
632-
impl From<NumericError> for ListTransactionsItemError {
580+
impl From<NumericError> for TransactionItemError {
633581
fn from(e: NumericError) -> Self { Self::Numeric(e) }
634582
}
635583

types/src/v17/wallet/into.rs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,10 @@ impl ListSinceBlock {
578578
}
579579
}
580580

581-
impl ListSinceBlockTransaction {
581+
impl TransactionItem {
582582
/// Converts version specific type to a version nonspecific, more strongly typed type.
583-
pub fn into_model(
584-
self,
585-
) -> Result<model::ListSinceBlockTransaction, ListSinceBlockTransactionError> {
586-
use ListSinceBlockTransactionError as E;
583+
pub fn into_model(self) -> Result<model::TransactionItem, TransactionItemError> {
584+
use TransactionItemError as E;
587585

588586
let address = self.address.parse::<Address<_>>().map_err(E::Address)?;
589587
let category = self.category.into_model();
@@ -599,7 +597,7 @@ impl ListSinceBlockTransaction {
599597
let txid = self.txid.map(|s| s.parse::<Txid>().map_err(E::Txid)).transpose()?;
600598
let bip125_replaceable = self.bip125_replaceable.into_model();
601599

602-
Ok(model::ListSinceBlockTransaction {
600+
Ok(model::TransactionItem {
603601
involves_watch_only: None,
604602
address: Some(address),
605603
category,
@@ -633,50 +631,13 @@ impl ListSinceBlockTransaction {
633631

634632
impl ListTransactions {
635633
/// Converts version specific type to a version nonspecific, more strongly typed type.
636-
pub fn into_model(self) -> Result<model::ListTransactions, ListTransactionsItemError> {
634+
pub fn into_model(self) -> Result<model::ListTransactions, TransactionItemError> {
637635
let transactions =
638636
self.0.into_iter().map(|tx| tx.into_model()).collect::<Result<Vec<_>, _>>()?;
639637
Ok(model::ListTransactions(transactions))
640638
}
641639
}
642640

643-
impl ListTransactionsItem {
644-
/// Converts version specific type to a version nonspecific, more strongly typed type.
645-
pub fn into_model(self) -> Result<model::ListTransactionsItem, ListTransactionsItemError> {
646-
use ListTransactionsItemError as E;
647-
648-
let address = self.address.parse::<Address<_>>().map_err(E::Address)?;
649-
let category = self.category.into_model();
650-
let amount = SignedAmount::from_btc(self.amount).map_err(E::Amount)?;
651-
let vout = crate::to_u32(self.vout, "vout")?;
652-
let fee = SignedAmount::from_btc(self.fee).map_err(E::Fee)?;
653-
let block_hash = self.block_hash.parse::<BlockHash>().map_err(E::BlockHash)?;
654-
let block_index = crate::to_u32(self.block_index, "block_index")?;
655-
let txid = self.txid.parse::<Txid>().map_err(E::Txid)?;
656-
let bip125_replaceable = self.bip125_replaceable.into_model();
657-
658-
Ok(model::ListTransactionsItem {
659-
address,
660-
category,
661-
amount,
662-
label: self.label,
663-
vout,
664-
fee,
665-
confirmations: self.confirmations,
666-
trusted: self.trusted,
667-
block_hash,
668-
block_index,
669-
block_time: self.block_time,
670-
txid,
671-
time: self.time,
672-
time_received: self.time_received,
673-
comment: self.comment,
674-
bip125_replaceable,
675-
abandoned: self.abandoned,
676-
})
677-
}
678-
}
679-
680641
impl ListUnspent {
681642
/// Converts version specific type to a version nonspecific, more strongly typed type.
682643
pub fn into_model(self) -> Result<model::ListUnspent, ListUnspentItemError> {

0 commit comments

Comments
 (0)