Skip to content

Commit

Permalink
Use versioned gas configuration to modify or add gas entries on-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Dec 17, 2024
1 parent 233f95b commit 1d83a45
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rooch-genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ move-vm-runtime = { workspace = true, features = ["stacktrace", "debugging", "te
moveos-types = { workspace = true }
moveos = { workspace = true }
moveos-store = { workspace = true }
moveos-stdlib = { workspace = true }
accumulator = { workspace = true }

rooch-framework = { workspace = true }
Expand Down
89 changes: 58 additions & 31 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use move_core_types::{account_address::AccountAddress, identifier::Identifier};
use move_vm_runtime::native_functions::NativeFunction;
use moveos::gas::table::VMGasParameters;
use moveos::moveos::{MoveOS, MoveOSConfig};
use moveos_stdlib::natives::moveos_stdlib::base64::EncodeDecodeGasParametersOption;
use moveos_store::MoveOSStore;
use moveos_types::genesis_info::GenesisInfo;
use moveos_types::h256::H256;
Expand Down Expand Up @@ -96,7 +97,7 @@ impl FrameworksGasParameters {
}
}

pub fn latest() -> Self {
pub fn v1() -> Self {
let mut gas_parameter = Self {
max_gas_amount: GasScheduleConfig::INITIAL_MAX_GAS_AMOUNT,
vm_gas_params: VMGasParameters::initial(),
Expand All @@ -105,38 +106,64 @@ impl FrameworksGasParameters {
rooch_nursery_gas_params: Some(rooch_nursery::natives::GasParameters::initial()),
};

if LATEST_GAS_SCHEDULE_VERSION >= GAS_SCHEDULE_RELEASE_V1 {
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_base = InternalGas::new(167);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_per_arg = InternalGasPerArg::new(15);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_per_local = InternalGasPerArg::new(15);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_base = InternalGas::new(167);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_arg = InternalGasPerArg::new(15);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_local = InternalGasPerArg::new(15);
gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_ty_arg = InternalGasPerArg::new(15);
}
gas_parameter
.rooch_framework_gas_params
.moveos_stdlib
.base64
.encode = EncodeDecodeGasParametersOption {
base: Some(1000.into()),
per_byte: Some(30.into()),
};

gas_parameter
.rooch_framework_gas_params
.moveos_stdlib
.base64
.decode = EncodeDecodeGasParametersOption {
base: Some(1000.into()),
per_byte: Some(30.into()),
};

gas_parameter
}

pub fn v2() -> Self {
let mut v1_gas_parameter = FrameworksGasParameters::v1();

v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_base = InternalGas::new(167);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_per_arg = InternalGasPerArg::new(15);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_per_local = InternalGasPerArg::new(15);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_base = InternalGas::new(167);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_arg = InternalGasPerArg::new(15);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_local = InternalGasPerArg::new(15);
v1_gas_parameter
.vm_gas_params
.instruction_gas_parameter
.call_generic_per_ty_arg = InternalGasPerArg::new(15);

v1_gas_parameter
}

pub fn latest() -> Self {
FrameworksGasParameters::v2()
}

pub fn to_gas_schedule_config(&self, chain_id: ChainID) -> GasScheduleConfig {
Expand Down
54 changes: 53 additions & 1 deletion crates/rooch/src/commands/upgrade/commands/upgrade_gas_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use rooch_genesis::{FrameworksGasParameters, LATEST_GAS_SCHEDULE_VERSION};
use rooch_types::error::{RoochError, RoochResult};
use rooch_types::rooch_network::BuiltinChainID;
use std::collections::BTreeMap;
use std::io;
use std::io::Write;

/// Upgrade the onchain gas config
#[derive(Debug, clap::Parser)]
Expand Down Expand Up @@ -91,6 +93,21 @@ impl CommandAction<Option<FileOutput>> for UpgradeGasConfigCommand {
local_gas_entries.into_iter().collect();

if local_gas_schedule_map.len() < onchain_gas_schedule_map.len() {
println!(
"local gas entries {:?} != onchain gas entries {:?}",
local_gas_schedule_map.len(),
onchain_gas_schedule_map.len()
);

for (gas_key, _) in onchain_gas_schedule_map.iter() {
match local_gas_schedule_map.get(gas_key) {
None => {
println!("gas entry {:?} is onchain, but not in local.", gas_key);
}
Some(_) => {}
}
}

return Err(RoochError::LessLocalGasScheduleLength);
}

Expand Down Expand Up @@ -130,10 +147,23 @@ impl CommandAction<Option<FileOutput>> for UpgradeGasConfigCommand {
modified_gas_entries.len()
);
for (gas_key, gas_value) in modified_gas_entries.iter() {
println!("modified gas: {:}, value: {:}", gas_key, gas_value);
let old_value = onchain_gas_schedule_map.get(gas_key).unwrap();
println!(
"modified gas: {:}, old value: {}, new value: {:}",
gas_key, old_value, gas_value
);
}
}

if modified_gas_entries.is_empty() && added_gas_entries.is_empty() {
println!("No local gas entries to be upgraded.");
std::process::exit(1);
}

if !get_confirmation() {
std::process::exit(1);
}

(onchain_gas_schedule_version, onchain_gas_schedule_map)
}
};
Expand Down Expand Up @@ -179,3 +209,25 @@ impl CommandAction<Option<FileOutput>> for UpgradeGasConfigCommand {
}
}
}

fn get_confirmation() -> bool {
loop {
print!("Continue? (Yes/No): ");
if io::stdout().flush().is_err() {
return false;
}

let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => match input.trim() {
"Yes" => return true,
"No" => return false,
_ => println!("Please enter 'Yes' or 'No'"),
},
Err(e) => {
eprintln!("Error reading input: {}", e);
return false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::natives::gas_parameter::native::MUL;
use moveos_stdlib::natives::moveos_stdlib::base64::GasParameters;

crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "base64", [
[.encode.base, optional "encode.base", 1000 * MUL],
[.encode.per_byte, optional "encode.per_byte", 30 * MUL],
[.decode.base, optional "decode.base", 1000 * MUL],
[.decode.per_byte, optional "decode.per_byte", 30 * MUL],
[.encode.base, optional "encode.base", 0],
[.encode.per_byte, optional "encode.per_byte", 0],
[.decode.base, optional "decode.base", 0],
[.decode.per_byte, optional "decode.per_byte", 0],
]);
8 changes: 4 additions & 4 deletions frameworks/rooch-framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub mod rooch_framework;

#[derive(Debug, Clone)]
pub struct NativeGasParameters {
moveos_stdlib: MoveOSStdlibGasParameters,
ed25519: rooch_framework::crypto::ed25519::GasParameters,
ecdsa_k1: rooch_framework::crypto::ecdsa_k1::GasParameters,
bitcoin_address: rooch_framework::bitcoin_address::GasParameters,
pub moveos_stdlib: MoveOSStdlibGasParameters,
pub ed25519: rooch_framework::crypto::ed25519::GasParameters,
pub ecdsa_k1: rooch_framework::crypto::ecdsa_k1::GasParameters,
pub bitcoin_address: rooch_framework::bitcoin_address::GasParameters,
}

impl FromOnChainGasSchedule for NativeGasParameters {
Expand Down

0 comments on commit 1d83a45

Please sign in to comment.