Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4988: casper-networkGH-4985: Transaction vm2 settings r=mpapierski a=mpapierski

Transaction settings valid only for VM2 are stored in an enum. This solves situation when transaction target has specified extra values that will not be used by VM1 runtime.

Closes casper-network#4985 

Co-authored-by: Michał Papierski <[email protected]>
Co-authored-by: Ed Hastings <[email protected]>
Co-authored-by: Michał Papierski <[email protected]>
  • Loading branch information
4 people authored Dec 10, 2024
2 parents a4a2dec + 874e518 commit 73b3205
Show file tree
Hide file tree
Showing 31 changed files with 510 additions and 551 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ lint-smart-contracts:

.PHONY: audit-rs
audit-rs:
$(CARGO) audit --ignore RUSTSEC-2024-0344 --ignore RUSTSEC-2024-0367 --ignore RUSTSEC-2024-0371
$(CARGO) audit --ignore RUSTSEC-2024-0344 --ignore RUSTSEC-2024-0367 --ignore RUSTSEC-2024-0371 --ignore RUSTSEC-2024-0421

.PHONY: audit-as
audit-as:
Expand Down
4 changes: 2 additions & 2 deletions 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, TransactionRuntime, URef,
ContractRuntimeTag, EntityVersionKey, Key, PackageHash, StoredValueTypeMismatch, URef,
};
use casper_wasm::elements;

Expand Down Expand Up @@ -191,7 +191,7 @@ pub enum Error {
InvalidUtf8Encoding(Utf8Error),
/// Incompatible transaction runtime.
#[error("Incompatible runtime: {0}")]
IncompatibleRuntime(TransactionRuntime),
IncompatibleRuntime(ContractRuntimeTag),
}

