Skip to content

Commit

Permalink
Refactor xsupport (paritytech#150)
Browse files Browse the repository at this point in the history
* Refactor xsupport pallet

* Optimize using try_for_each

* Rename to as_string and as_string_hex

* .

* Rename to as_addr()

* Remove useless Str
  • Loading branch information
liuchengxu authored Jul 22, 2020
1 parent 1a5b419 commit d40999d
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 190 deletions.
6 changes: 3 additions & 3 deletions xpallets/bridge/bitcoin/src/tx/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use xr_primitives::generic::b58;

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

// light-bitcoin
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op
error!(
"[parse_output_addr]|parse output script error|e:{:?}|script:{:?}",
_e,
u8array_to_hex(&script)
as_string_hex(&script)
);
_e
}).ok().and_then(|script_addresses| {
Expand All @@ -54,7 +54,7 @@ pub fn parse_output_addr_with_networkid(script: &Script, network: Network) -> Op
return Some(addr);
}
// the type is `NonStandard`, `Multisig`, `NullData`, `WitnessScript`, `WitnessKey`
warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, u8array_to_hex(&script));
warn!("[parse_output_addr]|can't parse addr from output script|type:{:?}|addr:{:?}|script:{:}", script.script_type(), script_addresses, as_string_hex(&script));
None
})
}
Expand Down
4 changes: 2 additions & 2 deletions xpallets/bridge/bitcoin/src/tx/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::Trait;

// ChainX
#[cfg(feature = "std")]
use xsupport::u8array_to_hex;
use xsupport::as_string_hex;
use xsupport::{debug, error};

pub fn validate_transaction<T: Trait, RT: RelayTransaction + MaybeDebug>(
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn parse_and_check_signed_tx_impl(
}
}
if !verify {
error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, u8array_to_hex(&bytes_redeem_script));
error!("[parse_and_check_signed_tx]|Verify sign failed|tx:{:?}|input:{:?}|bytes_sedeem_script:{:?}", tx, i, as_string_hex(&bytes_redeem_script));
return Err("Verify sign failed");
}
}
Expand Down
141 changes: 5 additions & 136 deletions xpallets/support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod base58;
pub mod rlog;
pub use rlog::RUNTIME_TARGET;
mod macros;
#[cfg(feature = "std")]
pub mod x_std;

use frame_support::dispatch::{DispatchError, DispatchResult};

pub use frame_support::fail;
pub use macros::*;

/// Although xss is imperceptible on-chain, we merely want to make it look safer off-chain.
#[inline]
Expand All @@ -17,137 +20,3 @@ pub fn xss_check(input: &[u8]) -> DispatchResult {
}
Ok(())
}

#[cfg(feature = "std")]
pub mod _std {
use std::fmt;

pub struct Str<'a>(pub &'a String);
impl<'a> fmt::Debug for Str<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[inline]
pub fn u8array_to_string(s: &[u8]) -> String {
String::from_utf8_lossy(s).into_owned()
}

