Skip to content

Commit

Permalink
feat: encode source address correctly in gateway (#1548)
Browse files Browse the repository at this point in the history
* feat: encode source address correctly in gateway

* chore: bumb dev runtime version to next one on moonbase

* fix: linter

* chore: extend error signalers
  • Loading branch information
mustermeiszer authored Sep 14, 2023
1 parent 597eb77 commit 5d09fe7
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 131 deletions.
37 changes: 24 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/mocks/src/liquidity_pools.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfg_traits::liquidity_pools::Codec;
use codec::{Error, Input};
use codec::{Decode, Error, Input};

#[derive(Debug, Eq, PartialEq, Clone)]
#[derive(Debug, Eq, PartialEq, Clone, codec::Encode, Decode)]
pub enum MessageMock {
First,
Second,
Expand Down
2 changes: 2 additions & 0 deletions libs/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"], default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
hex = { version = "0.4.3", default_features = false }
pallet-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }
Expand All @@ -33,6 +34,7 @@ std = [
"codec/std",
"scale-info/std",
"sp-consensus-aura/std",
"hex/std",
]

runtime-benchmarks = [
Expand Down
58 changes: 58 additions & 0 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,68 @@ where
pallet_timestamp::Pallet::<T>::set_timestamp(timestamp);
}

pub fn decode_var_source<const EXPECTED_SOURCE_ADDRESS_SIZE: usize>(
source_address: &[u8],
) -> Option<[u8; EXPECTED_SOURCE_ADDRESS_SIZE]> {
const HEX_PREFIX: &str = "0x";

let mut address = [0u8; EXPECTED_SOURCE_ADDRESS_SIZE];

if source_address.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(source_address);
return Some(address);
}

let try_bytes = match sp_std::str::from_utf8(source_address) {
Ok(res) => res.as_bytes(),
Err(_) => source_address,
};

// Attempt to hex decode source address.
let bytes = match hex::decode(try_bytes) {
Ok(res) => Some(res),
Err(_) => {
// Strip 0x prefix.
let res = try_bytes.strip_prefix(HEX_PREFIX.as_bytes())?;

hex::decode(res).ok()
}
}?;

if bytes.len() == EXPECTED_SOURCE_ADDRESS_SIZE {
address.copy_from_slice(bytes.as_slice());
Some(address)
} else {
None
}
}

#[cfg(test)]
mod tests {
use super::*;

mod get_source_address_bytes {
const EXPECTED: usize = 20;
use super::*;

#[test]
fn get_source_address_bytes_works() {
let hash = [1u8; 20];

decode_var_source::<EXPECTED>(&hash).expect("address bytes from H160 works");

let str = String::from("d47ed02acbbb66ee8a3fe0275bd98add0aa607c3");

decode_var_source::<EXPECTED>(str.as_bytes())
.expect("address bytes from un-prefixed hex works");

let str = String::from("0xd47ed02acbbb66ee8a3fe0275bd98add0aa607c3");

decode_var_source::<EXPECTED>(str.as_bytes())
.expect("address bytes from prefixed hex works");
}
}

mod vec_to_fixed_array {
use super::*;

Expand Down
7 changes: 7 additions & 0 deletions pallets/liquidity-pools-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"], default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
hex = { version = "0.4.3", default-features = false }
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
Expand All @@ -27,9 +28,11 @@ sp-core = { git = "https://github.com/paritytech/substrate", default-features =
# Our custom pallets
cfg-traits = { path = "../../libs/traits", default-features = false }
cfg-types = { path = "../../libs/types", default-features = false }
cfg-utils = { path = "../../libs/utils", default-features = false }

[dev-dependencies]
cfg-mocks = { path = "../../libs/mocks", features = ["runtime-benchmarks", "std"] }
hex-literal = "0.4.1"
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }

Expand All @@ -49,13 +52,16 @@ std = [
"sp-core/std",
"sp-runtime/std",
"scale-info/std",
"cfg-utils/std",
"hex/std",
]
try-runtime = [
"cfg-traits/try-runtime",
"cfg-types/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"cfg-utils/try-runtime",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand All @@ -64,4 +70,5 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hex = { version = "0.4.3", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"], default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
hex = { version = "0.4.3", default-features = false }
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
Expand All @@ -25,6 +25,7 @@ precompile-utils = { git = "https://github.com/PureStake/moonbeam", default-feat

cfg-traits = { path = "../../../libs/traits", default-features = false }
cfg-types = { path = "../../../libs/types", default-features = false }
cfg-utils = { path = "../../../libs/utils", default-features = false }
pallet-liquidity-pools-gateway = { path = "../../liquidity-pools-gateway", default-features = false }

[features]
Expand All @@ -44,13 +45,15 @@ std = [
"pallet-liquidity-pools-gateway/std",
"cfg-types/std",
"cfg-traits/std",
"cfg-utils/std",
"ethabi/std",
"frame-benchmarking/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"cfg-types/runtime-benchmarks",
"cfg-traits/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
Expand All @@ -63,6 +66,7 @@ try-runtime = [
"pallet-liquidity-pools-gateway/try-runtime",
"cfg-types/try-runtime",
"cfg-traits/try-runtime",
"cfg-utils/try-runtime",
"pallet-evm/try-runtime",
"sp-runtime/try-runtime",
]
Loading

0 comments on commit 5d09fe7

Please sign in to comment.