Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: api event naming conventions #286

Merged
merged 32 commits into from
Sep 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e8a3045
refactor: encoding scheme
Daanvdplas Sep 10, 2024
b16430b
tests: pallet api extension
Daanvdplas Sep 10, 2024
2179645
docs: tests
Daanvdplas Sep 10, 2024
dad4beb
refactor: remove unnecessary
Daanvdplas Sep 10, 2024
1c6f03a
test: primitives
Daanvdplas Sep 10, 2024
a51d619
style: naming variable
Daanvdplas Sep 10, 2024
9ff9915
test: devnet runtime versioning
Daanvdplas Sep 10, 2024
a2b9e76
test: runtime devnet mod (small tests)
Daanvdplas Sep 10, 2024
dc0f90a
refactor: fallible conversion
evilrobot-01 Sep 10, 2024
6f8205e
refactor: fallible conversion (#275)
evilrobot-01 Sep 11, 2024
1547dca
fix: tests after fallible conversion feat
Daanvdplas Sep 11, 2024
2b07130
rebase
Daanvdplas Sep 11, 2024
36e8735
fix: test after fallible conversion feat
Daanvdplas Sep 11, 2024
438de63
Merge branch 'daan/tests-pallet_api_extension' into daan/test-devnet_…
Daanvdplas Sep 11, 2024
a1ee377
test: config contracts
Daanvdplas Sep 11, 2024
8949e13
test: ensure signed test coverage
Daanvdplas Sep 11, 2024
d2eeb08
test: coverage fungibles pallet
Daanvdplas Sep 11, 2024
538b19e
fix: comment
Daanvdplas Sep 11, 2024
ca1ccca
test: error case test coverage
Daanvdplas Sep 12, 2024
1060a91
test: no state set test
Daanvdplas Sep 12, 2024
98e89dd
refactor: test variables
Daanvdplas Sep 12, 2024
0ad8b36
test: encoding read result
Daanvdplas Sep 12, 2024
682e8f3
refactor: pallet fungibles mod.rs
Daanvdplas Sep 13, 2024
db382a8
refactor: split tests and test badorigin first
Daanvdplas Sep 13, 2024
497b2f7
refactor: assets helper functions
Daanvdplas Sep 13, 2024
65ad654
refactor: api event naming conventions
chungquantin Sep 13, 2024
6163132
fix: formatting
chungquantin Sep 13, 2024
9b7a62d
fix: event naming
chungquantin Sep 13, 2024
b38aede
fix: formatting
chungquantin Sep 13, 2024
35bedb6
refactor: event Approve -> Approval
peterwht Sep 13, 2024
3df17e1
docs: add comment for differing event names
peterwht Sep 13, 2024
5d82f9f
merge daan/api attempt 2
Daanvdplas Sep 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: pallet api extension
Daanvdplas committed Sep 10, 2024

Verified

This commit was signed with the committer’s verified signature.
Daanvdplas Daan van der Plas
commit b16430bf25f0d1a0b9dd31cde66b28def80bca5c
4 changes: 2 additions & 2 deletions extension/src/matching.rs
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ mod tests {
}

#[test]
fn func_id_matches() {
fn with_func_id_matches() {
let env = MockEnvironment::default();
assert!(WithFuncId::<ConstU32<0>>::matches(&env));

@@ -84,7 +84,7 @@ mod tests {
}

#[test]
fn func_id_does_not_match() {
fn with_func_id_does_not_match() {
let env = MockEnvironment::new(1, vec![]);
assert!(!WithFuncId::<ConstU32<0>>::matches(&env));
}
166 changes: 160 additions & 6 deletions pallets/api/src/extension.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use core::{fmt::Debug, marker::PhantomData};

use frame_support::traits::Get;
pub use pop_chain_extension::{
Config, ContractWeightsOf, DecodingFailed, DispatchCall, ReadState, Readable,
Config, ContractWeightsOf, DecodingFailed, DispatchCall, ErrorConverter, ReadState, Readable,
};
use pop_chain_extension::{
Converter, Decodes, Environment, LogTarget, Matches, Processor, Result, RetVal,
@@ -70,7 +70,7 @@ impl LogTarget for ReadStateLogTarget {

/// Conversion of a `DispatchError` to a versioned error.
pub struct VersionedErrorConverter<E>(PhantomData<E>);
impl<Error: From<(DispatchError, u8)> + Into<u32> + Debug> pop_chain_extension::ErrorConverter
impl<Error: From<(DispatchError, u8)> + Into<u32> + Debug> ErrorConverter
for VersionedErrorConverter<Error>
{
/// The log target.
@@ -136,17 +136,171 @@ fn version(env: &impl Environment) -> u8 {
mod tests {
use frame_support::pallet_prelude::Weight;
use pop_chain_extension::Ext;
use sp_core::ConstU8;

use super::*;
use super::{DispatchError::*, *};
use crate::extension::Prepender;

#[test]
fn func_id_works() {
let env = MockEnvironment { func_id: u16::from_le_bytes([1, 2]), ext_id: 0u16 };
assert_eq!(func_id(&env), 1);
let env = MockEnvironment { func_id: u16::from_le_bytes([2, 1]), ext_id: 0u16 };
assert_eq!(func_id(&env), 2);
}

#[test]
fn module_and_index_works() {
let env = MockEnvironment { func_id: 0u16, ext_id: u16::from_le_bytes([2, 3]) };
assert_eq!(module_and_index(&env), (2, 3));
let env = MockEnvironment { func_id: 0u16, ext_id: u16::from_le_bytes([3, 2]) };
assert_eq!(module_and_index(&env), (3, 2));
}

#[test]
fn version_works() {
let env = MockEnvironment { func_id: u16::from_le_bytes([1, 2]), ext_id: 0u16 };
assert_eq!(version(&env), 2);
let env = MockEnvironment { func_id: u16::from_le_bytes([2, 1]), ext_id: 0u16 };
assert_eq!(version(&env), 1);
}

#[test]
fn prepender_works() {
let env = MockEnvironment {
func_id: u16::from_le_bytes([0, 1]),
ext_id: u16::from_le_bytes([2, 3]),
func_id: u16::from_le_bytes([1, 2]),
ext_id: u16::from_le_bytes([3, 4]),
};
assert_eq!(Prepender::process(vec![0u8], &env), vec![1, 2, 3, 0]);
assert_eq!(Prepender::process(vec![0u8], &env), vec![2, 3, 4, 0]);
assert_eq!(Prepender::process(vec![0u8, 5, 10], &env), vec![2, 3, 4, 0, 5, 10]);

let env = MockEnvironment {
func_id: u16::from_le_bytes([2, 1]),
ext_id: u16::from_le_bytes([4, 3]),
};
assert_eq!(Prepender::process(vec![0u8], &env), vec![1, 4, 3, 0]);
assert_eq!(Prepender::process(vec![0u8, 5, 10], &env), vec![1, 4, 3, 0, 5, 10]);
}

#[test]
fn identified_by_first_byte_of_function_id_matches() {
let env = MockEnvironment { func_id: u16::from_le_bytes([1, 2]), ext_id: 0u16 };
assert!(IdentifiedByFirstByteOfFunctionId::<ConstU8<1>>::matches(&env));
let env = MockEnvironment { func_id: u16::from_le_bytes([2, 1]), ext_id: 0u16 };
assert!(IdentifiedByFirstByteOfFunctionId::<ConstU8<2>>::matches(&env));
}

#[test]
fn identified_by_first_byte_of_function_id_does_not_match() {
let env = MockEnvironment { func_id: u16::from_le_bytes([1, 2]), ext_id: 0u16 };
assert!(!IdentifiedByFirstByteOfFunctionId::<ConstU8<2>>::matches(&env));
let env = MockEnvironment { func_id: u16::from_le_bytes([2, 1]), ext_id: 0u16 };
assert!(!IdentifiedByFirstByteOfFunctionId::<ConstU8<1>>::matches(&env));
}

#[test]
fn dispatch_call_log_target_works() {
assert!(matches!(
<DispatchCallLogTarget as LogTarget>::LOG_TARGET,
"pop-api::extension::dispatch"
));
}

#[test]
fn read_state_log_target_works() {
assert!(matches!(
<ReadStateLogTarget as LogTarget>::LOG_TARGET,
"pop-api::extension::read-state"
));
}

#[test]
fn versioned_error_converter_works() {
// Mock versioned error.
#[derive(Debug)]
pub enum VersionedError {
V0(DispatchError),
V1(DispatchError),
}

impl From<(DispatchError, u8)> for VersionedError {
fn from(value: (DispatchError, u8)) -> Self {
let (error, version) = value;
match version {
0 => VersionedError::V0(error),
1 => VersionedError::V1(error),
_ => unimplemented!(),
}
}
}

impl From<VersionedError> for u32 {
// Mock conversion based on error and version.
fn from(value: VersionedError) -> Self {
match value {
VersionedError::V0(error) => match error {
BadOrigin => 1,
_ => 100,
},
VersionedError::V1(error) => match error {
BadOrigin => 2,
_ => 200,
},
}
}
}

for (version, error, expected_result) in vec![
(0, BadOrigin, 1),
(0, CannotLookup, 100),
(1, BadOrigin, 2),
(1, CannotLookup, 200),
] {
let env = MockEnvironment { func_id: u16::from_le_bytes([0, version]), ext_id: 0u16 };
let RetVal::Converging(result) =
VersionedErrorConverter::<VersionedError>::convert(error, &env)
.expect("should always result `Ok`")
else {
unimplemented!();
};
assert_eq!(result, expected_result);
}
}

#[test]
fn versioned_result_converter_works() {
// Mock versioned runtime result.
#[derive(Debug, PartialEq)]
pub enum VersionedRuntimeResult {
V0(u8),
V1(u8),
}

impl From<(u8, u8)> for VersionedRuntimeResult {
// Mock conversion based on result and version.
fn from(value: (u8, u8)) -> Self {
let (result, version) = value;
match version {
0 if result <= 50 => VersionedRuntimeResult::V0(result),
0 if result > 50 => VersionedRuntimeResult::V0(50),
1 if result <= 100 => VersionedRuntimeResult::V1(result),
1 if result > 100 => VersionedRuntimeResult::V1(100),
_ => unimplemented!(),
}
}
}

for (version, value, expected_result) in vec![
(0, 10, VersionedRuntimeResult::V0(10)),
(0, 100, VersionedRuntimeResult::V0(50)),
(1, 10, VersionedRuntimeResult::V1(10)),
(1, 100, VersionedRuntimeResult::V1(100)),
] {
let env = MockEnvironment { func_id: u16::from_le_bytes([0, version]), ext_id: 0u16 };
let result =
VersionedResultConverter::<u8, VersionedRuntimeResult>::convert(value, &env);
assert_eq!(result, expected_result);
}
}

struct MockEnvironment {