#[inline]
pub fn u8array_to_addr(s: &[u8]) -> String {
for i in s {
// 0x30 = '0' 0x39 = '9'; 0x41 = 'A' 0x7A = 'z'
if (0x30 <= *i && *i <= 0x39) || (0x41 <= *i && *i <= 0x7A) {
continue;
} else {
// 0x30 = '0' 0x7A = 'z'
return u8array_to_hex(s); // when any item is not a char, use hex to decode it
}
}
return u8array_to_string(s);
}
#[inline]
pub fn u8array_to_hex(s: &[u8]) -> String {
use rustc_hex::ToHex;
let s: String = s.to_hex();
"0x".to_string() + &s
}
#[inline]
pub fn try_hex_or_str(src: &[u8]) -> String {
let check_is_str = |src: &[u8]| -> bool {
for c in src {
// 0x21 = !, 0x71 = ~
if 0x21 <= *c && *c <= 0x7E {
continue;
} else {
return false;
}
}
return true;
};
if check_is_str(src) {
u8array_to_string(src)
} else {
u8array_to_hex(src)
}
}
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! str {
( $x:expr ) => {{
use $crate::_std::u8array_to_string;
$crate::_std::Str(&u8array_to_string(&$x))
}};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! str {
( $x:expr ) => {{
&$x
}};
}

#[macro_export]
macro_rules! token {
( $x:expr ) => {
$crate::str!($x)
};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! try_addr {
( $x:expr ) => {{
use $crate::_std::u8array_to_addr;
$crate::_std::Str(&u8array_to_addr(&$x))
}};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! try_addr {
( $x:expr ) => {{
&$x
}};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! try_hex {
( $x:expr ) => {{
use $crate::_std::try_hex_or_str;
$crate::_std::Str(&try_hex_or_str(&$x))
}};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! try_hex {
( $x:expr ) => {{
&$x
}};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! ensure_with_errorlog {
( $x:expr, $y:expr, $($arg:tt)*) => {{
if !$x {
$crate::error!("{:?}|{}", $y, format!($($arg)*));
$crate::fail!($y);
}
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! ensure_with_errorlog {
( $x:expr, $y:expr, $($arg:tt)*) => {{
if !$x {
$crate::fail!($y);
}
}};
}
131 changes: 131 additions & 0 deletions xpallets/support/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
pub use log::*;

#[cfg(feature = "std")]
#[macro_export]
macro_rules! str {
( $x:expr ) => {
$crate::x_std::as_string(&$x)
};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! str {
( $x:expr ) => {
&x
};
}

#[macro_export]
macro_rules! token {
( $x:expr ) => {
$crate::str!($x)
};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! try_addr {
( $x:expr ) => {{
$crate::x_std::as_addr(&$x)
}};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! try_addr {
( $x:expr ) => {{
&$x
}};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! try_hex {
( $x:expr ) => {{
$crate::x_std::try_hex_or_str(&$x)
}};
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! try_hex {
( $x:expr ) => {{
&$x
}};
}

#[cfg(feature = "std")]
#[macro_export]
macro_rules! ensure_with_errorlog {
( $x:expr, $y:expr, $($arg:tt)*) => {{
if !$x {
$crate::error!("{:?}|{}", $y, format!($($arg)*));
$crate::fail!($y);
}
}}
}

#[cfg(not(feature = "std"))]
#[macro_export]
macro_rules! ensure_with_errorlog {
( $x:expr, $y:expr, $($arg:tt)*) => {{
if !$x {
$crate::fail!($y);
}
}};
}

mod log {
pub const RUNTIME_TARGET: &str = "runtime";

#[macro_export]
macro_rules! error {
(target: $target:expr, $($arg:tt)+) => (
frame_support::debug::error!(target: $target, $($arg)+);
);
($($arg:tt)+) => (
$crate::error!(target: "runtime", $($arg)+);
)
}

#[macro_export]
macro_rules! warn {
(target: $target:expr, $($arg:tt)+) => (
frame_support::debug::warn!(target: $target, $($arg)+);
);
($($arg:tt)+) => (
$crate::warn!(target: "runtime", $($arg)+);
)
}

#[macro_export]
macro_rules! info {
(target: $target:expr, $($arg:tt)+) => (
frame_support::debug::info!(target: $target, $($arg)+);
);
($($arg:tt)+) => (
$crate::info!(target: "runtime", $($arg)+);
)
}

#[macro_export]
macro_rules! debug {
(target: $target:expr, $($arg:tt)+) => (
frame_support::debug::debug!(target: $target, $($arg)+);
);
($($arg:tt)+) => (
$crate::debug!(target: "runtime", $($arg)+);
)
}

#[macro_export]
macro_rules! trace {
(target: $target:expr, $($arg:tt)+) => (
frame_support::debug::trace!(target: $target, $($arg)+);
);
($($arg:tt)+) => (
$crate::trace!(target: "runtime", $($arg)+);
)
}
}
49 changes: 0 additions & 49 deletions xpallets/support/src/rlog.rs

This file was deleted.

Loading

0 comments on commit d40999d

Please sign in to comment.