diff --git a/execution_engine/src/engine_state/transfer.rs b/execution_engine/src/engine_state/transfer.rs index 26c1b0b95d..b9391faf00 100644 --- a/execution_engine/src/engine_state/transfer.rs +++ b/execution_engine/src/engine_state/transfer.rs @@ -321,20 +321,21 @@ impl TransferRuntimeArgsBuilder { where R: StateReader, { - let (to, target_uref) = - match self.resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy))? { - NewTransferTargetMode::ExistingAccount { - main_purse: purse_uref, - target_account_hash: target_account, - } => (Some(target_account), purse_uref), - NewTransferTargetMode::PurseExists(purse_uref) => (None, purse_uref), - NewTransferTargetMode::CreateAccount(_) => { - // Method "build()" is called after `resolve_transfer_target_mode` is first called - // and handled by creating a new account. Calling `resolve_transfer_target_mode` - // for the second time should never return `CreateAccount` variant. - return Err(Error::reverter(ApiError::Transfer)); - } - }; + let (to, target_uref) = match self + .resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy))? + { + NewTransferTargetMode::ExistingAccount { + main_purse: purse_uref, + target_account_hash: target_account, + } => (Some(target_account), purse_uref), + NewTransferTargetMode::PurseExists(purse_uref) => (None, purse_uref), + NewTransferTargetMode::CreateAccount(_) => { + // Method "build()" is called after `resolve_transfer_target_mode` is first called + // and handled by creating a new account. Calling `resolve_transfer_target_mode` + // for the second time should never return `CreateAccount` variant. + return Err(Error::reverter(ApiError::Transfer)); + } + }; let source_uref = self.resolve_source_uref(from, entity_named_keys, Rc::clone(&tracking_copy))?; diff --git a/execution_engine/src/engine_state/upgrade.rs b/execution_engine/src/engine_state/upgrade.rs index 79b50a3874..0669ec3ff1 100644 --- a/execution_engine/src/engine_state/upgrade.rs +++ b/execution_engine/src/engine_state/upgrade.rs @@ -16,9 +16,8 @@ use casper_types::{ package::{EntityVersions, Groups, PackageStatus}, system::{handle_payment::ACCUMULATION_PURSE_KEY, SystemEntityType}, AccessRights, AddressableEntity, AddressableEntityHash, ByteCode, ByteCodeAddr, ByteCodeHash, - ByteCodeKind, ByteCodeKind, CLValue, CLValueError, Digest, EntityAddr, EntryPoints, - FeeHandling, Key, Package, PackageHash, Phase, ProtocolVersion, PublicKey, StoredValue, URef, - U512, + ByteCodeKind, CLValue, CLValueError, Digest, EntityAddr, EntryPoints, FeeHandling, Key, + Package, PackageHash, Phase, ProtocolVersion, PublicKey, StoredValue, URef, U512, }; use super::EngineConfig; diff --git a/execution_engine/src/runtime_context/dictionary.rs b/execution_engine/src/runtime_context/dictionary.rs deleted file mode 100644 index cc8a851497..0000000000 --- a/execution_engine/src/runtime_context/dictionary.rs +++ /dev/null @@ -1,100 +0,0 @@ -use casper_types::{ - bytesrepr::{self, Bytes, FromBytes, ToBytes}, - CLType, CLTyped, CLValue, CLValueError, Key, StoredValue, -}; - -/// Wraps a [`CLValue`] for storage in a dictionary. -/// -/// Note that we include the dictionary [`casper_types::URef`] and key used to create the -/// `Key::Dictionary` under which this value is stored. This is to allow migration to a different -/// key representation in the future. -#[derive(Clone)] -pub struct DictionaryValue { - /// Actual [`CLValue`] written to global state. - cl_value: CLValue, - /// [`URef`] seed bytes. - seed_uref_addr: Bytes, - /// Original key bytes. - dictionary_item_key_bytes: Bytes, -} - -impl DictionaryValue { - pub fn new( - cl_value: CLValue, - seed_uref_addr: Vec, - dictionary_item_key_bytes: Vec, - ) -> Self { - Self { - cl_value, - seed_uref_addr: seed_uref_addr.into(), - dictionary_item_key_bytes: dictionary_item_key_bytes.into(), - } - } - - /// Get a reference to the [`DictionaryValue`]'s wrapper's cl value. - pub fn into_cl_value(self) -> CLValue { - self.cl_value - } -} - -impl CLTyped for DictionaryValue { - fn cl_type() -> CLType { - CLType::Any - } -} - -impl FromBytes for DictionaryValue { - fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> { - let (cl_value, remainder) = FromBytes::from_bytes(bytes)?; - let (uref_addr, remainder) = FromBytes::from_bytes(remainder)?; - let (key_bytes, remainder) = FromBytes::from_bytes(remainder)?; - let dictionary_value = DictionaryValue { - cl_value, - seed_uref_addr: uref_addr, - dictionary_item_key_bytes: key_bytes, - }; - Ok((dictionary_value, remainder)) - } -} - -impl ToBytes for DictionaryValue { - fn to_bytes(&self) -> Result, bytesrepr::Error> { - let mut buffer = bytesrepr::allocate_buffer(self)?; - buffer.extend(self.cl_value.to_bytes()?); - buffer.extend(self.seed_uref_addr.to_bytes()?); - buffer.extend(self.dictionary_item_key_bytes.to_bytes()?); - Ok(buffer) - } - - fn serialized_length(&self) -> usize { - self.cl_value.serialized_length() - + self.seed_uref_addr.serialized_length() - + self.dictionary_item_key_bytes.serialized_length() - } - - fn write_bytes(&self, writer: &mut Vec) -> Result<(), bytesrepr::Error> { - self.cl_value.write_bytes(writer)?; - self.seed_uref_addr.write_bytes(writer)?; - self.dictionary_item_key_bytes.write_bytes(writer)?; - Ok(()) - } -} - -/// Inspects `key` argument whether it contains a dictionary variant, and checks if `stored_value` -/// contains a [`CLValue`], then it will attempt a conversion from the held clvalue into -/// [`DictionaryValue`] and returns the real [`CLValue`] held by it. -/// -/// For any other combination of `key` and `stored_value` it returns its unmodified value. -pub fn handle_stored_value( - key: Key, - stored_value: StoredValue, -) -> Result { - match (key, stored_value) { - (Key::Dictionary(_), StoredValue::CLValue(cl_value)) => { - let wrapped_cl_value: DictionaryValue = cl_value.into_t()?; - let cl_value = wrapped_cl_value.into_cl_value(); - Ok(StoredValue::CLValue(cl_value)) - } - (_, stored_value) => Ok(stored_value), - } -} diff --git a/execution_engine/src/runtime_context/mod.rs b/execution_engine/src/runtime_context/mod.rs index 50a92d5805..e8f3efc7c8 100644 --- a/execution_engine/src/runtime_context/mod.rs +++ b/execution_engine/src/runtime_context/mod.rs @@ -1,6 +1,5 @@ //! The context of execution of WASM code. -pub(crate) mod dictionary; #[cfg(test)] mod tests; @@ -31,17 +30,16 @@ use casper_types::{ Message, MessageAddr, MessageChecksum, MessageTopicSummary, Messages, TopicNameHash, }, execution::Effects, + handle_stored_dictionary_value, system::auction::EraInfo, AccessRights, AddressableEntity, AddressableEntityHash, BlockTime, CLType, CLValue, - ContextAccessRights, DeployHash, EntityAddr, EntryPointType, Gas, GrantedAccess, Key, KeyTag, - Package, PackageHash, Phase, ProtocolVersion, PublicKey, RuntimeArgs, StoredValue, - SystemContractRegistry, Transfer, TransferAddr, URef, URefAddr, DICTIONARY_ITEM_KEY_MAX_LENGTH, - KEY_HASH_LENGTH, U512, + CLValueDictionary, ContextAccessRights, DeployHash, EntityAddr, EntryPointType, Gas, + GrantedAccess, Key, KeyTag, Package, PackageHash, Phase, ProtocolVersion, PublicKey, + RuntimeArgs, StoredValue, SystemContractRegistry, Transfer, TransferAddr, URef, URefAddr, + DICTIONARY_ITEM_KEY_MAX_LENGTH, KEY_HASH_LENGTH, U512, }; -use crate::{ - engine_state::EngineConfig, execution::Error, runtime_context::dictionary::DictionaryValue, -}; +use crate::{engine_state::EngineConfig, execution::Error}; /// Number of bytes returned from the `random_bytes` function. pub const RANDOM_BYTES_COUNT: usize = 32; @@ -456,7 +454,7 @@ where let maybe_stored_value = self.tracking_copy.borrow_mut().read(key)?; let stored_value = match maybe_stored_value { - Some(stored_value) => dictionary::handle_stored_value(*key, stored_value)?, + Some(stored_value) => handle_stored_dictionary_value(*key, stored_value)?, None => return Ok(None), }; @@ -1303,7 +1301,7 @@ where .map_err(Into::::into)?; if let Some(stored_value) = maybe_stored_value { - let stored_value = dictionary::handle_stored_value(dictionary_key, stored_value)?; + let stored_value = handle_stored_dictionary_value(dictionary_key, stored_value)?; let cl_value = CLValue::try_from(stored_value).map_err(Error::TypeMismatch)?; Ok(Some(cl_value)) } else { @@ -1330,7 +1328,7 @@ where self.validate_cl_value(&cl_value)?; let wrapped_cl_value = { - let dictionary_value = DictionaryValue::new( + let dictionary_value = CLValueDictionary::new( cl_value, seed_uref.addr().to_vec(), dictionary_item_key_bytes.to_vec(), diff --git a/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs b/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs index a5702ba705..3ef742219d 100644 --- a/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs +++ b/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use casper_execution_engine::engine_state::{EngineConfigBuilder, SystemContractRegistry}; +use casper_execution_engine::engine_state::EngineConfigBuilder; use num_rational::Ratio; use casper_engine_test_support::{ @@ -19,19 +19,19 @@ use casper_types::{ mint::ROUND_SEIGNIORAGE_RATE_KEY, }, BrTableCost, CLValue, ControlFlowCosts, EntityAddr, EraId, HostFunctionCosts, Key, - MessageLimits, OpcodeCosts, ProtocolVersion, StorageCosts, StoredValue, WasmConfig, - DEFAULT_ADD_COST, DEFAULT_BIT_COST, DEFAULT_CONST_COST, DEFAULT_CONTROL_FLOW_BLOCK_OPCODE, - DEFAULT_CONTROL_FLOW_BR_IF_OPCODE, DEFAULT_CONTROL_FLOW_BR_OPCODE, - DEFAULT_CONTROL_FLOW_BR_TABLE_MULTIPLIER, DEFAULT_CONTROL_FLOW_BR_TABLE_OPCODE, - DEFAULT_CONTROL_FLOW_CALL_INDIRECT_OPCODE, DEFAULT_CONTROL_FLOW_CALL_OPCODE, - DEFAULT_CONTROL_FLOW_DROP_OPCODE, DEFAULT_CONTROL_FLOW_ELSE_OPCODE, - DEFAULT_CONTROL_FLOW_END_OPCODE, DEFAULT_CONTROL_FLOW_IF_OPCODE, - DEFAULT_CONTROL_FLOW_LOOP_OPCODE, DEFAULT_CONTROL_FLOW_RETURN_OPCODE, - DEFAULT_CONTROL_FLOW_SELECT_OPCODE, DEFAULT_CONVERSION_COST, DEFAULT_CURRENT_MEMORY_COST, - DEFAULT_DIV_COST, DEFAULT_GLOBAL_COST, DEFAULT_GROW_MEMORY_COST, - DEFAULT_INTEGER_COMPARISON_COST, DEFAULT_LOAD_COST, DEFAULT_LOCAL_COST, - DEFAULT_MAX_STACK_HEIGHT, DEFAULT_MUL_COST, DEFAULT_NOP_COST, DEFAULT_STORE_COST, - DEFAULT_UNREACHABLE_COST, DEFAULT_WASM_MAX_MEMORY, U256, U512, + MessageLimits, OpcodeCosts, ProtocolVersion, StorageCosts, StoredValue, SystemContractRegistry, + WasmConfig, DEFAULT_ADD_COST, DEFAULT_BIT_COST, DEFAULT_CONST_COST, + DEFAULT_CONTROL_FLOW_BLOCK_OPCODE, DEFAULT_CONTROL_FLOW_BR_IF_OPCODE, + DEFAULT_CONTROL_FLOW_BR_OPCODE, DEFAULT_CONTROL_FLOW_BR_TABLE_MULTIPLIER, + DEFAULT_CONTROL_FLOW_BR_TABLE_OPCODE, DEFAULT_CONTROL_FLOW_CALL_INDIRECT_OPCODE, + DEFAULT_CONTROL_FLOW_CALL_OPCODE, DEFAULT_CONTROL_FLOW_DROP_OPCODE, + DEFAULT_CONTROL_FLOW_ELSE_OPCODE, DEFAULT_CONTROL_FLOW_END_OPCODE, + DEFAULT_CONTROL_FLOW_IF_OPCODE, DEFAULT_CONTROL_FLOW_LOOP_OPCODE, + DEFAULT_CONTROL_FLOW_RETURN_OPCODE, DEFAULT_CONTROL_FLOW_SELECT_OPCODE, + DEFAULT_CONVERSION_COST, DEFAULT_CURRENT_MEMORY_COST, DEFAULT_DIV_COST, DEFAULT_GLOBAL_COST, + DEFAULT_GROW_MEMORY_COST, DEFAULT_INTEGER_COMPARISON_COST, DEFAULT_LOAD_COST, + DEFAULT_LOCAL_COST, DEFAULT_MAX_STACK_HEIGHT, DEFAULT_MUL_COST, DEFAULT_NOP_COST, + DEFAULT_STORE_COST, DEFAULT_UNREACHABLE_COST, DEFAULT_WASM_MAX_MEMORY, U256, U512, }; const PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::V1_0_0; diff --git a/storage/src/tracking_copy/error.rs b/storage/src/tracking_copy/error.rs index 140369b796..695acd6622 100644 --- a/storage/src/tracking_copy/error.rs +++ b/storage/src/tracking_copy/error.rs @@ -5,7 +5,6 @@ use casper_types::{ addressable_entity::{AddKeyFailure, RemoveKeyFailure, SetThresholdFailure, UpdateKeyFailure}, bytesrepr, execution::TransformError, - package::PackageKind, system, AccessRights, AddressableEntityHash, ApiError, ByteCodeHash, CLType, CLValueError, EntityVersionKey, Key, PackageHash, StoredValueTypeMismatch, URef, }; @@ -169,9 +168,6 @@ pub enum Error { /// Invalid key #[error("Invalid key {0}")] UnexpectedKeyVariant(Key), - /// Invalid Contract package kind. - #[error("Invalid contract package kind: {0}")] - InvalidPackageKind(PackageKind), /// Failed to transfer tokens on a private chain. #[error("Failed to transfer with unrestricted transfers disabled")] DisabledUnrestrictedTransfers, diff --git a/storage/src/tracking_copy/ext.rs b/storage/src/tracking_copy/ext.rs index 2349d882cf..08fc8f8afc 100644 --- a/storage/src/tracking_copy/ext.rs +++ b/storage/src/tracking_copy/ext.rs @@ -15,7 +15,7 @@ use casper_types::{ addressable_entity::{EntityKindTag, MessageTopics, NamedKeyAddr, NamedKeys}, bytesrepr, package::{EntityVersions, Groups, PackageStatus}, - AccessRights, AddressableEntity, AddressableEntityHash, ByteCode, ByteCodeHash, ByteCodeKind, + AccessRights, AddressableEntity, AddressableEntityHash, ByteCode, ByteCodeAddr, ByteCodeHash, CLValue, ChecksumRegistry, EntityAddr, EntityKind, EntryPoints, Key, KeyTag, Motes, Package, PackageHash, Phase, ProtocolVersion, StoredValue, StoredValueTypeMismatch, SystemContractRegistry, URef, @@ -288,7 +288,7 @@ where } fn get_byte_code(&mut self, byte_code_hash: ByteCodeHash) -> Result { - let key = Key::ByteCode(ByteCodeKind::V1CasperWasm, byte_code_hash.value()); + let key = Key::ByteCode(ByteCodeAddr::V1CasperWasm(byte_code_hash.value())); match self.get(&key)? { Some(StoredValue::ByteCode(byte_code)) => Ok(byte_code), Some(other) => Err(TrackingCopyError::TypeMismatch( @@ -333,7 +333,7 @@ where Key::NamedKey(entry_addr) }; - let cl_value = CLValue::from_t(*key).map_err(execution::Error::CLValue)?; + let cl_value = CLValue::from_t(*key).map_err(TrackingCopyError::CLValue)?; self.write(entry_key, StoredValue::CLValue(cl_value)) } @@ -347,7 +347,7 @@ where .map_err(Self::Error::BytesRepr)?; let mut ret: BTreeSet = BTreeSet::new(); - let keys = self.reader.keys_with_prefix(&prefix).map_err(Into::into)?; + let keys = self.reader.keys_with_prefix(&prefix)?; let pruned = &self.cache.prunes_cached; // don't include keys marked for pruning for key in keys { @@ -374,14 +374,14 @@ where let mut named_keys = NamedKeys::new(); for entry_key in ret.iter() { - match self.read(entry_key).map_err(Into::into)? { + match self.read(entry_key)? { Some(StoredValue::NamedKey(named_key)) => { - let key = named_key.get_key().map_err(execution::Error::CLValue)?; - let name = named_key.get_name().map_err(execution::Error::CLValue)?; + let key = named_key.get_key().map_err(TrackingCopyError::CLValue)?; + let name = named_key.get_name().map_err(TrackingCopyError::CLValue)?; named_keys.insert(name, key); } Some(other) => { - return Err(execution::Error::TypeMismatch( + return Err(TrackingCopyError::TypeMismatch( StoredValueTypeMismatch::new("CLValue".to_string(), other.type_name()), )) } @@ -389,14 +389,14 @@ where Some(StoredValue::NamedKey(named_key_value)) => { let key = named_key_value .get_key() - .map_err(execution::Error::CLValue)?; + .map_err(TrackingCopyError::CLValue)?; let name = named_key_value .get_name() - .map_err(execution::Error::CLValue)?; + .map_err(TrackingCopyError::CLValue)?; named_keys.insert(name, key); } Some(_) | None => { - return Err(execution::Error::KeyNotFound(*entry_key)); + return Err(TrackingCopyError::KeyNotFound(*entry_key)); } }, }; diff --git a/storage/src/tracking_copy/tests.rs b/storage/src/tracking_copy/tests.rs index 992fe5219e..36373b0f1c 100644 --- a/storage/src/tracking_copy/tests.rs +++ b/storage/src/tracking_copy/tests.rs @@ -10,38 +10,25 @@ use casper_types::{ }, execution::{Effects, Transform, TransformKind}, gens::*, - handle_stored_dictionary_value,PackageHash, - package::PackageKindTag,ByteCodeHash,CLValueDictionary, Digest, - AccessRights, AddressableEntity, CLValue, EntityAddr, EntityKind, EntryPoints, HashAddr, Key, - KeyTag, ProtocolVersion, StoredValue, URef, U256, U512,UREF_ADDR_LENGTH, + 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, }; use super::{ - meter::count_meter::Count, AddResult, TrackingCopyCache, TrackingCopyError, - TrackingCopyQueryResult, ValidationError, -}; -use crate::{ - engine_state::{EngineConfig, ACCOUNT_BYTE_CODE_HASH}, - runtime_context::dictionary, - tracking_copy::{self}, + meter::count_meter::Count, TrackingCopyCache, TrackingCopyError, TrackingCopyQueryResult, }; use crate::{ global_state::{ state::{self, StateProvider, StateReader}, trie::merkle_proof::TrieMerkleProof, }, - tracking_copy::{validate_query_proof, TrackingCopy}, + tracking_copy::{self, TrackingCopy}, }; -const DEFAULT_ADDRESS: [u8; 32] = [0; 32]; - +use crate::global_state::DEFAULT_MAX_QUERY_DEPTH; use casper_types::contracts::ContractHash; -use once_cell::sync::Lazy; - -/// Default contract wasm hash. -#[cfg(any(feature = "testing", feature = "gens", test))] -pub static DEFAULT_BYTE_CODE_HASH: Lazy = - Lazy::new(|| ByteCodeHash::new(DEFAULT_ADDRESS)); +use proptest::proptest; struct CountingDb { count: Rc>, @@ -93,7 +80,7 @@ fn effects(transform_keys_and_kinds: Vec<(Key, TransformKind)>) -> Effects { fn tracking_copy_new() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(counter); - let tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); assert!(tc.effects.is_empty()); } @@ -102,7 +89,7 @@ fn tracking_copy_new() { fn tracking_copy_caching() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(Rc::clone(&counter)); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); let zero = StoredValue::CLValue(CLValue::from_t(0_i32).unwrap()); @@ -122,7 +109,7 @@ fn tracking_copy_caching() { fn tracking_copy_read() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(Rc::clone(&counter)); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); let zero = StoredValue::CLValue(CLValue::from_t(0_i32).unwrap()); @@ -137,7 +124,7 @@ fn tracking_copy_read() { fn tracking_copy_write() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(Rc::clone(&counter)); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); let one = StoredValue::CLValue(CLValue::from_t(1_i32).unwrap()); @@ -171,7 +158,7 @@ fn tracking_copy_write() { fn tracking_copy_add_i32() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(counter); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); let three = StoredValue::CLValue(CLValue::from_t(3_i32).unwrap()); @@ -196,7 +183,7 @@ fn tracking_copy_add_i32() { fn tracking_copy_rw() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(counter); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); // reading then writing should update the op @@ -216,7 +203,7 @@ fn tracking_copy_rw() { fn tracking_copy_ra() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(counter); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); // reading then adding should update the op @@ -236,7 +223,7 @@ fn tracking_copy_ra() { fn tracking_copy_aw() { let counter = Rc::new(Cell::new(0)); let db = CountingDb::new(counter); - let mut tc = TrackingCopy::new(db, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tc = TrackingCopy::new(db, DEFAULT_MAX_QUERY_DEPTH); let k = Key::Hash([0u8; 32]); // adding then writing should update the op @@ -260,7 +247,7 @@ fn should_return_value_not_found() { let missing_key = Key::Dictionary([2u8; 32]); let empty_path = Vec::new(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let result = tc.query(missing_key, &empty_path); assert_matches!(result, Ok(TrackingCopyQueryResult::ValueNotFound(_))); } @@ -276,7 +263,7 @@ fn should_find_existing_entry() { state::lmdb::make_temporary_global_state([(foo_key, stored_val.clone())]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let empty_path = Vec::new(); let query_result = tc.query(foo_key, &empty_path); if let Ok(TrackingCopyQueryResult::Success { value, .. }) = query_result { @@ -306,7 +293,7 @@ fn should_query_empty_path() { state::lmdb::make_temporary_global_state([(dictionary_key, stored_value.clone())]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let empty_path = Vec::new(); let query_result = tc.query(dictionary_key, &empty_path); let dictionary_stored_value = handle_stored_dictionary_value(dictionary_key, stored_value) @@ -318,7 +305,6 @@ fn should_query_empty_path() { } } - #[test] fn should_traverse_contract_pathing() { let account_hash = AccountHash::new([0u8; 32]); @@ -349,7 +335,7 @@ fn should_traverse_contract_pathing() { (contract_key, stored_contract), ]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let path = vec![account_alias]; if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query(contract_key, &path) { assert_eq!(value, stored_account, "should find expected account"); @@ -388,7 +374,7 @@ fn should_traverse_account_pathing() { (contract_key, stored_contract.clone()), ]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let path = vec![contract_alias]; if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query(account_key, &path) { assert_eq!(value, stored_contract, "should find expected contract"); @@ -455,7 +441,7 @@ fn should_traverse_all_paths() { (misc_uref_key, misc_stored_value.clone()), ]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); fn unpack( result: Result, @@ -533,6 +519,20 @@ fn should_traverse_all_paths() { "unexpected stored value" ); } +fn handle_stored_value_into( + key: Key, + stored_value: StoredValue, +) -> Result { + match (key, stored_value) { + (Key::Dictionary(_), StoredValue::CLValue(cl_value)) => { + let wrapped_dictionary_value = + CLValueDictionary::new(cl_value, vec![0; 32], vec![255; 32]); + let wrapped_cl_value = CLValue::from_t(wrapped_dictionary_value)?; + Ok(StoredValue::CLValue(wrapped_cl_value)) + } + (_, stored_value) => Ok(stored_value), + } +} proptest! { #[test] @@ -559,24 +559,28 @@ proptest! { )); let contract_key = Key::AddressableEntity(EntityAddr::SmartContract(hash)); - let value = dictionary::handle_stored_value_into(k, v.clone()).unwrap(); + let value = handle_stored_value_into(k, v.clone()).unwrap(); - let named_key = Key::NamedKey( NamedKeyAddr::new_from_string(EntityAddr::SmartContract(hash), name.clone()).unwrap()); + let named_key = Key::NamedKey( NamedKeyAddr::new_from_string(EntityAddr::SmartContract(hash), name.clone()).unwrap()); let named_value = StoredValue::NamedKey(NamedKeyValue::from_concrete_values(k, name.clone()).unwrap()); - let (gs, root_hash, _tempdir) = state::lmdb::make_temporary_global_state([ - (account_key, stored_account.clone()), - (named_key, named_value) ,(contract_key, stored_contract), - ]); - let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); - let path = vec![account_alias]; - if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query(contract_key, &path) { - assert_eq!(value, stored_account, "should find expected account"); - } else { - panic!("Query failed when it should not have!"); + let (gs, root_hash, _tempdir) = state::lmdb::make_temporary_global_state( + [(k, value), (named_key, named_value) ,(contract_key, contract)] + ); + let view = gs.checkout(root_hash).unwrap().unwrap(); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); + let path = vec!(name.clone()); + if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query( contract_key, &path) { + assert_eq!(v, value); + } else { + panic!("Query failed when it should not have!"); + } + + if missing_name != name { + let result = tc.query(contract_key, &[missing_name]); + assert_matches!(result, Ok(TrackingCopyQueryResult::ValueNotFound(_))); + } } -} #[test] fn query_account_state( @@ -591,7 +595,7 @@ proptest! { let associated_keys = AssociatedKeys::new(pk, Weight::new(1)); let entity = AddressableEntity::new( PackageHash::new([1u8;32]), - *ACCOUNT_BYTE_CODE_HASH, + ByteCodeHash::default(), EntryPoints::new_with_default_entry_point(), ProtocolVersion::V1_0_0, purse, @@ -602,7 +606,7 @@ proptest! { ); let account_key = Key::AddressableEntity(EntityAddr::Account([9;32])); - let value = dictionary::handle_stored_value_into(k, v.clone()).unwrap(); + let value = handle_stored_value_into(k, v.clone()).unwrap(); let named_key = Key::NamedKey( NamedKeyAddr::new_from_string(EntityAddr::Account([9;32]), name.clone()).unwrap()); let named_value = StoredValue::NamedKey(NamedKeyValue::from_concrete_values(k, name.clone()).unwrap()); @@ -611,16 +615,16 @@ proptest! { [(k, value), (named_key, named_value),(account_key, entity.into())], ); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let path = vec!(name.clone()); - if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query( &EngineConfig::default(), account_key, &path) { + if let Ok(TrackingCopyQueryResult::Success { value, .. }) = tc.query(account_key, &path) { assert_eq!(v, value); } else { panic!("Query failed when it should not have!"); } if missing_name != name { - let result = tc.query( &EngineConfig::default(), account_key, &[missing_name]); + let result = tc.query( account_key, &[missing_name]); assert_matches!(result, Ok(TrackingCopyQueryResult::ValueNotFound(_))); } } @@ -642,20 +646,6 @@ proptest! { [3; 32].into(), EntryPoints::new(), ProtocolVersion::V1_0_0, - ); - StoredValue::Contract(contract) - }; - - let stored_account = { - let account_named_keys = { - let mut named_keys = NamedKeys::new(); - named_keys.insert(contract_alias.clone(), contract_key); - named_keys.insert(misc_alias.clone(), misc_uref_key); - named_keys - }; - let account = casper_types::account::Account::create( - account_hash, - account_named_keys, URef::default(), AssociatedKeys::default(), ActionThresholds::default(), @@ -668,7 +658,7 @@ proptest! { let contract_value = NamedKeyValue::from_concrete_values(k, state_name.clone()).unwrap(); - let value = dictionary::handle_stored_value_into(k, v.clone()).unwrap(); + let value = handle_stored_value_into(k, v.clone()).unwrap(); let (gs, root_hash, _tempdir) = state::lmdb::make_temporary_global_state([ (k, value), @@ -676,84 +666,16 @@ proptest! { (Key::NamedKey(contract_named_key), StoredValue::NamedKey(contract_value)) ]); let view = gs.checkout(root_hash).unwrap().unwrap(); - let tc = TrackingCopy::new(view); + let tc = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let path = vec!(state_name); - fn unpack( - result: Result, - err_msg: String, - ) -> StoredValue { - if let Ok(TrackingCopyQueryResult::Success { value, .. }) = result { - value + let results = tc.query( contract_key, &path); + if let Ok(TrackingCopyQueryResult::Success { value, .. }) = results { + assert_eq!(v, value); } else { - panic!("{}", err_msg); + panic!("Query failed when it should not have!"); } } - - let expected_contract = unpack( - tc.query(account_key, &[contract_alias.clone()]), - "contract should exist".to_string(), - ); - assert_eq!( - expected_contract, stored_contract, - "unexpected stored value" - ); - - // from account, traverse to contract then to misc val - let expected_account_contract_misc = unpack( - tc.query( - account_key, - &[contract_alias, misc_alias.clone()], // <-- path magic here - ), - "misc value should exist via account to contract".to_string(), - ); - assert_eq!( - expected_account_contract_misc, misc_stored_value, - "unexpected stored value" - ); - - let expected_account = unpack( - tc.query(contract_key, &[account_alias.clone()]), - "account should exist".to_string(), - ); - assert_eq!(expected_account, stored_account, "unexpected stored value"); - - // from contract, traverse to account then to misc val - let expected_contract_account_misc = unpack( - tc.query( - contract_key, - &[account_alias, misc_alias.clone()], // <-- path magic here - ), - "misc value should exist via contract to account".to_string(), - ); - assert_eq!( - expected_contract_account_misc, misc_stored_value, - "unexpected stored value" - ); - - let expected_value = unpack( - tc.query(misc_uref_key, &[]), - "misc value should exist".to_string(), - ); - assert_eq!(expected_value, misc_stored_value, "unexpected stored value"); - - let expected_account_misc = unpack( - tc.query(account_key, &[misc_alias.clone()]), - "misc value should exist via account".to_string(), - ); - assert_eq!( - expected_account_misc, misc_stored_value, - "unexpected stored value" - ); - - let expected_contract_misc = unpack( - tc.query(contract_key, &[misc_alias]), - "misc value should exist via contract".to_string(), - ); - assert_eq!( - expected_contract_misc, misc_stored_value, - "unexpected stored value" - ); } #[test] @@ -852,7 +774,7 @@ fn query_for_circular_references_should_fail() { (name_key_contract, key_value_contract), ]); let view = global_state.checkout(root_hash).unwrap().unwrap(); - let tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); // query for the self-referential key (second path element of arbitrary value required to cause // iteration _into_ the self-referential key) @@ -883,7 +805,7 @@ fn validate_query_proof_should_work() { let a_e_key = Key::AddressableEntity(EntityAddr::Account([30; 32])); let a_e = StoredValue::AddressableEntity(AddressableEntity::new( PackageHash::new([20; 32]), - contract_wasm_hash, + ByteCodeHash::default(), EntryPoints::new_with_default_entry_point(), ProtocolVersion::V1_0_0, URef::default(), @@ -931,13 +853,11 @@ fn validate_query_proof_should_work() { .expect("should checkout") .expect("should have view"); - let tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let path = &[c_nk]; - let result = tracking_copy - .query(a_e_key, path) - .expect("should query"); + let result = tracking_copy.query(a_e_key, path).expect("should query"); let proofs = if let TrackingCopyQueryResult::Success { proofs, .. } = result { proofs @@ -985,7 +905,7 @@ fn get_keys_should_return_keys_in_the_account_keyspace() { .expect("should checkout") .expect("should have view"); - let mut tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let key_set = tracking_copy.get_keys(&KeyTag::Account).unwrap(); @@ -1026,7 +946,7 @@ fn get_keys_should_return_keys_in_the_uref_keyspace() { .expect("should checkout") .expect("should have view"); - let mut tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let key_set = tracking_copy.get_keys(&KeyTag::URef).unwrap(); @@ -1059,7 +979,7 @@ fn get_keys_should_handle_reads_from_empty_trie() { .expect("should checkout") .expect("should have view"); - let mut tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let mut tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let key_set = tracking_copy.get_keys(&KeyTag::URef).unwrap(); @@ -1134,7 +1054,7 @@ fn query_with_large_depth_with_fixed_path_should_fail() { // create a long chain of contract at address X with a named key that points to a contract X+1 // which has a size that exceeds configured max query depth. - for value in 1..=super::DEFAULT_MAX_QUERY_DEPTH { + for value in 1..=DEFAULT_MAX_QUERY_DEPTH { let contract_addr = EntityAddr::SmartContract(val_to_hashaddr(value)); let contract_key = Key::AddressableEntity(contract_addr); let next_contract_key = @@ -1171,7 +1091,7 @@ fn query_with_large_depth_with_fixed_path_should_fail() { let (global_state, root_hash, _tempdir) = state::lmdb::make_temporary_global_state(pairs); let view = global_state.checkout(root_hash).unwrap().unwrap(); - let tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); let contract_key = contract_keys[0]; let result = tracking_copy.query(contract_key, &path); @@ -1179,7 +1099,7 @@ fn query_with_large_depth_with_fixed_path_should_fail() { assert!( matches!(result, Ok(TrackingCopyQueryResult::DepthLimit { depth - }) if depth == super::DEFAULT_MAX_QUERY_DEPTH), + }) if depth == DEFAULT_MAX_QUERY_DEPTH), "{:?}", result ); @@ -1196,7 +1116,7 @@ fn query_with_large_depth_with_urefs_should_fail() { // create a long chain of urefs at address X with a uref that points to a uref X+1 // which has a size that exceeds configured max query depth. - for value in 1..=super::DEFAULT_MAX_QUERY_DEPTH { + for value in 1..=DEFAULT_MAX_QUERY_DEPTH { let uref_addr = val_to_hashaddr(value); let uref = Key::URef(URef::new(uref_addr, AccessRights::READ)); @@ -1237,7 +1157,7 @@ fn query_with_large_depth_with_urefs_should_fail() { let (global_state, root_hash, _tempdir) = state::lmdb::make_temporary_global_state(pairs); let view = global_state.checkout(root_hash).unwrap().unwrap(); - let tracking_copy = TrackingCopy::new(view, super::DEFAULT_MAX_QUERY_DEPTH); + let tracking_copy = TrackingCopy::new(view, DEFAULT_MAX_QUERY_DEPTH); // query for the beginning of a long chain of urefs // (second path element of arbitrary value required to cause iteration _into_ the nested key) @@ -1247,7 +1167,7 @@ fn query_with_large_depth_with_urefs_should_fail() { assert!( matches!(result, Ok(TrackingCopyQueryResult::DepthLimit { depth - }) if depth == super::DEFAULT_MAX_QUERY_DEPTH), + }) if depth == DEFAULT_MAX_QUERY_DEPTH), "{:?}", result );