Skip to content
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

Use the Option type to wrap the single gas parameter #1892

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,30 +531,30 @@ mod tests {
);
}

// #[test]
// fn test_builtin_genesis_init() {
// let _ = tracing_subscriber::fmt::try_init();
// {
// let network = BuiltinChainID::Local.into();
// let genesis = RoochGenesis::load(BuiltinChainID::Local).unwrap();
// genesis_init_test_case(network, genesis);
// }
// {
// let network = BuiltinChainID::Dev.into();
// let genesis = RoochGenesis::load(BuiltinChainID::Dev).unwrap();
// genesis_init_test_case(network, genesis);
// }
// {
// let network = BuiltinChainID::Test.into();
// let genesis = RoochGenesis::load(BuiltinChainID::Test).unwrap();
// genesis_init_test_case(network, genesis);
// }
// {
// let network = BuiltinChainID::Main.into();
// let genesis = RoochGenesis::load(BuiltinChainID::Main).unwrap();
// genesis_init_test_case(network, genesis);
// }
// }
#[test]
fn test_builtin_genesis_init() {
let _ = tracing_subscriber::fmt::try_init();
{
let network = BuiltinChainID::Local.into();
let genesis = RoochGenesis::load(BuiltinChainID::Local).unwrap();
genesis_init_test_case(network, genesis);
}
{
let network = BuiltinChainID::Dev.into();
let genesis = RoochGenesis::load(BuiltinChainID::Dev).unwrap();
genesis_init_test_case(network, genesis);
}
{
let network = BuiltinChainID::Test.into();
let genesis = RoochGenesis::load(BuiltinChainID::Test).unwrap();
genesis_init_test_case(network, genesis);
}
{
let network = BuiltinChainID::Main.into();
let genesis = RoochGenesis::load(BuiltinChainID::Main).unwrap();
genesis_init_test_case(network, genesis);
}
}

