Skip to content

Commit

Permalink
0.7.0 weight (#233)
Browse files Browse the repository at this point in the history
* feat: use new  weight, template save

* feat: file bank weights

* style: DispatchResultWithPostInfo

* style: generate new weight

* feat: DispatchResultWithPostInfo error

* fix: chain_spec update, and fix challenge proposal

* fix: build error

* test: test data
  • Loading branch information
ytqaljn authored Sep 25, 2023
1 parent ab177cd commit dc2e341
Show file tree
Hide file tree
Showing 18 changed files with 957 additions and 1,771 deletions.
53 changes: 23 additions & 30 deletions .maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

{{header}}
//! Autogenerated weights for {{pallet}}
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}}
//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}

// Executed Command:
Expand Down Expand Up @@ -55,27 +40,31 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
{{#each benchmark.comments as |comment|}}
// {{comment}}
{{/each}}
{{#each benchmark.component_ranges as |range|}}
/// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`.
{{/each}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}}))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
.saturating_add(T::DbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}}))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
.saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
{{/each}}
}
{{/each}}
Expand All @@ -87,28 +76,32 @@ impl WeightInfo for () {
{{#each benchmark.comments as |comment|}}
// {{comment}}
{{/each}}
{{#each benchmark.component_ranges as |range|}}
/// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`.
{{/each}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}}))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}}))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
{{/each}}
}
{{/each}}
}
}
82 changes: 53 additions & 29 deletions c-pallets/audit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub use pallet::*;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

pub mod weights;

