Skip to content

Commit

Permalink
Fix and test AddPool encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre committed Jul 21, 2022
1 parent cbb6942 commit 9b7f34f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pallets/connectors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ common-types = { path = "../../libs/common-types", default-features = false, opt
runtime-common = { path = "../../runtime/common", default-features = false, optional = true }

[dev-dependencies]
hex = "0.4.3"
# Substrate crates & pallets
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
Expand Down
5 changes: 4 additions & 1 deletion pallets/connectors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub mod pallet {
+ Copy
+ HasCompact
+ MaxEncodedLen
+ core::fmt::Debug;
+ core::fmt::Debug
+ Encode
+ Decode
+ Into<u64>;

type TrancheId: Member
+ Parameter
Expand Down
52 changes: 41 additions & 11 deletions pallets/connectors/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ use crate::*;

#[derive(Decode, Clone, PartialEq, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum Message<PoolId> {
pub enum Message<PoolId> where PoolId: Encode + Decode {
Invalid,
AddPool { pool_id: PoolId }, // More to come...
}

impl<PoolId> Encode for Message<PoolId> {
impl<PoolId: Encode + Decode> Message<PoolId> {
fn call_type(&self) -> u8 {
match self {
Self::Invalid => 0,
Self::AddPool { .. } => 1,
}
}
}

impl<PoolId: Encode + Decode> Encode for Message<PoolId> {
fn encode(&self) -> Vec<u8> {
match self {
Message::Invalid => vec![0u8],
Message::AddPool { pool_id: _ } => {
let mut message: Vec<u8> = vec![0u8];
message.append(&mut vec![1, 2, 3]); //todo(nuno): &mut pool_id.as_bytes().to_vec());
Message::Invalid => vec![self.call_type()],
Message::AddPool { pool_id } => {
let mut message: Vec<u8> = vec![];
message.push(self.call_type());
//todo(nuno): &mut pool_id.as_bytes().to_vec());
// to do this, we need to need a stricter PoolId bound to be able to convert it to byte array
let mut encoded_pool_id = pool_id.encode();
encoded_pool_id.reverse();
message.append(&mut encoded_pool_id);
message
}
}
Expand All @@ -24,6 +38,7 @@ impl<PoolId> Encode for Message<PoolId> {
mod tests {
use crate::Message;
use codec::Encode;
use hex::FromHex;

type PoolId = u64;

Expand All @@ -32,14 +47,29 @@ mod tests {

#[test]
fn invalid() {
let encoded = Message::<PoolId>::Invalid.encode();
assert_eq!(encoded, vec![0])
let msg = Message::<PoolId>::Invalid;
assert_eq!(msg.encode(), vec![msg.call_type()]);
assert_eq!(msg.encode(), vec![0]);
}

#[test]
fn add_pool() {
let encoded = Message::<PoolId>::AddPool { pool_id: 42 }.encode();
assert_eq!(encoded, vec![0, 1, 2, 3])
fn add_pool_0() {
let msg = Message::<PoolId>::AddPool { pool_id: 0 };
let encoded = msg.encode();

let input = "010000000000000000";
let expected = <[u8; 9]>::from_hex(input).expect("Decoding failed");
assert_eq!(encoded, expected);
}

#[test]
fn add_pool_long() {
let msg = Message::<PoolId>::AddPool { pool_id: 12378532 };
let encoded = msg.encode();

let input = "010000000000bce1a4";
let expected = <[u8; 9]>::from_hex(input).expect("Decoding failed");
assert_eq!(encoded, expected);
}
}
}

0 comments on commit 9b7f34f

Please sign in to comment.