Skip to content

Commit f1b5dbf

Browse files
committed
Merge #522: Fix types conversion error variants
9ca8e04 Fix types conversion error variants (Jamil Lambert, PhD) d2fce89 Remove duplicate error (Jamil Lambert, PhD) Pull request description: Each error variant should be for an individual field. Use an LLM to go through all into.rs files to address the issue, spend longer than doing the job from scratch myself fixing all its errors. - Remove a redefinition of an error in v30 that is unchanged from the v29 definition. - Add new variants where one variant was used for more than one field. Rename existing variants and update error messages to match the field names. Closes #521 ACKs for top commit: tcharding: ACK 9ca8e04 Tree-SHA512: 32d10cc5fa8a1eef6fe2d498be19eb802a6a01624b0ec795fb41a6a9953cb2504dd126b5c1d5195adb3380dae5e67e8d03b21b2bb6bad2c86a121fea650987d7
2 parents 9f2be16 + 9ca8e04 commit f1b5dbf

15 files changed

Lines changed: 64 additions & 118 deletions

File tree

types/src/v17/blockchain/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,14 @@ pub enum GetBlockHeaderError {
118118
/// Conversion of hex data to bytes failed.
119119
Hex(hex::HexToBytesError),
120120
/// Consensus decoding of bytes to header failed.
121-
Consensus(encode::Error),
121+
Header(encode::Error),
122122
}
123123

124124
impl fmt::Display for GetBlockHeaderError {
125125
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126126
match *self {
127127
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
128-
Self::Consensus(ref e) =>
129-
write_err!(f, "consensus decoding of bytes to header failed"; e),
128+
Self::Header(ref e) => write_err!(f, "consensus decoding of bytes to header failed"; e),
130129
}
131130
}
132131
}
@@ -136,7 +135,7 @@ impl std::error::Error for GetBlockHeaderError {
136135
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
137136
match *self {
138137
Self::Hex(ref e) => Some(e),
139-
Self::Consensus(ref e) => Some(e),
138+
Self::Header(ref e) => Some(e),
140139
}
141140
}
142141
}

types/src/v17/blockchain/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl GetBlockHeader {
163163
use GetBlockHeaderError as E;
164164

165165
let v = Vec::from_hex(&self.0).map_err(E::Hex)?;
166-
let header = encode::deserialize::<block::Header>(&v).map_err(E::Consensus)?;
166+
let header = encode::deserialize::<block::Header>(&v).map_err(E::Header)?;
167167

168168
Ok(model::GetBlockHeader(header))
169169
}

types/src/v17/mining/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub enum BlockTemplateTransactionError {
6565
Data(consensus::encode::FromHexError),
6666
/// Conversion of the `txid` field failed.
6767
Txid(hex::HexToArrayError),
68-
/// Conversion of the `hash` field failed.
68+
/// Conversion of the `hash` field to `wtxid` failed.
6969
Hash(hex::HexToArrayError),
7070
/// Conversion of the `fee` field failed.
7171
Fee(ParseAmountError),
@@ -77,7 +77,8 @@ impl fmt::Display for BlockTemplateTransactionError {
7777
Self::Numeric(ref e) => write_err!(f, "numeric"; e),
7878
Self::Data(ref e) => write_err!(f, "conversion of the `data` field failed"; e),
7979
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
80-
Self::Hash(ref e) => write_err!(f, "conversion of the `hash` field failed"; e),
80+
Self::Hash(ref e) =>
81+
write_err!(f, "conversion of the `hash` field to `wtxid` failed"; e),
8182
Self::Fee(ref e) => write_err!(f, "conversion of the `fee` field failed"; e),
8283
}
8384
}

