Skip to content

Commit

Permalink
Merge pull request #17 from mpapierski/migrate-entry-points
Browse files Browse the repository at this point in the history
Extend for vm2 entry points.
  • Loading branch information
darthsiroftardis authored Apr 10, 2024
2 parents b3fa127 + 958c56b commit 72620c1
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 68 deletions.
5 changes: 4 additions & 1 deletion execution_engine/src/execution/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use casper_types::{
bytesrepr,
execution::TransformError,
system, AccessRights, AddressableEntityHash, ApiError, ByteCodeHash, CLType, CLValueError,
EntityVersionKey, Key, PackageHash, StoredValueTypeMismatch, URef,
EntityVersionKey, Key, PackageHash, StoredValueTypeMismatch, TransactionRuntime, URef,
};
use casper_wasm::elements;

Expand Down Expand Up @@ -189,6 +189,9 @@ pub enum Error {
/// Invalid string encoding.
#[error("Invalid UTF-8 string encoding: {0}")]
InvalidUtf8Encoding(Utf8Error),
/// Incompatible transaction runtime.
#[error("Incompatible runtime: {0}")]
IncompatibleRuntime(TransactionRuntime),
}

impl From<PreprocessingError> for Error {
Expand Down
14 changes: 9 additions & 5 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ use casper_types::{
CLValue, ContextAccessRights, ContractWasm, EntityAddr, EntityKind, EntityVersion,
EntityVersionKey, EntityVersions, EntryPointAddr, EntryPointValue, Gas, GrantedAccess, Group,
Groups, HoldsEpoch, HostFunction, HostFunctionCost, InitiatorAddr, Key, NamedArg, Package,
PackageHash, PackageStatus, Phase, PublicKey, RuntimeArgs, StoredValue, Transfer,
TransferResult, TransferV2, TransferredTo, URef, DICTIONARY_ITEM_KEY_MAX_LENGTH, U512,
PackageHash, PackageStatus, Phase, PublicKey, RuntimeArgs, StoredValue, TransactionRuntime,
Transfer, TransferResult, TransferV2, TransferredTo, URef, DICTIONARY_ITEM_KEY_MAX_LENGTH,
U512,
};

use crate::{
Expand Down Expand Up @@ -1444,9 +1445,12 @@ where
EntityKind::System(_) | EntityKind::Account(_) => {
Key::ByteCode(ByteCodeAddr::Empty)
}
EntityKind::SmartContract => {
EntityKind::SmartContract(TransactionRuntime::VmCasperV1) => {
Key::ByteCode(ByteCodeAddr::new_wasm_addr(byte_code_addr))
}
EntityKind::SmartContract(runtime @ TransactionRuntime::VmCasperV2) => {
return Err(ExecError::IncompatibleRuntime(runtime))
}
};

let byte_code: ByteCode = match self.context.read_gs(&byte_code_key)? {
Expand Down Expand Up @@ -1849,7 +1853,7 @@ where
associated_keys,
action_thresholds,
previous_message_topics.clone(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
);

self.context.metered_write_gs_unsafe(entity_key, entity)?;
Expand Down Expand Up @@ -3383,7 +3387,7 @@ where
associated_keys,
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
);

let previous_wasm = self.context.read_gs_typed::<ContractWasm>(&Key::Hash(
Expand Down
15 changes: 8 additions & 7 deletions storage/src/tracking_copy/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use casper_types::{
global_state::TrieMerkleProof,
handle_stored_dictionary_value, AccessRights, AddressableEntity, ByteCodeHash, CLValue,
CLValueDictionary, CLValueError, EntityAddr, EntityKind, EntryPoints, HashAddr, Key, KeyTag,
PackageHash, ProtocolVersion, StoredValue, URef, U256, U512, UREF_ADDR_LENGTH,
PackageHash, ProtocolVersion, StoredValue, TransactionRuntime, URef, U256, U512,
UREF_ADDR_LENGTH,
};

use super::{
Expand Down Expand Up @@ -553,7 +554,7 @@ proptest! {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract
EntityKind::SmartContract(TransactionRuntime::VmCasperV1)
));
let contract_key = Key::AddressableEntity(EntityAddr::SmartContract(hash));

Expand Down Expand Up @@ -646,7 +647,7 @@ proptest! {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract
EntityKind::SmartContract(TransactionRuntime::VmCasperV1)
));
let contract_key = Key::AddressableEntity(EntityAddr::SmartContract(hash));
let contract_named_key = NamedKeyAddr::new_from_string(EntityAddr::SmartContract(hash), state_name.clone())
Expand Down Expand Up @@ -743,7 +744,7 @@ fn query_for_circular_references_should_fail() {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
));

let name_key_cl_value = Key::NamedKey(
Expand Down Expand Up @@ -818,7 +819,7 @@ fn validate_query_proof_should_work() {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
));

let c_nk = "abc".to_string();
Expand Down Expand Up @@ -1073,7 +1074,7 @@ fn query_with_large_depth_with_fixed_path_should_fail() {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
));
pairs.push((contract_key, contract));
contract_keys.push(contract_key);
Expand Down Expand Up @@ -1140,7 +1141,7 @@ fn query_with_large_depth_with_urefs_should_fail() {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(casper_types::TransactionRuntime::VmCasperV1),
));
let contract_key = Key::AddressableEntity(contract_addr);
pairs.push((contract_key, contract));
Expand Down
8 changes: 4 additions & 4 deletions types/benches/bytesrepr_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use casper_types::{
system::auction::{Bid, Delegator, EraInfo, SeigniorageAllocation},
AccessRights, AddressableEntityHash, ByteCodeHash, CLTyped, CLValue, DeployHash, DeployInfo,
EntityVersionKey, EntityVersions, Gas, Group, Groups, InitiatorAddr, Key, Package, PackageHash,
PackageStatus, ProtocolVersion, PublicKey, SecretKey, TransactionHash, TransactionV1Hash,
TransferAddr, TransferV2, URef, KEY_HASH_LENGTH, TRANSFER_ADDR_LENGTH, U128, U256, U512,
UREF_ADDR_LENGTH,
PackageStatus, ProtocolVersion, PublicKey, SecretKey, TransactionHash, TransactionRuntime,
TransactionV1Hash, TransferAddr, TransferV2, URef, KEY_HASH_LENGTH, TRANSFER_ADDR_LENGTH, U128,
U256, U512, UREF_ADDR_LENGTH,
};

