Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9a458d3
fix(pallet-referenda): represent `Tracks` constant as `Vec<(TrackId, …
pandres95 Feb 22, 2025
f03a5a8
fix(pallet-referenda): define `StringLike` to represent a static arra…
pandres95 Feb 22, 2025
44b58c7
chore: add prdoc
pandres95 Feb 22, 2025
906a0a0
feat(pallet-referenda): apply suggestions from @ggwpez's review
pandres95 Feb 24, 2025
279cff2
fix(pallet-referenda): make clippy happy
pandres95 Feb 24, 2025
18662d0
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
pandres95 Feb 24, 2025
df8d03e
fix: make ci happy
pandres95 Feb 24, 2025
29d1650
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
pandres95 Feb 26, 2025
91b963b
chore(pallet-referenda): fmt/add docs for `StringLike`
pandres95 Mar 13, 2025
18393f3
chore(pallet-referenda): set different types for `TrackInfo` (storage…
pandres95 Mar 13, 2025
28ad818
chore(pallet-referenda): revert change of `str_array`
pandres95 Mar 13, 2025
1afaf8c
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
pandres95 Mar 13, 2025
65706fe
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
pandres95 Mar 13, 2025
0bc3716
chore(pallet-referenda):document `tracks` constant function.
pandres95 Mar 13, 2025
3130968
feat(pallet-referenda): test `StringLike` encoding and decoding.
pandres95 Mar 13, 2025
7ef3632
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
gui1117 Mar 20, 2025
b5762a7
Merge branch 'master' into fix/represent-tracks-constant-as-a-vec-id-…
ggwpez Mar 21, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

use super::Origin;
use crate::{Balance, BlockNumber, RuntimeOrigin, DAYS, DOLLARS, HOURS};
use sp_runtime::{str_array as s, Perbill};
use pallet_referenda::string_like_track_name as s;
use sp_runtime::Perbill;
use sp_std::borrow::Cow;

/// Referendum `TrackId` type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

use crate::{Balance, BlockNumber, RuntimeOrigin, DAYS, DOLLARS, HOURS, MINUTES};
use pallet_ranked_collective::Rank;
use sp_runtime::{str_array as s, traits::Convert, Perbill};
use pallet_referenda::string_like_track_name as s;
use sp_runtime::{traits::Convert, Perbill};
use sp_std::borrow::Cow;

/// Referendum `TrackId` type.
Expand Down
7 changes: 2 additions & 5 deletions polkadot/runtime/rococo/src/governance/fellowship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

use alloc::borrow::Cow;
use frame_support::traits::{MapSuccess, TryMapSuccess};
use pallet_referenda::{Track, TrackInfo};
use sp_runtime::{
str_array as s,
traits::{CheckedReduceBy, ConstU16, Replace, ReplaceWithDefault},
};
use pallet_referenda::{string_like_track_name as s, Track, TrackInfo};
use sp_runtime::traits::{CheckedReduceBy, ConstU16, Replace, ReplaceWithDefault};

use super::*;
use crate::{CENTS, DAYS};
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/governance/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::*;

use alloc::borrow::Cow;
use sp_runtime::str_array as s;
use pallet_referenda::string_like_track_name as s;

const fn percent(x: i32) -> sp_arithmetic::FixedI64 {
sp_arithmetic::FixedI64::from_rational(x as u128, 100)
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/governance/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::*;

use alloc::borrow::Cow;
use sp_runtime::str_array as s;
use pallet_referenda::string_like_track_name as s;

const fn percent(x: i32) -> sp_arithmetic::FixedI64 {
sp_arithmetic::FixedI64::from_rational(x as u128, 100)
Expand Down
24 changes: 24 additions & 0 deletions prdoc/pr_7671.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
title: 'Fix: [Referenda Tracks] Resolve representation issues that are breaking PJS apps'

doc:
- audience: Runtime Dev
description: |-
The PR #2072 introduces a change in the representation of the `name` field, from a `&str` to a `[u8; N]` array. This is because
tracks can be retrieves from storage, and thus, a static string representation doesn't meet with the storage traits requirements.

This PR encapsulates this array into a `StringLike` structure that allows representing the value as a `str` for SCALE and metadata
purposes. This is to avoid breaking changes.

This PR also reverts the representation of the `Tracks` constant as a tuple of `(TrackId, TrackInfo)` to accomplish the same
purpose of avoid breaking changes to runtime users and clients.
crates:
- name: pallet-referenda
bump: minor
- name: collectives-westend-runtime
bump: minor
- name: kitchensink-runtime
bump: minor
- name: rococo-runtime
bump: minor
- name: westend-runtime
bump: minor
3 changes: 2 additions & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ use sp_runtime::traits::TransactionExtension;
// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles
// <https://github.com/paritytech/polkadot-sdk/issues/226>
use pallet_broker::TaskId;
use pallet_referenda::string_like_track_name as s;
#[allow(deprecated)]
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
Expand All @@ -107,7 +108,7 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_runtime::{
curve::PiecewiseLinear,
generic, impl_opaque_keys, str_array as s,
generic, impl_opaque_keys,
traits::{
self, AccountIdConversion, BlakeTwo256, Block as BlockT, Bounded, ConvertInto,
MaybeConvert, NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup,
Expand Down
16 changes: 10 additions & 6 deletions substrate/frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ use self::branch::{BeginDecidingBranch, OneFewerDecidingBranch, ServiceBranch};
pub use self::{
pallet::*,
types::{
BalanceOf, BlockNumberFor, BoundedCallOf, CallOf, Curve, DecidingStatus, DecidingStatusOf,
Deposit, InsertSorted, NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex,
ReferendumInfo, ReferendumInfoOf, ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf,
TallyOf, Track, TrackIdOf, TrackInfo, TrackInfoOf, TracksInfo, VotesOf,
string_like_track_name, BalanceOf, BlockNumberFor, BoundedCallOf, CallOf, Curve,
DecidingStatus, DecidingStatusOf, Deposit, InsertSorted, NegativeImbalanceOf,
PalletsOriginOf, ReferendumIndex, ReferendumInfo, ReferendumInfoOf, ReferendumStatus,
ReferendumStatusOf, ScheduleAddressOf, StringLike, TallyOf, Track, TrackIdOf, TrackInfo,
TrackInfoOf, TracksInfo, VotesOf,
},
weights::WeightInfo,
};
Expand Down Expand Up @@ -225,8 +226,11 @@ pub mod pallet {
#[pallet::extra_constants]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
#[pallet::constant_name(Tracks)]
Comment thread
pandres95 marked this conversation as resolved.
fn tracks() -> Vec<Track<TrackIdOf<T, I>, BalanceOf<T, I>, BlockNumberFor<T, I>>> {
T::Tracks::tracks().map(|t| t.into_owned()).collect()
fn tracks() -> Vec<(TrackIdOf<T, I>, TrackInfo<BalanceOf<T, I>, BlockNumberFor<T, I>>)> {
Comment thread
pandres95 marked this conversation as resolved.
Outdated
T::Tracks::tracks()
.map(|t| t.into_owned())
.map(|Track { id, info }| (id, info))
.collect()
}
}

Expand Down
3 changes: 1 addition & 2 deletions substrate/frame/referenda/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! The crate's tests.

use super::*;
use crate::{self as pallet_referenda, types::Track};
use crate::{self as pallet_referenda, string_like_track_name as s, types::Track};
use alloc::borrow::Cow;
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::{
Expand All @@ -30,7 +30,6 @@ use frame_support::{
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use sp_runtime::{
str_array as s,
traits::{BlakeTwo256, Hash},
BuildStorage, DispatchResult, Perbill,
};
Expand Down
53 changes: 48 additions & 5 deletions substrate/frame/referenda/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use super::*;
use alloc::borrow::Cow;
use codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
use codec::{Compact, Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
use core::fmt::Debug;
use frame_support::{
traits::{schedule::v3::Anon, Bounded},
Expand Down Expand Up @@ -118,13 +118,56 @@ pub struct Deposit<AccountId, Balance> {

pub const DEFAULT_MAX_TRACK_NAME_LEN: usize = 25;

#[inline]
pub const fn string_like_track_name<const N: usize>(name: &str) -> StringLike<N> {
StringLike(sp_runtime::str_array(name))
}

#[derive(Clone, Eq, DecodeWithMemTracking, PartialEq, Debug)]
pub struct StringLike<const N: usize>(pub [u8; N]);
Comment thread
pandres95 marked this conversation as resolved.
Outdated

use codec::Input;
use scale_info::Type;
Comment thread
pandres95 marked this conversation as resolved.
Outdated
impl<const N: usize> TypeInfo for StringLike<N> {
type Identity = <&'static str as TypeInfo>::Identity;

fn type_info() -> Type {
<&str as TypeInfo>::type_info()
}
}

impl<const N: usize> MaxEncodedLen for StringLike<N> {
fn max_encoded_len() -> usize {
<Compact<u32> as MaxEncodedLen>::max_encoded_len().saturating_add(N)
}
}

impl<const N: usize> Encode for StringLike<N> {
fn encode(&self) -> Vec<u8> {
use codec::Compact;
(Compact(N as u32), self.0).encode()
}
}

impl<const N: usize> Decode for StringLike<N> {
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
let Compact(size): Compact<u32> = Decode::decode(input)?;
if size != N as u32 {
return Err("Invalid size".into());
}

let bytes: [u8; N] = Decode::decode(input)?;
Ok(Self(bytes))
}
}
Comment thread
pandres95 marked this conversation as resolved.

/// Detailed information about the configuration of a referenda track
#[derive(
Clone, Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, Eq, PartialEq, Debug,
)]
pub struct TrackInfo<Balance, Moment, const N: usize = DEFAULT_MAX_TRACK_NAME_LEN> {
/// Name of this track.
pub name: [u8; N],
pub name: StringLike<N>,
Comment thread
bkchr marked this conversation as resolved.
Outdated
/// A limit for the number of referenda on this track that can be being decided at once.
/// For Root origin this should generally be just one.
pub max_deciding: u32,
Expand Down Expand Up @@ -584,7 +627,7 @@ impl Debug for Curve {
mod tests {
use super::*;
use frame_support::traits::ConstU32;
use sp_runtime::{str_array as s, PerThing};
use sp_runtime::PerThing;

const fn percent(x: u128) -> FixedI64 {
FixedI64::from_rational(x, 100)
Expand Down Expand Up @@ -741,7 +784,7 @@ mod tests {
Track {
id: 1u8,
info: TrackInfo {
name: s("root"),
name: string_like_track_name("root"),
max_deciding: 1,
decision_deposit: 10,
prepare_period: 4,
Expand All @@ -763,7 +806,7 @@ mod tests {
Track {
id: 0u8,
info: TrackInfo {
name: s("none"),
name: string_like_track_name("none"),
max_deciding: 3,
decision_deposit: 1,
prepare_period: 2,
Expand Down