-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[gas] add script to generate gas schedule update proposal #4471
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Result; | ||
use aptos_gas::gen::{generate_update_proposal, GenArgs}; | ||
use clap::Parser; | ||
|
||
fn main() -> Result<()> { | ||
let args = GenArgs::parse(); | ||
|
||
generate_update_proposal(&args) | ||
} |
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,125 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::gas_meter::{ | ||
AptosGasParameters, InitialGasSchedule, ToOnChainGasSchedule, LATEST_GAS_FEATURE_VERSION, | ||
}; | ||
use anyhow::Result; | ||
use aptos_types::on_chain_config::GasScheduleV2; | ||
use clap::Parser; | ||
use move_core_types::account_address::AccountAddress; | ||
use move_model::{code_writer::CodeWriter, emit, emitln, model::Loc}; | ||
use package_builder::PackageBuilder; | ||
use std::path::{Path, PathBuf}; | ||
|
||
fn current_gas_schedule() -> GasScheduleV2 { | ||
GasScheduleV2 { | ||
feature_version: LATEST_GAS_FEATURE_VERSION, | ||
entries: AptosGasParameters::initial().to_on_chain_gas_schedule(), | ||
} | ||
} | ||
|
||
fn generate_blob(writer: &CodeWriter, data: &[u8]) { | ||
emitln!(writer, "vector["); | ||
writer.indent(); | ||
for (i, b) in data.iter().enumerate() { | ||
if i % 20 == 0 { | ||
if i > 0 { | ||
emitln!(writer); | ||
} | ||
} else { | ||
emit!(writer, " "); | ||
} | ||
emit!(writer, "{},", b); | ||
} | ||
emitln!(writer); | ||
writer.unindent(); | ||
emit!(writer, "]") | ||
} | ||
|
||
fn generate_script(gas_schedule: &GasScheduleV2) -> Result<String> { | ||
let gas_schedule_blob = bcs::to_bytes(gas_schedule).unwrap(); | ||
|
||
assert!(gas_schedule_blob.len() < 65536); | ||
|
||
let writer = CodeWriter::new(Loc::default()); | ||
emitln!(writer, "// Gas schedule upgrade proposal\n"); | ||
|
||
emitln!( | ||
writer, | ||
"// Feature version: {}", | ||
gas_schedule.feature_version | ||
); | ||
emitln!(writer, "//"); | ||
emitln!(writer, "// Entries:"); | ||
let max_len = gas_schedule | ||
.entries | ||
.iter() | ||
.fold(0, |acc, (name, _)| usize::max(acc, name.len())); | ||
for (name, val) in &gas_schedule.entries { | ||
let name_with_spaces = format!("{}{}", name, " ".repeat(max_len - name.len())); | ||
emitln!(writer, "// {} : {}", name_with_spaces, val); | ||
} | ||
emitln!(writer); | ||
|
||
emitln!(writer, "script {"); | ||
writer.indent(); | ||
|
||
emitln!(writer, "use aptos_framework::aptos_governance;"); | ||
emitln!(writer, "use aptos_framework::gas_schedule;"); | ||
emitln!(writer); | ||
|
||
emitln!(writer, "fun main(proposal_id: u64) {"); | ||
writer.indent(); | ||
|
||
emitln!( | ||
writer, | ||
"let framework_signer = aptos_governance::resolve(proposal_id, @{});\n", | ||
AccountAddress::ONE, | ||
); | ||
|
||
emit!(writer, "let gas_schedule_blob: vector<u8> = "); | ||
generate_blob(&writer, &gas_schedule_blob); | ||
emitln!(writer, ";\n"); | ||
|
||
emitln!( | ||
writer, | ||
"gas_schedule::set_gas_schedule(&framework_signer, gas_schedule_blob);" | ||
); | ||
|
||
writer.unindent(); | ||
emitln!(writer, "}"); | ||
|
||
writer.unindent(); | ||
emitln!(writer, "}"); | ||
|
||
Ok(writer.process_result(|s| s.to_string())) | ||
} | ||
|
||
fn aptos_framework_path() -> PathBuf { | ||
Path::join( | ||
Path::new(env!("CARGO_MANIFEST_DIR")), | ||
"../framework/aptos-framework", | ||
) | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct GenArgs { | ||
#[clap(short, long)] | ||
pub output: Option<String>, | ||
} | ||
|
||
pub fn generate_update_proposal(args: &GenArgs) -> Result<()> { | ||
let mut pack = PackageBuilder::new("GasScheduleUpdate"); | ||
|
||
pack.add_source( | ||
"update_gas_schedule.move", | ||
&generate_script(¤t_gas_schedule())?, | ||
); | ||
// TODO: use relative path here | ||
pack.add_local_dep("AptosFramework", &aptos_framework_path().to_string_lossy()); | ||
|
||
pack.write_to_disk(args.output.as_deref().unwrap_or("./proposal"))?; | ||
|
||
Ok(()) | ||
} |
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,17 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use aptos_gas::gen::{generate_update_proposal, GenArgs}; | ||
use framework::{BuildOptions, BuiltPackage}; | ||
|
||
#[test] | ||
fn can_generate_and_build_update_proposal() { | ||
let output_dir = tempfile::tempdir().unwrap(); | ||
|
||
generate_update_proposal(&GenArgs { | ||
output: Some(output_dir.path().to_string_lossy().to_string()), | ||
}) | ||
.unwrap(); | ||
|
||
BuiltPackage::build(output_dir.path().to_path_buf(), BuildOptions::default()).unwrap(); | ||
} |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
|
||
pub mod aggregator; | ||
pub mod harness; | ||
pub mod package_builder; | ||
pub mod stake; | ||
|
||
use anyhow::bail; | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably
proposal.move
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a move package.