types/src/v17/raw_transactions/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::psbt::{
1616
/// Error when converting a `DecodePsbt` type into the model type.
1717
#[derive(Debug)]
1818
pub enum DecodePsbtError {
19-
/// Conversion of the `tx` field failed.
19+
/// Conversion of the `tx` field to `unsigned_tx` failed.
2020
Tx(RawTransactionError),
2121
/// Conversion of one the map items in the `unknown` field failed.
2222
Unknown(hex::HexToBytesError),
@@ -31,7 +31,8 @@ pub enum DecodePsbtError {
3131
impl fmt::Display for DecodePsbtError {
3232
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3333
match *self {
34-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
34+
Self::Tx(ref e) =>
35+
write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3536
Self::Unknown(ref e) =>
3637
write_err!(f, "conversion of one the map items in the `unknown` field failed"; e),
3738
Self::Inputs(ref e) => write_err!(f, "conversion of one of the PSBT inputs failed"; e),

types/src/v23/raw_transactions/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::error::write_err;
1111
/// Error when converting a `DecodePsbt` type into the model type.
1212
#[derive(Debug)]
1313
pub enum DecodePsbtError {
14-
/// Conversion of the `tx` field failed.
14+
/// Conversion of the `tx` field to `unsigned_tx` failed.
1515
Tx(RawTransactionError),
1616
/// Conversion of the `global_xpubs` field failed.
1717
GlobalXpubs(GlobalXpubError),
@@ -30,7 +30,8 @@ pub enum DecodePsbtError {
3030
impl fmt::Display for DecodePsbtError {
3131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3232
match *self {
33-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
33+
Self::Tx(ref e) =>
34+
write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3435
Self::GlobalXpubs(ref e) =>
3536
write_err!(f, "conversion of one the map items in the `global_xbubs` field failed"; e),
3637
Self::Proprietary(ref e) =>

types/src/v24/raw_transactions/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::error::write_err;
1212
/// Error when converting a `DecodePsbt` type into the model type.
1313
#[derive(Debug)]
1414
pub enum DecodePsbtError {
15-
/// Conversion of the `tx` field failed.
15+
/// Conversion of the `tx` field to `unsigned_tx` failed.
1616
Tx(RawTransactionError),
1717
/// Conversion of the `global_xpubs` field failed.
1818
GlobalXpubs(GlobalXpubError),
@@ -31,7 +31,8 @@ pub enum DecodePsbtError {
3131
impl fmt::Display for DecodePsbtError {
3232
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3333
match *self {
34-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
34+
Self::Tx(ref e) =>
35+
write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3536
Self::GlobalXpubs(ref e) =>
3637
write_err!(f, "conversion of one the map items in the `global_xbubs` field failed"; e),
3738
Self::Proprietary(ref e) =>

types/src/v29/blockchain/error.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,14 @@ pub enum GetBlockHeaderError {
304304
/// Conversion of hex data to bytes failed.
305305
Hex(hex::HexToBytesError),
306306
/// Consensus decoding of bytes to header failed.
307-
Consensus(encode::Error),
307+
Header(encode::Error),
308308
}
309309

310310
impl fmt::Display for GetBlockHeaderError {
311311
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
312312
match *self {
313313
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
314-
Self::Consensus(ref e) =>
315-
write_err!(f, "consensus decoding of bytes to header failed"; e),
314+
Self::Header(ref e) => write_err!(f, "consensus decoding of bytes to header failed"; e),
316315
}
317316
}
318317
}
@@ -322,7 +321,7 @@ impl std::error::Error for GetBlockHeaderError {
322321
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
323322
match *self {
324323
Self::Hex(ref e) => Some(e),
325-
Self::Consensus(ref e) => Some(e),
324+
Self::Header(ref e) => Some(e),
326325
}
327326
}
328327
}
@@ -442,8 +441,14 @@ impl From<NumericError> for GetChainStatesError {
442441
pub enum GetDescriptorActivityError {
443442
/// Conversion of numeric type (e.g., height) to expected type failed.
444443
Numeric(NumericError),
445-
/// Conversion of a hash string (BlockHash, Txid) failed.
446-
Hash(hex::HexToArrayError),
444+
/// Conversion of the `spend_txid` field failed.
445+
SpendTxid(hex::HexToArrayError),
446+
/// Conversion of the `prevout_txid` field failed.
447+
PrevoutTxid(hex::HexToArrayError),
448+
/// Conversion of the `txid` field failed.
449+
Txid(hex::HexToArrayError),
450+
/// Conversion of the `block_hash` field failed.
451+
BlockHash(hex::HexToArrayError),
447452
/// Conversion of the `amount` field (f64 BTC) failed.
448453
Amount(amount::ParseAmountError),
449454
/// Conversion of script hex to ScriptBuf failed.
@@ -464,7 +469,13 @@ impl fmt::Display for GetDescriptorActivityError {
464469
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
465470
match *self {
466471
Self::Numeric(ref e) => write_err!(f, "numeric conversion failed"; e),
467-
Self::Hash(ref e) => write_err!(f, "conversion of a hash string failed"; e),
472+
Self::SpendTxid(ref e) =>
473+
write_err!(f, "conversion of the `spend_txid` field failed"; e),
474+
Self::PrevoutTxid(ref e) =>
475+
write_err!(f, "conversion of the `prevout_txid` field failed"; e),
476+
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
477+
Self::BlockHash(ref e) =>
478+
write_err!(f, "conversion of the `block_hash` field failed"; e),
468479
Self::Amount(ref e) => write_err!(f, "conversion of the `amount` field failed"; e),
469480
Self::Script(ref e) => write_err!(f, "conversion of the script `hex` field failed"; e),
470481
Self::Address(ref e) => write_err!(f, "conversion of the `address` field failed"; e),
@@ -485,7 +496,10 @@ impl std::error::Error for GetDescriptorActivityError {
485496

486497
match *self {
487498
Numeric(ref e) => Some(e),
488-
Hash(ref e) => Some(e),
499+
SpendTxid(ref e) => Some(e),
500+
PrevoutTxid(ref e) => Some(e),
501+
Txid(ref e) => Some(e),
502+
BlockHash(ref e) => Some(e),
489503
Amount(ref e) => Some(e),
490504
Script(ref e) => Some(e),
491505
Address(ref e) => Some(e),
@@ -502,7 +516,7 @@ impl From<NumericError> for GetDescriptorActivityError {
502516
}
503517

504518
impl From<hex::HexToArrayError> for GetDescriptorActivityError {
505-
fn from(e: hex::HexToArrayError) -> Self { Self::Hash(e) }
519+
fn from(e: hex::HexToArrayError) -> Self { Self::BlockHash(e) }
506520
}
507521

508522
impl From<amount::ParseAmountError> for GetDescriptorActivityError {

types/src/v29/blockchain/into.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl GetBlockHeader {
303303
use GetBlockHeaderError as E;
304304

305305
let v = Vec::from_hex(&self.0).map_err(E::Hex)?;
306-
let header = encode::deserialize::<block::Header>(&v).map_err(E::Consensus)?;
306+
let header = encode::deserialize::<block::Header>(&v).map_err(E::Header)?;
307307

308308
Ok(model::GetBlockHeader(header))
309309
}
@@ -407,11 +407,12 @@ impl GetDescriptorActivity {
407407
.block_hash
408408
.map(|s| BlockHash::from_str(&s))
409409
.transpose()
410-
.map_err(E::Hash)?;
410+
.map_err(E::BlockHash)?;
411411
let height =
412412
spend.height.map(|h| crate::to_u32(h, "height")).transpose()?;
413-
let spend_txid = Txid::from_str(&spend.spend_txid).map_err(E::Hash)?;
414-
let prevout_txid = Txid::from_str(&spend.prevout_txid).map_err(E::Hash)?;
413+
let spend_txid = Txid::from_str(&spend.spend_txid).map_err(E::SpendTxid)?;
414+
let prevout_txid =
415+
Txid::from_str(&spend.prevout_txid).map_err(E::PrevoutTxid)?;
415416
let prevout_spk = spend.prevout_spk.into_model().map_err(E::PrevoutSpk)?;
416417

417418
Ok(model::ActivityEntry::Spend(model::SpendActivity {
@@ -431,10 +432,10 @@ impl GetDescriptorActivity {
431432
.block_hash
432433
.map(|s| BlockHash::from_str(&s))
433434
.transpose()
434-
.map_err(E::Hash)?;
435+
.map_err(E::BlockHash)?;
435436
let height =
436437
receive.height.map(|h| crate::to_u32(h, "height")).transpose()?; // Uses From<NumericError>
437-
let txid = Txid::from_str(&receive.txid).map_err(E::Hash)?;
438+
let txid = Txid::from_str(&receive.txid).map_err(E::Txid)?;
438439
let output_spk = receive.output_spk.into_model().map_err(E::OutputSpk)?;
439440

440441
Ok(model::ActivityEntry::Receive(model::ReceiveActivity {

types/src/v29/hidden/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ pub enum GetOrphanTxsVerboseTwoEntryError {
6666
Txid(hex::HexToArrayError),
6767
/// Conversion of the transaction `wtxid` field failed.
6868
Wtxid(hex::HexToArrayError),
69-
/// Conversion of hex data to bytes failed.
69+
/// Conversion of the `hex` field to bytes failed.
7070
Hex(hex::HexToBytesError),
71-
/// Consensus decoding of `hex` to transaction failed.
72-
Consensus(encode::Error),
71+
/// Consensus decoding of the `hex` bytes to `transaction` failed.
72+
Transaction(encode::Error),
7373
}
7474

7575
impl fmt::Display for GetOrphanTxsVerboseTwoEntryError {
7676
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7777
match *self {
7878
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
7979
Self::Wtxid(ref e) => write_err!(f, "conversion of the `wtxid` field failed"; e),
80-
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
81-
Self::Consensus(ref e) =>
82-
write_err!(f, "consensus decoding of `hex` to transaction failed"; e),
80+
Self::Hex(ref e) => write_err!(f, "conversion of the `hex` field to bytes failed"; e),
81+
Self::Transaction(ref e) =>
82+
write_err!(f, "consensus decoding of the `hex` bytes to `transaction` failed"; e),
8383
}
8484
}
8585
}
@@ -91,7 +91,7 @@ impl std::error::Error for GetOrphanTxsVerboseTwoEntryError {
9191
Self::Txid(ref e) => Some(e),
9292
Self::Wtxid(ref e) => Some(e),
9393
Self::Hex(ref e) => Some(e),
94-
Self::Consensus(ref e) => Some(e),
94+
Self::Transaction(ref e) => Some(e),
9595
}
9696
}
9797
}

types/src/v29/hidden/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl GetOrphanTxsVerboseTwoEntry {
5959
let txid = self.txid.parse::<Txid>().map_err(E::Txid)?;
6060
let wtxid = self.wtxid.parse::<Wtxid>().map_err(E::Wtxid)?;
6161
let v = Vec::from_hex(&self.hex).map_err(E::Hex)?;
62-
let transaction = encode::deserialize::<Transaction>(&v).map_err(E::Consensus)?;
62+
let transaction = encode::deserialize::<Transaction>(&v).map_err(E::Transaction)?;
6363

6464
Ok(model::GetOrphanTxsVerboseTwoEntry {
6565
txid,

0 commit comments

Comments
 (0)