Skip to content

Commit f90a8a9

Browse files
Developed bitcoin-vaults pallet configuration to add a weight info type, and use the new weights module to calculate extrinsic weights. Developed rbac pallet configuration to add a weight info type, and use the new weights module to calculate extrinsic weights. Developed len method for the IdOrVec type of the rbac pallet. Added the weight info module for the rbac pallet. Added the weight info module for the bitcoin vaults pallet.
1 parent 7c1a04a commit f90a8a9

File tree

10 files changed

+1195
-32
lines changed

10 files changed

+1195
-32
lines changed

pallets/bitcoin-vaults/src/benchmarking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use super::*;
66
use crate::{types::*, Pallet as BitcoinVaults};
7-
use scale_info::prelude::*;
7+
use scale_info::prelude::{vec::Vec, *};
88

99
use frame_benchmarking::v2::*;
1010
use frame_support::{assert_ok, pallet_prelude::*, traits::Get};
@@ -33,7 +33,7 @@ pub fn generate_psbt_sized<T: Config>(id: u8, size: u32) -> PSBT<T> {
3333
generate_vector(4, id, size).try_into().unwrap()
3434
}
3535

36-
pub fn generate_vector(prefix: u8, id: u8, size: u32) -> scale_info::prelude::vec::Vec<u8> {
36+
pub fn generate_vector(prefix: u8, id: u8, size: u32) -> Vec<u8> {
3737
assert!(size > 0, "vector size must be greater than 0");
3838
let mut v = vec![id; size as usize];
3939
v[0] = prefix;

pallets/bitcoin-vaults/src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ mod tests;
1212

1313
mod functions;
1414
pub mod types;
15+
pub mod weights;
16+
use weights::WeightInfo;
1517

1618
#[frame_support::pallet]
1719
pub mod pallet {
1820
use frame_support::pallet_prelude::*;
1921
//#[cfg(feature = "std")]
2022
//use frame_support::serde::{Deserialize, Serialize};
23+
use super::*;
2124
use crate::types::*;
2225
use frame_support::{pallet_prelude::BoundedVec, traits::Get};
2326
use frame_system::{
@@ -89,6 +92,9 @@ pub mod pallet {
8992
type OutputDescriptorMaxLen: Get<u32>;
9093
#[pallet::constant]
9194
type MaxProposalsPerVault: Get<u32>;
95+
96+
/// Weight information for extrinsics in this pallet.
97+
type WeightInfo: WeightInfo;
9298
}
9399

94100
#[pallet::pallet]
@@ -350,7 +356,7 @@ pub mod pallet {
350356
/// first and insert
351357
/// a new one.
352358
#[pallet::call_index(1)]
353-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(2))]
359+
#[pallet::weight(<T as Config>::WeightInfo::set_xpub(xpub.len() as u32))]
354360
pub fn set_xpub(origin: OriginFor<T>, xpub: BoundedVec<u8, T::XPubLen>) -> DispatchResult {
355361
// Check that the extrinsic was signed and get the signer.
356362
let who = ensure_signed(origin.clone())?;
@@ -392,7 +398,7 @@ pub mod pallet {
392398
///
393399
/// This tx does not takes any parameters.
394400
#[pallet::call_index(2)]
395-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(2))]
401+
#[pallet::weight(<T as Config>::WeightInfo::remove_xpub(T::XPubLen::get(), T::VaultDescriptionMaxLen::get(), T::MaxCosignersPerVault::get(), T::MaxVaultsPerUser::get()))]
396402
pub fn remove_xpub(origin: OriginFor<T>) -> DispatchResult {
397403
let who = ensure_signed(origin.clone())?;
398404
// The xpub must exists
@@ -432,7 +438,7 @@ pub mod pallet {
432438
/// ### Considerations
433439
/// - Do not include the vault owner on the `cosigners` list.
434440
#[pallet::call_index(3)]
435-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
441+
#[pallet::weight(<T as Config>::WeightInfo::create_vault(T::XPubLen::get(), description.len() as u32, cosigners.len() as u32, T::MaxVaultsPerUser::get()))]
436442
pub fn create_vault(
437443
origin: OriginFor<T>,
438444
threshold: u32,
@@ -481,7 +487,7 @@ pub mod pallet {
481487
/// ### Considerations:
482488
/// - Only the vault owner can perform this extrinsic
483489
#[pallet::call_index(4)]
484-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
490+
#[pallet::weight(<T as Config>::WeightInfo::remove_vault(T::XPubLen::get(), T::VaultDescriptionMaxLen::get(), T::MaxCosignersPerVault::get(), T::MaxVaultsPerUser::get(), T::MaxProposalsPerVault::get(), T::PSBTMaxLen::get()))]
485491
pub fn remove_vault(origin: OriginFor<T>, vault_id: [u8; 32]) -> DispatchResult {
486492
let who = ensure_signed(origin.clone())?;
487493

@@ -501,7 +507,7 @@ pub mod pallet {
501507
/// ### Considerations
502508
/// - Please ensure the recipient address is a valid mainnet address.
503509
#[pallet::call_index(5)]
504-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
510+
#[pallet::weight(<T as Config>::WeightInfo::propose(T::XPubLen::get(), description.len() as u32, T::MaxProposalsPerVault::get(), T::PSBTMaxLen::get()))]
505511
pub fn propose(
506512
origin: OriginFor<T>,
507513
vault_id: [u8; 32],
@@ -536,7 +542,7 @@ pub mod pallet {
536542
/// ### Parameters:
537543
/// - `proposal_id`: the proposal identifier
538544
#[pallet::call_index(6)]
539-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
545+
#[pallet::weight(<T as Config>::WeightInfo::remove_proposal(T::XPubLen::get(), T::VaultDescriptionMaxLen::get(), T::MaxProposalsPerVault::get(), T::PSBTMaxLen::get()))]
540546
pub fn remove_proposal(origin: OriginFor<T>, proposal_id: [u8; 32]) -> DispatchResult {
541547
let who = ensure_signed(origin.clone())?;
542548
let proposal = <Proposals<T>>::get(proposal_id).ok_or(Error::<T>::ProposalNotFound)?;
@@ -559,7 +565,7 @@ pub mod pallet {
559565
/// - Ensure the new url is valid.
560566
/// - The url has a maximum length of 32 bytes
561567
#[pallet::call_index(7)]
562-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
568+
#[pallet::weight(<T as Config>::WeightInfo::set_bdk_url(new_url.len() as u32))]
563569
pub fn set_bdk_url(origin: OriginFor<T>, new_url: URL) -> DispatchResult {
564570
T::ChangeBDKOrigin::ensure_origin(origin.clone())?;
565571
<BDKServicesURL<T>>::put(new_url);
@@ -579,7 +585,7 @@ pub mod pallet {
579585
/// - If successful, this process cannot be undone
580586
/// - A user can only sign a proposal once
581587
#[pallet::call_index(8)]
582-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
588+
#[pallet::weight(<T as Config>::WeightInfo::save_psbt(T::MaxCosignersPerVault::get(), signature_payload.len() as u32))]
583589
pub fn save_psbt(
584590
origin: OriginFor<T>,
585591
proposal_id: [u8; 32],
@@ -603,7 +609,7 @@ pub mod pallet {
603609
/// - The proposal must have a valid PSBT
604610
/// - Any vault member can perform this extrinsic
605611
#[pallet::call_index(9)]
606-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
612+
#[pallet::weight(<T as Config>::WeightInfo::finalize_psbt(T::MaxCosignersPerVault::get(), T::PSBTMaxLen::get()))]
607613
pub fn finalize_psbt(
608614
origin: OriginFor<T>,
609615
proposal_id: [u8; 32],
@@ -625,7 +631,7 @@ pub mod pallet {
625631
/// - The proposal must be finalized already
626632
/// - Any vault member can perform this extrinsic
627633
#[pallet::call_index(10)]
628-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
634+
#[pallet::weight(<T as Config>::WeightInfo::broadcast_psbt(T::PSBTMaxLen::get()))]
629635
pub fn broadcast_psbt(origin: OriginFor<T>, proposal_id: [u8; 32]) -> DispatchResult {
630636
let who = ensure_signed(origin.clone())?;
631637
Self::do_finalize_psbt(who, proposal_id, true)
@@ -644,7 +650,7 @@ pub mod pallet {
644650
/// - Any vault member can perform this extrinsic
645651
/// - A vault can only have a PoR at a time.
646652
#[pallet::call_index(11)]
647-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
653+
#[pallet::weight(<T as Config>::WeightInfo::create_proof(message.len() as u32, psbt.len() as u32))]
648654
pub fn create_proof(
649655
origin: OriginFor<T>,
650656
vault_id: [u8; 32],
@@ -667,7 +673,7 @@ pub mod pallet {
667673
/// - Any vault member can perform this extrinsic
668674
/// - A vault signer can only sabe its PSBT once.
669675
#[pallet::call_index(12)]
670-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
676+
#[pallet::weight(<T as Config>::WeightInfo::save_proof_psbt(psbt.len() as u32))]
671677
pub fn save_proof_psbt(
672678
origin: OriginFor<T>,
673679
vault_id: [u8; 32],
@@ -689,7 +695,7 @@ pub mod pallet {
689695
/// - Any vault member can perform this extrinsic
690696
/// - A vault signer can only sabe its PSBT once.
691697
#[pallet::call_index(13)]
692-
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().writes(1))]
698+
#[pallet::weight(<T as Config>::WeightInfo::finalize_proof(psbt.len() as u32))]
693699
pub fn finalize_proof(
694700
origin: OriginFor<T>,
695701
vault_id: [u8; 32],

pallets/bitcoin-vaults/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl pallet_bitcoin_vaults::Config for Test {
6666
type VaultDescriptionMaxLen = VaultDescriptionMaxLen;
6767
type OutputDescriptorMaxLen = OutputDescriptorMaxLen;
6868
type MaxProposalsPerVault = MaxProposalsPerVault;
69+
type WeightInfo = ();
6970
}
7071

7172
type Extrinsic = TestXt<RuntimeCall, ()>;

0 commit comments

Comments
 (0)