diff --git a/code/Cargo.lock b/code/Cargo.lock index 7ac7a090bea..f7a7301bcdd 100644 --- a/code/Cargo.lock +++ b/code/Cargo.lock @@ -830,12 +830,6 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" -[[package]] -name = "bech32-no_std" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4756f018d917ebf7d60361ce6b48e88a9f1904525429e501bc0841b9436d416e" - [[package]] name = "beef" version = "0.5.2" @@ -2053,7 +2047,7 @@ dependencies = [ name = "composable-traits" version = "1.0.0" dependencies = [ - "bech32-no_std", + "bech32", "bitflags 1.3.2", "composable-support", "cosmwasm-schema 1.2.5", @@ -3326,7 +3320,7 @@ dependencies = [ name = "cw-xc-gateway" version = "0.1.0" dependencies = [ - "bech32-no_std", + "bech32", "cosmwasm-std", "cw-storage-plus", "cw-utils", @@ -9448,7 +9442,7 @@ dependencies = [ name = "pallet-multihop-xcm-ibc" version = "1.0.0" dependencies = [ - "bech32-no_std", + "bech32", "composable-support", "composable-traits", "frame-benchmarking", @@ -19219,7 +19213,7 @@ dependencies = [ name = "xc-core" version = "0.1.0" dependencies = [ - "bech32-no_std", + "bech32", "cosmwasm-schema 1.2.5", "cosmwasm-std", "cw-storage-plus", diff --git a/code/Cargo.toml b/code/Cargo.toml index f869d96f4e2..f54deadd540 100644 --- a/code/Cargo.toml +++ b/code/Cargo.toml @@ -102,7 +102,7 @@ session-benchmarking = { package = "cumulus-pallet-session-benchmarking", git = aura = { package = "pallet-aura", git = "https://github.com/ComposableFi/substrate", rev = "c74d73bfe1e4f725b3f7d81c997ba9c940bf6005", default-features = false } sha2 = { version = "0.10.7", default-features = false } -bech32-no_std = { version = "0.7.3", default-features = false, features = [ +bech32-no_std = { package = "bech32", version = "0.9.1", default-features = false, features = [ "strict", ] } polkadot-parachain = { git = "https://github.com/paritytech/polkadot", rev = "c22e1c4173bf6966f5d1980f4299f7abe836f0c1", default-features = false } diff --git a/code/parachain/frame/cosmwasm/src/lib.rs b/code/parachain/frame/cosmwasm/src/lib.rs index 6c71d32c258..62f47f6147d 100644 --- a/code/parachain/frame/cosmwasm/src/lib.rs +++ b/code/parachain/frame/cosmwasm/src/lib.rs @@ -782,8 +782,8 @@ impl Pallet { /// This function will update the state if the `code_id` is changing: /// 1. Refcount of the new `code_id` is incremented. /// 2. Refcount of the old `code_id` is decremented. - /// 3. Delete every entry related to old `code_id` if - /// the refcount is 0. And unreserve the bonded funds. + /// 3. Delete every entry related to old `code_id` if the refcount is 0. And unreserve the + /// bonded funds. pub(crate) fn do_set_contract_meta( contract: &AccountIdOf, code_id: CosmwasmCodeId, diff --git a/code/parachain/frame/farming/src/lib.rs b/code/parachain/frame/farming/src/lib.rs index 0d00bc13bcf..8264df6303c 100644 --- a/code/parachain/frame/farming/src/lib.rs +++ b/code/parachain/frame/farming/src/lib.rs @@ -3,9 +3,9 @@ //! staking certain tokens. //! //! A reward schedule consists of two items: -//! 1. The number of periods set globally as a configuration for all pools. -//! This number is ultimately measured in blocks; e.g., if a period is -//! defined as 10 blocks, then a period count of 10 means 100 blocks. +//! 1. The number of periods set globally as a configuration for all pools. This number is +//! ultimately measured in blocks; e.g., if a period is defined as 10 blocks, then a period count +//! of 10 means 100 blocks. //! 2. The amount of reward tokens paid in that period. //! //! Users are only paid a share of the rewards in the period if they have diff --git a/code/parachain/frame/pallet-multihop-xcm-ibc/src/lib.rs b/code/parachain/frame/pallet-multihop-xcm-ibc/src/lib.rs index bb6441d2974..d18389e6fa7 100644 --- a/code/parachain/frame/pallet-multihop-xcm-ibc/src/lib.rs +++ b/code/parachain/frame/pallet-multihop-xcm-ibc/src/lib.rs @@ -34,6 +34,9 @@ pub use pallet::*; mod prelude; +#[cfg(test)] +mod tests; + #[frame_support::pallet] pub mod pallet { @@ -241,10 +244,10 @@ pub mod pallet { ) -> Option<()> { let signed_account_id = RawOrigin::Signed(from.clone()); let acc_bytes = T::AccountId::encode(&to); - let Ok(id) = acc_bytes.try_into() else{ - //we need to emit event when error or succseed for front-end becase - //this function is called from pallet-ibc - //deliver extrinsic and only relayer will get the error. + let Ok(id) = acc_bytes.try_into() else { + //we need to emit event when error or succseed for front-end becase + //this function is called from pallet-ibc + //deliver extrinsic and only relayer will get the error. >::deposit_event(crate::Event::::MultihopXcmMemo { reason: MultihopEventReason::FailedToConvertAddressToBytes, from, @@ -253,7 +256,7 @@ pub mod pallet { asset_id: currency, is_error: true, }); - return None; + return None }; //if para id is none then parent is 1 if para id is some then parent is 0 let parent = if para_id.is_some() { 0 } else { 1 }; @@ -361,7 +364,8 @@ pub mod pallet { let name = String::from_utf8(name).map_err(|_| { DispatchError::Other("Failed to convert chain name from utf8") })?; - bech32_no_std::encode(&name, data.clone()).map_err(|_| { + bech32_no_std::encode(&name, data.clone(), bech32_no_std::Variant::Bech32) + .map_err(|_| { DispatchError::Other( "Failed to convert chain name and address into bech32", ) @@ -553,13 +557,13 @@ pub mod pallet { } //route does not exist - let Ok(mut route) = RouteIdToRoutePath::::try_get(route_id) else{ + let Ok(mut route) = RouteIdToRoutePath::::try_get(route_id) else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::MultiHopRouteDoesNotExist, }); - return None; + return None }; let route_len = route.len(); @@ -568,13 +572,13 @@ pub mod pallet { let mut chain_info_iter = route.into_iter(); //route does not exist - let Some((next_chain_info, chain_name)) = chain_info_iter.next() else{ + let Some((next_chain_info, chain_name)) = chain_info_iter.next() else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::MultiHopRouteExistButNotConfigured, }); - return None; + return None }; if addresses.len() != route_len { @@ -592,13 +596,13 @@ pub mod pallet { let result: core::result::Result, bech32_no_std::Error> = raw_address_to.into_iter().map(bech32_no_std::u5::try_from_u8).collect(); - let Ok(data) = result else{ + let Ok(data) = result else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::FailedToDeriveCosmosAddressFromBytes, }); - return None; + return None }; let Ok(name) = String::from_utf8(chain_name.into()) else { @@ -607,29 +611,30 @@ pub mod pallet { route_id, reason: MultihopEventReason::FailedToDeriveChainNameFromUtf8, }); - return None; + return None }; - let Ok(name) = bech32_no_std::encode(&name, data) else { + let Ok(name) = bech32_no_std::encode(&name, data, bech32_no_std::Variant::Bech32) + else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::FailedToEncodeBech32Address, }); - return None; + return None }; MultiAddress::>::Raw(name.into_bytes()) } else { let account_from = sp_runtime::AccountId32::new(raw_address_to); let mut account_from_32: &[u8] = sp_runtime::AccountId32::as_ref(&account_from); - let Ok(account_id_from) = T::AccountId::decode(&mut account_from_32) else{ + let Ok(account_id_from) = T::AccountId::decode(&mut account_from_32) else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::FailedToDecodeDestAccountId, }); - return None; + return None }; MultiAddress::>::Id(account_id_from) }; @@ -644,24 +649,24 @@ pub mod pallet { let account_from = sp_runtime::AccountId32::new(address_from); let mut account_from_32: &[u8] = sp_runtime::AccountId32::as_ref(&account_from); - let Ok(account_id_from) = T::AccountId::decode(&mut account_from_32) else{ + let Ok(account_id_from) = T::AccountId::decode(&mut account_from_32) else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::FailedToDecodeSenderAccountId, }); - return None; + return None }; let signed_account_id = RawOrigin::Signed(account_id_from.clone()); //do not support non fungible. - let Fungibility::Fungible(ref amount) = asset.fun else{ + let Fungibility::Fungible(ref amount) = asset.fun else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::DoesNotSupportNonFungible, }); - return None; + return None }; let mut memo: Option<::MemoMessage> = None; @@ -675,27 +680,28 @@ pub mod pallet { .collect(); let memo_data = Pallet::::create_memo(vec); - let Ok(memo_data) = memo_data else{ + let Ok(memo_data) = memo_data else { >::deposit_event(crate::Event::::FailedCallback { origin_address: address_from, route_id, reason: MultihopEventReason::FailedCreateMemo, }); - return None; + return None }; if let Some(memo_data) = memo_data { let memo_result = ::MemoMessage::try_from( Map::try_from_xc_memo(memo_data)?, ); - let Ok(memo_result) = memo_result else{ - >::deposit_event(crate::Event::::FailedCallback { - origin_address: address_from, - route_id, - reason: MultihopEventReason::FailedToConvertMemoIntoPalletIbcMemoMessageType, - }); - return None; - }; + let Ok(memo_result) = memo_result else { + >::deposit_event(crate::Event::::FailedCallback { + origin_address: address_from, + route_id, + reason: + MultihopEventReason::FailedToConvertMemoIntoPalletIbcMemoMessageType, + }); + return None + }; memo = Some(memo_result) } diff --git a/code/parachain/frame/pallet-multihop-xcm-ibc/src/tests.rs b/code/parachain/frame/pallet-multihop-xcm-ibc/src/tests.rs new file mode 100644 index 00000000000..0f5451c0b2e --- /dev/null +++ b/code/parachain/frame/pallet-multihop-xcm-ibc/src/tests.rs @@ -0,0 +1,38 @@ +#[test] +fn test_memo() { + let encoded_address = "centauri17ak6lczt2c4gxja432enf870lv8y99qvs84g9k"; + println!("start: {}", encoded_address); + let (hrp, data, v) = bech32_no_std::decode(encoded_address).unwrap(); + println!("Decoded Variant: {:?}", v); + assert!(v == bech32_no_std::Variant::Bech32); + + println!("Decoded HRP: {}", hrp); + println!("Decoded Data: {:?}", data); + println!("Decoded Data len: {:?}", data.len()); + let mut decoded_data = Vec::new(); + for byte in data.clone() { + decoded_data.push(u8::try_from(byte).unwrap()); + } + print!("Decoded Data: {:?}\n", decoded_data.clone()); + let res = bech32_no_std::encode(&hrp, data.clone(), bech32_no_std::Variant::Bech32).unwrap(); + print!("Encided back Data: {:?}\n", res); + assert_eq!(encoded_address, res); + + let address: [u8; 32] = [ + 30, 29, 22, 26, 31, 24, 2, 11, 10, 24, 21, 8, 6, 18, 29, 21, 17, 10, 25, 19, 9, 7, 30, 15, + 31, 12, 7, 4, 5, 5, 0, 12, + ]; + assert_eq!(decoded_data, address); + let name = "centauri"; + let data1: Vec = address + .into_iter() + .map(|byte| bech32_no_std::u5::try_from_u8(byte).unwrap()) + .collect::>(); + assert_eq!(data, data1); + println!("data1 : {:?}", data1.clone()); + // println!("data : {:?}", data.clone()); + let name = String::from_utf8(name.into()).unwrap(); + let res = bech32_no_std::encode(&name, data1.clone(), bech32_no_std::Variant::Bech32).unwrap(); + println!("res : {}", res); + assert_eq!(encoded_address, res); +} diff --git a/code/parachain/frame/staking-rewards/src/lib.rs b/code/parachain/frame/staking-rewards/src/lib.rs index 2f0408b10bd..7a17167a754 100644 --- a/code/parachain/frame/staking-rewards/src/lib.rs +++ b/code/parachain/frame/staking-rewards/src/lib.rs @@ -1729,20 +1729,17 @@ pub(crate) fn accumulate_reward( // test with proptest/ kani. The nonzero checks can be left outside of said function. // short-circuit if the reward rate amount is zero - let Some(reward_rate_amount) = NonZeroU128::new(reward.reward_rate.amount.into()) - else { - return RewardAccumulationCalculationOutcome::Success - }; + let Some(reward_rate_amount) = NonZeroU128::new(reward.reward_rate.amount.into()) else { + return RewardAccumulationCalculationOutcome::Success + }; // REVIEW(benluelo): Should this be a user-facing error? Or would defensively saturating at zero // for elapsed_time be better? This should never be hit, and if it is then it's either a logic // error or the chain state is wonky (in which case there are probably bigger issues than this // hook!) - let Some(elapsed_time) = now_seconds - .checked_sub(reward.last_updated_timestamp) - else { - return RewardAccumulationCalculationOutcome::BackToTheFuture - }; + let Some(elapsed_time) = now_seconds.checked_sub(reward.last_updated_timestamp) else { + return RewardAccumulationCalculationOutcome::BackToTheFuture + }; let reward_rate_period_seconds = reward.reward_rate.period.as_secs(); @@ -1751,10 +1748,10 @@ pub(crate) fn accumulate_reward( // reward_rate_period_seconds let Some(periods_surpassed) = NonZeroU64::new(>::div(elapsed_time, reward_rate_period_seconds)) - else { - // if no periods have been surpassed, short-circuit - return RewardAccumulationCalculationOutcome::Success - }; + else { + // if no periods have been surpassed, short-circuit + return RewardAccumulationCalculationOutcome::Success + }; let total_locked_rewards: u128 = T::AssetsTransactor::balance_on_hold(asset_id, pool_account).into(); log::info!("total_locked_rewards = {total_locked_rewards}"); @@ -1767,11 +1764,11 @@ pub(crate) fn accumulate_reward( // reward_rate_amount let Some(maximum_releasable_periods) = NonZeroU128::new(>::div(total_locked_rewards, reward_rate_amount)) - else { - // if the maximum releasable periods is zero, then that means the pot doesn't have - // enough in it to fund a single period. - return RewardAccumulationCalculationOutcome::RewardsPotEmpty; - }; + else { + // if the maximum releasable periods is zero, then that means the pot doesn't have + // enough in it to fund a single period. + return RewardAccumulationCalculationOutcome::RewardsPotEmpty + }; // ( total_locked_rewards elapsed_time ) // min ( -------------------- , -------------------------- ) @@ -1808,7 +1805,7 @@ pub(crate) fn accumulate_reward( total_shares.into(), Rounding::Down, ) else { - return RewardAccumulationCalculationOutcome::Overflow; + return RewardAccumulationCalculationOutcome::Overflow }; value @@ -1817,9 +1814,9 @@ pub(crate) fn accumulate_reward( let Some(new_total_rewards) = newly_accumulated_rewards .checked_add(unstaked_shares_adjustment) .and_then(|x| x.checked_add(reward.total_rewards.into())) - else { - return RewardAccumulationCalculationOutcome::Overflow; - }; + else { + return RewardAccumulationCalculationOutcome::Overflow + }; log::info!("asset_id: {asset_id:?}; new_total_rewards = {new_total_rewards}"); diff --git a/code/xcvm/lib/core/src/transport/ibc/ics20/hook.rs b/code/xcvm/lib/core/src/transport/ibc/ics20/hook.rs index 1b42f251435..0677d8290d7 100644 --- a/code/xcvm/lib/core/src/transport/ibc/ics20/hook.rs +++ b/code/xcvm/lib/core/src/transport/ibc/ics20/hook.rs @@ -38,7 +38,7 @@ pub fn derive_intermediate_sender( let sender_str = alloc::format!("{channel}/{original_sender}"); let sender_hash_32 = addess_hash(SENDER_PREFIX, sender_str.as_bytes()); let sender = sender_hash_32.to_base32(); - bech32_no_std::encode(bech32_prefix, sender) + bech32_no_std::encode(bech32_prefix, sender, bech32_no_std::Variant::Bech32) } /// see https://github.com/osmosis-labs/osmosis/tree/main/x/ibc-hooks