Skip to content

Commit fda293d

Browse files
committed
Always add backtrace to cosmwasm-crypto
1 parent b7f003f commit fda293d

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

packages/crypto/src/backtrace.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use core::fmt::{Debug, Display, Formatter, Result};
2+
use std::backtrace::Backtrace;
3+
4+
/// This wraps an actual backtrace to achieve two things:
5+
/// - being able to fill this with a stub implementation in `no_std` environments
6+
/// - being able to use this in conjunction with [`thiserror::Error`]
7+
pub struct BT(Backtrace);
8+
9+
impl BT {
10+
#[track_caller]
11+
pub fn capture() -> Self {
12+
BT(Backtrace::capture())
13+
}
14+
}
15+
16+
impl Debug for BT {
17+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
18+
Debug::fmt(&self.0, f)
19+
}
20+
}
21+
22+
impl Display for BT {
23+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
24+
Display::fmt(&self.0, f)
25+
}
26+
}

packages/crypto/src/errors.rs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#[cfg(feature = "backtraces")]
2-
use std::backtrace::Backtrace;
1+
use crate::BT;
32
use std::fmt::Debug;
43
use thiserror::Error;
54

@@ -8,81 +7,55 @@ pub type CryptoResult<T> = core::result::Result<T, CryptoError>;
87
#[derive(Error, Debug)]
98
pub enum CryptoError {
109
#[error("Batch verify error: {msg}")]
11-
BatchErr {
12-
msg: String,
13-
#[cfg(feature = "backtraces")]
14-
backtrace: Backtrace,
15-
},
10+
BatchErr { msg: String, backtrace: BT },
1611
#[error("Crypto error: {msg}")]
17-
GenericErr {
18-
msg: String,
19-
#[cfg(feature = "backtraces")]
20-
backtrace: Backtrace,
21-
},
12+
GenericErr { msg: String, backtrace: BT },
2213
#[error("Invalid hash format")]
23-
InvalidHashFormat {
24-
#[cfg(feature = "backtraces")]
25-
backtrace: Backtrace,
26-
},
14+
InvalidHashFormat { backtrace: BT },
2715
#[error("Invalid public key format")]
28-
InvalidPubkeyFormat {
29-
#[cfg(feature = "backtraces")]
30-
backtrace: Backtrace,
31-
},
16+
InvalidPubkeyFormat { backtrace: BT },
3217
#[error("Invalid signature format")]
33-
InvalidSignatureFormat {
34-
#[cfg(feature = "backtraces")]
35-
backtrace: Backtrace,
36-
},
18+
InvalidSignatureFormat { backtrace: BT },
3719
#[error("Invalid recovery parameter. Supported values: 0 and 1.")]
38-
InvalidRecoveryParam {
39-
#[cfg(feature = "backtraces")]
40-
backtrace: Backtrace,
41-
},
20+
InvalidRecoveryParam { backtrace: BT },
4221
}
4322

4423
impl CryptoError {
4524
pub fn batch_err(msg: impl Into<String>) -> Self {
4625
CryptoError::BatchErr {
4726
msg: msg.into(),
48-
#[cfg(feature = "backtraces")]
49-
backtrace: Backtrace::capture(),
27+
backtrace: BT::capture(),
5028
}
5129
}
5230

5331
pub fn generic_err(msg: impl Into<String>) -> Self {
5432
CryptoError::GenericErr {
5533
msg: msg.into(),
56-
#[cfg(feature = "backtraces")]
57-
backtrace: Backtrace::capture(),
34+
backtrace: BT::capture(),
5835
}
5936
}
6037

6138
pub fn invalid_hash_format() -> Self {
6239
CryptoError::InvalidHashFormat {
63-
#[cfg(feature = "backtraces")]
64-
backtrace: Backtrace::capture(),
40+
backtrace: BT::capture(),
6541
}
6642
}
6743

6844
pub fn invalid_pubkey_format() -> Self {
6945
CryptoError::InvalidPubkeyFormat {
70-
#[cfg(feature = "backtraces")]
71-
backtrace: Backtrace::capture(),
46+
backtrace: BT::capture(),
7247
}
7348
}
7449

7550
pub fn invalid_signature_format() -> Self {
7651
CryptoError::InvalidSignatureFormat {
77-
#[cfg(feature = "backtraces")]
78-
backtrace: Backtrace::capture(),
52+
backtrace: BT::capture(),
7953
}
8054
}
8155

8256
pub fn invalid_recovery_param() -> Self {
8357
CryptoError::InvalidRecoveryParam {
84-
#[cfg(feature = "backtraces")]
85-
backtrace: Backtrace::capture(),
58+
backtrace: BT::capture(),
8659
}
8760
}
8861

packages/crypto/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
//! Please don't use any of these types directly, as
33
//! they might change frequently, or be removed in the future.
44
//! This crate does not adhere to semantic versioning.
5-
#![cfg_attr(feature = "backtraces", feature(error_generic_member_access))]
6-
#![cfg_attr(feature = "backtraces", feature(provide_any))]
7-
5+
mod backtrace;
86
mod ed25519;
97
mod errors;
108
mod identity_digest;
@@ -20,3 +18,4 @@ pub use crate::errors::{CryptoError, CryptoResult};
2018
pub use crate::secp256k1::{secp256k1_recover_pubkey, secp256k1_verify};
2119
#[doc(hidden)]
2220
pub use crate::secp256k1::{ECDSA_PUBKEY_MAX_LEN, ECDSA_SIGNATURE_LEN, MESSAGE_HASH_MAX_LEN};
21+
pub(crate) use backtrace::BT;

0 commit comments

Comments
 (0)