Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tc-subxt to bounded types #1205

Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

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

22 changes: 11 additions & 11 deletions chronicle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use time_primitives::traits::IdentifyAccount;
use time_primitives::{
sr25519, AccountId, Balance, BatchId, BlockHash, BlockNumber, ChainName, ChainNetwork,
Commitment, Gateway, GatewayMessage, MemberStatus, NetworkId, PeerId, ProofOfKnowledge,
PublicKey, ShardId, ShardStatus, Task, TaskId, TaskResult,
sr25519, AccountId, Balance, BatchId, BlockHash, BlockNumber, Gateway, GatewayMessage,
MemberStatus, NetworkId, PeerId, ProofOfKnowledge, PublicKey, ShardId, ShardStatus, Task,
TaskId, TaskResult, TssPublicKey,
};
use tokio::time::Duration;
use tss::{sum_commitments, VerifiableSecretSharingCommitment, VerifyingKey};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MockNetwork {
pub chain_name: ChainName,
pub chain_network: ChainNetwork,
pub chain_name: String,
pub chain_network: String,
}

impl MockNetwork {
pub fn new(chain_name: ChainName, chain_network: ChainNetwork) -> Self {
pub fn new(chain_name: String, chain_network: String) -> Self {
Self { chain_name, chain_network }
}
}
Expand All @@ -30,7 +30,7 @@ impl MockNetwork {
pub struct MockShard {
pub members: Vec<(AccountId, MemberStatus)>,
pub threshold: u16,
pub commitments: Vec<Commitment>,
pub commitments: Vec<Vec<TssPublicKey>>,
pub online: usize,
}

Expand Down Expand Up @@ -89,7 +89,7 @@ impl Mock {
mock
}

pub fn create_network(&self, chain_name: ChainName, chain_network: ChainNetwork) -> NetworkId {
pub fn create_network(&self, chain_name: String, chain_network: String) -> NetworkId {
let mock_network = MockNetwork::new(chain_name, chain_network);
let mut networks = self.networks.lock().unwrap();
if let Some(existing_id) =
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Runtime for Mock {
.boxed()
}

async fn get_network(&self, network: NetworkId) -> Result<Option<(ChainName, ChainNetwork)>> {
async fn get_network(&self, network: NetworkId) -> Result<Option<(String, String)>> {
Ok(self
.networks
.lock()
Expand Down Expand Up @@ -248,7 +248,7 @@ impl Runtime for Mock {
Ok(ShardStatus::Created)
}

async fn get_shard_commitment(&self, shard_id: ShardId) -> Result<Option<Commitment>> {
async fn get_shard_commitment(&self, shard_id: ShardId) -> Result<Option<Vec<TssPublicKey>>> {
let shards = self.shards.lock().unwrap();
let Some(shard) = shards.get(&shard_id) else {
return Ok(None);
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Runtime for Mock {
async fn submit_commitment(
&self,
shard_id: ShardId,
commitment: Commitment,
commitment: Vec<TssPublicKey>,
_proof_of_knowledge: ProofOfKnowledge,
) -> Result<()> {
let mut shards = self.shards.lock().unwrap();
Expand Down
7 changes: 4 additions & 3 deletions pallets/dmail/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use scale_info::prelude::string::String;
use frame_system::RawOrigin;
use polkadot_sdk::frame_benchmarking::benchmarks;
use polkadot_sdk::frame_system;
use polkadot_sdk::sp_runtime::BoundedVec;
use scale_codec::Encode;
use time_primitives::{DmailPath, DmailTo, DMAIL_PATH_LEN, DMAIL_TO_LEN};
use time_primitives::{DMAIL_PATH_LEN, DMAIL_TO_LEN};

benchmarks! {
send_email {
Expand All @@ -22,8 +23,8 @@ benchmarks! {
for _ in 0..b {
path.push('b');
}
let to = DmailTo::truncate_from(to.as_str().encode());
let path = DmailPath::truncate_from(path.as_str().encode());
let to = BoundedVec::truncate_from(to.as_str().encode());
let path = BoundedVec::truncate_from(path.as_str().encode());
}: _(RawOrigin::Signed([0u8; 32].into()), to, path)
verify {}

Expand Down
11 changes: 8 additions & 3 deletions pallets/dmail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ pub use pallet::*;

#[polkadot_sdk::frame_support::pallet]
pub mod pallet {
use polkadot_sdk::{frame_support, frame_system};
use polkadot_sdk::{frame_support, frame_system, sp_runtime};

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use time_primitives::{AccountId, DmailMessage, DmailPath, DmailTo};
use sp_runtime::{traits::ConstU32, BoundedVec};
use time_primitives::{AccountId, DmailMessage, DMAIL_PATH_LEN, DMAIL_TO_LEN};

pub trait WeightInfo {
fn send_email(to: u32, path: u32) -> Weight;
Expand Down Expand Up @@ -50,7 +51,11 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::send_email(to.len() as u32, path.len() as u32))]
pub fn send_email(origin: OriginFor<T>, to: DmailTo, path: DmailPath) -> DispatchResult {
pub fn send_email(
origin: OriginFor<T>,
to: BoundedVec<u8, ConstU32<DMAIL_TO_LEN>>,
path: BoundedVec<u8, ConstU32<DMAIL_PATH_LEN>>,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let message = DmailMessage { owner, to, path };
Self::deposit_event(Event::Message(message));
Expand Down
10 changes: 5 additions & 5 deletions pallets/dmail/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::mock::*;
use crate::Event;

use polkadot_sdk::{frame_support, frame_system};

use frame_support::assert_ok;
use frame_system::RawOrigin;
use polkadot_sdk::sp_runtime::BoundedVec;
use polkadot_sdk::{frame_support, frame_system};
use scale_codec::Encode;
use time_primitives::{DmailMessage, DmailPath, DmailTo};
use time_primitives::DmailMessage;

#[test]
fn test_dmail_event() {
let to: DmailTo = DmailTo::truncate_from("Self".encode());
let path: DmailPath = DmailPath::truncate_from("//self".encode());
let to = BoundedVec::truncate_from("Self".encode());
let path = BoundedVec::truncate_from("//self".encode());
let sender: AccountId = [1; 32].into();
let event = DmailMessage {
owner: sender.clone(),
Expand Down
11 changes: 6 additions & 5 deletions pallets/networks/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use crate::Pallet;
use frame_system::RawOrigin;
use polkadot_sdk::frame_benchmarking::benchmarks;
use polkadot_sdk::frame_system;
use polkadot_sdk::sp_runtime::BoundedVec;
use scale_codec::Encode;
use scale_info::prelude::string::String;
use time_primitives::{
ChainName, ChainNetwork, Network, NetworkConfig, CHAIN_NAME_LEN, CHAIN_NET_LEN,
};
use time_primitives::{ChainNetwork, Network, NetworkConfig, CHAIN_NAME_LEN, CHAIN_NET_LEN};

fn mock_network_config() -> NetworkConfig {
NetworkConfig {
Expand All @@ -22,8 +21,10 @@ fn mock_network_config() -> NetworkConfig {
fn mock_network(chain_name: String, chain_network: String) -> Network {
Network {
id: 42,
chain_name: ChainName::truncate_from(chain_name.as_str().encode()),
chain_network: ChainNetwork::truncate_from(chain_network.as_str().encode()),
name: ChainNetwork {
chain: BoundedVec::truncate_from(chain_name.as_str().encode()),
net: BoundedVec::truncate_from(chain_network.as_str().encode()),
},
gateway: [0; 32],
gateway_block: 99,
config: mock_network_config(),
Expand Down
14 changes: 5 additions & 9 deletions pallets/networks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use scale_info::prelude::vec::Vec;
use time_primitives::{
Address, ChainName, ChainNetwork, Network, NetworkConfig, NetworkId, NetworksInterface,
TasksInterface,
Address, ChainNetwork, Network, NetworkConfig, NetworkId, NetworksInterface, TasksInterface,
};

pub trait WeightInfo {
Expand Down Expand Up @@ -91,7 +90,7 @@ pub mod pallet {

#[pallet::storage]
pub type NetworkName<T: Config> =
StorageMap<_, Twox64Concat, NetworkId, (ChainName, ChainNetwork), OptionQuery>;
StorageMap<_, Twox64Concat, NetworkId, ChainNetwork, OptionQuery>;

/// Map storage for network gateways.
#[pallet::storage]
Expand Down Expand Up @@ -159,10 +158,7 @@ pub mod pallet {
fn insert_network(network: &Network) -> Result<(), Error<T>> {
ensure!(Networks::<T>::get(network.id).is_none(), Error::<T>::NetworkExists);
Networks::<T>::insert(network.id, network.id);
NetworkName::<T>::insert(
network.id,
(network.chain_name.clone(), network.chain_network.clone()),
);
NetworkName::<T>::insert(network.id, network.name.clone());
NetworkGatewayAddress::<T>::insert(network.id, network.gateway);
NetworkGatewayBlock::<T>::insert(network.id, network.gateway_block);
T::Tasks::gateway_registered(network.id, network.gateway_block);
Expand Down Expand Up @@ -200,7 +196,7 @@ pub mod pallet {
/// 3. Emit the [`Event::NetworkRegistered`] event with the new `NetworkId`.
/// 4. Return `Ok(())` to indicate success.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::register_network(network.chain_name.len() as u32, network.chain_network.len() as u32))]
#[pallet::weight(T::WeightInfo::register_network(network.name.chain.len() as u32, network.name.net.len() as u32))]
pub fn register_network(origin: OriginFor<T>, network: Network) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
Self::insert_network(&network)?;
Expand Down Expand Up @@ -234,7 +230,7 @@ pub mod pallet {
/// # Flow
/// 1. Call [`Networks`] to fetch the network information.
/// 2. Return the network information if it exists, otherwise return `None`.
pub fn get_network(network: NetworkId) -> Option<(ChainName, ChainNetwork)> {
pub fn get_network(network: NetworkId) -> Option<ChainNetwork> {
NetworkName::<T>::get(network)
}
}
Expand Down
14 changes: 7 additions & 7 deletions pallets/networks/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::{self as pallet_networks};
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};
use frame_system::RawOrigin;
use polkadot_sdk::sp_runtime::BoundedVec;
use polkadot_sdk::{frame_support, frame_system};
use scale_codec::Encode;
use time_primitives::{ChainName, ChainNetwork, Network, NetworkConfig};
use time_primitives::{ChainNetwork, Network, NetworkConfig};

fn mock_network_config() -> NetworkConfig {
NetworkConfig {
Expand All @@ -18,8 +19,10 @@ fn mock_network_config() -> NetworkConfig {
fn mock_network() -> Network {
Network {
id: 42,
chain_name: ChainName::truncate_from("Ethereum".encode()),
chain_network: ChainNetwork::truncate_from("Mainnet".encode()),
name: ChainNetwork {
chain: BoundedVec::truncate_from("Ethereum".encode()),
net: BoundedVec::truncate_from("Mainnet".encode()),
},
gateway: [0; 32],
gateway_block: 99,
config: mock_network_config(),
Expand All @@ -32,10 +35,7 @@ fn test_register_network() {
new_test_ext().execute_with(|| {
assert_ok!(Networks::register_network(RawOrigin::Root.into(), network.clone(),));
assert_eq!(pallet_networks::Networks::<Test>::get(42), Some(network.id));
assert_eq!(
pallet_networks::NetworkName::<Test>::get(42),
Some((network.chain_name, network.chain_network))
);
assert_eq!(pallet_networks::NetworkName::<Test>::get(42), Some(network.name.clone()));
assert_eq!(pallet_networks::NetworkGatewayAddress::<Test>::get(42), Some(network.gateway));
assert_eq!(
pallet_networks::NetworkGatewayBlock::<Test>::get(42),
Expand Down
15 changes: 8 additions & 7 deletions pallets/shards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use super::*;
use crate::Pallet;

use polkadot_sdk::{
frame_benchmarking, frame_support, frame_system, pallet_balances, sp_core, sp_std,
};

use frame_benchmarking::benchmarks;
use frame_support::traits::{Currency, Get};
use frame_system::RawOrigin;
use polkadot_sdk::sp_runtime::{traits::ConstU32, BoundedVec};
use polkadot_sdk::{
frame_benchmarking, frame_support, frame_system, pallet_balances, sp_core, sp_std,
};

use sp_std::vec;
use sp_std::vec::Vec;

use time_primitives::{
AccountId, Commitment, NetworkId, ProofOfKnowledge, PublicKey, ShardsInterface,
AccountId, NetworkId, ProofOfKnowledge, PublicKey, ShardsInterface, TssPublicKey,
MAX_SHARD_SIZE,
};

pub const ALICE: [u8; 32] = [1u8; 32];
Expand Down Expand Up @@ -60,7 +61,7 @@ pub const CHARLIE_POK: ProofOfKnowledge = [
147, 134,
];

pub fn get_commitment(member: [u8; 32]) -> Commitment {
pub fn get_commitment(member: [u8; 32]) -> BoundedVec<TssPublicKey, ConstU32<MAX_SHARD_SIZE>> {
let commitment = match member {
ALICE => ALICE_COMMITMENT,
BOB => BOB_COMMITMENT,
Expand All @@ -69,7 +70,7 @@ pub fn get_commitment(member: [u8; 32]) -> Commitment {
panic!("Invalid member")
},
};
Commitment::truncate_from(vec![commitment])
BoundedVec::truncate_from(vec![commitment])
}
pub fn get_proof_of_knowledge(member: [u8; 32]) -> ProofOfKnowledge {
match member {
Expand Down
25 changes: 16 additions & 9 deletions pallets/shards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ pub mod pallet {
use frame_support::pallet_prelude::{EnsureOrigin, ValueQuery, *};
use frame_system::pallet_prelude::*;

use sp_runtime::Saturating;
use sp_runtime::{traits::ConstU32, BoundedVec, Saturating};
use sp_std::vec;
use sp_std::vec::Vec;

use schnorr_evm::VerifyingKey;

use time_primitives::{
AccountId, Balance, Commitment, ElectionsInterface, MemberStatus, MembersInterface,
NetworkId, ProofOfKnowledge, PublicKey, ShardId, ShardStatus, ShardsInterface,
TasksInterface, TssPublicKey,
AccountId, Balance, ElectionsInterface, MemberStatus, MembersInterface, NetworkId,
ProofOfKnowledge, PublicKey, ShardId, ShardStatus, ShardsInterface, TasksInterface,
TssPublicKey, MAX_SHARD_SIZE,
};

/// Trait to define the weights for various extrinsics in the pallet.
Expand Down Expand Up @@ -162,8 +162,13 @@ pub mod pallet {

/// Maps `ShardId` to `Commitment` indicating the commitment of each shard.
#[pallet::storage]
pub type ShardCommitment<T: Config> =
StorageMap<_, Blake2_128Concat, ShardId, Commitment, OptionQuery>;
pub type ShardCommitment<T: Config> = StorageMap<
_,
Blake2_128Concat,
ShardId,
BoundedVec<TssPublicKey, ConstU32<MAX_SHARD_SIZE>>,
OptionQuery,
>;

/// Maps `AccountId` to `ShardId` indicating the shard a member is part of.
#[pallet::storage]
Expand Down Expand Up @@ -197,7 +202,7 @@ pub mod pallet {
/// New shard was created
ShardCreated(ShardId, NetworkId),
/// Shard commited
ShardCommitted(ShardId, Commitment),
ShardCommitted(ShardId, BoundedVec<TssPublicKey, ConstU32<MAX_SHARD_SIZE>>),
/// Shard DKG timed out
ShardKeyGenTimedOut(ShardId),
/// Shard completed dkg and submitted public key to runtime
Expand Down Expand Up @@ -244,7 +249,7 @@ pub mod pallet {
pub fn commit(
origin: OriginFor<T>,
shard_id: ShardId,
commitment: Commitment,
commitment: BoundedVec<TssPublicKey, ConstU32<MAX_SHARD_SIZE>>,
proof_of_knowledge: ProofOfKnowledge,
) -> DispatchResult {
let member = ensure_signed(origin)?;
Expand Down Expand Up @@ -441,7 +446,9 @@ pub mod pallet {
///
/// # Flow
/// 1. Retrieve and return the commitment from [`ShardCommitment`] storage.
pub fn get_shard_commitment(shard_id: ShardId) -> Option<Commitment> {
pub fn get_shard_commitment(
shard_id: ShardId,
) -> Option<BoundedVec<TssPublicKey, ConstU32<MAX_SHARD_SIZE>>> {
ShardCommitment::<T>::get(shard_id)
}
}
Expand Down
Loading
Loading