static KB: usize = 1024;
Expand Down Expand Up @@ -468,7 +468,7 @@ fn sample_contract() -> AddressableEntity {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
)
}

Expand Down
50 changes: 25 additions & 25 deletions types/src/addressable_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ use crate::{
system::SystemEntityType,
uref::{self, URef},
AccessRights, ApiError, CLType, CLTyped, CLValue, CLValueError, ContextAccessRights, HashAddr,
Key, KeyTag, PackageHash, ProtocolVersion, PublicKey, Tagged, BLAKE2B_DIGEST_LENGTH,
KEY_HASH_LENGTH,
Key, KeyTag, PackageHash, ProtocolVersion, PublicKey, Tagged, TransactionRuntime,
BLAKE2B_DIGEST_LENGTH, KEY_HASH_LENGTH,
};

/// Maximum number of distinct user groups.
Expand Down Expand Up @@ -634,9 +634,7 @@ impl Distribution<EntityKindTag> for Standard {
}
}

#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
/// The type of Package.
Expand All @@ -646,24 +644,23 @@ pub enum EntityKind {
/// Package associated with an Account hash.
Account(AccountHash),
/// Packages associated with Wasm stored on chain.
#[default]
SmartContract,
SmartContract(TransactionRuntime),
}

impl EntityKind {
/// Returns the Account hash associated with a Package based on the package kind.
pub fn maybe_account_hash(&self) -> Option<AccountHash> {
match self {
Self::Account(account_hash) => Some(*account_hash),
Self::SmartContract | Self::System(_) => None,
Self::SmartContract(_) | Self::System(_) => None,
}
}

/// Returns the associated key set based on the Account hash set in the package kind.
pub fn associated_keys(&self) -> AssociatedKeys {
match self {
Self::Account(account_hash) => AssociatedKeys::new(*account_hash, Weight::new(1)),
Self::SmartContract | Self::System(_) => AssociatedKeys::default(),
Self::SmartContract(_) | Self::System(_) => AssociatedKeys::default(),
}
}

Expand Down Expand Up @@ -701,7 +698,7 @@ impl Tagged<EntityKindTag> for EntityKind {
match self {
EntityKind::System(_) => EntityKindTag::System,
EntityKind::Account(_) => EntityKindTag::Account,
EntityKind::SmartContract => EntityKindTag::SmartContract,
EntityKind::SmartContract(_) => EntityKindTag::SmartContract,
}
}
}
Expand All @@ -723,17 +720,19 @@ impl ToBytes for EntityKind {
fn serialized_length(&self) -> usize {
U8_SERIALIZED_LENGTH
+ match self {
EntityKind::SmartContract => 0,
EntityKind::SmartContract(transaction_runtime) => {
transaction_runtime.serialized_length()
}
EntityKind::System(system_entity_type) => system_entity_type.serialized_length(),
EntityKind::Account(account_hash) => account_hash.serialized_length(),
}
}

fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
match self {
EntityKind::SmartContract => {
EntityKind::SmartContract(transaction_runtime) => {
writer.push(self.tag());
Ok(())
transaction_runtime.write_bytes(writer)
}
EntityKind::System(system_entity_type) => {
writer.push(self.tag());
Expand All @@ -759,7 +758,10 @@ impl FromBytes for EntityKind {
let (account_hash, remainder) = AccountHash::from_bytes(remainder)?;
Ok((EntityKind::Account(account_hash), remainder))
}
EntityKindTag::SmartContract => Ok((EntityKind::SmartContract, remainder)),
EntityKindTag::SmartContract => {
let (transaction_runtime, remainder) = TransactionRuntime::from_bytes(remainder)?;
Ok((EntityKind::SmartContract(transaction_runtime), remainder))
}
}
}
}
Expand All @@ -773,8 +775,8 @@ impl Display for EntityKind {
EntityKind::Account(account_hash) => {
write!(f, "account-entity-kind({})", account_hash)
}
EntityKind::SmartContract => {
write!(f, "smart-contract-entity-kind")
EntityKind::SmartContract(transaction_runtime) => {
write!(f, "smart-contract-entity-kind({})", transaction_runtime)
}
}
}
Expand All @@ -786,7 +788,7 @@ impl Distribution<EntityKind> for Standard {
match rng.gen_range(0..=2) {
0 => EntityKind::System(rng.gen()),
1 => EntityKind::Account(rng.gen()),
2 => EntityKind::SmartContract,
2 => EntityKind::SmartContract(rng.gen()),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -829,7 +831,7 @@ impl EntityAddr {
match entity_kind {
EntityKind::System(_) => Self::new_system(hash_addr),
EntityKind::Account(_) => Self::new_account(hash_addr),
EntityKind::SmartContract => Self::new_smart_contract(hash_addr),
EntityKind::SmartContract(_) => Self::new_smart_contract(hash_addr),
}
}

Expand Down Expand Up @@ -1386,8 +1388,6 @@ pub struct AddressableEntity {
package_hash: PackageHash,
byte_code_hash: ByteCodeHash,
main_purse: URef,

// entry_points: EntryPoints,
associated_keys: AssociatedKeys,
action_thresholds: ActionThresholds,
message_topics: MessageTopics,
Expand Down Expand Up @@ -1446,7 +1446,7 @@ impl AddressableEntity {
match self.entity_kind {
EntityKind::System(_) => EntityAddr::new_system(hash_addr),
EntityKind::Account(_) => EntityAddr::new_account(hash_addr),
EntityKind::SmartContract => EntityAddr::new_smart_contract(hash_addr),
EntityKind::SmartContract(_) => EntityAddr::new_smart_contract(hash_addr),
}
}

Expand Down Expand Up @@ -1694,7 +1694,7 @@ impl AddressableEntity {
EntityKind::Account(_) => {
Key::addressable_entity_key(EntityKindTag::Account, entity_hash)
}
EntityKind::SmartContract => {
EntityKind::SmartContract(_) => {
Key::addressable_entity_key(EntityKindTag::SmartContract, entity_hash)
}
}
Expand Down Expand Up @@ -1795,7 +1795,7 @@ impl Default for AddressableEntity {
action_thresholds: ActionThresholds::default(),
associated_keys: AssociatedKeys::default(),
message_topics: MessageTopics::default(),
entity_kind: EntityKind::SmartContract,
entity_kind: EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
}
}
}
Expand All @@ -1810,7 +1810,7 @@ impl From<Contract> for AddressableEntity {
AssociatedKeys::default(),
ActionThresholds::default(),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
)
}
}
Expand Down Expand Up @@ -1934,7 +1934,7 @@ mod tests {
ActionThresholds::new(Weight::new(1), Weight::new(1), Weight::new(1))
.expect("should create thresholds"),
MessageTopics::default(),
EntityKind::SmartContract,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
);
let access_rights = contract.extract_access_rights(entity_hash, &named_keys);
let expected_uref = URef::new([42; UREF_ADDR_LENGTH], AccessRights::READ_ADD_WRITE);
Expand Down
Loading

0 comments on commit 72620c1

Please sign in to comment.