Skip to content

Commit

Permalink
Clean up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre committed Feb 24, 2023
1 parent 633698a commit d368e22
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
21 changes: 21 additions & 0 deletions libs/utils/src/lib.rs
Expand Up @@ -13,6 +13,7 @@
// Ensure we're `no_std` when compiling for WebAssembly.
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Input};
use sp_std::{cmp::min, vec::Vec};

/// Build a fixed-size array using as many elements from `src` as possible without
Expand All @@ -26,6 +27,26 @@ pub fn vec_to_fixed_array<const S: usize>(src: Vec<u8>) -> [u8; S] {
dest
}

/// Decode a type O by reading S bytes from I. Those bytes are expected to be encoded
/// as big-endian and thus needs reversing to little-endian before decoding to O.
pub fn decode_be_bytes<const S: usize, O: Decode, I: Input>(
input: &mut I,
) -> Result<O, codec::Error> {
let mut bytes = [0; S];
input.read(&mut bytes[..])?;
bytes.reverse();

O::decode(&mut bytes.as_slice())
}

/// Decode a type 0 by reading S bytes from I.
pub fn decode<const S: usize, O: Decode, I: Input>(input: &mut I) -> Result<O, codec::Error> {
let mut bytes = [0; S];
input.read(&mut bytes[..])?;

O::decode(&mut bytes.as_slice())
}

/// Function that initializes the frame system & Aura, so a timestamp can be set and pass validation
#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
pub fn set_block_number_timestamp<T>(block_number: T::BlockNumber, timestamp: T::Moment)
Expand Down
2 changes: 1 addition & 1 deletion pallets/connectors/src/lib.rs
Expand Up @@ -14,7 +14,7 @@
use core::convert::TryFrom;

use cfg_traits::PoolInspect;
use cfg_utils::vec_to_fixed_array;
use cfg_utils::{decode_be_bytes, vec_to_fixed_array};
use codec::{Decode, Encode, EncodeLike, Input, MaxEncodedLen};
use frame_support::traits::{
fungibles::{Inspect, Mutate, Transfer},
Expand Down
31 changes: 5 additions & 26 deletions pallets/connectors/src/message.rs
@@ -1,8 +1,9 @@
use cfg_primitives::Moment;
use cfg_utils::{decode, decode_be_bytes};
use codec::{Decode, Encode, EncodeLike, Input};
use scale_info::TypeInfo;
use sp_std::{vec, vec::Vec};

use crate::*;

/// Address type
/// Note: It can be used to represent any address type with a length <= 32 bytes;
/// For example, it can represent an Ethereum address (20-bytes long) by padding it with 12 zeros.
Expand Down Expand Up @@ -139,26 +140,6 @@ impl<
}
}

/// Decode a type O by reading S bytes from I. Those bytes are expected to be encoded
/// as big-endian and thus needs reversing to little-endian before decoding to O.
pub fn decode_be_bytes<const S: usize, O: Decode, I: Input>(
input: &mut I,
) -> Result<O, codec::Error> {
let mut bytes = [0; S];
input.read(&mut bytes[..])?;
bytes.reverse();

O::decode(&mut bytes.as_slice())
}

/// Decode a type 0 by reading S bytes from I.
pub fn decode<const S: usize, O: Decode, I: Input>(input: &mut I) -> Result<O, codec::Error> {
let mut bytes = [0; S];
input.read(&mut bytes[..])?;

O::decode(&mut bytes.as_slice())
}

impl<
Domain: Encode + Decode,
PoolId: Encode + Decode,
Expand Down Expand Up @@ -249,17 +230,15 @@ fn to_be(x: impl Encode) -> Vec<u8> {

#[cfg(test)]
mod tests {
use cfg_primitives::{Balance, PoolId, TrancheId};
use cfg_types::fixed_point::Rate;
use cfg_utils::vec_to_fixed_array;
use codec::{Decode, Encode};
use hex::FromHex;
use sp_runtime::traits::One;

use super::*;

type PoolId = u64;
type TrancheId = [u8; 16];
type Balance = cfg_primitives::Balance;
use crate::{Domain, DomainAddress};

#[test]
fn invalid() {
Expand Down

0 comments on commit d368e22

Please sign in to comment.