Skip to content

Commit

Permalink
[gas] generate delta when upgrading the gas schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
vgao1996 committed Jun 12, 2024
1 parent ac636d2 commit b27da8c
Show file tree
Hide file tree
Showing 21 changed files with 540 additions and 216 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,14 +1180,14 @@ impl Context {
let gas_schedule_params =
match GasScheduleV2::fetch_config(&state_view).and_then(|gas_schedule| {
let feature_version = gas_schedule.feature_version;
let gas_schedule = gas_schedule.to_btree_map();
let gas_schedule = gas_schedule.into_btree_map();
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule, feature_version)
.ok()
}) {
Some(gas_schedule) => Ok(gas_schedule),
None => GasSchedule::fetch_config(&state_view)
.and_then(|gas_schedule| {
let gas_schedule = gas_schedule.to_btree_map();
let gas_schedule = gas_schedule.into_btree_map();
AptosGasParameters::from_on_chain_gas_schedule(&gas_schedule, 0).ok()
})
.ok_or_else(|| {
Expand Down
2 changes: 2 additions & 0 deletions aptos-move/aptos-release-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ hex = { workspace = true }
move-core-types = { workspace = true }
move-model = { workspace = true }
once_cell = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
sha3 = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
tokio = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion aptos-move/aptos-release-builder/data/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ proposals:
- Framework:
bytecode_version: 6
git_hash: ~
- DefaultGas
- Gas:
new: current
150 changes: 111 additions & 39 deletions aptos-move/aptos-release-builder/src/components/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,65 @@

use crate::{components::get_signer_arg, utils::*};
use anyhow::Result;
use aptos_types::on_chain_config::GasScheduleV2;
use aptos_types::on_chain_config::{DiffItem, GasScheduleV2};
use move_model::{code_writer::CodeWriter, emit, emitln, model::Loc};
use sha3::{Digest, Sha3_512};

pub fn generate_gas_upgrade_proposal(
post_randomness_framework: bool,
gas_schedule: &GasScheduleV2,
is_testnet: bool,
next_execution_hash: Vec<u8>,
) -> Result<Vec<(String, String)>> {
let signer_arg = get_signer_arg(is_testnet, &next_execution_hash);
let mut result = vec![];

let writer = CodeWriter::new(Loc::default());
fn emit_gas_schedule_diff(
writer: &CodeWriter,
old_gas_schedule: &GasScheduleV2,
new_gas_schedule: &GasScheduleV2,
) -> Result<()> {
emitln!(writer, "// Changes");
if old_gas_schedule.feature_version != new_gas_schedule.feature_version {
emitln!(
writer,
"// Feature version: {} -> {}",
old_gas_schedule.feature_version,
new_gas_schedule.feature_version
);
}
let changes = GasScheduleV2::diff(old_gas_schedule, new_gas_schedule);
if !changes.is_empty() {
let max_len = changes
.iter()
.fold(0, |acc, (name, _)| usize::max(acc, name.len()));

emitln!(
writer,
"// source commit hash: {}\n",
aptos_build_info::get_git_hash()
);
emitln!(writer, "// Parameters");
for (param_name, delta) in &changes {
let name_with_spaces =
format!("{}{}", param_name, " ".repeat(max_len - param_name.len()));
match delta {
DiffItem::Add { new_val } => {
emitln!(writer, "// + {} : {}", name_with_spaces, new_val);
},
DiffItem::Delete { old_val } => {
emitln!(writer, "// - {} : {}", name_with_spaces, old_val);
},
DiffItem::Modify { old_val, new_val } => {
emitln!(
writer,
"// {} : {} -> {}",
name_with_spaces,
old_val,
new_val
);
},
}
}
}

emitln!(writer, "// Gas schedule upgrade proposal\n");
Ok(())
}

fn emit_full_gas_schedule(writer: &CodeWriter, gas_schedule: &GasScheduleV2) -> Result<()> {
emitln!(writer, "// Full gas schedule");
emitln!(
writer,
"// Feature version: {}",
"// Feature version: {}",
gas_schedule.feature_version
);
emitln!(writer, "//");
emitln!(writer, "// Entries:");
emitln!(writer, "// Parameters:");
let max_len = gas_schedule
.entries
.iter()
Expand All @@ -42,35 +72,77 @@ pub fn generate_gas_upgrade_proposal(
}
emitln!(writer);

Ok(())
}

pub fn generate_gas_upgrade_proposal(
old_gas_schedule: Option<&GasScheduleV2>,
new_gas_schedule: &GasScheduleV2,
is_testnet: bool,
next_execution_hash: Vec<u8>,
) -> Result<Vec<(String, String)>> {
let signer_arg = get_signer_arg(is_testnet, &next_execution_hash);
let mut result = vec![];

let writer = CodeWriter::new(Loc::default());

emitln!(
writer,
"// Source commit hash: {}",
aptos_build_info::get_git_hash()
);
emitln!(writer);

emitln!(writer, "// Gas schedule upgrade proposal");

let old_hash = match old_gas_schedule {
Some(old_gas_schedule) => {
let old_bytes = bcs::to_bytes(old_gas_schedule)?;
let old_hash = hex::encode(Sha3_512::digest(old_bytes.as_slice()));
emitln!(writer, "//");
emitln!(writer, "// Old Gas Schedule Hash (Sha3-512): {}", old_hash);

emit_gas_schedule_diff(&writer, old_gas_schedule, new_gas_schedule)?;

Some(old_hash)
},
None => None,
};
emitln!(writer, "//");
emit_full_gas_schedule(&writer, new_gas_schedule)?;

let proposal = generate_governance_proposal(
&writer,
is_testnet,
next_execution_hash.clone(),
&["aptos_framework::gas_schedule"],
|writer| {
let gas_schedule_blob = bcs::to_bytes(gas_schedule).unwrap();
let gas_schedule_blob = bcs::to_bytes(new_gas_schedule).unwrap();
assert!(gas_schedule_blob.len() < 65536);

emit!(writer, "let gas_schedule_blob: vector<u8> = ");
generate_blob_as_hex_string(writer, &gas_schedule_blob);
emitln!(writer, ";\n");
if !post_randomness_framework {
emitln!(
writer,
"gas_schedule::set_gas_schedule({}, gas_schedule_blob)",
signer_arg
);
} else {
// The else statement has & before the framework_signer.
// The testnet single-step generation had something like let framework_signer = &core_signer;
// so that their framework_signer is of type &signer, but for mainnet single-step and multi-step,
// the framework_signer is of type signer.
emitln!(
writer,
"gas_schedule::set_for_next_epoch({}, gas_schedule_blob);",
signer_arg
);
emitln!(writer, "aptos_governance::reconfigure({});", signer_arg);
emitln!(writer, ";");
emitln!(writer);

match old_hash {
Some(old_hash) => {
emitln!(
writer,
"gas_schedule::set_for_next_epoch_check_hash({}, x\"{}\", gas_schedule_blob);",
signer_arg,
old_hash,
);
},
None => {
emitln!(
writer,
"gas_schedule::set_for_next_epoch({}, gas_schedule_blob);",
signer_arg
);
},
}
emitln!(writer, "aptos_governance::reconfigure({});", signer_arg);
},
);

Expand Down
Loading

0 comments on commit b27da8c

Please sign in to comment.