Repository: paritytech/substrate
File: frame/im-online/src/lib.rs (Rust)

Code before:

pub type AuthIndex = u32;

/// Heartbeat which is sent/received.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Heartbeat<BlockNumber>
where
	BlockNumber: PartialEq + Eq + Decode + Encode,
{
	/// Block number at the time heartbeat is created..
	pub block_number: BlockNumber,
	/// A state of local network (peer id and external addresses)
	pub network_state: OpaqueNetworkState,
	/// Index of the current session.
	pub session_index: SessionIndex,
	/// An index of the authority on the list of validators.
	pub authority_index: AuthIndex,
	/// The length of session validator set
	pub validators_len: u32,
}

/// A type that is the same as [`OpaqueNetworkState`] but with [`Vec`] replaced with
/// [`WeakBoundedVec<Limit>`] where Limit is the respective size limit
#[derive(Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen)]
#[codec(mel_bound(EncodingLimit: Get<u32>, AddressesLimit: Get<u32>))]
pub struct BoundedOpaqueNetworkState<EncodingLimit, AddressesLimit>
where
	EncodingLimit: Get<u32>,
	AddressesLimit: Get<u32>,
{
	/// PeerId of the local node in SCALE encoded.
	pub peer_id: WeakBoundedVec<u8, EncodingLimit>,
	/// List of addresses the node knows it can be reached as.
	pub external_addresses: WeakBoundedVec<WeakBoundedVec<u8, EncodingLimit>, AddressesLimit>,
}

impl<EncodingLimit: Get<u32>, AddressesLimit: Get<u32>>
	BoundedOpaqueNetworkState<EncodingLimit, AddressesLimit>
{
	fn force_from(ons: &OpaqueNetworkState) -> Self {
		let peer_id = WeakBoundedVec::<_, EncodingLimit>::force_from(
			ons.peer_id.0.clone(),
			Some(
				"Warning: The size of the encoding of PeerId \
				is bigger than expected. A runtime configuration \
				adjustment may be needed.",
			),
		);

		let external_addresses = WeakBoundedVec::<_, AddressesLimit>::force_from(
			ons.external_addresses

Review comment (suggestion):
note that you use here `EncodingLimit` for both the limit of `OpaquePeerId` and the limit of `OpaqueMultiAddr`.
Maybe we should also differenciate both. or at least put in the doc that this limit should be the max of both limits.

Code after:

/// Heartbeat which is sent/received.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct Heartbeat<BlockNumber>
where
	BlockNumber: PartialEq + Eq + Decode + Encode,
{
	/// Block number at the time heartbeat is created..
	pub block_number: BlockNumber,
	/// A state of local network (peer id and external addresses)
	pub network_state: OpaqueNetworkState,
	/// Index of the current session.
	pub session_index: SessionIndex,
	/// An index of the authority on the list of validators.
	pub authority_index: AuthIndex,
	/// The length of session validator set
	pub validators_len: u32,
}

/// A type that is the same as [`OpaqueNetworkState`] but with [`Vec`] replaced with
/// [`WeakBoundedVec<Limit>`] where Limit is the respective size limit
/// `PeerIdEncodingLimit` represents the size limit of the encoding of `PeerId`
/// `MultiAddrEncodingLimit` represents the size limit of the encoding of `MultiAddr`
/// `AddressesLimit` represents the size limit of the vector of peers connected
#[derive(Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen, TypeInfo)]
#[codec(mel_bound(PeerIdEncodingLimit: Get<u32>,
  	MultiAddrEncodingLimit: Get<u32>, AddressesLimit: Get<u32>))]
#[scale_info(skip_type_params(PeerIdEncodingLimit, MultiAddrEncodingLimit, AddressesLimit))]
pub struct BoundedOpaqueNetworkState<PeerIdEncodingLimit, MultiAddrEncodingLimit, AddressesLimit>
where
	PeerIdEncodingLimit: Get<u32>,
	MultiAddrEncodingLimit: Get<u32>,
	AddressesLimit: Get<u32>,
{
	/// PeerId of the local node in SCALE encoded.
	pub peer_id: WeakBoundedVec<u8, PeerIdEncodingLimit>,
	/// List of addresses the node knows it can be reached as.
	pub external_addresses:
		WeakBoundedVec<WeakBoundedVec<u8, MultiAddrEncodingLimit>, AddressesLimit>,
}

impl<PeerIdEncodingLimit: Get<u32>, MultiAddrEncodingLimit: Get<u32>, AddressesLimit: Get<u32>>
	BoundedOpaqueNetworkState<PeerIdEncodingLimit, MultiAddrEncodingLimit, AddressesLimit>
{
	fn force_from(ons: &OpaqueNetworkState) -> Self {
		let peer_id = WeakBoundedVec::<_, PeerIdEncodingLimit>::force_from(
			ons.peer_id.0.clone(),
			Some(
				"Warning: The size of the encoding of PeerId \
  				is bigger than expected. A runtime configuration \
  				adjustment may be needed.",