use sp_runtime::{
traits::{CheckedAdd, SaturatedConversion},
RuntimeDebug, Permill,
Expand Down Expand Up @@ -94,7 +96,6 @@ use sp_std::{
};
use cp_enclave_verify::verify_rsa;
use cp_cess_common::*;
pub mod weights;
pub use weights::WeightInfo;
use cp_bloom_filter::BloomFilter;
use cp_scheduler_credit::SchedulerCreditCounter;
Expand Down Expand Up @@ -152,7 +153,7 @@ pub mod pallet {
use super::*;
// use frame_benchmarking::baseline::Config;
use frame_support::{traits::Get};
use frame_system::{ensure_signed, pallet_prelude::*};
use frame_system::{ensure_signed, pallet_prelude::{*, OriginFor}};

///18446744073709551615
pub const LIMIT: u64 = u64::MAX;
Expand Down Expand Up @@ -353,6 +354,10 @@ pub mod pallet {
#[pallet::getter(fn verify_reassign_count)]
pub(super) type VerifyReassignCount<T: Config> = StorageValue<_, u8, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn exec_block)]
pub(super) type ExecBlock<T: Config> = StorageValue<_, BlockNumberOf<T>, ValueQuery>;

// FOR TESTING
#[pallet::storage]
#[pallet::getter(fn lock)]
Expand Down Expand Up @@ -380,6 +385,8 @@ pub mod pallet {
if now > deadline {
//Determine whether to trigger a challenge
// if Self::trigger_challenge(now) {
let exec_block = <ExecBlock<T>>::get();
if now == exec_block {
log::info!("offchain worker random challenge start");
if let Err(e) = Self::offchain_work_start(now) {
match e {
Expand All @@ -388,6 +395,7 @@ pub mod pallet {
};
}
log::info!("offchain worker random challenge end");
}
// }
}
}
Expand Down Expand Up @@ -418,31 +426,36 @@ pub mod pallet {
.checked_mul(2).ok_or(Error::<T>::Overflow)?
.checked_div(3).ok_or(Error::<T>::Overflow)?;
let now = <frame_system::Pallet<T>>::block_number();

let cur_block = <VerifyDuration<T>>::get();

if now <= cur_block {
return Ok(());
}

if ChallengeProposal::<T>::contains_key(&hash) {
let proposal = ChallengeProposal::<T>::get(&hash).unwrap();
if proposal.0 + 1 >= limit {
let cur_blcok = <ChallengeDuration<T>>::get();

if now > cur_blcok {
let duration = now.checked_add(&proposal.1.net_snap_shot.life).ok_or(Error::<T>::Overflow)?;
<ChallengeDuration<T>>::put(duration);
let idle_duration = duration;
let one_hour = T::OneHours::get();
let tee_length = T::TeeWorkerHandler::get_controller_list().len();
let duration: u32 = (proposal.1.net_snap_shot.total_idle_space
.checked_add(proposal.1.net_snap_shot.total_service_space).ok_or(Error::<T>::Overflow)?
.checked_div(IDLE_VERIFY_RATE).ok_or(Error::<T>::Overflow)?
.checked_div(tee_length as u128).ok_or(Error::<T>::Overflow)?
) as u32;
let v_duration = idle_duration
.checked_add(&duration.saturated_into()).ok_or(Error::<T>::Overflow)?
.checked_add(&one_hour).ok_or(Error::<T>::Overflow)?;
<VerifyDuration<T>>::put(v_duration);
<ChallengeSnapShot<T>>::put(proposal.1);
let _ = ChallengeProposal::<T>::clear(ChallengeProposal::<T>::count(), None);
Self::deposit_event(Event::<T>::GenerateChallenge);
}
let mut proposal = ChallengeProposal::<T>::get(&hash).unwrap();
proposal.0 += 1;
if proposal.0 >= limit {
let duration = now.checked_add(&proposal.1.net_snap_shot.life).ok_or(Error::<T>::Overflow)?;
<ChallengeDuration<T>>::put(duration);
let idle_duration = duration;
let one_hour = T::OneHours::get();
let tee_length = T::TeeWorkerHandler::get_controller_list().len();
let duration: u32 = (proposal.1.net_snap_shot.total_idle_space
.checked_add(proposal.1.net_snap_shot.total_service_space).ok_or(Error::<T>::Overflow)?
.checked_div(IDLE_VERIFY_RATE).ok_or(Error::<T>::Overflow)?
.checked_div(tee_length as u128).ok_or(Error::<T>::Overflow)?
) as u32;
let v_duration = idle_duration
.checked_add(&duration.saturated_into()).ok_or(Error::<T>::Overflow)?
.checked_add(&one_hour).ok_or(Error::<T>::Overflow)?;
<VerifyDuration<T>>::put(v_duration);
<ChallengeSnapShot<T>>::put(proposal.1);
let _ = ChallengeProposal::<T>::clear(ChallengeProposal::<T>::count(), None);
Self::deposit_event(Event::<T>::GenerateChallenge);
} else {
ChallengeProposal::<T>::insert(&hash, proposal);
}
} else {
if ChallengeProposal::<T>::count() > count {
Expand All @@ -461,7 +474,7 @@ pub mod pallet {

#[pallet::call_index(1)]
#[transactional]
#[pallet::weight(100_000_000)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::submit_idle_proof())]
pub fn submit_idle_proof(
origin: OriginFor<T>,
idle_prove: BoundedVec<u8, T::IdleTotalHashLength>,
Expand Down Expand Up @@ -516,7 +529,7 @@ pub mod pallet {

#[pallet::call_index(2)]
#[transactional]
#[pallet::weight(100_000_000)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::submit_service_proof())]
pub fn submit_service_proof(
origin: OriginFor<T>,
service_prove: BoundedVec<u8, T::SigmaMax>,
Expand Down Expand Up @@ -573,7 +586,7 @@ pub mod pallet {

#[pallet::call_index(3)]
#[transactional]
#[pallet::weight(100_000_000)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::submit_verify_idle_result())]
pub fn submit_verify_idle_result(
origin: OriginFor<T>,
total_prove_hash: BoundedVec<u8, T::IdleTotalHashLength>,
Expand Down Expand Up @@ -657,7 +670,7 @@ pub mod pallet {

#[pallet::call_index(4)]
#[transactional]
#[pallet::weight(100_000_000)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::submit_verify_service_result())]
pub fn submit_verify_service_result(
origin: OriginFor<T>,
service_result: bool,
Expand Down Expand Up @@ -789,6 +802,17 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(9)]
#[transactional]
#[pallet::weight(100_000_000)]
pub fn update_exec_block(origin: OriginFor<T>, target: BlockNumberOf<T>) -> DispatchResult {
let _ = ensure_root(origin)?;

<ExecBlock<T>>::put(target);

Ok(())
}
}

#[pallet::validate_unsigned]
Expand Down
Loading

0 comments on commit dc2e341

Please sign in to comment.