impl From<PreprocessingError> for Error {
Expand Down
17 changes: 8 additions & 9 deletions execution_engine/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ use casper_types::{
},
addressable_entity::{
self, ActionThresholds, ActionType, AddressableEntity, AddressableEntityHash,
AssociatedKeys, EntityKindTag, EntryPoint, EntryPointAccess, EntryPointType, EntryPoints,
MessageTopicError, MessageTopics, NamedKeyAddr, NamedKeyValue, Parameter, Weight,
DEFAULT_ENTRY_POINT_NAME,
AssociatedKeys, ContractRuntimeTag, EntityKindTag, EntryPoint, EntryPointAccess,
EntryPointType, EntryPoints, MessageTopicError, MessageTopics, NamedKeyAddr, NamedKeyValue,
Parameter, Weight, DEFAULT_ENTRY_POINT_NAME,
},
bytesrepr::{self, Bytes, FromBytes, ToBytes},
contract_messages::{
Expand All @@ -60,9 +60,8 @@ use casper_types::{
ByteCodeKind, CLTyped, CLValue, ContextAccessRights, Contract, ContractWasm, EntityAddr,
EntityKind, EntityVersion, EntityVersionKey, EntityVersions, Gas, GrantedAccess, Group, Groups,
HashAddr, HostFunction, HostFunctionCost, InitiatorAddr, Key, NamedArg, Package, PackageHash,
PackageStatus, Phase, PublicKey, RuntimeArgs, RuntimeFootprint, StoredValue,
TransactionRuntime, Transfer, TransferResult, TransferV2, TransferredTo, URef,
DICTIONARY_ITEM_KEY_MAX_LENGTH, U512,
PackageStatus, Phase, PublicKey, RuntimeArgs, RuntimeFootprint, StoredValue, Transfer,
TransferResult, TransferV2, TransferredTo, URef, DICTIONARY_ITEM_KEY_MAX_LENGTH, U512,
};

use crate::{
Expand Down Expand Up @@ -1893,14 +1892,14 @@ where
EntityKind::System(_) | EntityKind::Account(_) => {
Key::ByteCode(ByteCodeAddr::Empty)
}
EntityKind::SmartContract(TransactionRuntime::VmCasperV1) => {
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV1) => {
if self.context.engine_config().enable_entity {
Key::ByteCode(ByteCodeAddr::new_wasm_addr(byte_code_addr))
} else {
Key::Hash(byte_code_addr)
}
}
EntityKind::SmartContract(runtime @ TransactionRuntime::VmCasperV2) => {
EntityKind::SmartContract(runtime @ ContractRuntimeTag::VmCasperV2) => {
return Err(ExecError::IncompatibleRuntime(runtime));
}
};
Expand Down Expand Up @@ -2576,7 +2575,7 @@ where
main_purse,
associated_keys,
action_thresholds,
EntityKind::SmartContract(TransactionRuntime::VmCasperV1),
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV1),
);
let entity_key = Key::AddressableEntity(entity_addr);
self.context.metered_write_gs_unsafe(entity_key, entity)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use casper_types::{
bytesrepr::{Bytes, ToBytes},
ApiError, BlockTime, Digest, EraId, InitiatorAddr, Key, PricingMode, ProtocolVersion,
PublicKey, RuntimeArgs, SecretKey, TimeDiff, Timestamp, Transaction, TransactionArgs,
TransactionEntryPoint, TransactionRuntime, TransactionScheduling, TransactionTarget,
TransactionEntryPoint, TransactionRuntimeParams, TransactionScheduling, TransactionTarget,
TransactionV1, TransactionV1Payload,
};

Expand Down Expand Up @@ -49,8 +49,7 @@ fn try_add_contract_version(
let txn = new_transaction_v1_session(
is_install_upgrade,
module_bytes,
casper_types::TransactionRuntime::VmCasperV1,
0,
TransactionRuntimeParams::VmCasperV1,
&DEFAULT_ACCOUNT_SECRET_KEY,
);

Expand Down Expand Up @@ -111,8 +110,7 @@ fn try_add_contract_version(
pub fn new_transaction_v1_session(
is_install_upgrade: bool,
module_bytes: Bytes,
runtime: TransactionRuntime,
transferred_value: u64,
runtime: TransactionRuntimeParams,
secret_key: &SecretKey,
) -> TransactionV1 {
let timestamp = Timestamp::now();
Expand All @@ -121,8 +119,6 @@ pub fn new_transaction_v1_session(
is_install_upgrade,
module_bytes,
runtime,
transferred_value,
seed: None,
};
let args = TransactionArgs::Named(RuntimeArgs::new());
let entry_point = TransactionEntryPoint::Call;
Expand Down
9 changes: 4 additions & 5 deletions executor/wasm-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use casper_types::{
account::AccountHash,
addressable_entity::{ActionThresholds, AssociatedKeys, NamedKeyAddr},
AddressableEntity, AddressableEntityHash, BlockHash, ByteCode, ByteCodeAddr, ByteCodeHash,
ByteCodeKind, CLType, Digest, EntityAddr, EntityKind, EntryPoint, EntryPointAccess,
EntryPointAddr, EntryPointPayment, EntryPointType, EntryPointValue, Groups, HashAddr, Key,
Package, PackageHash, PackageStatus, ProtocolVersion, StoredValue, TransactionRuntime, URef,
U512,
ByteCodeKind, CLType, ContractRuntimeTag, Digest, EntityAddr, EntityKind, EntryPoint,
EntryPointAccess, EntryPointAddr, EntryPointPayment, EntryPointType, EntryPointValue, Groups,
HashAddr, Key, Package, PackageHash, PackageStatus, ProtocolVersion, StoredValue, URef, U512,
};
use either::Either;
use num_derive::FromPrimitive;
Expand Down Expand Up @@ -506,7 +505,7 @@ pub fn casper_create<S: GlobalStateReader + 'static, E: Executor + 'static>(
main_purse,
AssociatedKeys::default(),
ActionThresholds::default(),
EntityKind::SmartContract(TransactionRuntime::VmCasperV2),
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV2),
);

caller.context_mut().tracking_copy.write(
Expand Down
12 changes: 6 additions & 6 deletions executor/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use casper_types::{
account::AccountHash,
addressable_entity::{ActionThresholds, AssociatedKeys},
bytesrepr, AddressableEntity, AddressableEntityHash, ByteCode, ByteCodeAddr, ByteCodeHash,
ByteCodeKind, Digest, EntityAddr, EntityKind, Gas, Groups, InitiatorAddr, Key, Package,
PackageHash, PackageStatus, Phase, ProtocolVersion, StoredValue, TransactionInvocationTarget,
TransactionRuntime, URef, U512,
ByteCodeKind, ContractRuntimeTag, Digest, EntityAddr, EntityKind, Gas, Groups, InitiatorAddr,
Key, Package, PackageHash, PackageStatus, Phase, ProtocolVersion, StoredValue,
TransactionInvocationTarget, URef, U512,
};
use either::Either;
use install::{InstallContractError, InstallContractRequest, InstallContractResult};
Expand Down Expand Up @@ -230,7 +230,7 @@ impl ExecutorV2 {
main_purse,
AssociatedKeys::default(),
ActionThresholds::default(),
EntityKind::SmartContract(TransactionRuntime::VmCasperV2),
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV2),
);

tracking_copy.write(
Expand Down Expand Up @@ -374,7 +374,7 @@ impl ExecutorV2 {
let wasm_key = match addressable_entity.kind() {
EntityKind::System(_) => todo!(),
EntityKind::Account(_) => todo!(),
EntityKind::SmartContract(TransactionRuntime::VmCasperV1) => {
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV1) => {
// We need to short circuit here to execute v1 contracts with legacy
// execut

Expand All @@ -398,7 +398,7 @@ impl ExecutorV2 {
gas_limit,
);
}
EntityKind::SmartContract(TransactionRuntime::VmCasperV2) => {
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV2) => {
Key::ByteCode(ByteCodeAddr::V2CasperWasm(
addressable_entity.byte_code_addr(),
))
Expand Down
33 changes: 24 additions & 9 deletions node/src/components/contract_runtime/operations/wasm_v2_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use casper_storage::{
};
use casper_types::{
execution::Effects, BlockHash, Digest, Gas, Key, TransactionEntryPoint,
TransactionInvocationTarget, TransactionTarget, U512,
TransactionInvocationTarget, TransactionRuntimeParams, TransactionTarget, U512,
};
use thiserror::Error;
use tracing::info;
Expand Down Expand Up @@ -102,6 +102,8 @@ pub(crate) enum InvalidRequest {
InvalidGasLimit(U512),
#[error("Expected transferred value")]
ExpectedTransferredValue,
#[error("Expected V2 runtime")]
ExpectedV2Runtime,
}

impl WasmV2Request {
Expand Down Expand Up @@ -139,6 +141,7 @@ impl WasmV2Request {
Install {
module_bytes: Bytes,
entry_point: String,
transferred_value: u64,
seed: Option<[u8; 32]>,
},
Session {
Expand All @@ -153,22 +156,28 @@ impl WasmV2Request {
let target = transaction.target().ok_or(InvalidRequest::ExpectedTarget)?;
let target = match target {
TransactionTarget::Native => todo!(), //
TransactionTarget::Stored {
id,
runtime: _,
transferred_value: _,
} => match transaction.entry_point() {
TransactionTarget::Stored { id, runtime: _ } => match transaction.entry_point() {
TransactionEntryPoint::Custom(entry_point) => Target::Stored {
id: id.clone(),
entry_point: entry_point.clone(),
},
_ => todo!(),
},

TransactionTarget::Session {
module_bytes: _,
runtime: TransactionRuntimeParams::VmCasperV1,
is_install_upgrade: _, // TODO: Handle this
} => {
return Err(InvalidRequest::ExpectedV2Runtime);
}
TransactionTarget::Session {
module_bytes,
runtime: _,
transferred_value: _,
seed,
runtime:
TransactionRuntimeParams::VmCasperV2 {
transferred_value,
seed,
},
is_install_upgrade: _, // TODO: Handle this
} => match transaction.entry_point() {
TransactionEntryPoint::Call => Target::Session {
Expand All @@ -177,6 +186,7 @@ impl WasmV2Request {
TransactionEntryPoint::Custom(entry_point) => Target::Install {
module_bytes: module_bytes.clone().take_inner().into(),
entry_point: entry_point.to_string(),
transferred_value,
seed,
},
_ => todo!(),
Expand All @@ -189,6 +199,7 @@ impl WasmV2Request {
Target::Install {
module_bytes,
entry_point,
transferred_value,
seed,
} => {
let mut builder = InstallContractRequestBuilder::default();
Expand All @@ -213,6 +224,10 @@ impl WasmV2Request {
builder = builder.with_seed(seed);
}

// Value is expected to be the same as transferred value, it's just taken through
// different API.
debug_assert_eq!(transferred_value, value);

let install_request = builder
.with_initiator(initiator_addr.account_hash())
.with_gas_limit(gas_limit)
Expand Down
8 changes: 4 additions & 4 deletions node/src/components/transaction_acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests;

use std::{collections::BTreeSet, fmt::Debug, sync::Arc};

use casper_types::{InvalidTransaction, InvalidTransactionV1};
use casper_types::{ContractRuntimeTag, InvalidTransaction, InvalidTransactionV1};
use datasize::DataSize;
use prometheus::Registry;
use tracing::{debug, error, trace};
Expand All @@ -19,7 +19,7 @@ use casper_types::{
EntityKind, EntityVersion, EntityVersionKey, ExecutableDeployItem,
ExecutableDeployItemIdentifier, InitiatorAddr, Key, Package, PackageAddr, PackageHash,
PackageIdentifier, Timestamp, Transaction, TransactionEntryPoint, TransactionInvocationTarget,
TransactionRuntime, TransactionTarget, DEFAULT_ENTRY_POINT_NAME, U512,
TransactionTarget, DEFAULT_ENTRY_POINT_NAME, U512,
};

use crate::{
Expand Down Expand Up @@ -657,7 +657,7 @@ impl TransactionAcceptor {
entry_point_exist: bool,
) -> Effects<Event> {
match addressable_entity.kind() {
EntityKind::SmartContract(TransactionRuntime::VmCasperV1)
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV1)
| EntityKind::Account(_)
| EntityKind::System(_) => {
if !entry_point_exist {
Expand All @@ -672,7 +672,7 @@ impl TransactionAcceptor {
}
self.validate_transaction_cryptography(effect_builder, event_metadata)
}
EntityKind::SmartContract(TransactionRuntime::VmCasperV2) => {
EntityKind::SmartContract(ContractRuntimeTag::VmCasperV2) => {
// Engine V2 does not store entrypoint information on chain and relies entirely on
// the Wasm itself.
self.validate_transaction_cryptography(effect_builder, event_metadata)
Expand Down
Loading

0 comments on commit 73b3205

Please sign in to comment.