77//!
88//! ```rust
99//! use sidechain_domain::byte_string::*;
10- //! use sidechain_domain::{ CrossChainSignature, CrossChainPublicKey };
11- //! use sp_core::ConstU32;
10+ //! use sidechain_domain::{ CrossChainSignature, CrossChainPublicKey, UtxoId };
11+ //! use sp_core::{ ConstU32, Encode } ;
1212//! use hex_literal::hex;
13+ //! use sp_runtime::AccountId32;
1314//!
15+ //! #[derive(Encode)]
1416//! struct BlockProducerMetadata {
1517//! pub url: BoundedString<ConstU32<512>>,
1618//! pub hash: SizedByteString<32>,
1719//! }
1820//!
1921//! struct ExampleBenchmarkHelper;
2022//!
21- //! impl pallet_block_producer_metadata::benchmarking::BenchmarkHelper<BlockProducerMetadata> for ExampleBenchmarkHelper {
23+ //! impl pallet_block_producer_metadata::benchmarking::BenchmarkHelper<BlockProducerMetadata, AccountId32> for ExampleBenchmarkHelper {
24+ //! fn genesis_utxo() -> UtxoId {
25+ //! UtxoId::new([1;32], 0)
26+ //! }
27+ //!
2228//! fn metadata() -> BlockProducerMetadata {
2329//! BlockProducerMetadata {
2430//! url: BoundedString::try_from("https://cool.stuff/spo.json").unwrap(),
2834//! fn cross_chain_pub_key() -> CrossChainPublicKey {
2935//! CrossChainPublicKey(hex!("020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1").to_vec())
3036//! }
31- //! fn upsert_cross_chain_signature() -> sidechain_domain::CrossChainSignature {
32- //! CrossChainSignature(hex!("810854f5bd1d06dc8583ebd58ff4877dddb1646511edb10afd021f716bf51a8e617353b6c5d5f92a2005e2c3c24b782a6f74132d6b54251854cce186c981862c").to_vec())
33- //! }
34- //! fn delete_cross_chain_signature() -> sidechain_domain::CrossChainSignature {
35- //! CrossChainSignature(hex!("5c1a701c8adffdf53a371409a24cc6c2d778a4c65c2c105c5fccfc5eeb69e3fa59bd723e7c10893f53fcfdfff8c02954f2230953cb9596119c11d4a9a29564c5").to_vec())
37+ //! fn cross_chain_sign_key() -> k256::SecretKey {
38+ //! k256::SecretKey::from_slice(&[2;32]).unwrap()
3639//! }
3740//! fn upsert_valid_before() -> u64 {
3841//! 100_000_000
6568use super :: * ;
6669use frame_benchmarking:: v2:: * ;
6770use frame_system:: RawOrigin ;
71+ pub use k256:: SecretKey ;
6872use sidechain_domain:: * ;
6973
7074/// Helper trait for injecting mock values for use in benchmarks
71- pub trait BenchmarkHelper < BlockProducerMetadata > {
75+ pub trait BenchmarkHelper < BlockProducerMetadata : Encode , AccountId : Encode > {
76+ /// Should return the chain's genesis utxo
77+ fn genesis_utxo ( ) -> UtxoId ;
7278 /// Should return mock metadata
7379 fn metadata ( ) -> BlockProducerMetadata ;
7480 /// Should return mock cross-chain pubkey
7581 fn cross_chain_pub_key ( ) -> CrossChainPublicKey ;
82+ /// Should return mock cross-chain signing key
83+ fn cross_chain_sign_key ( ) -> SecretKey ;
7684 /// Should return mock cross-chain signature for upsert operation
7785 ///
7886 /// This signature must match the cross-chain pubkey returned by `cross_chain_pub_key` and be a valid
7987 /// signature of [MetadataSignedMessage] created using values returned by `metadata` and `cross_chain_pub_key`
8088 /// and the genesis UTXO used for benchmarks.
81- fn upsert_cross_chain_signature ( ) -> CrossChainSignature ;
89+ fn upsert_cross_chain_signature ( owner : AccountId ) -> CrossChainSignature {
90+ MetadataSignedMessage {
91+ cross_chain_pub_key : Self :: cross_chain_pub_key ( ) ,
92+ metadata : Some ( Self :: metadata ( ) ) ,
93+ genesis_utxo : Self :: genesis_utxo ( ) ,
94+ valid_before : Self :: upsert_valid_before ( ) ,
95+ owner,
96+ }
97+ . sign_with_key ( & Self :: cross_chain_sign_key ( ) )
98+ }
8299
83100 /// Should return mock cross-chain signature for delete operation
84101 ///
85102 /// This signature must match the cross-chain pubkey returned by `cross_chain_pub_key` and be a valid
86103 /// for the genesis UTXO used for benchmarks.
87- fn delete_cross_chain_signature ( ) -> CrossChainSignature ;
104+ fn delete_cross_chain_signature ( owner : AccountId ) -> CrossChainSignature {
105+ MetadataSignedMessage {
106+ cross_chain_pub_key : Self :: cross_chain_pub_key ( ) ,
107+ metadata : None :: < BlockProducerMetadata > ,
108+ genesis_utxo : Self :: genesis_utxo ( ) ,
109+ valid_before : Self :: delete_valid_before ( ) ,
110+ owner,
111+ }
112+ . sign_with_key ( & Self :: cross_chain_sign_key ( ) )
113+ }
88114
89115 /// Should return the valid-before value for the upsert
90116 fn upsert_valid_before ( ) -> u64 ;
@@ -103,13 +129,15 @@ mod benchmarks {
103129 fn upsert_metadata ( ) {
104130 let metadata = T :: BenchmarkHelper :: metadata ( ) ;
105131 let cross_chain_pub_key = T :: BenchmarkHelper :: cross_chain_pub_key ( ) ;
106- let cross_chain_signature = T :: BenchmarkHelper :: upsert_cross_chain_signature ( ) ;
107132 let valid_before = T :: BenchmarkHelper :: upsert_valid_before ( ) ;
108133
109134 // Create an account and fund it with sufficient balance
110135 let caller: T :: AccountId = account ( "caller" , 0 , 0 ) ;
111136 let _ = T :: Currency :: mint_into ( & caller, T :: HoldAmount :: get ( ) * 2u32 . into ( ) ) ;
112137
138+ let cross_chain_signature =
139+ T :: BenchmarkHelper :: upsert_cross_chain_signature ( caller. clone ( ) ) ;
140+
113141 #[ extrinsic_call]
114142 _ (
115143 RawOrigin :: Signed ( caller) ,
@@ -124,13 +152,15 @@ mod benchmarks {
124152 fn delete_metadata ( ) {
125153 let metadata = T :: BenchmarkHelper :: metadata ( ) ;
126154 let cross_chain_pub_key = T :: BenchmarkHelper :: cross_chain_pub_key ( ) ;
127- let cross_chain_signature = T :: BenchmarkHelper :: delete_cross_chain_signature ( ) ;
128155 let valid_before = T :: BenchmarkHelper :: delete_valid_before ( ) ;
129156
130157 let caller: T :: AccountId = account ( "caller" , 0 , 0 ) ;
131158 let _ =
132159 T :: Currency :: hold ( & HoldReason :: MetadataDeposit . into ( ) , & caller, T :: HoldAmount :: get ( ) ) ;
133160
161+ let cross_chain_signature =
162+ T :: BenchmarkHelper :: delete_cross_chain_signature ( caller. clone ( ) ) ;
163+
134164 BlockProducerMetadataStorage :: < T > :: insert (
135165 cross_chain_pub_key. hash ( ) ,
136166 ( metadata, caller. clone ( ) , T :: HoldAmount :: get ( ) ) ,
0 commit comments