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

Bound extrinsic inputs #1185

Merged
merged 32 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8e6902b
init by starting to bound all task pallet extrinsics
4meta5 Oct 7, 2024
332021d
tasks pallet unit tests and benchmarking updated with boundedvecs
4meta5 Oct 7, 2024
3106d5a
rm strings from tasksapi and runtimeapis altogether
4meta5 Oct 7, 2024
533c7ce
bound shard commitment
4meta5 Oct 7, 2024
ec02eac
update shards benchmark to use bounded commitment
4meta5 Oct 7, 2024
334feff
network register network replace unbounded strings
4meta5 Oct 7, 2024
0685724
dmail bound path and to fields
4meta5 Oct 7, 2024
af487d6
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 8, 2024
f6279ca
fix
4meta5 Oct 8, 2024
5805057
runtimes: update pallet weights
github-actions[bot] Oct 9, 2024
acac726
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 11, 2024
bf9bc8e
fix testnet test setup for network struct
4meta5 Oct 11, 2024
496bd60
fix
4meta5 Oct 11, 2024
71bc841
fix another merge conflict
4meta5 Oct 11, 2024
d7d6c0f
runtimes: update pallet weights
github-actions[bot] Oct 11, 2024
623b60e
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 12, 2024
6236bd4
runtimes: update pallet weights
github-actions[bot] Oct 12, 2024
7340918
Update shard.rs
4meta5 Oct 12, 2024
16ff0af
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 15, 2024
673055c
Update subxt to bounded types (#1212)
4meta5 Oct 17, 2024
182d349
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 17, 2024
d927cfc
Merge branch 'amar-bound-tx-inputs-postv2' of https://github.com/Anal…
4meta5 Oct 17, 2024
9cc220b
Merge branch 'development' into amar-bound-tx-inputs-postv2
4meta5 Oct 17, 2024
e71d41c
fix
4meta5 Oct 17, 2024
e0240f5
try fix subxt didnt learn shit just wasted time and therefore analogs…
4meta5 Oct 17, 2024
f90ce92
runtimes: update pallet weights
github-actions[bot] Oct 17, 2024
8ce3e13
deref and clone to convert static to t
4meta5 Oct 17, 2024
e338d7b
Merge branch 'amar-bound-tx-inputs-postv2' of https://github.com/Anal…
4meta5 Oct 17, 2024
d79707f
ok clippy chill
4meta5 Oct 17, 2024
f6a6f41
update chronicle
4meta5 Oct 18, 2024
252cf6c
update tc cli
4meta5 Oct 18, 2024
06918f6
update chronicle mock and tests
4meta5 Oct 18, 2024
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
22 changes: 10 additions & 12 deletions pallets/dmail/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ use crate::Pallet;

use scale_info::prelude::string::String;

use polkadot_sdk::frame_benchmarking::benchmarks;
use polkadot_sdk::frame_system;

use frame_system::RawOrigin;
use time_primitives::AccountId;

//TODO: choose & enforce MAX in code
const MAX_LENGTH: u32 = 1000;
pub const ALICE: [u8; 32] = [1u8; 32];
use polkadot_sdk::frame_benchmarking::benchmarks;
use polkadot_sdk::{frame_system, sp_runtime};
use scale_codec::Encode;
use sp_runtime::BoundedVec;
use time_primitives::{DmailPath, DmailTo, DMAIL_PATH_LEN, DMAIL_TO_LEN};

benchmarks! {
send_email {
let a in 1..MAX_LENGTH;
let b in 1..MAX_LENGTH;
let caller: AccountId = ALICE.into();
let a in 1..DMAIL_TO_LEN;
let b in 1..DMAIL_PATH_LEN;

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

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
2 changes: 1 addition & 1 deletion pallets/dmail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::send_email(to.len() as u32, path.len() as u32))]
#[pallet::weight(T::WeightInfo::send_email(to.0.len() as u32, path.0.len() as u32))]
pub fn send_email(origin: OriginFor<T>, to: DmailTo, path: DmailPath) -> DispatchResult {
let owner = ensure_signed(origin)?;
let message = DmailMessage { owner, to, path };
Expand Down
9 changes: 5 additions & 4 deletions pallets/dmail/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use crate::mock::*;
use crate::Event;

use polkadot_sdk::{frame_support, frame_system};
use polkadot_sdk::{frame_support, frame_system, sp_runtime};

use frame_support::assert_ok;
use frame_system::RawOrigin;

use scale_codec::Encode;
use sp_runtime::BoundedVec;
use time_primitives::{DmailMessage, DmailPath, DmailTo};

#[test]
fn test_dmail_event() {
let to: DmailTo = "Self".into();
let path: DmailPath = "//self".into();
let to: DmailTo = DmailTo(BoundedVec::truncate_from("Self".encode()));
let path: DmailPath = DmailPath(BoundedVec::truncate_from("//self".encode()));
let sender: AccountId = [1; 32].into();
let event = DmailMessage {
owner: sender.clone(),
Expand Down
19 changes: 10 additions & 9 deletions pallets/networks/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use crate::Pallet;

use frame_system::RawOrigin;
use polkadot_sdk::frame_benchmarking::benchmarks;
use polkadot_sdk::frame_system;
use polkadot_sdk::{frame_system, sp_runtime};
use scale_codec::Encode;
use scale_info::prelude::string::String;
use time_primitives::{Network, NetworkConfig};

//TODO: choose & enforce MAX in code
const MAX_LENGTH: u32 = 1000;
use sp_runtime::BoundedVec;
use time_primitives::{
ChainName, ChainNetwork, Network, NetworkConfig, CHAIN_NAME_LEN, CHAIN_NET_LEN,
};

fn mock_network_config() -> NetworkConfig {
NetworkConfig {
Expand All @@ -22,8 +23,8 @@ fn mock_network_config() -> NetworkConfig {
fn mock_network(chain_name: String, chain_network: String) -> Network {
Network {
id: 42,
chain_name,
chain_network,
chain_name: ChainName(BoundedVec::truncate_from(chain_name.as_str().encode())),
chain_network: ChainNetwork(BoundedVec::truncate_from(chain_network.as_str().encode())),
gateway: [0; 32],
gateway_block: 99,
config: mock_network_config(),
Expand All @@ -32,8 +33,8 @@ fn mock_network(chain_name: String, chain_network: String) -> Network {

benchmarks! {
register_network {
let a in 1..MAX_LENGTH;
let b in 1..MAX_LENGTH;
let a in 1..CHAIN_NAME_LEN;
let b in 1..CHAIN_NET_LEN;
let mut name = String::new();
let mut network = String::new();
for _ in 0..a {
Expand Down
2 changes: 1 addition & 1 deletion pallets/networks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,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.chain_name.0.len() as u32, network.chain_network.0.len() as u32))]
pub fn register_network(origin: OriginFor<T>, network: Network) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
Self::insert_network(&network)?;
Expand Down
10 changes: 6 additions & 4 deletions pallets/networks/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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::{frame_support, frame_system};
use time_primitives::{Network, NetworkConfig};
use polkadot_sdk::{frame_support, frame_system, sp_runtime};
use scale_codec::Encode;
use sp_runtime::BoundedVec;
use time_primitives::{ChainName, ChainNetwork, Network, NetworkConfig};

fn mock_network_config() -> NetworkConfig {
NetworkConfig {
Expand All @@ -17,8 +19,8 @@ fn mock_network_config() -> NetworkConfig {
fn mock_network() -> Network {
Network {
id: 42,
chain_name: "Ethereum".into(),
chain_network: "Mainnet".into(),
chain_name: ChainName(BoundedVec::truncate_from("Ethereum".encode())),
chain_network: ChainNetwork(BoundedVec::truncate_from("Mainnet".encode())),
gateway: [0; 32],
gateway_block: 99,
config: mock_network_config(),
Expand Down
10 changes: 5 additions & 5 deletions pallets/shards/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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::{
frame_benchmarking, frame_support, frame_system, pallet_balances, sp_core, sp_runtime, sp_std,
};
use sp_runtime::BoundedVec;

use sp_std::vec;
use sp_std::vec::Vec;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn get_commitment(member: [u8; 32]) -> Commitment {
panic!("Invalid member")
},
};
vec![commitment]
Commitment(BoundedVec::truncate_from(vec![commitment]))
}
pub fn get_proof_of_knowledge(member: [u8; 32]) -> ProofOfKnowledge {
match member {
Expand Down
14 changes: 7 additions & 7 deletions pallets/shards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ pub mod pallet {
);
let threshold = ShardThreshold::<T>::get(shard_id).unwrap_or_default();
ensure!(
commitment.len() == threshold as usize,
commitment.0.len() == threshold as usize,
Error::<T>::CommitmentLenNotEqualToThreshold
);
for c in &commitment {
for c in &commitment.0 {
ensure!(
VerifyingKey::from_bytes(*c).is_ok(),
Error::<T>::InvalidVerifyingKeyInCommitment
Expand All @@ -267,7 +267,7 @@ pub mod pallet {
T::Members::member_peer_id(&member).ok_or(Error::<T>::MemberPeerIdNotFound)?;
schnorr_evm::proof_of_knowledge::verify_proof_of_knowledge(
&peer_id,
&commitment,
&commitment.0,
proof_of_knowledge,
)
.map_err(|_| Error::<T>::InvalidProofOfKnowledge)?;
Expand All @@ -277,7 +277,7 @@ pub mod pallet {
.filter_map(|(_, status)| status.commitment().cloned())
.reduce(|mut group_commitment, commitment| {
for (group_commitment, commitment) in
group_commitment.iter_mut().zip(commitment.iter())
group_commitment.0.iter_mut().zip(commitment.0.iter())
{
*group_commitment = VerifyingKey::new(
VerifyingKey::from_bytes(*group_commitment)
Expand Down Expand Up @@ -327,7 +327,7 @@ pub mod pallet {
.all(|(_, status)| status == MemberStatus::Ready)
{
<ShardState<T>>::insert(shard_id, ShardStatus::Online);
Self::deposit_event(Event::ShardOnline(shard_id, commitment[0]));
Self::deposit_event(Event::ShardOnline(shard_id, commitment.0[0]));
T::Tasks::shard_online(shard_id, network);
}
Ok(())
Expand Down Expand Up @@ -443,7 +443,7 @@ pub mod pallet {
///
/// # Flow
/// 1. Retrieve and return the commitment from [`ShardCommitment`] storage.
pub fn get_shard_commitment(shard_id: ShardId) -> Option<Vec<TssPublicKey>> {
pub fn get_shard_commitment(shard_id: ShardId) -> Option<Commitment> {
ShardCommitment::<T>::get(shard_id)
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ pub mod pallet {
/// 1. Retrieves the commitment [`Vec<TssPublicKey>`] associated with the `shard_id` from [`ShardCommitment`].
/// 2. Returns the first element of the commitment [`TssPublicKey`] if it exists; otherwise, returns `None`.
fn tss_public_key(shard_id: ShardId) -> Option<TssPublicKey> {
ShardCommitment::<T>::get(shard_id).map(|commitment| commitment[0])
ShardCommitment::<T>::get(shard_id).map(|commitment| commitment.0[0])
}
}
}
10 changes: 5 additions & 5 deletions pallets/shards/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::mock::*;
use crate::{Event, ShardMembers, ShardNetwork, ShardState};

use polkadot_sdk::{frame_support, frame_system, pallet_balances, sp_core};
use polkadot_sdk::{frame_support, frame_system, pallet_balances, sp_core, sp_runtime};

use frame_support::assert_ok;
use frame_support::traits::{Currency, Get};
use frame_system::RawOrigin;

use schnorr_evm::k256::elliptic_curve::PrimeField;
use schnorr_evm::k256::{ProjectivePoint, Scalar};
use schnorr_evm::proof_of_knowledge::construct_proof_of_knowledge;
use schnorr_evm::VerifyingKey;
use sp_runtime::BoundedVec;

use time_primitives::{
AccountId, NetworkId, PeerId, PublicKey, ShardId, ShardStatus, ShardsInterface,
AccountId, Commitment, NetworkId, PeerId, PublicKey, ShardId, ShardStatus, ShardsInterface,
};

const ETHEREUM: NetworkId = 0;
Expand Down Expand Up @@ -88,7 +88,7 @@ fn create_shard(shard_id: ShardId, shard: &[Member], threshold: u16) {
assert_ok!(Shards::commit(
RawOrigin::Signed(member.account_id.clone()).into(),
shard_id as _,
member.commitment(threshold),
Commitment(BoundedVec::truncate_from(member.commitment(threshold))),
member.proof_of_knowledge(),
));
roll(1);
Expand Down Expand Up @@ -139,7 +139,7 @@ fn test_register_shard() {
assert_ok!(Shards::commit(
RawOrigin::Signed(member.account_id.clone()).into(),
shard_id as _,
member.commitment(threshold),
Commitment(BoundedVec::truncate_from(member.commitment(threshold))),
member.proof_of_knowledge(),
));
}
Expand Down
11 changes: 6 additions & 5 deletions pallets/tasks/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use frame_support::traits::OnInitialize;
use frame_system::RawOrigin;
use pallet_networks::NetworkGatewayAddress;
use pallet_shards::{ShardCommitment, ShardState};
use polkadot_sdk::{frame_benchmarking, frame_support, frame_system, sp_std};
use polkadot_sdk::{frame_benchmarking, frame_support, frame_system, sp_runtime, sp_std};
use sp_runtime::BoundedVec;
use sp_std::vec;
use time_primitives::{
NetworkId, ShardStatus, ShardsInterface, Task, TaskResult, TasksInterface, TssPublicKey,
TssSignature,
Commitment, GmpEvents, NetworkId, ShardStatus, ShardsInterface, Task, TaskResult,
TasksInterface, TssPublicKey, TssSignature,
};

// Generated by running tests::bench_helper::print_valid_result
Expand All @@ -32,7 +33,7 @@ fn create_simple_task<T: Config + pallet_shards::Config>() {
1,
);
ShardState::<T>::insert(shard_id, ShardStatus::Online);
ShardCommitment::<T>::insert(shard_id, vec![PUBKEY]);
ShardCommitment::<T>::insert(shard_id, Commitment(BoundedVec::truncate_from(vec![PUBKEY])));
Pallet::<T>::shard_online(shard_id, ETHEREUM);
Pallet::<T>::create_task(ETHEREUM, Task::ReadGatewayEvents { blocks: 0..10 });
}
Expand All @@ -44,7 +45,7 @@ benchmarks! {
NetworkGatewayAddress::<T>::insert(0, [0; 32]);
create_simple_task::<T>();
Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
let result = TaskResult::ReadGatewayEvents { events: vec![], signature: SIGNATURE };
let result = TaskResult::ReadGatewayEvents { events: GmpEvents(BoundedVec::truncate_from(vec![])), signature: SIGNATURE };
}: _(RawOrigin::Signed([0u8; 32].into()), 0, result) verify {}

schedule_tasks {
Expand Down
17 changes: 8 additions & 9 deletions pallets/tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ mod tests;
#[polkadot_sdk::frame_support::pallet]
pub mod pallet {
use crate::queue::*;
use scale_info::prelude::string::String;

use polkadot_sdk::{
frame_support, frame_system, pallet_balances, pallet_treasury, sp_runtime, sp_std,
Expand All @@ -68,8 +67,8 @@ pub mod pallet {
use sp_std::vec::Vec;

use time_primitives::{
AccountId, Balance, BatchBuilder, BatchId, GatewayMessage, GatewayOp, GmpEvent, MessageId,
NetworkId, NetworksInterface, PublicKey, ShardId, ShardsInterface, Task, TaskId,
AccountId, Balance, BatchBuilder, BatchId, ErrorMsg, GatewayMessage, GatewayOp, GmpEvent,
MessageId, NetworkId, NetworksInterface, PublicKey, ShardId, ShardsInterface, Task, TaskId,
TaskResult, TasksInterface, TssPublicKey, TssSignature,
};

Expand Down Expand Up @@ -201,7 +200,7 @@ pub mod pallet {

#[pallet::storage]
pub type TaskOutput<T: Config> =
StorageMap<_, Blake2_128Concat, TaskId, Result<(), String>, OptionQuery>;
StorageMap<_, Blake2_128Concat, TaskId, Result<(), ErrorMsg>, OptionQuery>;

#[pallet::storage]
pub type TaskNetwork<T: Config> =
Expand Down Expand Up @@ -252,7 +251,7 @@ pub mod pallet {
/// the record id that uniquely identify
TaskCreated(TaskId),
/// Task succeeded with optional error message
TaskResult(TaskId, Result<(), String>),
TaskResult(TaskId, Result<(), ErrorMsg>),
/// Set the maximum number of assigned tasks for all shards on the network
ShardTaskLimitSet(NetworkId, u32),
/// Set the network batch size
Expand Down Expand Up @@ -321,15 +320,15 @@ pub mod pallet {
TaskResult::ReadGatewayEvents { events, signature },
) => {
// verify signature
let bytes = time_primitives::encode_gmp_events(task_id, &events);
let bytes = time_primitives::encode_gmp_events(task_id, &events.0);
Self::verify_signature(shard, &bytes, signature)?;
// start next batch
let start = blocks.end;
let size = T::Networks::next_batch_size(network, start) as u64;
let end = start + size;
Self::create_task(network, Task::ReadGatewayEvents { blocks: start..end });
// process events
for event in events {
for event in events.0 {
match event {
GmpEvent::ShardRegistered(pubkey) => {
ShardRegistered::<T>::insert(pubkey, ());
Expand Down Expand Up @@ -424,7 +423,7 @@ pub mod pallet {
task_id
}

fn finish_task(network: NetworkId, task_id: TaskId, result: Result<(), String>) {
fn finish_task(network: NetworkId, task_id: TaskId, result: Result<(), ErrorMsg>) {
TaskOutput::<T>::insert(task_id, result.clone());
if let Some(shard) = TaskShard::<T>::take(task_id) {
ShardTasks::<T>::remove(shard, task_id);
Expand Down Expand Up @@ -635,7 +634,7 @@ pub mod pallet {

/// Retrieves the result of a given task.
/// Look up the `TaskResult` associated with the provided `task_id` in the storage.
pub fn get_task_result(task_id: TaskId) -> Option<Result<(), String>> {
pub fn get_task_result(task_id: TaskId) -> Option<Result<(), ErrorMsg>> {
TaskOutput::<T>::get(task_id)
}

Expand Down
Loading
Loading