Skip to content

Commit d40999d

Browse files
authored
Refactor xsupport (paritytech#150)
* Refactor xsupport pallet * Optimize using try_for_each * Rename to as_string and as_string_hex * . * Rename to as_addr() * Remove useless Str
1 parent 1a5b419 commit d40999d

File tree

6 files changed

+188
-190
lines changed

6 files changed

+188
-190
lines changed

xpallets/bridge/bitcoin/src/tx/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use xr_primitives::generic::b58;
88

99
use xbridge_common::{traits::TrusteeSession, types::TrusteeSessionInfo, utils::two_thirds_unsafe};
1010
#[cfg(feature = "std")]
11-
use xsupport::u8array_to_hex;
11+
use xsupport::as_string_hex;
1212
use xsupport::{error, warn};
1313

1414
// light-bitcoin
@@ -39,7 +39,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op
3939
error!(
4040
"[parse_output_addr]|parse output script error|e:{:?}|script:{:?}",
4141
_e,
42-
u8array_to_hex(&script)
42+
as_string_hex(&script)
4343
);
4444
_e
4545
}).ok().and_then(|script_addresses| {
@@ -54,7 +54,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op
5454
return Some(addr);
5555
}
5656
// the type is `NonStandard`, `Multisig`, `NullData`, `WitnessScript`, `WitnessKey`
57-
warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, u8array_to_hex(&script));
57+
warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, as_string_hex(&script));
5858
None
5959
})
6060
}

xpallets/bridge/bitcoin/src/tx/validator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::Trait;
1818

1919
// ChainX
2020
#[cfg(feature = "std")]
21-
use xsupport::u8array_to_hex;
21+
use xsupport::as_string_hex;
2222
use xsupport::{debug, error};
2323

2424
pub fn validate_transaction<T: Trait, RT: RelayTransaction + MaybeDebug>(
@@ -134,7 +134,7 @@ pub fn parse_and_check_signed_tx_impl(
134134
}
135135
}
136136
if !verify {
137-
error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, u8array_to_hex(&bytes_redeem_script));
137+
error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, as_string_hex(&bytes_redeem_script));
138138
return Err("Verify sign failed");
139139
}
140140
}

xpallets/support/src/lib.rs

Lines changed: 5 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

33
pub mod base58;
4-
pub mod rlog;
5-
pub use rlog::RUNTIME_TARGET;
4+
mod macros;
5+
#[cfg(feature = "std")]
6+
pub mod x_std;
67

78
use frame_support::dispatch::{DispatchError, DispatchResult};
9+
810
pub use frame_support::fail;
11+
pub use macros::*;
912

1013
/// Although xss is imperceptible on-chain, we merely want to make it look safer off-chain.
1114
#[inline]
@@ -17,137 +20,3 @@ pub fn xss_check(input: &[u8]) -> DispatchResult {
1720
}
1821
Ok(())
1922
}
20-
21-
#[cfg(feature = "std")]
22-
pub mod _std {
23-
use std::fmt;
24-
25-
pub struct Str<'a>(pub &'a String);
26-
impl<'a> fmt::Debug for Str<'a> {
27-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28-
write!(f, "{}", self.0)
29-
}
30-
}
31-
#[inline]
32-
pub fn u8array_to_string(s: &[u8]) -> String {
33-
String::from_utf8_lossy(s).into_owned()
34-
}
35-
36-
#[inline]
37-
pub fn u8array_to_addr(s: &[u8]) -> String {
38-
for i in s {
39-
// 0x30 = '0' 0x39 = '9'; 0x41 = 'A' 0x7A = 'z'
40-
if (0x30 <= *i && *i <= 0x39) || (0x41 <= *i && *i <= 0x7A) {
41-
continue;
42-
} else {
43-
// 0x30 = '0' 0x7A = 'z'
44-
return u8array_to_hex(s); // when any item is not a char, use hex to decode it
45-
}
46-
}
47-
return u8array_to_string(s);
48-
}
49-
#[inline]
50-
pub fn u8array_to_hex(s: &[u8]) -> String {
51-
use rustc_hex::ToHex;
52-
let s: String = s.to_hex();
53-
"0x".to_string() + &s
54-
}
55-
#[inline]
56-
pub fn try_hex_or_str(src: &[u8]) -> String {
57-
let check_is_str = |src: &[u8]| -> bool {
58-
for c in src {
59-
// 0x21 = !, 0x71 = ~
60-
if 0x21 <= *c && *c <= 0x7E {
61-
continue;
62-
} else {
63-
return false;
64-
}
65-
}
66-
return true;
67-
};
68-
if check_is_str(src) {
69-
u8array_to_string(src)
70-
} else {
71-
u8array_to_hex(src)
72-
}
73-
}
74-
}
75-
76-
#[cfg(feature = "std")]
77-
#[macro_export]
78-
macro_rules! str {
79-
( $x:expr ) => {{
80-
use $crate::_std::u8array_to_string;
81-
$crate::_std::Str(&u8array_to_string(&$x))
82-
}};
83-
}
84-
85-
#[cfg(not(feature = "std"))]
86-
#[macro_export]
87-
macro_rules! str {
88-
( $x:expr ) => {{
89-
&$x
90-
}};
91-
}
92-
93-
#[macro_export]
94-
macro_rules! token {
95-
( $x:expr ) => {
96-
$crate::str!($x)
97-
};
98-
}
99-
100-
#[cfg(feature = "std")]
101-
#[macro_export]
102-
macro_rules! try_addr {
103-
( $x:expr ) => {{
104-
use $crate::_std::u8array_to_addr;
105-
$crate::_std::Str(&u8array_to_addr(&$x))
106-
}};
107-
}
108-
109-
#[cfg(not(feature = "std"))]
110-
#[macro_export]
111-
macro_rules! try_addr {
112-
( $x:expr ) => {{
113-
&$x
114-
}};
115-
}
116-
117-
#[cfg(feature = "std")]
118-
#[macro_export]
119-
macro_rules! try_hex {
120-
( $x:expr ) => {{
121-
use $crate::_std::try_hex_or_str;
122-
$crate::_std::Str(&try_hex_or_str(&$x))
123-
}};
124-
}
125-
126-
#[cfg(not(feature = "std"))]
127-
#[macro_export]
128-
macro_rules! try_hex {
129-
( $x:expr ) => {{
130-
&$x
131-
}};
132-
}
133-
134-
#[cfg(feature = "std")]
135-
#[macro_export]
136-
macro_rules! ensure_with_errorlog {
137-
( $x:expr, $y:expr, $($arg:tt)*) => {{
138-
if !$x {
139-
$crate::error!("{:?}|{}", $y, format!($($arg)*));
140-
$crate::fail!($y);
141-
}
142-
}}
143-
}
144-
145-
#[cfg(not(feature = "std"))]
146-
#[macro_export]
147-
macro_rules! ensure_with_errorlog {
148-
( $x:expr, $y:expr, $($arg:tt)*) => {{
149-
if !$x {
150-
$crate::fail!($y);
151-
}
152-
}};
153-
}