#[test]
fn test_custom_genesis_init() {
Expand Down
4 changes: 2 additions & 2 deletions frameworks/bitcoin-move/src/natives/gas_parameter/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use rooch_framework::natives::gas_parameter::native::MUL;
rooch_framework::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "ord", [
[.from_witness.base, "from_witness.base", 10000 * MUL],
[.from_witness.per_byte, "from_witness.per_byte", 50 * MUL],
[.parse_inscription_from_witness.base, "parse_inscription_from_witness.base", 10000 * MUL],
[.parse_inscription_from_witness.per_byte, "parse_inscription_from_witness.per_byte", 50 * MUL]
[.parse_inscription_from_witness.base, optional "parse_inscription_from_witness.base", 10000 * MUL],
[.parse_inscription_from_witness.per_byte, optional "parse_inscription_from_witness.per_byte", 50 * MUL]
]);
37 changes: 23 additions & 14 deletions frameworks/bitcoin-move/src/natives/ord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,25 @@ pub(crate) fn native_from_witness(

#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
pub struct ParseInscriptionFromWitnessGasParameters {
pub base: InternalGas,
pub per_byte: InternalGasPerByte,
pub base: Option<InternalGas>,
pub per_byte: Option<InternalGasPerByte>,
}

impl ParseInscriptionFromWitnessGasParameters {
pub fn zeros() -> Self {
Self {
base: 0.into(),
per_byte: 0.into(),
base: None,
per_byte: None,
}
}
}

impl ParseInscriptionFromWitnessGasParameters {
pub fn is_empty(&self) -> bool {
self.base.is_none() || self.per_byte.is_none()
}
}

/// Rust implementation of parse Inscription from witness
#[inline]
pub(crate) fn native_parse_inscription_from_witness(
Expand All @@ -110,15 +116,16 @@ pub(crate) fn native_parse_inscription_from_witness(
debug_assert_eq!(ty_args.len(), 0);
debug_assert_eq!(args.len(), 1);

let mut cost = gas_params.base;
let mut cost = gas_params.base.unwrap();

let witness_ref = pop_arg!(args, StructRef);
let wintness_value = witness_ref.read_ref()?;
let witness = Witness::from_runtime_value(wintness_value).map_err(|e| {
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message(format!("Failed to parse witness: {}", e))
})?;
cost += gas_params.per_byte
let per_byte = gas_params.per_byte.unwrap();
cost += per_byte
* NumBytes::new(
witness
.witness
Expand Down Expand Up @@ -158,19 +165,21 @@ impl GasParameters {
}

pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [
(
"from_witness",
make_native(gas_params.from_witness, native_from_witness),
),
(
let mut natives = [(
"from_witness",
make_native(gas_params.from_witness, native_from_witness),
)]
.to_vec();

if !gas_params.parse_inscription_from_witness.is_empty() {
natives.push((
"parse_inscription_from_witness",
make_native(
gas_params.parse_inscription_from_witness,
native_parse_inscription_from_witness,
),
),
];
));
}

make_module_natives(natives)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasPar

[.signer.borrow_address.base, "signer.borrow_address.base", (353 + 1) * MUL],

[.string.check_utf8.per_byte, optional "string.check_utf8.per_byte", (4 + 1) * MUL],
[.string.is_char_boundary.base, optional "string.is_char_boundary.base", (4 + 1) * MUL],
[.string.sub_string.per_byte, optional "string.sub_string.per_byte", (4 + 1) * MUL],
[.string.index_of.per_byte_searched, optional "string.index_of.per_byte_searched", (4 + 1) * MUL],
[.string.check_utf8.per_byte, "string.check_utf8.per_byte", (4 + 1) * MUL],
[.string.is_char_boundary.base, "string.is_char_boundary.base", (4 + 1) * MUL],
[.string.sub_string.per_byte, "string.sub_string.per_byte", (4 + 1) * MUL],
[.string.index_of.per_byte_searched, "string.index_of.per_byte_searched", (4 + 1) * MUL],

[.vector.length.base, "vector.length.base", (98 + 1) * MUL],
[.vector.empty.base, "vector.empty.base", (84 + 1) * MUL],
Expand Down
32 changes: 27 additions & 5 deletions frameworks/rooch-framework/src/natives/gas_parameter/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
// SPDX-License-Identifier: Apache-2.0
pub const MUL: u64 = 1;

#[macro_export]
macro_rules! set_first_segment {
($params: ident . $first:ident . $($rest:ident).*, $val: ident, $param_ty: ty) => {
$params .$first.$($rest)* = Some((*$val).into());
};
}

#[macro_export]
macro_rules! expand_get_impl_for_native_gas_params {
($params: ident $(.$field: ident)+, $map: ident, $prefix: literal, optional $key: literal) => {
if let Some(val) = $map.get(&format!("{}.{}", $prefix, $key)) {
$params $(.$field)+ = (*val).into();
$params $(.$field)+ = Some((*val).into());
}
};
($params: ident $(.$field: ident)+, $map: ident, $prefix: literal, $key: literal) => {
Expand Down Expand Up @@ -58,9 +65,19 @@ macro_rules! expand_set_for_native_gas_params {
assign(&mut $params);
}
};
($(.$field: ident)+, $(optional)? $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => {
($(.$field: ident)+, $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => {
$params $(.$field)+ = $initial_val.into()
};
($(.$field: ident)+, optional $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => {
$params $(.$field)+ = Some($initial_val.into())
};
}

#[macro_export]
macro_rules! get_self_key_value {
(.$first:ident . $($rest:ident).*, $initial_val: expr, $self: ident, $key: literal) => {
($key, u64::from($self .$first.$($rest)*.unwrap_or(0.into())))
};
}

#[macro_export]
Expand All @@ -69,9 +86,12 @@ macro_rules! expand_kv_for_native_gas_params {
#[cfg(feature = "testing")]
($key, u64::from($self $(.$field)+))
};
($(.$field: ident)+, $(optional)? $key: literal, $initial_val: expr, $self: ident) => {
($(.$field: ident)+, $key: literal, $initial_val: expr, $self: ident) => {
($key, u64::from($self $(.$field)+))
}
};
($(.$field: ident)+, optional $key: literal, $initial_val: expr, $self: ident) => {
$crate::natives::gas_parameter::native::get_self_key_value!($(.$field)+, $initial_val, $self, $key)
};
}

#[macro_export]
Expand All @@ -92,7 +112,7 @@ macro_rules! define_gas_parameters_for_natives {
impl $crate::natives::gas_parameter::gas_member::ToOnChainGasSchedule for $param_ty {
fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)> {
[$($crate::natives::gas_parameter::native::expand_kv_for_native_gas_params!($($t)*, self)),*]
.into_iter().map(|(key, val)| (format!("{}.{}", $package_name, key), val)).collect()
.into_iter().filter(|(_, val)| *val > 0).map(|(key, val)| (format!("{}.{}", $package_name, key), val)).collect()
}
}

Expand Down Expand Up @@ -128,3 +148,5 @@ pub use expand_get_for_native_gas_params;
pub use expand_get_impl_for_native_gas_params;
pub use expand_kv_for_native_gas_params;
pub use expand_set_for_native_gas_params;
pub use get_self_key_value;
pub use set_first_segment;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use move_stdlib::natives::NurseryGasParameters;

crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(NurseryGasParameters, "nursery", [
[.event.write_to_event_store.unit_cost, "event.write_to_event_store.unit_cost", (5 + 1) * MUL],
[.debug.print.base_cost, optional "debug.print.base_cost", MUL],
[.debug.print_stack_trace.base_cost, optional "debug.print_stack_trace.base_cost", MUL],
[.debug.print.base_cost, "debug.print.base_cost", MUL],
[.debug.print_stack_trace.base_cost, "debug.print_stack_trace.base_cost", MUL],
]);
Loading