Skip to content

Commit

Permalink
Merge pull request #4320 from darthsiroftardis/refactor-contract-types
Browse files Browse the repository at this point in the history
Disable stored session and rework standard payment
  • Loading branch information
sacherjj authored Oct 19, 2023
2 parents ece8660 + 58cb770 commit a1cdc10
Show file tree
Hide file tree
Showing 221 changed files with 8,703 additions and 5,832 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

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

13 changes: 10 additions & 3 deletions execution_engine/benches/trie_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use casper_storage::global_state::trie::{Pointer, PointerBlock, Trie};
use casper_types::{
account::AccountHash,
bytesrepr::{FromBytes, ToBytes},
CLValue, ContractHash, Digest, Key, StoredValue,
package::PackageKindTag,
AddressableEntityHash, CLValue, Digest, Key, StoredValue,
};

fn serialize_trie_leaf(b: &mut Bencher) {
let contract_key: Key = ContractHash::new([42; 32]).into();
let contract_key = Key::addressable_entity_key(
PackageKindTag::SmartContract,
AddressableEntityHash::new([42; 32]),
);
let leaf = Trie::Leaf {
key: Key::Account(AccountHash::new([0; 32])),
value: StoredValue::CLValue(CLValue::from_t(contract_key).unwrap()),
Expand All @@ -17,7 +21,10 @@ fn serialize_trie_leaf(b: &mut Bencher) {
}

fn deserialize_trie_leaf(b: &mut Bencher) {
let contract_key: Key = ContractHash::new([42; 32]).into();
let contract_key: Key = Key::addressable_entity_key(
PackageKindTag::SmartContract,
AddressableEntityHash::new([42; 32]),
);
let leaf = Trie::Leaf {
key: Key::Account(AccountHash::new([0; 32])),
value: StoredValue::CLValue(CLValue::from_t(contract_key).unwrap()),
Expand Down
6 changes: 3 additions & 3 deletions execution_engine/src/engine_state/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use thiserror::Error;

use casper_storage::global_state::{self, state::CommitError};
use casper_types::{
account::AccountHash, bytesrepr, system::mint, ApiError, ContractPackageHash, Digest, Key,
KeyTag, ProtocolVersion,
account::AccountHash, bytesrepr, system::mint, ApiError, Digest, Key, KeyTag, PackageHash,
ProtocolVersion,
};

use crate::{
Expand Down Expand Up @@ -107,7 +107,7 @@ pub enum Error {
MissingContractByAccountHash(AccountHash),
/// Failed to retrieve the entity's package
#[error("Failed to retrieve the entity package as {0}")]
MissingEntityPackage(ContractPackageHash),
MissingEntityPackage(PackageHash),
/// Failed to retrieve accumulation purse from handle payment system contract.
#[error("Failed to retrieve accumulation purse from the handle payment contract")]
FailedToRetrieveAccumulationPurse,
Expand Down
106 changes: 54 additions & 52 deletions execution_engine/src/engine_state/execution_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{cell::RefCell, rc::Rc};

use casper_storage::global_state::state::StateReader;
use casper_types::{
addressable_entity::NamedKeys, bytesrepr::Bytes, ContractHash, ContractPackageHash,
ContractVersionKey, ExecutableDeployItem, Key, Package, Phase, ProtocolVersion, StoredValue,
addressable_entity::NamedKeys, bytesrepr::Bytes, AddressableEntityHash, EntityVersionKey,
ExecutableDeployItem, Key, Package, PackageHash, Phase, ProtocolVersion, StoredValue,
};

use crate::{
Expand All @@ -21,8 +21,8 @@ pub(crate) enum ExecutionKind {
Module(Bytes),
/// Stored contract.
Contract {
/// Contract's hash.
contract_hash: ContractHash,
/// AddressableEntity's hash.
entity_hash: AddressableEntityHash,
/// Entry point's name.
entry_point_name: String,
},
Expand All @@ -35,9 +35,12 @@ impl ExecutionKind {
}

/// Returns a new contract variant of `ExecutionKind`.
pub fn new_contract(contract_hash: ContractHash, entry_point_name: String) -> Self {
pub fn new_addressable_entity(
entity_hash: AddressableEntityHash,
entry_point_name: String,
) -> Self {
ExecutionKind::Contract {
contract_hash,
entity_hash,
entry_point_name,
}
}
Expand All @@ -56,8 +59,7 @@ impl ExecutionKind {
R: StateReader<Key, StoredValue>,
R::Error: Into<ExecError>,
{
let contract_hash: ContractHash;
let contract_package: Package;
let package: Package;

let is_payment_phase = phase == Phase::Payment;

Expand All @@ -77,101 +79,101 @@ impl ExecutionKind {
}
ExecutableDeployItem::StoredContractByHash {
hash, entry_point, ..
} => Ok(ExecutionKind::new_contract(hash, entry_point)),
} => Ok(ExecutionKind::new_addressable_entity(hash, entry_point)),
ExecutableDeployItem::StoredContractByName {
name, entry_point, ..
} => {
let contract_key = named_keys.get(&name).cloned().ok_or_else(|| {
let entity_key = named_keys.get(&name).cloned().ok_or_else(|| {
Error::Exec(execution::Error::NamedKeyNotFound(name.to_string()))
})?;

contract_hash =
ContractHash::new(contract_key.into_hash().ok_or(Error::InvalidKeyVariant)?);
let entity_hash = match entity_key {
Key::Hash(hash) | Key::AddressableEntity((_, hash)) => {
AddressableEntityHash::new(hash)
}
_ => return Err(Error::InvalidKeyVariant),
};

Ok(ExecutionKind::new_contract(contract_hash, entry_point))
Ok(ExecutionKind::new_addressable_entity(
entity_hash,
entry_point,
))
}
ExecutableDeployItem::StoredVersionedContractByName {
name,
version,
entry_point,
..
} => {
let contract_package_hash: ContractPackageHash = {
named_keys
.get(&name)
.cloned()
.ok_or_else(|| {
Error::Exec(execution::Error::NamedKeyNotFound(name.to_string()))
})?
.into_hash()
.ok_or(Error::InvalidKeyVariant)?
.into()
let package_key = named_keys.get(&name).cloned().ok_or_else(|| {
Error::Exec(execution::Error::NamedKeyNotFound(name.to_string()))
})?;

let package_hash = match package_key {
Key::Hash(hash) | Key::Package(hash) => PackageHash::new(hash),
_ => return Err(Error::InvalidKeyVariant),
};

contract_package = tracking_copy
.borrow_mut()
.get_contract_package(contract_package_hash)?;
package = tracking_copy.borrow_mut().get_package(package_hash)?;

let maybe_version_key =
version.map(|ver| ContractVersionKey::new(protocol_version.value().major, ver));
version.map(|ver| EntityVersionKey::new(protocol_version.value().major, ver));

let contract_version_key = maybe_version_key
.or_else(|| contract_package.current_contract_version())
.ok_or(Error::Exec(execution::Error::NoActiveContractVersions(
contract_package_hash,
.or_else(|| package.current_entity_version())
.ok_or(Error::Exec(execution::Error::NoActiveEntityVersions(
package_hash,
)))?;

if !contract_package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(execution::Error::InvalidContractVersion(
if !package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(execution::Error::InvalidEntityVersion(
contract_version_key,
)));
}

let looked_up_contract_hash: ContractHash = contract_package
.lookup_contract_hash(contract_version_key)
.ok_or(Error::Exec(execution::Error::InvalidContractVersion(
let looked_up_entity_hash: AddressableEntityHash = package
.lookup_entity_hash(contract_version_key)
.ok_or(Error::Exec(execution::Error::InvalidEntityVersion(
contract_version_key,
)))?
.to_owned();

Ok(ExecutionKind::new_contract(
looked_up_contract_hash,
Ok(ExecutionKind::new_addressable_entity(
looked_up_entity_hash,
entry_point,
))
}
ExecutableDeployItem::StoredVersionedContractByHash {
hash: contract_package_hash,
hash: package_hash,
version,
entry_point,
..
} => {
contract_package = tracking_copy
.borrow_mut()
.get_contract_package(contract_package_hash)?;
package = tracking_copy.borrow_mut().get_package(package_hash)?;

let maybe_version_key =
version.map(|ver| ContractVersionKey::new(protocol_version.value().major, ver));
version.map(|ver| EntityVersionKey::new(protocol_version.value().major, ver));

let contract_version_key = maybe_version_key
.or_else(|| contract_package.current_contract_version())
.ok_or(Error::Exec(execution::Error::NoActiveContractVersions(
contract_package_hash,
.or_else(|| package.current_entity_version())
.ok_or(Error::Exec(execution::Error::NoActiveEntityVersions(
package_hash,
)))?;

if !contract_package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(execution::Error::InvalidContractVersion(
if !package.is_version_enabled(contract_version_key) {
return Err(Error::Exec(execution::Error::InvalidEntityVersion(
contract_version_key,
)));
}

let looked_up_contract_hash = *contract_package
.lookup_contract_hash(contract_version_key)
.ok_or(Error::Exec(execution::Error::InvalidContractVersion(
let looked_up_entity_hash = *package
.lookup_entity_hash(contract_version_key)
.ok_or(Error::Exec(execution::Error::InvalidEntityVersion(
contract_version_key,
)))?;

Ok(ExecutionKind::new_contract(
looked_up_contract_hash,
Ok(ExecutionKind::new_addressable_entity(
looked_up_entity_hash,
entry_point,
))
}
Expand Down
Loading

0 comments on commit a1cdc10

Please sign in to comment.