From e1f67a8889be714ed06300d7455db03f736d8156 Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Thu, 20 Jun 2024 14:26:30 +0200 Subject: [PATCH] refactor: easier review --- pop-api/examples/fungibles/lib.rs | 15 +++---- pop-api/src/lib.rs | 2 +- pop-api/src/v0/nfts.rs | 42 +++++++++++++++++++ .../src/{extensions/mod.rs => extensions.rs} | 6 +-- runtime/devnet/src/lib.rs | 2 + .../{extensions => }/tests/local_fungibles.rs | 15 +++---- .../devnet/src/{extensions => }/tests/mod.rs | 16 ++++--- 7 files changed, 71 insertions(+), 27 deletions(-) rename runtime/devnet/src/{extensions/mod.rs => extensions.rs} (99%) rename runtime/devnet/src/{extensions => }/tests/local_fungibles.rs (98%) rename runtime/devnet/src/{extensions => }/tests/mod.rs (94%) diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index e84f4e1a..59040590 100755 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -25,7 +25,7 @@ mod fungibles { impl Fungibles { #[ink(constructor, payable)] pub fn new() -> Self { - ink::env::debug_println!("PopApiAssetsExample::new"); + ink::env::debug_println!("PopApiFungiblesExample::new"); Default::default() } @@ -62,7 +62,7 @@ mod fungibles { #[ink(message)] pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::transfer: id: {:?}, to: {:?} value: {:?}", + "PopApiFungiblesExample::transfer: id: {:?}, to: {:?} value: {:?}", id, to, value, @@ -80,11 +80,12 @@ mod fungibles { from: Option, to: Option, value: Balance, - // Size needs to be known at compile time or ink's `Vec` + // In the standard a `[u8]`, but the size needs to be known at compile time ink's `Vec` + // has to be used. data: Vec, ) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", + "PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}", id, from, to, @@ -113,7 +114,7 @@ mod fungibles { #[ink(message)] pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}", + "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}", id, admin, min_balance, @@ -132,7 +133,7 @@ mod fungibles { decimals: u8, ) -> Result<()> { ink::env::debug_println!( - "PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", + "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}", id, name, symbol, @@ -155,7 +156,7 @@ mod fungibles { #[ink::test] fn default_works() { - PopApiAssetsExample::new(); + PopApiFungiblesExample::new(); } } } diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index 665f522b..a785aebe 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -12,8 +12,8 @@ pub mod error; pub mod primitives; pub mod v0; -// type AccountId = ::AccountId; type AccountId = AccountId32; +// TODO: do the same as above and check expanded code. type Balance = ::Balance; type BlockNumber = ::BlockNumber; type StringLimit = u32; diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 9615e1c3..2de306a9 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -828,4 +828,46 @@ mod types { /// Tokens will be received. Receive, } + + macro_rules! impl_codec_bitflags { + ($wrapper:ty, $size:ty, $bitflag_enum:ty) => { + impl MaxEncodedLen for $wrapper { + fn max_encoded_len() -> usize { + <$size>::max_encoded_len() + } + } + impl Encode for $wrapper { + fn using_encoded R>(&self, f: F) -> R { + self.0.bits().using_encoded(f) + } + } + impl EncodeLike for $wrapper {} + impl Decode for $wrapper { + fn decode( + input: &mut I, + ) -> core::result::Result { + let field = <$size>::decode(input)?; + Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?)) + } + } + + impl TypeInfo for $wrapper { + type Identity = Self; + + fn type_info() -> Type { + Type::builder() + .path(Path::new("BitFlags", module_path!())) + .type_params(vec![TypeParameter::new( + "T", + Some(meta_type::<$bitflag_enum>()), + )]) + .composite( + Fields::unnamed() + .field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))), + ) + } + } + }; + } + pub(crate) use impl_codec_bitflags; } diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions.rs similarity index 99% rename from runtime/devnet/src/extensions/mod.rs rename to runtime/devnet/src/extensions.rs index c9cd5f46..014893f9 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions.rs @@ -29,9 +29,6 @@ use crate::{ RuntimeOrigin, UNIT, }; -#[cfg(test)] -mod tests; - const LOG_TARGET: &str = "pop-api::extension"; type ContractSchedule = ::Schedule; @@ -39,6 +36,7 @@ type ContractSchedule = ::Schedule; #[derive(Default)] pub struct PopApiExtension; +// TODO: check removal or simplification of trait bounds. impl ChainExtension for PopApiExtension where T: pallet_contracts::Config @@ -75,7 +73,7 @@ where } } -fn convert_to_status_code(error: DispatchError) -> u32 { +pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 { match error { _ => { let mut encoded_error = error.encode(); diff --git a/runtime/devnet/src/lib.rs b/runtime/devnet/src/lib.rs index a82be804..416a3298 100644 --- a/runtime/devnet/src/lib.rs +++ b/runtime/devnet/src/lib.rs @@ -10,6 +10,8 @@ mod extensions; mod weights; // Public due to integration tests crate. pub mod config; +#[cfg(test)] +mod tests; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; diff --git a/runtime/devnet/src/extensions/tests/local_fungibles.rs b/runtime/devnet/src/tests/local_fungibles.rs similarity index 98% rename from runtime/devnet/src/extensions/tests/local_fungibles.rs rename to runtime/devnet/src/tests/local_fungibles.rs index e7e71019..bc13fbde 100644 --- a/runtime/devnet/src/extensions/tests/local_fungibles.rs +++ b/runtime/devnet/src/tests/local_fungibles.rs @@ -3,11 +3,8 @@ use super::*; use pop_api::{ - v0::{ - assets::use_cases::fungibles::FungiblesError::*, - dispatch_error::{ArithmeticError::*, TokenError::*}, - }, - PopApiError::*, + error::{ArithmeticError::*, PopApiError::*, TokenError::*}, + v0::assets::use_cases::fungibles::FungiblesError::*, }; const ASSET_ID: AssetId = 1; @@ -260,6 +257,10 @@ fn asset_exists_works() { // - reserve(): Overflow, LiquidityRestrictions; frozen // - Callback // - StorageDepositLimitExhausted +// todo: errors: +// - TokenErrors +// - Arithmetic +// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn create_works() { @@ -322,10 +323,6 @@ fn set_metadata_works() { }); } -// todo: errors: -// - TokenErrors -// - Arithmetic -// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125 #[test] #[ignore] fn transfer_from_mint_works() { diff --git a/runtime/devnet/src/extensions/tests/mod.rs b/runtime/devnet/src/tests/mod.rs similarity index 94% rename from runtime/devnet/src/extensions/tests/mod.rs rename to runtime/devnet/src/tests/mod.rs index 26b0802e..c13030a3 100644 --- a/runtime/devnet/src/extensions/tests/mod.rs +++ b/runtime/devnet/src/tests/mod.rs @@ -1,11 +1,15 @@ #![cfg(test)] -use super::*; -use crate::{config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, System}; +use crate::{ + config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System, + Weight, UNIT, +}; +use codec::{Decode, Encode}; +use frame_support::traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect}; use frame_system::Config; use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue}; use pop_api::error::{ArithmeticError, PopApiError, TokenError, TransactionalError}; -use sp_runtime::{traits::Hash, AccountId32, BuildStorage}; +use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError}; mod local_fungibles; @@ -124,7 +128,7 @@ fn encoding_decoding_dispatch_error() { let index = get_pallet_index::(); let mut error = pallet_assets::Error::NotFrozen::.encode(); - error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0); + error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0); let message = None; let error = DispatchError::Module(ModuleError { index, @@ -251,8 +255,8 @@ fn dispatch_error_to_status_code_to_pop_api_error_works() { ]; for (error, pop_api_error) in test_cases { // Show that the encoding and decoding of the PopApiError <> u32 (status code) works. - let status_code = convert_to_status_code(error); - let error = pop_api::convert_to_pop_api_error(status_code); + let status_code = crate::extensions::convert_to_status_code(error); + let error = pop_api::error::convert_to_pop_api_error(status_code); assert_eq!(pop_api_error, error,); } }