xpallets/support/src/macros.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
pub use log::*;
2+
3+
#[cfg(feature = "std")]
4+
#[macro_export]
5+
macro_rules! str {
6+
( $x:expr ) => {
7+
$crate::x_std::as_string(&$x)
8+
};
9+
}
10+
11+
#[cfg(not(feature = "std"))]
12+
#[macro_export]
13+
macro_rules! str {
14+
( $x:expr ) => {
15+
&x
16+
};
17+
}
18+
19+
#[macro_export]
20+
macro_rules! token {
21+
( $x:expr ) => {
22+
$crate::str!($x)
23+
};
24+
}
25+
26+
#[cfg(feature = "std")]
27+
#[macro_export]
28+
macro_rules! try_addr {
29+
( $x:expr ) => {{
30+
$crate::x_std::as_addr(&$x)
31+
}};
32+
}
33+
34+
#[cfg(not(feature = "std"))]
35+
#[macro_export]
36+
macro_rules! try_addr {
37+
( $x:expr ) => {{
38+
&$x
39+
}};
40+
}
41+
42+
#[cfg(feature = "std")]
43+
#[macro_export]
44+
macro_rules! try_hex {
45+
( $x:expr ) => {{
46+
$crate::x_std::try_hex_or_str(&$x)
47+
}};
48+
}
49+
50+
#[cfg(not(feature = "std"))]
51+
#[macro_export]
52+
macro_rules! try_hex {
53+
( $x:expr ) => {{
54+
&$x
55+
}};
56+
}
57+
58+
#[cfg(feature = "std")]
59+
#[macro_export]
60+
macro_rules! ensure_with_errorlog {
61+
( $x:expr, $y:expr, $($arg:tt)*) => {{
62+
if !$x {
63+
$crate::error!("{:?}|{}", $y, format!($($arg)*));
64+
$crate::fail!($y);
65+
}
66+
}}
67+
}
68+
69+
#[cfg(not(feature = "std"))]
70+
#[macro_export]
71+
macro_rules! ensure_with_errorlog {
72+
( $x:expr, $y:expr, $($arg:tt)*) => {{
73+
if !$x {
74+
$crate::fail!($y);
75+
}
76+
}};
77+
}
78+
79+
mod log {
80+
pub const RUNTIME_TARGET: &str = "runtime";
81+
82+
#[macro_export]
83+
macro_rules! error {
84+
(target: $target:expr, $($arg:tt)+) => (
85+
frame_support::debug::error!(target: $target, $($arg)+);
86+
);
87+
($($arg:tt)+) => (
88+
$crate::error!(target: "runtime", $($arg)+);
89+
)
90+
}
91+
92+
#[macro_export]
93+
macro_rules! warn {
94+
(target: $target:expr, $($arg:tt)+) => (
95+
frame_support::debug::warn!(target: $target, $($arg)+);
96+
);
97+
($($arg:tt)+) => (
98+
$crate::warn!(target: "runtime", $($arg)+);
99+
)
100+
}
101+
102+
#[macro_export]
103+
macro_rules! info {
104+
(target: $target:expr, $($arg:tt)+) => (
105+
frame_support::debug::info!(target: $target, $($arg)+);
106+
);
107+
($($arg:tt)+) => (
108+
$crate::info!(target: "runtime", $($arg)+);
109+
)
110+
}
111+
112+
#[macro_export]
113+
macro_rules! debug {
114+
(target: $target:expr, $($arg:tt)+) => (
115+
frame_support::debug::debug!(target: $target, $($arg)+);
116+
);
117+
($($arg:tt)+) => (
118+
$crate::debug!(target: "runtime", $($arg)+);
119+
)
120+
}
121+
122+
#[macro_export]
123+
macro_rules! trace {
124+
(target: $target:expr, $($arg:tt)+) => (
125+
frame_support::debug::trace!(target: $target, $($arg)+);
126+
);
127+
($($arg:tt)+) => (
128+
$crate::trace!(target: "runtime", $($arg)+);
129+
)
130+
}
131+
}

xpallets/support/src/rlog.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)