From 636b1a24fe98114b4e727f8fa315c1fa9d2f5423 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Tue, 20 Aug 2024 11:31:48 -0500 Subject: [PATCH 1/3] Make indirection consistent for migrated records --- execution_engine/src/runtime/mod.rs | 44 +++++++++++++++------ execution_engine/src/runtime_context/mod.rs | 7 ---- storage/src/tracking_copy/ext_entity.rs | 25 +++++++++--- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/execution_engine/src/runtime/mod.rs b/execution_engine/src/runtime/mod.rs index 1972dfafaa..da5bab1ec6 100644 --- a/execution_engine/src/runtime/mod.rs +++ b/execution_engine/src/runtime/mod.rs @@ -35,8 +35,9 @@ use casper_types::{ addressable_entity::{ self, ActionThresholds, ActionType, AddKeyFailure, AddressableEntity, AddressableEntityHash, AssociatedKeys, EntityKindTag, EntryPoint, EntryPointAccess, - EntryPointType, EntryPoints, MessageTopicError, MessageTopics, NamedKeys, Parameter, - RemoveKeyFailure, SetThresholdFailure, UpdateKeyFailure, Weight, DEFAULT_ENTRY_POINT_NAME, + EntryPointType, EntryPoints, MessageTopicError, MessageTopics, NamedKeyAddr, NamedKeyValue, + NamedKeys, Parameter, RemoveKeyFailure, SetThresholdFailure, UpdateKeyFailure, Weight, + DEFAULT_ENTRY_POINT_NAME, }, bytesrepr::{self, Bytes, FromBytes, ToBytes}, contract_messages::{ @@ -1908,6 +1909,9 @@ where } if entry_points.contains_stored_session() { + // As of 2.0 we do not allow stored session logic to be + // installed or upgraded. Pre-existing stored + // session logic is still callable. return Err(ExecError::InvalidEntryPointType); } @@ -1953,13 +1957,15 @@ where } } + // We generate the byte code hash because a byte code record + // must exist for a contract record to exist. let byte_code_hash = self.context.new_hash_address()?; let entity_hash = self.context.new_hash_address()?; let protocol_version = self.context.protocol_version(); - let insert_contract_result = + let insert_entity_version_result = package.insert_entity_version(protocol_version.value().major, entity_hash.into()); let byte_code = { @@ -1974,12 +1980,24 @@ where let entity_addr = EntityAddr::new_smart_contract(entity_hash); - let entity_key = Key::AddressableEntity(entity_addr); - - named_keys.append(previous_named_keys); - self.context.write_named_keys(entity_addr, named_keys)?; - - self.context.write_entry_points(entity_addr, entry_points)?; + { + // DO NOT EXTRACT INTO SEPARATE FUNCTION. + for (_, key) in named_keys.iter() { + // Validate all the imputed named keys + // against the installers permissions + self.validate_key(key)?; + } + // Carry forward named keys from previous version + // Grant all the imputed named keys + previous named keys. + named_keys.append(previous_named_keys); + for (name, key) in named_keys.iter() { + let named_key_value = + StoredValue::NamedKey(NamedKeyValue::from_concrete_values(*key, name.clone())?); + let named_key_addr = NamedKeyAddr::new_from_string(entity_addr, name.clone())?; + self.metered_write_gs_unsafe(Key::NamedKey(named_key_addr), named_key_value)?; + } + self.context.write_entry_points(entity_addr, entry_points)?; + } let entity = AddressableEntity::new( package_hash, @@ -1991,7 +2009,7 @@ where previous_message_topics.clone(), EntityKind::SmartContract(TransactionRuntime::VmCasperV1), ); - + let entity_key = Key::AddressableEntity(entity_addr); self.context.metered_write_gs_unsafe(entity_key, entity)?; self.context .metered_write_gs_unsafe(package_hash, package)?; @@ -2018,7 +2036,7 @@ where } // Set version into VM shared memory - let version_value: u32 = insert_contract_result.entity_version(); + let version_value: u32 = insert_entity_version_result.entity_version(); let version_bytes = version_value.to_le_bytes(); if let Err(error) = self.try_get_memory()?.set(version_ptr, &version_bytes) { return Err(ExecError::Interpreter(error.into())); @@ -2064,7 +2082,7 @@ where // addressable entity format let account_hash = self.context.get_caller(); - let access_key = match self + let (_package_key, access_key) = match self .context .read_gs(&Key::Hash(previous_entity.package_hash().value()))? .and_then(|stored_value| stored_value.into_cl_value()) @@ -2072,7 +2090,7 @@ where None => { return Err(ExecError::UpgradeAuthorizationFailure); } - Some(cl_value) => cl_value.into_t::().map_err(ExecError::CLValue), + Some(cl_value) => cl_value.into_t::<(Key, URef)>().map_err(ExecError::CLValue), }?; let has_access = self.context.validate_uref(&access_key).is_ok(); diff --git a/execution_engine/src/runtime_context/mod.rs b/execution_engine/src/runtime_context/mod.rs index 1a62a3a5bb..ef30af8547 100644 --- a/execution_engine/src/runtime_context/mod.rs +++ b/execution_engine/src/runtime_context/mod.rs @@ -398,13 +398,6 @@ where entity_addr: EntityAddr, named_keys: NamedKeys, ) -> Result<(), ExecError> { - for (name, key) in named_keys.iter() { - let named_key_value = - StoredValue::NamedKey(NamedKeyValue::from_concrete_values(*key, name.clone())?); - let named_key_addr = NamedKeyAddr::new_from_string(entity_addr, name.clone())?; - self.metered_write_gs_unsafe(Key::NamedKey(named_key_addr), named_key_value)?; - } - Ok(()) } diff --git a/storage/src/tracking_copy/ext_entity.rs b/storage/src/tracking_copy/ext_entity.rs index f78ec9bb11..1aceaa1115 100644 --- a/storage/src/tracking_copy/ext_entity.rs +++ b/storage/src/tracking_copy/ext_entity.rs @@ -348,12 +348,9 @@ where ) -> Result<(), Self::Error> { for (string, key) in named_keys.into_inner().into_iter() { let entry_addr = NamedKeyAddr::new_from_string(entity_addr, string.clone())?; - let named_key_value = StoredValue::NamedKey(NamedKeyValue::from_concrete_values(key, string.clone())?); - let entry_key = Key::NamedKey(entry_addr); - self.write(entry_key, named_key_value) } @@ -652,6 +649,14 @@ where let byte_code_key = Key::byte_code_key(ByteCodeAddr::new_wasm_addr( updated_entity.byte_code_addr(), )); + let byte_code_cl_value = match CLValue::from_t(byte_code_key) { + Ok(cl_value) => cl_value, + Err(err) => return Err(Self::Error::CLValue(err)), + }; + self.write( + Key::Hash(updated_entity.byte_code_addr()), + StoredValue::CLValue(byte_code_cl_value), + ); let byte_code: ByteCode = contract_wasm.into(); self.write(byte_code_key, StoredValue::ByteCode(byte_code)); @@ -660,19 +665,27 @@ where let entity_hash = AddressableEntityHash::new(contract_hash.value()); let entity_key = Key::contract_entity_key(entity_hash); + let indirection = match CLValue::from_t(entity_key) { + Ok(cl_value) => cl_value, + Err(err) => return Err(Self::Error::CLValue(err)), + }; + self.write( + Key::Hash(contract_hash.value()), + StoredValue::CLValue(indirection), + ); + self.write(entity_key, StoredValue::AddressableEntity(updated_entity)); } - let access_key_value = CLValue::from_t(access_uref).map_err(Self::Error::CLValue)?; - let package_key = Key::Package( legacy_package_key .into_hash_addr() .ok_or(Self::Error::UnexpectedKeyVariant(legacy_package_key))?, ); + let access_key_value = + CLValue::from_t((package_key, access_uref)).map_err(Self::Error::CLValue)?; self.write(legacy_package_key, StoredValue::CLValue(access_key_value)); - self.write(package_key, StoredValue::Package(package)); Ok(()) } From 81911e91f7d7f5b7e06c9d1ebf1251988776a2a4 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Tue, 20 Aug 2024 12:01:48 -0500 Subject: [PATCH 2/3] Clear EE tests --- execution_engine/src/runtime/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/execution_engine/src/runtime/mod.rs b/execution_engine/src/runtime/mod.rs index da5bab1ec6..8debe05174 100644 --- a/execution_engine/src/runtime/mod.rs +++ b/execution_engine/src/runtime/mod.rs @@ -1985,7 +1985,7 @@ where for (_, key) in named_keys.iter() { // Validate all the imputed named keys // against the installers permissions - self.validate_key(key)?; + self.context.validate_key(key)?; } // Carry forward named keys from previous version // Grant all the imputed named keys + previous named keys. @@ -1994,7 +1994,8 @@ where let named_key_value = StoredValue::NamedKey(NamedKeyValue::from_concrete_values(*key, name.clone())?); let named_key_addr = NamedKeyAddr::new_from_string(entity_addr, name.clone())?; - self.metered_write_gs_unsafe(Key::NamedKey(named_key_addr), named_key_value)?; + self.context + .metered_write_gs_unsafe(Key::NamedKey(named_key_addr), named_key_value)?; } self.context.write_entry_points(entity_addr, entry_points)?; } From 6092eaeb5b64ca9966fd0e4f55d5cae9e6693231 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Tue, 20 Aug 2024 12:19:23 -0500 Subject: [PATCH 3/3] Run make format --- execution_engine/src/runtime_context/mod.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/execution_engine/src/runtime_context/mod.rs b/execution_engine/src/runtime_context/mod.rs index ef30af8547..96d4638256 100644 --- a/execution_engine/src/runtime_context/mod.rs +++ b/execution_engine/src/runtime_context/mod.rs @@ -393,14 +393,6 @@ where Ok(()) } - pub(crate) fn write_named_keys( - &mut self, - entity_addr: EntityAddr, - named_keys: NamedKeys, - ) -> Result<(), ExecError> { - Ok(()) - } - pub(crate) fn get_named_keys(&mut self, entity_key: Key) -> Result { let entity_addr = if let Key::AddressableEntity(entity_addr) = entity_key { entity_addr