Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4831: Consistent carry forward r=darthsiroftardis a=darthsiroftardis

CHANGELOG:


- Make the carry forward insertions consistent across all migrated records

Co-authored-by: Karan Dhareshwar <[email protected]>
  • Loading branch information
2 parents b92f881 + 6092eae commit 4e2ddf4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
45 changes: 32 additions & 13 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 = {
Expand All @@ -1974,12 +1980,25 @@ 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.context.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.context
.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,
Expand All @@ -1991,7 +2010,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)?;
Expand All @@ -2018,7 +2037,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()));
Expand Down Expand Up @@ -2064,15 +2083,15 @@ 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())
{
None => {
return Err(ExecError::UpgradeAuthorizationFailure);
}
Some(cl_value) => cl_value.into_t::<URef>().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();
Expand Down
15 changes: 0 additions & 15 deletions execution_engine/src/runtime_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,6 @@ where
Ok(())
}

pub(crate) fn write_named_keys(
&mut self,
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(())
}

pub(crate) fn get_named_keys(&mut self, entity_key: Key) -> Result<NamedKeys, ExecError> {
let entity_addr = if let Key::AddressableEntity(entity_addr) = entity_key {
entity_addr
Expand Down
25 changes: 19 additions & 6 deletions storage/src/tracking_copy/ext_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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));
Expand All @@ -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(())
}
Expand Down

0 comments on commit 4e2ddf4

Please sign in to comment.