Skip to content

Commit

Permalink
Add max_runtime_module_exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon committed Aug 14, 2024
1 parent ac14924 commit 734a280
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ massa_wallet = { path = "./massa-wallet" }

# Massa projects dependencies
massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs", "rev" = "38950875a7aa406fedc4f0b8336864e5ff290f2c" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", "rev" = "80352eb9f2a6b90a441cd64433d8874c33fb384f" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", "branch" = "add_condom" }
peernet = { git = "https://github.com/massalabs/PeerNet", "rev" = "04b05ddd320fbe76cc858115af7b5fc28bdb8310" }

# Common dependencies
Expand Down
2 changes: 2 additions & 0 deletions massa-execution-exports/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ pub struct ExecutionConfig {
pub max_execution_traces_slot_limit: usize,
/// Where to dump blocks
pub block_dump_folder_path: PathBuf,
/// Max runtime module exports
pub max_runtime_module_exports: usize,
}
6 changes: 6 additions & 0 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl ExecutionState {
hd_cache_size: config.hd_cache_size,
snip_amount: config.snip_amount,
max_module_length: config.max_bytecode_size,
max_runtime_module_exports: config.max_runtime_module_exports,
})));

// Create an empty placeholder execution context, with shared atomic access
Expand Down Expand Up @@ -970,6 +971,7 @@ impl ExecutionState {
module,
*max_gas,
self.config.gas_costs.clone(),
self.config.max_runtime_module_exports,
)
.map_err(|error| ExecutionError::VMError {
context: "ExecuteSC".to_string(),
Expand Down Expand Up @@ -1070,6 +1072,7 @@ impl ExecutionState {
param,
max_gas,
self.config.gas_costs.clone(),
self.config.max_runtime_module_exports,
);
match response {
Ok(Response { init_gas_cost, .. })
Expand Down Expand Up @@ -1195,6 +1198,7 @@ impl ExecutionState {
&message.function_params,
message.max_gas,
self.config.gas_costs.clone(),
self.config.max_runtime_module_exports,
);
match response {
Ok(res) => {
Expand Down Expand Up @@ -1824,6 +1828,7 @@ impl ExecutionState {
module,
req.max_gas,
self.config.gas_costs.clone(),
self.config.max_runtime_module_exports,
)
.map_err(|error| ExecutionError::VMError {
context: "ReadOnlyExecutionTarget::BytecodeExecution".to_string(),
Expand Down Expand Up @@ -1878,6 +1883,7 @@ impl ExecutionState {
&parameter,
req.max_gas,
self.config.gas_costs.clone(),
self.config.max_runtime_module_exports,
);

match response {
Expand Down
1 change: 1 addition & 0 deletions massa-execution-worker/src/interface_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl InterfaceImpl {
hd_cache_size: config.hd_cache_size,
snip_amount: config.snip_amount,
max_module_length: config.max_bytecode_size,
max_runtime_module_exports: config.max_runtime_module_exports,
})));

// create an empty default store
Expand Down
3 changes: 3 additions & 0 deletions massa-models/src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ pub const MAX_FUNCTION_NAME_LENGTH: u16 = u16::MAX;
pub const MAX_PARAMETERS_SIZE: u32 = 10_000_000;
/// Maximum length of `rng_seed` in thread cycle
pub const MAX_RNG_SEED_LENGTH: u32 = PERIODS_PER_CYCLE.saturating_mul(THREAD_COUNT as u64) as u32;
/// Maximum number of exports for a smart contract module
pub const MAX_RUNTIME_MODULE_EXPORTS: u32 = 500; // TODO: Calibrate

// ***********************
// Bootstrap constants
//
Expand Down
2 changes: 2 additions & 0 deletions massa-module-cache/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ pub struct ModuleCacheConfig {
pub snip_amount: usize,
/// Maximum length of a module
pub max_module_length: u64,
/// Maximum number of exports in a module
pub max_runtime_module_exports: usize,
}
26 changes: 22 additions & 4 deletions massa-module-cache/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ impl ModuleCache {

/// Internal function to compile and build `ModuleInfo`
fn compile_cached(&mut self, bytecode: &[u8], hash: Hash) -> ModuleInfo {
match RuntimeModule::new(bytecode, self.cfg.gas_costs.clone(), Compiler::CL) {
match RuntimeModule::new(
bytecode,
self.cfg.gas_costs.clone(),
Compiler::CL,
self.cfg.max_runtime_module_exports,
) {
Ok(module) => {
debug!("compilation of module {} succeeded", hash);
ModuleInfo::Module(module)
Expand All @@ -57,7 +62,11 @@ impl ModuleCache {
/// Save a new or an already existing module in the cache
pub fn save_module(&mut self, bytecode: &[u8]) {
let hash = Hash::compute_from(bytecode);
if let Some(hd_module_info) = self.hd_cache.get(hash, self.cfg.gas_costs.clone()) {
if let Some(hd_module_info) = self.hd_cache.get(
hash,
self.cfg.gas_costs.clone(),
self.cfg.max_runtime_module_exports,
) {
debug!("save_module: {} present in hd", hash);
self.lru_cache.insert(hash, hd_module_info);
} else if let Some(lru_module_info) = self.lru_cache.get(hash) {
Expand Down Expand Up @@ -110,7 +119,11 @@ impl ModuleCache {
if let Some(lru_module_info) = self.lru_cache.get(hash) {
debug!("load_module: {} present in lru", hash);
lru_module_info
} else if let Some(hd_module_info) = self.hd_cache.get(hash, self.cfg.gas_costs.clone()) {
} else if let Some(hd_module_info) = self.hd_cache.get(
hash,
self.cfg.gas_costs.clone(),
self.cfg.max_runtime_module_exports,
) {
debug!("load_module: {} missing in lru but present in hd", hash);
self.lru_cache.insert(hash, hd_module_info.clone());
hd_module_info
Expand Down Expand Up @@ -181,7 +194,12 @@ impl ModuleCache {
"Provided gas {} is lower than the base instance creation gas cost {}",
limit, self.cfg.gas_costs.max_instance_cost
)))?;
let module = RuntimeModule::new(bytecode, self.cfg.gas_costs.clone(), Compiler::SP)?;
let module = RuntimeModule::new(
bytecode,
self.cfg.gas_costs.clone(),
Compiler::SP,
self.cfg.max_runtime_module_exports,
)?;
Ok(module)
}
}
22 changes: 13 additions & 9 deletions massa-module-cache/src/hd_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl HDCache {
}

/// Retrieve a module
pub fn get(&self, hash: Hash, gas_costs: GasCosts) -> Option<ModuleInfo> {
pub fn get(&self, hash: Hash, gas_costs: GasCosts, max_exports: usize) -> Option<ModuleInfo> {
let mut iterator = self
.db
.iterator(IteratorMode::From(&module_key!(hash), Direction::Forward));
Expand All @@ -153,9 +153,13 @@ impl HDCache {
if let ModuleMetadata::Invalid(err_msg) = metadata {
return Some(ModuleInfo::Invalid(err_msg));
}
let module =
RuntimeModule::deserialize(&ser_module, gas_costs.max_instance_cost, gas_costs)
.expect(MOD_DESER_ERROR);
let module = RuntimeModule::deserialize(
&ser_module,
gas_costs.max_instance_cost,
gas_costs,
max_exports,
)
.expect(MOD_DESER_ERROR);
let result = match metadata {
ModuleMetadata::Invalid(err_msg) => ModuleInfo::Invalid(err_msg),
ModuleMetadata::NotExecuted => ModuleInfo::Module(module),
Expand Down Expand Up @@ -233,7 +237,7 @@ mod tests {
0x70, 0x30,
];
ModuleInfo::Module(
RuntimeModule::new(&bytecode, GasCosts::default(), Compiler::CL).unwrap(),
RuntimeModule::new(&bytecode, GasCosts::default(), Compiler::CL, 100).unwrap(),
)
}

Expand All @@ -253,16 +257,16 @@ mod tests {
let gas_costs = GasCosts::default();

cache.insert(hash, module);
let cached_module_v1 = cache.get(hash, gas_costs.clone()).unwrap();
let cached_module_v1 = cache.get(hash, gas_costs.clone(), 100).unwrap();
assert!(matches!(cached_module_v1, ModuleInfo::Module(_)));

cache.set_init_cost(hash, init_cost);
let cached_module_v2 = cache.get(hash, gas_costs.clone()).unwrap();
let cached_module_v2 = cache.get(hash, gas_costs.clone(), 100).unwrap();
assert!(matches!(cached_module_v2, ModuleInfo::ModuleAndDelta(_)));

let err_msg = "test_error".to_string();
cache.set_invalid(hash, err_msg.clone());
let cached_module_v3 = cache.get(hash, gas_costs).unwrap();
let cached_module_v3 = cache.get(hash, gas_costs, 100).unwrap();
let ModuleInfo::Invalid(res_err) = cached_module_v3 else {
panic!("expected ModuleInfo::Invalid");
};
Expand Down Expand Up @@ -309,7 +313,7 @@ mod tests {
let mut rbytes = [0u8; 16];
thread_rng().fill_bytes(&mut rbytes);
let get_key = Hash::compute_from(&rbytes);
let cached_module = cache.get(get_key, gas_costs.clone());
let cached_module = cache.get(get_key, gas_costs.clone(), 100);
assert!(cached_module.is_none());
}
}
Expand Down
1 change: 1 addition & 0 deletions massa-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ async fn launch(
.broadcast_slot_execution_traces_channel_capacity,
max_execution_traces_slot_limit: SETTINGS.execution.execution_traces_limit,
block_dump_folder_path,
max_runtime_module_exports: MAX_RUNTIME_MODULE_EXPORTS,
};

let execution_channels = ExecutionChannels {
Expand Down

0 comments on commit 734a280

Please sign in to comment.