-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Verifier] verify-bytecode-meter prints module/function breakdown (#1…
…6963) ## Description Use a custom meter to track every function and module that is verified, and display all of them. ## Test Plan ``` deepbook$ cargo run --bin sui -p sui \ -- client verify-bytecode-meter \ --module build/DeepBook/bytecode_modules/math.mv ╭──────────────────────────────────────╮ │ Package will pass metering check! │ ├──────────────────────────────────────┤ │ Limits │ ├─────────────────────────┬────────────┤ │ packages │ 16,000,000 │ │ modules │ 16,000,000 │ │ functions │ 16,000,000 │ ├─────────────────────────┴────────────┤ │ Ticks Used │ ├─────────────────────────┬────────────┤ │ <unknown> │ 41,915 │ │ math │ 41,915 │ │ unsafe_mul │ 1,225 │ │ unsafe_mul_round │ 5,675 │ │ mul │ 2,410 │ │ mul_round │ 2,905 │ │ unsafe_div │ 1,225 │ │ unsafe_div_round │ 6,085 │ │ div_round │ 2,905 │ │ count_leading_zeros │ 19,485 │ ├─────────────────────────┴────────────┤ │ Package will pass metering check! │ ╰──────────────────────────────────────╯ ``` ## Stack - #16903 - #16941 - #16945 --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [x] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes `sui client verify-bytecode-meter` prints a breakdown of verifier cost by module and function in a package.
- Loading branch information
Showing
3 changed files
with
212 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use move_binary_format::errors::PartialVMResult; | ||
use move_bytecode_verifier_meter::{Meter, Scope}; | ||
use serde::Serialize; | ||
|
||
/// A meter that accumulates all the scopes that it sees, without enforcing a limit. | ||
#[derive(Debug)] | ||
pub(crate) struct AccumulatingMeter { | ||
pkg_acc: Accumulator, | ||
mod_acc: Accumulator, | ||
fun_acc: Accumulator, | ||
} | ||
|
||
/// Ticks and child scopes recorded for an individual scope. | ||
#[derive(Clone, Debug, Serialize)] | ||
pub struct Accumulator { | ||
pub name: String, | ||
#[serde(skip)] | ||
pub scope: Scope, | ||
pub ticks: u128, | ||
pub children: Vec<Accumulator>, | ||
} | ||
|
||
impl AccumulatingMeter { | ||
pub fn new() -> Self { | ||
Self { | ||
pkg_acc: Accumulator::new("<unknown>", Scope::Package), | ||
mod_acc: Accumulator::new("<unknown>", Scope::Module), | ||
fun_acc: Accumulator::new("<unknown>", Scope::Function), | ||
} | ||
} | ||
|
||
pub fn accumulator(&self, scope: Scope) -> &Accumulator { | ||
match scope { | ||
Scope::Transaction => unreachable!("transaction scope is not supported"), | ||
Scope::Package => &self.pkg_acc, | ||
Scope::Module => &self.mod_acc, | ||
Scope::Function => &self.fun_acc, | ||
} | ||
} | ||
|
||
pub fn accumulator_mut(&mut self, scope: Scope) -> &mut Accumulator { | ||
match scope { | ||
Scope::Transaction => unreachable!("transaction scope is not supported"), | ||
Scope::Package => &mut self.pkg_acc, | ||
Scope::Module => &mut self.mod_acc, | ||
Scope::Function => &mut self.fun_acc, | ||
} | ||
} | ||
} | ||
|
||
impl Accumulator { | ||
fn new(name: &str, scope: Scope) -> Self { | ||
Self { | ||
name: name.to_string(), | ||
scope, | ||
ticks: 0, | ||
children: vec![], | ||
} | ||
} | ||
|
||
/// Find the max ticks spent verifying `scope`s within this scope (including itself). | ||
pub fn max_ticks(&self, scope: Scope) -> u128 { | ||
let mut accs = vec![self]; | ||
|
||
let mut curr = 0u128; | ||
while let Some(acc) = accs.pop() { | ||
if acc.scope == scope { | ||
curr = curr.max(acc.ticks); | ||
} | ||
|
||
accs.extend(acc.children.iter()); | ||
} | ||
|
||
curr | ||
} | ||
} | ||
|
||
impl Meter for AccumulatingMeter { | ||
fn enter_scope(&mut self, name: &str, scope: Scope) { | ||
*self.accumulator_mut(scope) = Accumulator::new(name, scope); | ||
} | ||
|
||
fn transfer(&mut self, from: Scope, to: Scope, factor: f32) -> PartialVMResult<()> { | ||
let from_acc = self.accumulator(from).clone(); | ||
let to_acc = self.accumulator_mut(to); | ||
|
||
to_acc.ticks += (from_acc.ticks as f32 * factor) as u128; | ||
to_acc.children.push(from_acc); | ||
Ok(()) | ||
} | ||
|
||
fn add(&mut self, scope: Scope, units: u128) -> PartialVMResult<()> { | ||
self.accumulator_mut(scope).ticks += units; | ||
Ok(()) | ||
} | ||
} |