Skip to content

Commit

Permalink
Replace copied LocalProtocolConfig with &ProtocolConfig (MystenLabs#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
aschran authored Dec 14, 2023
1 parent afd02c4 commit 96bcaf4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 81 deletions.
2 changes: 1 addition & 1 deletion crates/sui-move/src/unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn new_testing_object_and_natives_cost_runtime(ext: &mut NativeContextExtensions
store,
BTreeMap::new(),
false,
&ProtocolConfig::get_for_max_version_UNSAFE(),
Box::leak(Box::new(ProtocolConfig::get_for_max_version_UNSAFE())), // leak for testing
metrics,
0, // epoch id
));
Expand Down
2 changes: 1 addition & 1 deletion sui-execution/latest/sui-adapter/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ mod checked {
child_resolver: &'r dyn ChildObjectResolver,
input_objects: BTreeMap<ObjectID, object_runtime::InputObject>,
is_metered: bool,
protocol_config: &ProtocolConfig,
protocol_config: &'r ProtocolConfig,
metrics: Arc<LimitsMetrics>,
current_epoch_id: EpochId,
) -> NativeContextExtensions<'r> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ mod checked {
tx_context: &'a mut TxContext,
gas_charger: &'a mut GasCharger,
inputs: Vec<CallArg>,
) -> Result<Self, ExecutionError> {
) -> Result<Self, ExecutionError>
where
'a: 'state,
{
let mut linkage_view = LinkageView::new(Box::new(state_view.as_sui_resolver()));
let mut input_object_map = BTreeMap::new();
let inputs = inputs
Expand Down
7 changes: 5 additions & 2 deletions sui-execution/latest/sui-move-natives/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn emit(
);

let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut();
let max_event_emit_size = obj_runtime.local_config.max_event_emit_size;
let max_event_emit_size = obj_runtime.protocol_config.max_event_emit_size();
let ev_size = u64::from(tag_size + event_value_size);
// Check if the event size is within the limit
if ev_size > max_event_emit_size {
Expand All @@ -90,7 +90,10 @@ pub fn emit(

// Check that the size contribution of the event is within the total size limit
// This feature is guarded as its only present in some versions
if let Some(max_event_emit_size_total) = obj_runtime.local_config.max_event_emit_size_total {
if let Some(max_event_emit_size_total) = obj_runtime
.protocol_config
.max_event_emit_size_total_as_option()
{
let total_events_size = obj_runtime.state.total_events_size() + ev_size;
if total_events_size > max_event_emit_size_total {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand Down
73 changes: 14 additions & 59 deletions sui-execution/latest/sui-move-natives/src/object_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,6 @@ pub(crate) struct ObjectRuntimeState {
received: LinkedHashMap<ObjectID, DynamicallyLoadedObjectMetadata>,
}

#[derive(Clone)]
pub(crate) struct LocalProtocolConfig {
pub(crate) max_num_deleted_move_object_ids: u64,
pub(crate) max_num_deleted_move_object_ids_system_tx: u64,
pub(crate) max_num_event_emit: u64,
pub(crate) max_num_new_move_object_ids: u64,
pub(crate) max_num_new_move_object_ids_system_tx: u64,
pub(crate) max_num_transferred_move_object_ids: u64,
pub(crate) max_num_transferred_move_object_ids_system_tx: u64,
pub(crate) max_event_emit_size: u64,
pub(crate) max_event_emit_size_total: Option<u64>,
pub(crate) object_runtime_max_num_cached_objects: u64,
pub(crate) object_runtime_max_num_cached_objects_system_tx: u64,
pub(crate) object_runtime_max_num_store_entries: u64,
pub(crate) object_runtime_max_num_store_entries_system_tx: u64,
pub(crate) loaded_child_object_format: bool,
pub(crate) loaded_child_object_format_type: bool,
}

impl LocalProtocolConfig {
fn new(config: &ProtocolConfig) -> Self {
Self {
max_num_deleted_move_object_ids: config.max_num_deleted_move_object_ids(),
max_num_event_emit: config.max_num_event_emit(),
max_num_new_move_object_ids: config.max_num_new_move_object_ids(),
max_num_transferred_move_object_ids: config.max_num_transferred_move_object_ids(),
max_event_emit_size: config.max_event_emit_size(),
max_event_emit_size_total: config.max_event_emit_size_total_as_option(),
max_num_deleted_move_object_ids_system_tx: config
.max_num_deleted_move_object_ids_system_tx(),
max_num_new_move_object_ids_system_tx: config.max_num_new_move_object_ids_system_tx(),
max_num_transferred_move_object_ids_system_tx: config
.max_num_transferred_move_object_ids_system_tx(),

object_runtime_max_num_cached_objects: config.object_runtime_max_num_cached_objects(),
object_runtime_max_num_cached_objects_system_tx: config
.object_runtime_max_num_cached_objects_system_tx(),
object_runtime_max_num_store_entries: config.object_runtime_max_num_store_entries(),
object_runtime_max_num_store_entries_system_tx: config
.object_runtime_max_num_store_entries_system_tx(),
loaded_child_object_format: config.loaded_child_object_format(),
loaded_child_object_format_type: config.loaded_child_object_format_type(),
}
}
}

#[derive(Tid)]
pub struct ObjectRuntime<'a> {
child_object_store: ChildObjectStore<'a>,
Expand All @@ -151,7 +105,7 @@ pub struct ObjectRuntime<'a> {
// whether or not this TX is gas metered
is_metered: bool,

pub(crate) local_config: LocalProtocolConfig,
pub(crate) protocol_config: &'a ProtocolConfig,
pub(crate) metrics: Arc<LimitsMetrics>,
}

Expand All @@ -178,7 +132,7 @@ impl<'a> ObjectRuntime<'a> {
object_resolver: &'a dyn ChildObjectResolver,
input_objects: BTreeMap<ObjectID, InputObject>,
is_metered: bool,
protocol_config: &ProtocolConfig,
protocol_config: &'a ProtocolConfig,
metrics: Arc<LimitsMetrics>,
epoch_id: EpochId,
) -> Self {
Expand All @@ -201,7 +155,7 @@ impl<'a> ObjectRuntime<'a> {
object_resolver,
root_version,
is_metered,
LocalProtocolConfig::new(protocol_config),
protocol_config,
metrics.clone(),
epoch_id,
),
Expand All @@ -216,7 +170,7 @@ impl<'a> ObjectRuntime<'a> {
received: LinkedHashMap::new(),
},
is_metered,
local_config: LocalProtocolConfig::new(protocol_config),
protocol_config,
metrics,
}
}
Expand All @@ -227,8 +181,8 @@ impl<'a> ObjectRuntime<'a> {
if let LimitThresholdCrossed::Hard(_, lim) = check_limit_by_meter!(
self.is_metered,
self.state.new_ids.len(),
self.local_config.max_num_new_move_object_ids,
self.local_config.max_num_new_move_object_ids_system_tx,
self.protocol_config.max_num_new_move_object_ids(),
self.protocol_config.max_num_new_move_object_ids_system_tx(),
self.metrics.excessive_new_move_object_ids
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand Down Expand Up @@ -256,8 +210,9 @@ impl<'a> ObjectRuntime<'a> {
if let LimitThresholdCrossed::Hard(_, lim) = check_limit_by_meter!(
self.is_metered,
self.state.deleted_ids.len(),
self.local_config.max_num_deleted_move_object_ids,
self.local_config.max_num_deleted_move_object_ids_system_tx,
self.protocol_config.max_num_deleted_move_object_ids(),
self.protocol_config
.max_num_deleted_move_object_ids_system_tx(),
self.metrics.excessive_deleted_move_object_ids
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand Down Expand Up @@ -319,9 +274,9 @@ impl<'a> ObjectRuntime<'a> {
// TODO: is this not redundant? Metered TX implies framework obj cannot be transferred
self.is_metered && !is_framework_obj, // We have higher limits for unmetered transactions and framework obj
self.state.transfers.len(),
self.local_config.max_num_transferred_move_object_ids,
self.local_config
.max_num_transferred_move_object_ids_system_tx,
self.protocol_config.max_num_transferred_move_object_ids(),
self.protocol_config
.max_num_transferred_move_object_ids_system_tx(),
self.metrics.excessive_transferred_move_object_ids
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand All @@ -336,8 +291,8 @@ impl<'a> ObjectRuntime<'a> {
}

pub fn emit_event(&mut self, ty: Type, tag: StructTag, event: Value) -> PartialVMResult<()> {
if self.state.events.len() >= (self.local_config.max_num_event_emit as usize) {
return Err(max_event_error(self.local_config.max_num_event_emit));
if self.state.events.len() >= (self.protocol_config.max_num_event_emit() as usize) {
return Err(max_event_error(self.protocol_config.max_num_event_emit()));
}
self.state.events.push((ty, tag, event));
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::object_runtime::{get_all_uids, LocalProtocolConfig};
use crate::object_runtime::get_all_uids;
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::{
annotated_value as A, effects::Op, runtime_value as R, vm_status::StatusCode,
Expand All @@ -14,7 +14,7 @@ use std::{
collections::{btree_map, BTreeMap},
sync::Arc,
};
use sui_protocol_config::{check_limit_by_meter, LimitThresholdCrossed};
use sui_protocol_config::{check_limit_by_meter, LimitThresholdCrossed, ProtocolConfig};
use sui_types::{
base_types::{MoveObjectType, ObjectID, SequenceNumber},
committee::EpochId,
Expand Down Expand Up @@ -51,8 +51,8 @@ struct Inner<'a> {
cached_objects: BTreeMap<ObjectID, Option<Object>>,
// whether or not this TX is gas metered
is_metered: bool,
// Local protocol config used to enforce limits
local_config: LocalProtocolConfig,
// Protocol config used to enforce limits
protocol_config: &'a ProtocolConfig,
// Metrics for reporting exceeded limits
metrics: Arc<LimitsMetrics>,
// Epoch ID for the current transaction. Used for receiving objects.
Expand Down Expand Up @@ -207,9 +207,9 @@ impl<'a> Inner<'a> {
if let LimitThresholdCrossed::Hard(_, lim) = check_limit_by_meter!(
self.is_metered,
cached_objects_count,
self.local_config.object_runtime_max_num_cached_objects,
self.local_config
.object_runtime_max_num_cached_objects_system_tx,
self.protocol_config.object_runtime_max_num_cached_objects(),
self.protocol_config
.object_runtime_max_num_cached_objects_system_tx(),
self.metrics.excessive_object_runtime_cached_objects
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<'a> ChildObjectStore<'a> {
resolver: &'a dyn ChildObjectResolver,
root_version: BTreeMap<ObjectID, SequenceNumber>,
is_metered: bool,
local_config: LocalProtocolConfig,
protocol_config: &'a ProtocolConfig,
metrics: Arc<LimitsMetrics>,
current_epoch_id: EpochId,
) -> Self {
Expand All @@ -344,7 +344,7 @@ impl<'a> ChildObjectStore<'a> {
root_version,
cached_objects: BTreeMap::new(),
is_metered,
local_config,
protocol_config,
metrics,
current_epoch_id,
},
Expand Down Expand Up @@ -451,10 +451,12 @@ impl<'a> ChildObjectStore<'a> {
if let LimitThresholdCrossed::Hard(_, lim) = check_limit_by_meter!(
self.is_metered,
store_entries_count,
self.inner.local_config.object_runtime_max_num_store_entries,
self.inner
.local_config
.object_runtime_max_num_store_entries_system_tx,
.protocol_config
.object_runtime_max_num_store_entries(),
self.inner
.protocol_config
.object_runtime_max_num_store_entries_system_tx(),
self.inner.metrics.excessive_object_runtime_store_entries
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand Down Expand Up @@ -497,10 +499,12 @@ impl<'a> ChildObjectStore<'a> {
if let LimitThresholdCrossed::Hard(_, lim) = check_limit_by_meter!(
self.is_metered,
self.store.len(),
self.inner.local_config.object_runtime_max_num_store_entries,
self.inner
.local_config
.object_runtime_max_num_store_entries_system_tx,
.protocol_config
.object_runtime_max_num_store_entries(),
self.inner
.protocol_config
.object_runtime_max_num_store_entries_system_tx(),
self.inner.metrics.excessive_object_runtime_store_entries
) {
return Err(PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED)
Expand All @@ -526,9 +530,10 @@ impl<'a> ChildObjectStore<'a> {
),
);
}
if self.inner.local_config.loaded_child_object_format {
if self.inner.protocol_config.loaded_child_object_format() {
// double check format did not change
if !self.inner.local_config.loaded_child_object_format_type && child_ty != &ty {
if !self.inner.protocol_config.loaded_child_object_format_type() && child_ty != &ty
{
let msg = format!("Type changed for child {child} when setting the value back");
return Err(
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
Expand Down

0 comments on commit 96bcaf4

Please sign in to comment.