Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 4 additions & 10 deletions code/Cargo.lock

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

2 changes: 1 addition & 1 deletion code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions code/parachain/frame/cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,8 @@ impl<T: Config> Pallet<T> {
/// 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<T>,
code_id: CosmwasmCodeId,
Expand Down
6 changes: 3 additions & 3 deletions code/parachain/frame/farming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 37 additions & 31 deletions code/parachain/frame/pallet-multihop-xcm-ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub use pallet::*;

mod prelude;

#[cfg(test)]
mod tests;

#[frame_support::pallet]
pub mod pallet {

Expand Down Expand Up @@ -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.
<Pallet<T>>::deposit_event(crate::Event::<T>::MultihopXcmMemo {
reason: MultihopEventReason::FailedToConvertAddressToBytes,
from,
Expand All @@ -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 };
Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -553,13 +557,13 @@ pub mod pallet {
}

//route does not exist
let Ok(mut route) = RouteIdToRoutePath::<T>::try_get(route_id) else{
let Ok(mut route) = RouteIdToRoutePath::<T>::try_get(route_id) else {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::MultiHopRouteDoesNotExist,
});
return None;
return None
};

let route_len = route.len();
Expand All @@ -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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::MultiHopRouteExistButNotConfigured,
});
return None;
return None
};

if addresses.len() != route_len {
Expand All @@ -592,13 +596,13 @@ pub mod pallet {
let result: core::result::Result<Vec<bech32_no_std::u5>, 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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::FailedToDeriveCosmosAddressFromBytes,
});
return None;
return None
};

let Ok(name) = String::from_utf8(chain_name.into()) else {
Expand All @@ -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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::FailedToEncodeBech32Address,
});
return None;
return None
};

MultiAddress::<AccoindIdOf<T>>::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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::FailedToDecodeDestAccountId,
});
return None;
return None
};
MultiAddress::<AccoindIdOf<T>>::Id(account_id_from)
};
Expand All @@ -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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::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 {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::DoesNotSupportNonFungible,
});
return None;
return None
};

let mut memo: Option<<T as pallet_ibc::Config>::MemoMessage> = None;
Expand All @@ -675,27 +680,28 @@ pub mod pallet {
.collect();

let memo_data = Pallet::<T>::create_memo(vec);
let Ok(memo_data) = memo_data else{
let Ok(memo_data) = memo_data else {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::FailedCreateMemo,
});
return None;
return None
};
if let Some(memo_data) = memo_data {
let memo_result = <T as pallet_ibc::Config>::MemoMessage::try_from(
Map::try_from_xc_memo(memo_data)?,
);

let Ok(memo_result) = memo_result else{
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason: MultihopEventReason::FailedToConvertMemoIntoPalletIbcMemoMessageType,
});
return None;
};
let Ok(memo_result) = memo_result else {
<Pallet<T>>::deposit_event(crate::Event::<T>::FailedCallback {
origin_address: address_from,
route_id,
reason:
MultihopEventReason::FailedToConvertMemoIntoPalletIbcMemoMessageType,
});
return None
};
memo = Some(memo_result)
}

Expand Down
38 changes: 38 additions & 0 deletions code/parachain/frame/pallet-multihop-xcm-ibc/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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<bech32_no_std::u5> = address
.into_iter()
.map(|byte| bech32_no_std::u5::try_from_u8(byte).unwrap())
.collect::<Vec<bech32_no_std::u5>>();
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);
}
41 changes: 19 additions & 22 deletions code/parachain/frame/staking-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,20 +1729,17 @@ pub(crate) fn accumulate_reward<T: Config>(
// 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();

Expand All @@ -1751,10 +1748,10 @@ pub(crate) fn accumulate_reward<T: Config>(
// reward_rate_period_seconds
let Some(periods_surpassed) =
NonZeroU64::new(<u64 as Div<NonZeroU64>>::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}");
Expand All @@ -1767,11 +1764,11 @@ pub(crate) fn accumulate_reward<T: Config>(
// reward_rate_amount
let Some(maximum_releasable_periods) =
NonZeroU128::new(<u128 as Div<NonZeroU128>>::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 ( -------------------- , -------------------------- )
Expand Down Expand Up @@ -1808,7 +1805,7 @@ pub(crate) fn accumulate_reward<T: Config>(
total_shares.into(),
Rounding::Down,
) else {
return RewardAccumulationCalculationOutcome::Overflow;
return RewardAccumulationCalculationOutcome::Overflow
};

value
Expand All @@ -1817,9 +1814,9 @@ pub(crate) fn accumulate_reward<T: Config>(
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}");

Expand Down
Loading