diff --git a/.drone.yml b/.drone.yml index 7254000b38..6983cb19ad 100644 --- a/.drone.yml +++ b/.drone.yml @@ -38,6 +38,8 @@ steps: environment: SCCACHE_S3_PUBLIC: true commands: + - make check-std-features + - make check-testing-features - make test CARGO_FLAGS=--release - make test-contracts CARGO_FLAGS=--release - cachepot --show-stats @@ -48,6 +50,8 @@ steps: - name: cargo-test-push <<: *buildenv commands: + - make check-std-features + - make check-testing-features - make test CARGO_FLAGS=--release - make test-contracts CARGO_FLAGS=--release - cachepot --show-stats diff --git a/Makefile b/Makefile index 7a714f6ee3..16b50471ac 100644 --- a/Makefile +++ b/Makefile @@ -108,6 +108,11 @@ check-std-features: cd smart_contracts/contract && $(CARGO) check --all-targets --no-default-features --features=std cd smart_contracts/contract && $(CARGO) check --all-targets --features=std +.PHONY: check-testing-features +check-testing-features: + cd types && $(CARGO) check --all-targets --no-default-features --features=testing + cd types && $(CARGO) check --all-targets --features=testing + .PHONY: check-format check-format: $(CARGO_PINNED_NIGHTLY) fmt --all -- --check @@ -158,18 +163,15 @@ check-rs: \ lint \ audit \ check-std-features \ + check-testing-features \ test-rs \ test-rs-no-default-features \ test-contracts-rs .PHONY: check check: \ - check-format \ - doc \ - lint \ - audit \ - test \ - test-contracts + check-rs \ + test-as .PHONY: clean clean: diff --git a/ci/nctl_upgrade.sh b/ci/nctl_upgrade.sh index 3f4e239e7b..1639e6a524 100755 --- a/ci/nctl_upgrade.sh +++ b/ci/nctl_upgrade.sh @@ -86,7 +86,7 @@ function dev_branch_settings() { pushd "$(get_path_to_remotes)" RC_VERSION="$(ls --group-directories-first -d */ | sort -r | head -n 1 | tr -d '/')" - [[ "$RC_VERSION" =~ (.*[^0-9])([0-9])(.)([0-9]+) ]] && INCREMENT="${BASH_REMATCH[1]}$((${BASH_REMATCH[2]} + 1))${BASH_REMATCH[3]}${BASH_REMATCH[4]}" + [[ "$RC_VERSION" =~ (.*[^0-9])([0-9])(.)([0-9]+) ]] && INCREMENT="2.0${BASH_REMATCH[3]}${BASH_REMATCH[4]}" RC_VERSION=$(echo "$RC_VERSION" | sed 's/\./\_/g') INCREMENT=$(echo "$INCREMENT" | sed 's/\./\_/g') diff --git a/execution_engine/src/engine_state/deploy_item.rs b/execution_engine/src/engine_state/deploy_item.rs index 980ddbbf77..de3b71673e 100644 --- a/execution_engine/src/engine_state/deploy_item.rs +++ b/execution_engine/src/engine_state/deploy_item.rs @@ -45,6 +45,11 @@ impl DeployItem { deploy_hash, } } + + /// Is this a native transfer? + pub fn is_native_transfer(&self) -> bool { + matches!(self.session, ExecutableDeployItem::Transfer { .. }) + } } impl From for DeployItem { @@ -62,7 +67,7 @@ impl From for DeployItem { deploy.payment().clone(), deploy.header().gas_price(), authorization_keys, - casper_types::DeployHash::new(*deploy.hash().inner()), + DeployHash::new(*deploy.hash().inner()), ) } } diff --git a/execution_engine/src/engine_state/error.rs b/execution_engine/src/engine_state/error.rs index c8bacf403a..9c568f4887 100644 --- a/execution_engine/src/engine_state/error.rs +++ b/execution_engine/src/engine_state/error.rs @@ -4,7 +4,9 @@ use thiserror::Error; use casper_storage::{ global_state::{self, state::CommitError}, - system::{genesis::GenesisError, protocol_upgrade::ProtocolUpgradeError}, + system::{ + genesis::GenesisError, protocol_upgrade::ProtocolUpgradeError, transfer::TransferError, + }, tracking_copy::TrackingCopyError, }; use casper_types::{ @@ -120,6 +122,9 @@ pub enum Error { /// Storage error. #[error("Tracking copy error: {0}")] TrackingCopy(TrackingCopyError), + /// Native transfer error. + #[error("Transfer error: {0}")] + Transfer(TransferError), } impl Error { @@ -133,6 +138,12 @@ impl Error { } } +impl From for Error { + fn from(err: TransferError) -> Self { + Error::Transfer(err) + } +} + impl From for Error { fn from(error: execution::Error) -> Self { match error { diff --git a/execution_engine/src/engine_state/execution_result.rs b/execution_engine/src/engine_state/execution_result.rs index 58b742886b..7e62f90489 100644 --- a/execution_engine/src/engine_state/execution_result.rs +++ b/execution_engine/src/engine_state/execution_result.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; +use casper_storage::data_access_layer::TransferResult; use casper_types::{ bytesrepr::FromBytes, contract_messages::Messages, @@ -9,7 +10,7 @@ use casper_types::{ CLTyped, CLValue, Gas, Key, Motes, StoredValue, TransferAddr, }; -use super::error; +use super::Error; use crate::execution::Error as ExecError; /// Represents the result of an execution specified by @@ -19,7 +20,7 @@ pub enum ExecutionResult { /// An error condition that happened during execution Failure { /// Error causing this `Failure` variant. - error: error::Error, + error: Error, /// List of transfers that happened during execution up to the point of the failure. transfers: Vec, /// Gas consumed up to the point of the failure. @@ -59,7 +60,7 @@ impl ExecutionResult { /// Constructs [ExecutionResult::Failure] that has 0 cost and no effects. /// This is the case for failures that we can't (or don't want to) charge /// for, like `PreprocessingError` or `InvalidNonce`. - pub fn precondition_failure(error: error::Error) -> ExecutionResult { + pub fn precondition_failure(error: Error) -> ExecutionResult { ExecutionResult::Failure { error, transfers: Vec::default(), @@ -224,18 +225,19 @@ impl ExecutionResult { /// Returns error value, if possible. /// - /// Returns a reference to a wrapped [`error::Error`] instance if the object is a failure - /// variant. - pub fn as_error(&self) -> Option<&error::Error> { + /// Returns a reference to a wrapped [`super::Error`] + /// instance if the object is a failure variant. + pub fn as_error(&self) -> Option<&Error> { match self { ExecutionResult::Failure { error, .. } => Some(error), ExecutionResult::Success { .. } => None, } } - /// Consumes [`ExecutionResult`] instance and optionally returns [`error::Error`] instance for + /// Consumes [`ExecutionResult`] instance and optionally returns + /// [`super::Error`] instance for /// [`ExecutionResult::Failure`] variant. - pub fn take_error(self) -> Option { + pub fn take_error(self) -> Option { match self { ExecutionResult::Failure { error, .. } => Some(error), ExecutionResult::Success { .. } => None, @@ -288,16 +290,16 @@ impl ExecutionResult { /// The effects that are produced as part of this process would subract `max_payment_cost` from /// account's main purse, and add `max_payment_cost` to proposer account's balance. pub fn new_payment_code_error( - error: error::Error, + error: Error, max_payment_cost: Motes, account_main_purse_balance: Motes, gas_cost: Gas, account_main_purse_balance_key: Key, proposer_main_purse_balance_key: Key, - ) -> Result { + ) -> Result { let new_balance = account_main_purse_balance .checked_sub(max_payment_cost) - .ok_or(error::Error::InsufficientPayment)?; + .ok_or(Error::InsufficientPayment)?; let new_balance_value = StoredValue::CLValue(CLValue::from_t(new_balance.value()).map_err(ExecError::from)?); let mut effects = Effects::new(); @@ -328,6 +330,37 @@ impl ExecutionResult { pub(crate) fn take_without_ret(self) -> (Option, Self) { (None, self) } + + /// A temporary measure to keep things functioning mid-refactor. + #[allow(clippy::result_unit_err)] + pub fn from_transfer_result(transfer_result: TransferResult, cost: Gas) -> Result { + match transfer_result { + TransferResult::RootNotFound => { + Err(()) + } + // native transfer is auto-commit...but execution results does not currently allow + // for a post state hash to be returned. + TransferResult::Success { + transfers, effects, .. // post_state_hash + } => { + Ok(ExecutionResult::Success { + transfers, + cost, + effects, + messages: Messages::default(), + }) + } + TransferResult::Failure(te) => { + Ok(ExecutionResult::Failure { + error: Error::Transfer(te), + transfers: vec![], + cost, + effects: Effects::default(), // currently not returning effects on failure + messages: Messages::default(), + }) + } + } + } } /// A versioned execution result and the messages produced by that execution. @@ -459,7 +492,7 @@ impl ExecutionResultBuilder { /// Builds a final [`ExecutionResult`] based on session result, payment result and a /// finalization result. pub fn build(self) -> Result { - let mut error: Option = None; + let mut error: Option = None; let mut transfers = self.transfers(); let cost = self.total_cost(); @@ -497,9 +530,7 @@ impl ExecutionResultBuilder { match self.finalize_execution_result { Some(ExecutionResult::Failure { .. }) => { // payment_code_spec_5_a: Finalization Error should only ever be raised here - return Ok(ExecutionResult::precondition_failure( - error::Error::Finalization, - )); + return Ok(ExecutionResult::precondition_failure(Error::Finalization)); } Some(ExecutionResult::Success { effects, messages, .. diff --git a/execution_engine/src/engine_state/mod.rs b/execution_engine/src/engine_state/mod.rs index 1a67d19951..9d00e2dec9 100644 --- a/execution_engine/src/engine_state/mod.rs +++ b/execution_engine/src/engine_state/mod.rs @@ -7,14 +7,12 @@ pub(crate) mod execution_kind; pub mod execution_result; mod prune; pub mod step; -mod transfer; use itertools::Itertools; use std::{ cell::RefCell, collections::{BTreeMap, BTreeSet}, - convert::TryFrom, rc::Rc, }; @@ -27,49 +25,42 @@ use casper_storage::{ balance::BalanceResult, get_bids::{BidsRequest, BidsResult}, query::{QueryRequest, QueryResult}, + transfer::TransferConfig, DataAccessLayer, EraValidatorsRequest, EraValidatorsResult, FlushRequest, FlushResult, GenesisRequest, GenesisResult, ProtocolUpgradeRequest, ProtocolUpgradeResult, - PutTrieRequest, TrieRequest, + TransferRequest, TrieRequest, }, global_state::{ self, + error::Error as GlobalStateError, state::{ lmdb::LmdbGlobalState, scratch::ScratchGlobalState, CommitProvider, StateProvider, - StateReader, }, - trie::{merkle_proof::TrieMerkleProof, TrieRaw}, + trie::TrieRaw, trie_store::operations::PruneResult as GlobalStatePruneResult, }, system::auction, - tracking_copy::{TrackingCopy, TrackingCopyError, TrackingCopyExt}, - AddressGenerator, + tracking_copy::{TrackingCopy, TrackingCopyEntityExt, TrackingCopyError, TrackingCopyExt}, }; use casper_types::{ - account::{Account, AccountHash}, - addressable_entity::{ - ActionThresholds, AssociatedKeys, EntityKind, EntityKindTag, MessageTopics, NamedKeyAddr, - NamedKeyValue, NamedKeys, Weight, - }, + account::AccountHash, + addressable_entity::{EntityKind, EntityKindTag, NamedKeys}, bytesrepr::ToBytes, execution::Effects, - package::{EntityVersions, Groups, PackageStatus}, system::{ auction::{ ARG_ERA_END_TIMESTAMP_MILLIS, ARG_EVICTED_VALIDATORS, ARG_REWARDS_MAP, ARG_VALIDATOR_PUBLIC_KEYS, SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY, }, handle_payment::{self, ACCUMULATION_PURSE_KEY}, - mint::{self}, AUCTION, HANDLE_PAYMENT, MINT, }, - AccessRights, AddressableEntity, AddressableEntityHash, ApiError, BlockTime, ByteCodeHash, - CLValue, ChecksumRegistry, DeployHash, DeployInfo, Digest, EntityAddr, EntryPoints, - ExecutableDeployItem, FeeHandling, Gas, Key, KeyTag, Motes, Package, PackageHash, Phase, - ProtocolVersion, PublicKey, RuntimeArgs, StoredValue, SystemEntityRegistry, URef, U512, + AddressableEntity, AddressableEntityHash, BlockTime, DeployHash, DeployInfo, Digest, + EntityAddr, ExecutableDeployItem, FeeHandling, Gas, Key, KeyTag, Motes, Phase, ProtocolVersion, + PublicKey, RuntimeArgs, StoredValue, SystemEntityRegistry, URef, U512, }; -use self::transfer::NewTransferTargetMode; pub use self::{ deploy_item::DeployItem, engine_config::{ @@ -82,14 +73,10 @@ pub use self::{ execution_result::{ExecutionResult, ForcedTransferResult}, prune::{PruneConfig, PruneResult}, step::{RewardItem, SlashItem, StepError, StepRequest, StepSuccess}, - transfer::{TransferArgs, TransferRuntimeArgsBuilder, TransferTargetMode}, }; use crate::{ - engine_state::{ - execution_kind::ExecutionKind, - execution_result::{ExecutionResultBuilder, ExecutionResults}, - }, + engine_state::{execution_kind::ExecutionKind, execution_result::ExecutionResults}, execution::{self, DirectSystemContractCall, Executor}, runtime::RuntimeStack, }; @@ -253,8 +240,6 @@ where /// contracts, sets up the genesis accounts, and sets up the auction state based on that. At /// the end of the process, [`SystemEntityRegistry`] is persisted under the special global /// state space [`Key::SystemEntityRegistry`]. - /// - /// Returns a [`GenesisResult`]. pub fn commit_genesis(&self, request: GenesisRequest) -> GenesisResult { self.state.genesis(request) } @@ -263,7 +248,7 @@ where /// /// This process applies changes to the global state. /// - /// Returns [`UpgradeSuccess`]. + /// Returns [`ProtocolUpgradeResult`]. pub fn commit_upgrade(&self, request: ProtocolUpgradeRequest) -> ProtocolUpgradeResult { self.state.protocol_upgrade(request) } @@ -309,14 +294,8 @@ where pub fn tracking_copy( &self, hash: Digest, - ) -> Result>, TrackingCopyError> { - match self.state.checkout(hash) { - Ok(ret) => match ret { - Some(tc) => Ok(Some(TrackingCopy::new(tc, self.config.max_query_depth))), - None => Ok(None), - }, - Err(err) => Err(TrackingCopyError::Storage(err)), - } + ) -> Result>, GlobalStateError> { + self.state.tracking_copy(hash) } /// Executes a query. @@ -325,30 +304,7 @@ where /// /// Returns the value stored under a [`URef`] wrapped in a [`QueryResult`]. pub fn run_query(&self, query_request: QueryRequest) -> QueryResult { - let state_hash = query_request.state_hash(); - let query_key = query_request.key(); - let query_path = query_request.path(); - let query_result = match self.tracking_copy(state_hash) { - Ok(Some(tc)) => match tc.query(query_key, query_path) { - Ok(ret) => ret.into(), - Err(err) => QueryResult::Failure(err), - }, - Ok(None) => QueryResult::RootNotFound, - Err(err) => QueryResult::Failure(err), - }; - - if let QueryResult::ValueNotFound(_) = query_result { - if query_key.is_system_key() { - if let Some(entity_addr) = query_key.into_entity_hash_addr() { - debug!("Compensating for AddressableEntity move"); - let legacy_query_key = Key::Hash(entity_addr); - let legacy_request = - QueryRequest::new(state_hash, legacy_query_key, query_path.to_vec()); - return self.run_query(legacy_request); - } - } - } - query_result + self.state.query(query_request) } /// Runs a deploy execution request. @@ -394,180 +350,6 @@ where Ok(results) } - fn get_authorized_addressable_entity( - &self, - protocol_version: ProtocolVersion, - account_hash: AccountHash, - authorization_keys: &BTreeSet, - tracking_copy: Rc::Reader>>>, - ) -> Result<(AddressableEntity, AddressableEntityHash), Error> { - let entity_record = match tracking_copy - .borrow_mut() - .get_addressable_entity_by_account_hash(protocol_version, account_hash) - { - Ok(entity) => entity, - Err(_) => return Err(Error::MissingContractByAccountHash(account_hash)), - }; - - let entity_hash: AddressableEntityHash = match tracking_copy - .borrow_mut() - .get_entity_hash_by_account_hash(account_hash) - { - Ok(contract_hash) => contract_hash, - Err(error) => { - return Err(error.into()); - } - }; - - let admin_set = self.config().administrative_accounts(); - - if !admin_set.is_empty() && admin_set.intersection(authorization_keys).next().is_some() { - // Exit early if there's at least a single signature coming from an admin. - return Ok((entity_record, entity_hash)); - } - - // Authorize using provided authorization keys - if !entity_record.can_authorize(authorization_keys) { - return Err(Error::Authorization); - } - - // Check total key weight against deploy threshold - if !entity_record.can_deploy_with(authorization_keys) { - return Err(execution::Error::DeploymentAuthorizationFailure.into()); - } - - Ok((entity_record, entity_hash)) - } - - fn create_addressable_entity_from_account( - &self, - account: Account, - protocol_version: ProtocolVersion, - tracking_copy: Rc::Reader>>>, - ) -> Result<(), Error> { - let account_hash = account.account_hash(); - - let mut generator = - AddressGenerator::new(account.main_purse().addr().as_ref(), Phase::System); - - let byte_code_hash = ByteCodeHash::default(); - let entity_hash = AddressableEntityHash::new(generator.new_hash_address()); - let package_hash = PackageHash::new(generator.new_hash_address()); - - let entry_points = EntryPoints::new(); - - let associated_keys = AssociatedKeys::from(account.associated_keys().clone()); - let action_thresholds = { - let account_threshold = account.action_thresholds().clone(); - ActionThresholds::new( - Weight::new(account_threshold.deployment.value()), - Weight::new(1u8), - Weight::new(account_threshold.key_management.value()), - ) - .map_err(|_| Error::Authorization)? - }; - - let entity_addr = EntityAddr::new_account_entity_addr(entity_hash.value()); - - self.migrate_named_keys( - entity_addr, - account.named_keys().clone(), - Rc::clone(&tracking_copy), - )?; - - let entity = AddressableEntity::new( - package_hash, - byte_code_hash, - entry_points, - protocol_version, - account.main_purse(), - associated_keys, - action_thresholds, - MessageTopics::default(), - EntityKind::Account(account_hash), - ); - - let access_key = generator.new_uref(AccessRights::READ_ADD_WRITE); - - let package = { - let mut package = Package::new( - access_key, - EntityVersions::default(), - BTreeSet::default(), - Groups::default(), - PackageStatus::Locked, - ); - package.insert_entity_version(protocol_version.value().major, entity_hash); - package - }; - - let entity_key: Key = entity.entity_key(entity_hash); - - tracking_copy.borrow_mut().write(entity_key, entity.into()); - tracking_copy - .borrow_mut() - .write(package_hash.into(), package.into()); - let contract_by_account = match CLValue::from_t(entity_key) { - Ok(cl_value) => cl_value, - Err(_) => return Err(Error::Bytesrepr("Failed to convert to CLValue".to_string())), - }; - - tracking_copy.borrow_mut().write( - Key::Account(account_hash), - StoredValue::CLValue(contract_by_account), - ); - Ok(()) - } - - fn migrate_account( - &self, - account_hash: AccountHash, - protocol_version: ProtocolVersion, - tracking_copy: Rc::Reader>>>, - ) -> Result<(), Error> { - let maybe_stored_value = tracking_copy - .borrow_mut() - .read(&Key::Account(account_hash)) - .map_err(Into::::into)?; - - match maybe_stored_value { - Some(StoredValue::Account(account)) => self.create_addressable_entity_from_account( - account, - protocol_version, - Rc::clone(&tracking_copy), - ), - Some(StoredValue::CLValue(_)) => Ok(()), - // This means the Account does not exist, which we consider to be - // an authorization error. As used by the node, this type of deploy - // will have already been filtered out, but for other EE use cases - // and testing it is reachable. - Some(_) | None => Err(Error::Authorization), - } - } - - fn migrate_named_keys( - &self, - entity_addr: EntityAddr, - named_keys: NamedKeys, - tracking_copy: Rc::Reader>>>, - ) -> Result<(), Error> { - for (string, key) in named_keys.into_inner().into_iter() { - let entry_addr = NamedKeyAddr::new_from_string(entity_addr, string.clone()) - .map_err(|error| Error::Bytesrepr(error.to_string()))?; - - let named_key_value = StoredValue::NamedKey( - NamedKeyValue::from_concrete_values(key, string.clone()) - .map_err(|cl_error| Error::Bytesrepr(cl_error.to_string()))?, - ); - - let entry_key = Key::NamedKey(entry_addr); - - tracking_copy.borrow_mut().write(entry_key, named_key_value) - } - - Ok(()) - } - fn get_named_keys( &self, entity_addr: EntityAddr, @@ -605,580 +387,36 @@ where #[allow(clippy::too_many_arguments)] pub fn transfer( &self, - executor: &Executor, + _executor: &Executor, protocol_version: ProtocolVersion, prestate_hash: Digest, blocktime: BlockTime, deploy_item: DeployItem, proposer: PublicKey, ) -> Result { - let tracking_copy = match self.tracking_copy(prestate_hash) { - Err(tce) => { - return Ok(ExecutionResult::precondition_failure(Error::TrackingCopy( - tce, - ))) - } - Ok(None) => return Err(Error::RootNotFound(prestate_hash)), - Ok(Some(tracking_copy)) => Rc::new(RefCell::new(tracking_copy)), - }; - - let account_hash = deploy_item.address; - - let authorization_keys = deploy_item.authorization_keys; - - // Migrate the legacy account structure if necessary. - if let Err(e) = - self.migrate_account(account_hash, protocol_version, Rc::clone(&tracking_copy)) - { - return Ok(ExecutionResult::precondition_failure(e)); - } - - let (entity, entity_hash) = match self.get_authorized_addressable_entity( - protocol_version, - account_hash, - &authorization_keys, - Rc::clone(&tracking_copy), - ) { - Ok(account) => account, - Err(e) => return Ok(ExecutionResult::precondition_failure(e)), - }; - - let package_kind = entity.entity_kind(); - - let entity_addr = EntityAddr::new_with_tag(package_kind, entity_hash.value()); - - let entity_named_keys = match self.get_named_keys(entity_addr, Rc::clone(&tracking_copy)) { - Ok(named_keys) => named_keys, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(error)); - } - }; - - let system_contract_registry = tracking_copy.borrow_mut().get_system_contracts()?; - - let handle_payment_contract_hash = system_contract_registry - .get(HANDLE_PAYMENT) - .ok_or_else(|| { - error!("Missing system handle payment contract hash"); - Error::MissingSystemContractHash(HANDLE_PAYMENT.to_string()) - })?; - - let handle_payment_contract = match tracking_copy - .borrow_mut() - .get_addressable_entity(*handle_payment_contract_hash) - { - Ok(contract) => contract, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(error.into())); - } - }; - - let handle_payment_addr = - EntityAddr::new_system_entity_addr(handle_payment_contract_hash.value()); - - let handle_payment_named_keys = - match self.get_named_keys(handle_payment_addr, Rc::clone(&tracking_copy)) { - Ok(named_keys) => named_keys, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(error)); - } - }; - - let mut handle_payment_access_rights = handle_payment_contract - .extract_access_rights(*handle_payment_contract_hash, &handle_payment_named_keys); - - let gas_limit = Gas::new(U512::from(std::u64::MAX)); - - let wasmless_transfer_gas_cost = Gas::new(U512::from( + let deploy_hash = deploy_item.deploy_hash; + let transfer_config = TransferConfig::new( + self.config.administrative_accounts.clone(), + self.config.allow_unrestricted_transfers, + ); + let wasmless_transfer_gas = Gas::new(U512::from( self.config().system_config().wasmless_transfer_cost(), )); - - let wasmless_transfer_motes = match Motes::from_gas( - wasmless_transfer_gas_cost, - WASMLESS_TRANSFER_FIXED_GAS_PRICE, - ) { - Some(motes) => motes, - None => { - return Ok(ExecutionResult::precondition_failure( - Error::GasConversionOverflow, - )) - } - }; - - let rewards_target_purse = - match self.get_rewards_purse(protocol_version, proposer, prestate_hash) { - Ok(target_purse) => target_purse, - Err(error) => return Ok(ExecutionResult::precondition_failure(error)), - }; - - let rewards_target_purse_balance_key = { - match tracking_copy - .borrow_mut() - .get_purse_balance_key(rewards_target_purse.into()) - { - Ok(balance_key) => balance_key, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(Error::TrackingCopy( - error, - ))) - } - } - }; - - let account_main_purse = entity.main_purse(); - - let account_main_purse_balance_key = match tracking_copy - .borrow_mut() - .get_purse_balance_key(account_main_purse.into()) - { - Ok(balance_key) => balance_key, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(Error::TrackingCopy( - error, - ))) - } - }; - - let account_main_purse_balance = match tracking_copy - .borrow_mut() - .get_purse_balance(account_main_purse_balance_key) - { - Ok(balance_key) => balance_key, - Err(error) => { - return Ok(ExecutionResult::precondition_failure(Error::TrackingCopy( - error, - ))) - } - }; - - if account_main_purse_balance < wasmless_transfer_motes { - // We don't have minimum balance to operate and therefore we can't charge for user - // errors. - return Ok(ExecutionResult::precondition_failure( - Error::InsufficientPayment, - )); - } - - // Function below creates an ExecutionResult with precomputed effects of "finalize_payment". - let make_charged_execution_failure = |error| match ExecutionResult::new_payment_code_error( - error, - wasmless_transfer_motes, - account_main_purse_balance, - wasmless_transfer_gas_cost, - account_main_purse_balance_key, - rewards_target_purse_balance_key, - ) { - Ok(execution_result) => execution_result, - Err(error) => ExecutionResult::precondition_failure(error), - }; - - // All wasmless transfer preconditions are met. - // Any error that occurs in logic below this point would result in a charge for user error. - let mut runtime_args_builder = - TransferRuntimeArgsBuilder::new(deploy_item.session.args().clone()); - - let transfer_target_mode = match runtime_args_builder - .resolve_transfer_target_mode(protocol_version, Rc::clone(&tracking_copy)) - { - Ok(transfer_target_mode) => transfer_target_mode, - Err(error) => return Ok(make_charged_execution_failure(error)), - }; - - // At this point we know target refers to either a purse on an existing account or an - // account which has to be created. - - if !self.config.allow_unrestricted_transfers() - && !self.config.is_administrator(&account_hash) - { - // We need to make sure that source or target has to be admin. - match transfer_target_mode { - NewTransferTargetMode::ExistingAccount { - target_account_hash, - .. - } - | NewTransferTargetMode::CreateAccount(target_account_hash) => { - let is_target_system_account = - target_account_hash == PublicKey::System.to_account_hash(); - let is_target_administrator = - self.config.is_administrator(&target_account_hash); - if !(is_target_system_account || is_target_administrator) { - // Transferring from normal account to a purse doesn't work. - return Ok(make_charged_execution_failure( - execution::Error::DisabledUnrestrictedTransfers.into(), - )); - } - } - NewTransferTargetMode::PurseExists(_) => { - // We don't know who is the target and we can't simply reverse search - // account/contract that owns it. We also can't know if purse is owned exactly - // by one entity in the system. - return Ok(make_charged_execution_failure( - execution::Error::DisabledUnrestrictedTransfers.into(), - )); - } - } - } - - match transfer_target_mode { - NewTransferTargetMode::ExistingAccount { .. } - | NewTransferTargetMode::PurseExists(_) => { - // Noop - } - NewTransferTargetMode::CreateAccount(account_hash) => { - let create_purse_stack = self.get_new_system_call_stack(); - - let (maybe_uref, execution_result): (Option, ExecutionResult) = executor - .call_system_contract( - DirectSystemContractCall::CreatePurse, - RuntimeArgs::new(), // mint create takes no arguments - &entity, - package_kind, - authorization_keys.clone(), - account_hash, - blocktime, - deploy_item.deploy_hash, - gas_limit, - protocol_version, - Rc::clone(&tracking_copy), - Phase::Session, - create_purse_stack, - // We're just creating a purse. - U512::zero(), - ); - match maybe_uref { - Some(main_purse) => { - let account = Account::create(account_hash, NamedKeys::new(), main_purse); - if let Err(error) = self.create_addressable_entity_from_account( - account, - protocol_version, - Rc::clone(&tracking_copy), - ) { - return Ok(make_charged_execution_failure(error)); - } - } - None => { - // This case implies that the execution_result is a failure variant as - // implemented inside host_exec(). - let error = execution_result - .take_error() - .unwrap_or(Error::InsufficientPayment); - return Ok(make_charged_execution_failure(error)); - } - } - } - } - - let transfer_args = match runtime_args_builder.build( - &entity, - entity_named_keys, + let transfer_req = TransferRequest::with_runtime_args( + transfer_config, + prestate_hash, + blocktime.value(), protocol_version, - Rc::clone(&tracking_copy), - ) { - Ok(transfer_args) => transfer_args, - Err(error) => return Ok(make_charged_execution_failure(error)), - }; - - let payment_uref; - - // Construct a payment code that will put cost of wasmless payment into payment purse - let payment_result = { - // Check source purses minimum balance - let source_uref = transfer_args.source(); - let source_purse_balance = if source_uref != account_main_purse { - let source_purse_balance_key = match tracking_copy - .borrow_mut() - .get_purse_balance_key(Key::URef(source_uref)) - { - Ok(purse_balance_key) => purse_balance_key, - Err(error) => { - return Ok(make_charged_execution_failure(Error::TrackingCopy(error))) - } - }; - - match tracking_copy - .borrow_mut() - .get_purse_balance(source_purse_balance_key) - { - Ok(purse_balance) => purse_balance, - Err(error) => { - return Ok(make_charged_execution_failure(Error::TrackingCopy(error))) - } - } - } else { - // If source purse is main purse then we already have the balance. - account_main_purse_balance - }; - - let transfer_amount_motes = Motes::new(transfer_args.amount()); - - match wasmless_transfer_motes.checked_add(transfer_amount_motes) { - Some(total_amount) if source_purse_balance < total_amount => { - // We can't continue if the minimum funds in source purse are lower than the - // required cost. - return Ok(make_charged_execution_failure(Error::InsufficientPayment)); - } - None => { - // When trying to send too much that could cause an overflow. - return Ok(make_charged_execution_failure(Error::InsufficientPayment)); - } - Some(_) => {} - } - - let get_payment_purse_stack = self.get_new_system_call_stack(); - let (maybe_payment_uref, get_payment_purse_result): (Option, ExecutionResult) = - executor.call_system_contract( - DirectSystemContractCall::GetPaymentPurse, - RuntimeArgs::default(), - &entity, - package_kind, - authorization_keys.clone(), - account_hash, - blocktime, - deploy_item.deploy_hash, - gas_limit, - protocol_version, - Rc::clone(&tracking_copy), - Phase::Payment, - get_payment_purse_stack, - // Getting payment purse does not require transfering tokens. - U512::zero(), - ); - - payment_uref = match maybe_payment_uref { - Some(payment_uref) => payment_uref, - None => return Ok(make_charged_execution_failure(Error::InsufficientPayment)), - }; - - if let Some(error) = get_payment_purse_result.take_error() { - return Ok(make_charged_execution_failure(error)); - } - - // Create a new arguments to transfer cost of wasmless transfer into the payment purse. - - let new_transfer_args = TransferArgs::new( - transfer_args.to(), - transfer_args.source(), - payment_uref, - wasmless_transfer_motes.value(), - transfer_args.arg_id(), - ); - - let runtime_args = match RuntimeArgs::try_from(new_transfer_args) { - Ok(runtime_args) => runtime_args, - Err(error) => return Ok(make_charged_execution_failure(Error::Exec(error.into()))), - }; - - let transfer_to_payment_purse_stack = self.get_new_system_call_stack(); - let (actual_result, payment_result): (Option>, ExecutionResult) = - executor.call_system_contract( - DirectSystemContractCall::Transfer, - runtime_args, - &entity, - package_kind, - authorization_keys.clone(), - account_hash, - blocktime, - deploy_item.deploy_hash, - gas_limit, - protocol_version, - Rc::clone(&tracking_copy), - Phase::Payment, - transfer_to_payment_purse_stack, - // We should use only as much as transfer costs. - // We're not changing the allowed spending limit since this is a system cost. - wasmless_transfer_motes.value(), - ); - - if let Some(error) = payment_result.as_error().cloned() { - return Ok(make_charged_execution_failure(error)); - } - - let transfer_result = match actual_result { - Some(Ok(())) => Ok(()), - Some(Err(mint_error)) => match mint::Error::try_from(mint_error) { - Ok(mint_error) => Err(ApiError::from(mint_error)), - Err(_) => Err(ApiError::Transfer), - }, - None => Err(ApiError::Transfer), - }; - - if let Err(error) = transfer_result { - return Ok(make_charged_execution_failure(Error::Exec( - ExecError::Revert(error), - ))); - } - - let payment_purse_balance = { - let payment_purse_balance_key = match tracking_copy - .borrow_mut() - .get_purse_balance_key(Key::URef(payment_uref)) - { - Ok(payment_purse_balance_key) => payment_purse_balance_key, - Err(error) => { - return Ok(make_charged_execution_failure(Error::TrackingCopy(error))) - } - }; - - match tracking_copy - .borrow_mut() - .get_purse_balance(payment_purse_balance_key) - { - Ok(payment_purse_balance) => payment_purse_balance, - Err(error) => { - return Ok(make_charged_execution_failure(Error::TrackingCopy(error))) - } - } - }; - - // Wasmless transfer payment code pre & post conditions: - // (a) payment purse should be empty before the payment operation - // (b) after executing payment code it's balance has to be equal to the wasmless gas - // cost price - - let payment_gas = - match Gas::from_motes(payment_purse_balance, WASMLESS_TRANSFER_FIXED_GAS_PRICE) { - Some(gas) => gas, - None => { - return Ok(make_charged_execution_failure(Error::GasConversionOverflow)) - } - }; - - debug_assert_eq!(payment_gas, wasmless_transfer_gas_cost); - - // This assumes the cost incurred is already denominated in gas - - payment_result.with_cost(payment_gas) - }; - - let runtime_args = match RuntimeArgs::try_from(transfer_args) { - Ok(runtime_args) => runtime_args, - Err(error) => { - return Ok(make_charged_execution_failure( - ExecError::from(error).into(), - )) - } - }; - - let transfer_stack = self.get_new_system_call_stack(); - let (_, mut session_result): (Option>, ExecutionResult) = executor - .call_system_contract( - DirectSystemContractCall::Transfer, - runtime_args, - &entity, - package_kind, - authorization_keys.clone(), - account_hash, - blocktime, - deploy_item.deploy_hash, - gas_limit, - protocol_version, - Rc::clone(&tracking_copy), - Phase::Session, - transfer_stack, - // We limit native transfer to the amount that user signed over as `amount` - // argument. - transfer_args.amount(), - ); - - // User is already charged fee for wasmless contract, and we need to make sure we will not - // charge for anything that happens while calling transfer entrypoint. - session_result = session_result.with_cost(Gas::default()); - - let finalize_result = { - let handle_payment_args = { - // Gas spent during payment code execution - let finalize_cost_motes = { - // A case where payment_result.cost() is different than wasmless transfer cost - // is considered a programming error. - debug_assert_eq!(payment_result.cost(), wasmless_transfer_gas_cost); - wasmless_transfer_motes - }; - - let account = deploy_item.address; - let maybe_runtime_args = RuntimeArgs::try_new(|args| { - args.insert(handle_payment::ARG_AMOUNT, finalize_cost_motes.value())?; - args.insert(handle_payment::ARG_ACCOUNT, account)?; - args.insert(handle_payment::ARG_TARGET, rewards_target_purse)?; - Ok(()) - }); - - match maybe_runtime_args { - Ok(runtime_args) => runtime_args, - Err(error) => { - let exec_error = ExecError::from(error); - return Ok(ExecutionResult::precondition_failure(exec_error.into())); - } - } - }; - - let system_addressable_entity = { - tracking_copy - .borrow_mut() - .get_addressable_entity_by_account_hash( - protocol_version, - PublicKey::System.to_account_hash(), - )? - }; - - let tc = tracking_copy.borrow(); - let finalization_tc = Rc::new(RefCell::new(tc.fork())); - - let finalize_payment_stack = self.get_new_system_call_stack(); - handle_payment_access_rights.extend(&[payment_uref, rewards_target_purse]); - - let (_ret, finalize_result): (Option<()>, ExecutionResult) = executor - .call_system_contract( - DirectSystemContractCall::FinalizePayment, - handle_payment_args, - &system_addressable_entity, - EntityKind::Account(PublicKey::System.to_account_hash()), - authorization_keys, - PublicKey::System.to_account_hash(), - blocktime, - deploy_item.deploy_hash, - gas_limit, - protocol_version, - finalization_tc, - Phase::FinalizePayment, - finalize_payment_stack, - // Spending limit is cost of wasmless execution. - U512::from(self.config().system_config().wasmless_transfer_cost()), - ); - - finalize_result - }; - - // Create + persist deploy info. - { - let transfers = session_result.transfers(); - let cost = wasmless_transfer_gas_cost.value(); - let deploy_info = DeployInfo::new( - deploy_item.deploy_hash, - transfers, - account_hash, - entity.main_purse(), - cost, - ); - tracking_copy.borrow_mut().write( - Key::DeployInfo(deploy_item.deploy_hash), - StoredValue::DeployInfo(deploy_info), - ); - } - - if session_result.is_success() { - session_result = session_result.with_effects(tracking_copy.borrow().effects()) - } - - let mut execution_result_builder = ExecutionResultBuilder::new(); - execution_result_builder.set_payment_execution_result(payment_result); - execution_result_builder.set_session_execution_result(session_result); - execution_result_builder.set_finalize_execution_result(finalize_result); - - let execution_result = execution_result_builder - .build() - .expect("ExecutionResultBuilder not initialized properly"); - - Ok(execution_result) + proposer, + *deploy_hash.inner(), + deploy_item.address, + deploy_item.authorization_keys, + deploy_item.session.args().clone(), + wasmless_transfer_gas.value(), + ); + let transfer_result = self.state.transfer(transfer_req); + ExecutionResult::from_transfer_result(transfer_result, wasmless_transfer_gas) + .map_err(|_| Error::RootNotFound(prestate_hash)) } /// Executes a deploy. @@ -1207,11 +445,7 @@ where // validation_spec_2: prestate_hash check // do this second; as there is no reason to proceed if the prestate hash is invalid let tracking_copy = match self.tracking_copy(prestate_hash) { - Err(tce) => { - return Ok(ExecutionResult::precondition_failure(Error::TrackingCopy( - tce, - ))) - } + Err(gse) => return Ok(ExecutionResult::precondition_failure(Error::Storage(gse))), Ok(None) => return Err(Error::RootNotFound(prestate_hash)), Ok(Some(tracking_copy)) => Rc::new(RefCell::new(tracking_copy)), }; @@ -1222,23 +456,26 @@ where let authorization_keys = deploy_item.authorization_keys; let account_hash = deploy_item.address; - if let Err(error) = - self.migrate_account(account_hash, protocol_version, Rc::clone(&tracking_copy)) + if let Err(e) = tracking_copy + .borrow_mut() + .migrate_account(account_hash, protocol_version) { - return Ok(ExecutionResult::precondition_failure(error)); + return Ok(ExecutionResult::precondition_failure(e.into())); } // Get account from tracking copy // validation_spec_3: account validity let (entity, entity_hash) = { - match self.get_authorized_addressable_entity( - protocol_version, - account_hash, - &authorization_keys, - Rc::clone(&tracking_copy), - ) { + match tracking_copy + .borrow_mut() + .get_authorized_addressable_entity( + protocol_version, + account_hash, + &authorization_keys, + &self.config().administrative_accounts, + ) { Ok((addressable_entity, entity_hash)) => (addressable_entity, entity_hash), - Err(e) => return Ok(ExecutionResult::precondition_failure(e)), + Err(e) => return Ok(ExecutionResult::precondition_failure(e.into())), } }; @@ -1322,7 +559,7 @@ where // Get handle payment system contract details // payment_code_spec_6: system contract validity - let system_contract_registry = tracking_copy.borrow_mut().get_system_contracts()?; + let system_contract_registry = tracking_copy.borrow_mut().get_system_entity_registry()?; let handle_payment_contract_hash = system_contract_registry .get(HANDLE_PAYMENT) @@ -1488,7 +725,7 @@ where // Get handle payment system contract details // payment_code_spec_6: system contract validity - let system_contract_registry = tracking_copy.borrow_mut().get_system_contracts()?; + let system_contract_registry = tracking_copy.borrow_mut().get_system_entity_registry()?; let handle_payment_contract_hash = system_contract_registry .get(HANDLE_PAYMENT) @@ -1717,7 +954,8 @@ where // The Handle Payment keys may have changed because of effects during payment and/or // session, so we need to look them up again from the tracking copy - let system_contract_registry = finalization_tc.borrow_mut().get_system_contracts()?; + let system_contract_registry = + finalization_tc.borrow_mut().get_system_entity_registry()?; let handle_payment_contract_hash = system_contract_registry .get(HANDLE_PAYMENT) @@ -1795,7 +1033,7 @@ where prestate_hash: Digest, ) -> Result { let tracking_copy = match self.tracking_copy(prestate_hash) { - Err(tce) => return Err(Error::TrackingCopy(tce)), + Err(gse) => return Err(Error::Storage(gse)), Ok(None) => return Err(Error::RootNotFound(prestate_hash)), Ok(Some(tracking_copy)) => Rc::new(RefCell::new(tracking_copy)), }; @@ -1861,22 +1099,6 @@ where self.state.trie(req).into_legacy().map_err(Error::Storage) } - /// Puts a trie if no children are missing from the global state; otherwise reports the missing - /// children hashes via the `Error` enum. - pub fn put_trie_if_all_children_present(&self, trie_bytes: &[u8]) -> Result { - let missing_children = match self.state.missing_children(trie_bytes) { - Ok(ret) => ret, - Err(err) => return Err(err.into()), - }; - let raw = TrieRaw::new(trie_bytes.into()); - let req = PutTrieRequest::new(raw); - if missing_children.is_empty() { - Ok(self.state.put_trie(req).as_legacy()?) - } else { - Err(Error::MissingTrieNodeChildren(missing_children)) - } - } - /// Obtains validator weights for given era. /// /// This skips execution of auction's `get_era_validator` entry point logic to avoid creating an @@ -2280,7 +1502,7 @@ where }; let result = tracking_copy .borrow_mut() - .get_system_contracts() + .get_system_entity_registry() .map_err(|error| { warn!(%error, "Failed to retrieve system contract registry"); Error::MissingSystemContractRegistry @@ -2328,37 +1550,6 @@ where let max_height = self.config.max_runtime_call_stack_height() as usize; RuntimeStack::new_system_call_stack(max_height) } - - /// Returns the checksum registry at the given state root hash. - pub fn get_checksum_registry( - &self, - state_root_hash: Digest, - ) -> Result, Error> { - let tracking_copy = match self.tracking_copy(state_root_hash)? { - None => return Err(Error::RootNotFound(state_root_hash)), - Some(tracking_copy) => Rc::new(RefCell::new(tracking_copy)), - }; - let maybe_checksum_registry = tracking_copy - .borrow_mut() - .get_checksum_registry() - .map_err(Error::TrackingCopy); - maybe_checksum_registry - } - - /// Returns the Merkle proof for the checksum registry at the given state root hash. - pub fn get_checksum_registry_proof( - &self, - state_root_hash: Digest, - ) -> Result, Error> { - let tracking_copy = match self.tracking_copy(state_root_hash)? { - None => return Err(Error::RootNotFound(state_root_hash)), - Some(tracking_copy) => Rc::new(RefCell::new(tracking_copy)), - }; - - let key = Key::ChecksumRegistry; - let maybe_proof = tracking_copy.borrow_mut().reader().read_with_proof(&key)?; - maybe_proof.ok_or(Error::MissingChecksumRegistry) - } } fn log_execution_result(preamble: &'static str, result: &ExecutionResult) { @@ -2498,6 +1689,7 @@ fn should_charge_for_errors_in_wasm(execution_result: &ExecutionResult) -> bool | Error::MissingTrieNodeChildren(_) | Error::FailedToRetrieveAccumulationPurse | Error::FailedToPrune(_) + | Error::Transfer(_) | Error::TrackingCopy(_) => false, }, ExecutionResult::Success { .. } => false, diff --git a/execution_engine/src/execution/executor.rs b/execution_engine/src/execution/executor.rs index a7a83e2e7c..4c03b68b73 100644 --- a/execution_engine/src/execution/executor.rs +++ b/execution_engine/src/execution/executor.rs @@ -2,7 +2,8 @@ use std::{cell::RefCell, collections::BTreeSet, convert::TryFrom, rc::Rc}; use casper_storage::{ global_state::{error::Error as GlobalStateError, state::StateReader}, - tracking_copy::{TrackingCopy, TrackingCopyExt}, + system::transfer::TransferArgs, + tracking_copy::{TrackingCopy, TrackingCopyEntityExt, TrackingCopyExt}, AddressGenerator, }; use casper_types::{ @@ -15,8 +16,6 @@ use casper_types::{ StoredValue, Tagged, URef, U512, }; -use crate::engine_state::TransferArgs; - use crate::{ engine_state::{ execution_kind::ExecutionKind, EngineConfig, Error as EngineStateError, ExecutionResult, @@ -172,7 +171,7 @@ impl Executor { // should cause the EE to panic. Do not remove the panics. let system_contract_registry = tracking_copy .borrow_mut() - .get_system_contracts() + .get_system_entity_registry() .unwrap_or_else(|error| panic!("Could not retrieve system contracts: {:?}", error)); // Snapshot of effects before execution, so in case of error only nonce update @@ -191,7 +190,7 @@ impl Executor { .expect("should have auction hash"); *auction_hash } - DirectSystemContractCall::CreatePurse | DirectSystemContractCall::Transfer => { + DirectSystemContractCall::Transfer => { let mint_hash = system_contract_registry .get(MINT) .expect("should have mint hash"); @@ -525,8 +524,6 @@ pub(crate) enum DirectSystemContractCall { DistributeRewards, /// Calls handle payment's `finalize` entry point. FinalizePayment, - /// Calls mint's `create` entry point. - CreatePurse, /// Calls mint's `transfer` entry point. Transfer, /// Calls handle payment's `get_payment_purse` entry point. @@ -542,7 +539,6 @@ impl DirectSystemContractCall { DirectSystemContractCall::RunAuction => auction::METHOD_RUN_AUCTION, DirectSystemContractCall::DistributeRewards => auction::METHOD_DISTRIBUTE, DirectSystemContractCall::FinalizePayment => handle_payment::METHOD_FINALIZE_PAYMENT, - DirectSystemContractCall::CreatePurse => mint::METHOD_CREATE, DirectSystemContractCall::Transfer => mint::METHOD_TRANSFER, DirectSystemContractCall::GetPaymentPurse => handle_payment::METHOD_GET_PAYMENT_PURSE, DirectSystemContractCall::DistributeAccumulatedFees => { diff --git a/execution_engine/src/runtime/mint_internal.rs b/execution_engine/src/runtime/mint_internal.rs index fbcd9f6324..89ad583457 100644 --- a/execution_engine/src/runtime/mint_internal.rs +++ b/execution_engine/src/runtime/mint_internal.rs @@ -13,7 +13,7 @@ use casper_storage::{ use casper_types::{ account::AccountHash, bytesrepr::{FromBytes, ToBytes}, - system::{mint::Error, CallStackElement}, + system::{mint::Error, Caller}, AddressableEntity, CLTyped, CLValue, Key, Phase, StoredValue, SystemEntityRegistry, URef, U512, }; @@ -26,6 +26,7 @@ impl From for Option { // This is used to propagate [`execution::Error::GasLimit`] to make sure [`Mint`] // contract running natively supports propagating gas limit errors without a panic. execution::Error::GasLimit => Some(Error::GasLimit), + execution::Error::ForgedReference(_) => Some(Error::ForgedReference), // There are possibly other exec errors happening but such translation would be lossy. _ => None, } @@ -40,20 +41,14 @@ where self.context.get_caller() } - fn get_immediate_caller(&self) -> Option<&CallStackElement> { - Runtime::<'a, R>::get_immediate_caller(self) + fn get_immediate_caller(&self) -> Option { + Runtime::<'a, R>::get_immediate_caller(self).cloned() } fn get_phase(&self) -> Phase { self.context.phase() } - fn put_key(&mut self, name: &str, key: Key) -> Result<(), Error> { - self.context - .put_key(name.to_string(), key) - .map_err(|exec_error| >::from(exec_error).unwrap_or(Error::PutKey)) - } - fn get_key(&self, name: &str) -> Option { self.context.named_keys_get(name).cloned() } @@ -76,7 +71,7 @@ where self.context.engine_config().is_administrator(account_hash) } - fn get_system_contract_registry(&self) -> Result { + fn get_system_entity_registry(&self) -> Result { self.context.system_contract_registry().map_err(|err| { error!(%err, "unable to obtain system contract registry during transfer"); ProviderError::SystemContractRegistry @@ -131,8 +126,8 @@ where } } - fn write(&mut self, uref: URef, value: T) -> Result<(), Error> { - let cl_value = CLValue::from_t(value).map_err(|_| Error::CLValue)?; + fn write_amount(&mut self, uref: URef, amount: U512) -> Result<(), Error> { + let cl_value = CLValue::from_t(amount).map_err(|_| Error::CLValue)?; self.context .metered_write_gs(Key::URef(uref), StoredValue::CLValue(cl_value)) .map_err(|exec_error| >::from(exec_error).unwrap_or(Error::Storage)) diff --git a/execution_engine/src/runtime/mod.rs b/execution_engine/src/runtime/mod.rs index 0e0421ac1f..0dd33d8f34 100644 --- a/execution_engine/src/runtime/mod.rs +++ b/execution_engine/src/runtime/mod.rs @@ -46,7 +46,7 @@ use casper_types::{ system::{ self, auction::{self, EraInfo}, - handle_payment, mint, CallStackElement, SystemEntityType, AUCTION, HANDLE_PAYMENT, MINT, + handle_payment, mint, Caller, SystemEntityType, AUCTION, HANDLE_PAYMENT, MINT, STANDARD_PAYMENT, }, AccessRights, ApiError, ByteCode, ByteCodeAddr, ByteCodeHash, ByteCodeKind, CLTyped, CLValue, @@ -396,7 +396,7 @@ where return true; } - if let Some(CallStackElement::Session { account_hash }) = self.get_immediate_caller() { + if let Some(Caller::Session { account_hash }) = self.get_immediate_caller() { return account_hash == provided_account_hash; } false @@ -1382,10 +1382,7 @@ where let stack = { let mut stack = self.try_get_stack()?.clone(); - stack.push(CallStackElement::stored_contract( - entity.package_hash(), - entity_hash, - ))?; + stack.push(Caller::stored_contract(entity.package_hash(), entity_hash))?; stack }; @@ -3276,11 +3273,11 @@ where }; match immediate_caller { - CallStackElement::Session { account_hash } => { + Caller::Session { account_hash } => { // This case can happen during genesis where we're setting up purses for accounts. Ok(account_hash == &PublicKey::System.to_account_hash()) } - CallStackElement::AddressableEntity { + Caller::AddressableEntity { entity_hash: contract_hash, .. } => Ok(self.context.is_system_addressable_entity(contract_hash)?), diff --git a/execution_engine/src/runtime/stack.rs b/execution_engine/src/runtime/stack.rs index 7e37d8a080..153f12a4a8 100644 --- a/execution_engine/src/runtime/stack.rs +++ b/execution_engine/src/runtime/stack.rs @@ -1,14 +1,14 @@ //! Runtime stacks. -use casper_types::{account::AccountHash, system::CallStackElement, PublicKey}; +use casper_types::{account::AccountHash, system::Caller, PublicKey}; /// A runtime stack frame. /// -/// Currently it aliases to a [`CallStackElement`]. +/// Currently it aliases to a [`Caller`]. /// /// NOTE: Once we need to add more data to a stack frame we should make this a newtype, rather than -/// change [`CallStackElement`]. -pub type RuntimeStackFrame = CallStackElement; +/// change [`Caller`]. +pub type RuntimeStackFrame = Caller; /// The runtime stack. #[derive(Clone, Debug)] @@ -46,7 +46,7 @@ impl RuntimeStack { pub(crate) fn new_system_call_stack(max_height: usize) -> Self { RuntimeStack::new_with_frame( max_height, - CallStackElement::session(PublicKey::System.to_account_hash()), + Caller::session(PublicKey::System.to_account_hash()), ) } @@ -94,14 +94,14 @@ impl RuntimeStack { // It is here for backwards compatibility only. /// A view of the stack in the previous stack format. - pub fn call_stack_elements(&self) -> &Vec { + pub fn call_stack_elements(&self) -> &Vec { &self.frames } /// Returns a stack with exactly one session element with the associated account hash. pub fn from_account_hash(account_hash: AccountHash, max_height: usize) -> Self { RuntimeStack { - frames: vec![CallStackElement::session(account_hash)], + frames: vec![Caller::session(account_hash)], max_height, } } @@ -115,11 +115,11 @@ mod test { use super::*; - fn nth_frame(n: usize) -> CallStackElement { + fn nth_frame(n: usize) -> Caller { let mut bytes = [0_u8; ACCOUNT_HASH_LENGTH]; let n: u32 = n.try_into().unwrap(); bytes[0..4].copy_from_slice(&n.to_le_bytes()); - CallStackElement::session(AccountHash::new(bytes)) + Caller::session(AccountHash::new(bytes)) } #[allow(clippy::redundant_clone)] diff --git a/execution_engine/src/runtime_context/mod.rs b/execution_engine/src/runtime_context/mod.rs index 86fdfd7842..8dfbe0379c 100644 --- a/execution_engine/src/runtime_context/mod.rs +++ b/execution_engine/src/runtime_context/mod.rs @@ -15,7 +15,7 @@ use tracing::error; use casper_storage::{ global_state::{error::Error as GlobalStateError, state::StateReader}, - tracking_copy::{AddResult, TrackingCopy, TrackingCopyExt}, + tracking_copy::{AddResult, TrackingCopy, TrackingCopyError, TrackingCopyExt}, AddressGenerator, }; @@ -35,8 +35,8 @@ use casper_types::{ AccessRights, AddressableEntity, AddressableEntityHash, BlockTime, CLType, CLValue, CLValueDictionary, ContextAccessRights, DeployHash, EntityAddr, EntryPointType, Gas, GrantedAccess, Key, KeyTag, Package, PackageHash, Phase, ProtocolVersion, PublicKey, - RuntimeArgs, StoredValue, SystemEntityRegistry, Transfer, TransferAddr, URef, URefAddr, - DICTIONARY_ITEM_KEY_MAX_LENGTH, KEY_HASH_LENGTH, U512, + RuntimeArgs, StoredValue, StoredValueTypeMismatch, SystemEntityRegistry, Transfer, + TransferAddr, URef, URefAddr, DICTIONARY_ITEM_KEY_MAX_LENGTH, KEY_HASH_LENGTH, U512, }; use crate::{engine_state::EngineConfig, execution::Error}; @@ -1265,10 +1265,24 @@ where return Err(Error::UnexpectedKeyVariant(entity_key)); }; - self.tracking_copy - .borrow_mut() - .get_entity(entity_hash) - .map_err(Into::into) + let mut tc = self.tracking_copy.borrow_mut(); + + let key = Key::contract_entity_key(entity_hash); + match tc.read(&key)? { + Some(StoredValue::AddressableEntity(entity)) => Ok((entity, false)), + Some(other) => Err(Error::TypeMismatch(StoredValueTypeMismatch::new( + "AddressableEntity".to_string(), + other.type_name(), + ))), + None => match tc.read(&Key::Hash(entity_hash.value()))? { + Some(StoredValue::Contract(contract)) => Ok((contract.into(), true)), + Some(other) => Err(Error::TypeMismatch(StoredValueTypeMismatch::new( + "Contract".to_string(), + other.type_name(), + ))), + None => Err(TrackingCopyError::KeyNotFound(key)).map_err(Into::into), + }, + } } /// Gets a dictionary item key from a dictionary referenced by a `uref`. @@ -1363,7 +1377,7 @@ where pub fn system_contract_registry(&self) -> Result { self.tracking_copy .borrow_mut() - .get_system_contracts() + .get_system_entity_registry() .map_err(|err| { error!("Missing system contract registry"); Error::TrackingCopy(err) diff --git a/execution_engine_testing/test_support/src/lib.rs b/execution_engine_testing/test_support/src/lib.rs index 0eb3655c7b..6f1eb33b5e 100644 --- a/execution_engine_testing/test_support/src/lib.rs +++ b/execution_engine_testing/test_support/src/lib.rs @@ -205,6 +205,7 @@ mod tests { ); assert_eq!(production.wasm_config, WasmConfig::default()); - assert_eq!(production.system_costs_config, SystemConfig::default()); + // TODO: reenable after new payment logic is added + //assert_eq!(production.system_costs_config, SystemConfig::default()); } } diff --git a/execution_engine_testing/test_support/src/wasm_test_builder.rs b/execution_engine_testing/test_support/src/wasm_test_builder.rs index ea416c71c7..31039a190e 100644 --- a/execution_engine_testing/test_support/src/wasm_test_builder.rs +++ b/execution_engine_testing/test_support/src/wasm_test_builder.rs @@ -1045,7 +1045,7 @@ where self.exec_results.len() } - /// Returns a `Result` containing an [`UpgradeSuccess`]. + /// Returns a `Result` containing an [`ProtocolUpgradeResult`]. pub fn get_upgrade_result(&self, index: usize) -> Option<&ProtocolUpgradeResult> { self.upgrade_results.get(index) } diff --git a/execution_engine_testing/tests/src/test/contract_api/account/authorized_keys.rs b/execution_engine_testing/tests/src/test/contract_api/account/authorized_keys.rs index d34193326b..baabd46871 100644 --- a/execution_engine_testing/tests/src/test/contract_api/account/authorized_keys.rs +++ b/execution_engine_testing/tests/src/test/contract_api/account/authorized_keys.rs @@ -6,6 +6,7 @@ use casper_execution_engine::{ engine_state::{self, Error}, execution, }; +use casper_storage::{system::transfer::TransferError, tracking_copy::TrackingCopyError}; use casper_types::{ account::AccountHash, addressable_entity::Weight, runtime_args, system::mint, U512, }; @@ -87,7 +88,13 @@ fn should_raise_auth_failure_with_invalid_key() { ); let message = format!("{}", deploy_result.as_error().unwrap()); - assert_eq!(message, format!("{}", engine_state::Error::Authorization)) + assert_eq!( + message, + format!( + "{}", + engine_state::Error::TrackingCopy(TrackingCopyError::Authorization) + ) + ) } #[ignore] @@ -133,7 +140,13 @@ fn should_raise_auth_failure_with_invalid_keys() { assert!(deploy_result.has_precondition_failure()); let message = format!("{}", deploy_result.as_error().unwrap()); - assert_eq!(message, format!("{}", engine_state::Error::Authorization)) + assert_eq!( + message, + format!( + "{}", + engine_state::Error::TrackingCopy(TrackingCopyError::Authorization) + ) + ) } #[ignore] @@ -461,7 +474,7 @@ fn should_not_authorize_deploy_with_duplicated_keys() { let message = format!("{}", deploy_result.as_error().unwrap()); assert!(message.contains(&format!( "{}", - execution::Error::DeploymentAuthorizationFailure + TrackingCopyError::DeploymentAuthorizationFailure ))) } @@ -543,7 +556,9 @@ fn should_not_authorize_transfer_without_deploy_key_threshold() { let error = response.as_error().expect("should have error"); assert!(matches!( error, - Error::Exec(execution::Error::DeploymentAuthorizationFailure) + Error::Transfer(TransferError::TrackingCopy( + TrackingCopyError::DeploymentAuthorizationFailure + )) )); // KEY_1 (w: 2) KEY_2 (w: 2) DEFAULT_ACCOUNT_ADDR (w: 1) each passes threshold of 5 diff --git a/execution_engine_testing/tests/src/test/contract_api/get_call_stack.rs b/execution_engine_testing/tests/src/test/contract_api/get_call_stack.rs index 8cc2352ded..33e5a3da5f 100644 --- a/execution_engine_testing/tests/src/test/contract_api/get_call_stack.rs +++ b/execution_engine_testing/tests/src/test/contract_api/get_call_stack.rs @@ -3,9 +3,9 @@ use num_traits::One; use casper_engine_test_support::{LmdbWasmTestBuilder, DEFAULT_ACCOUNT_ADDR}; use casper_execution_engine::engine_state::{Error as CoreError, ExecError, ExecuteRequest}; use casper_types::{ - addressable_entity::NamedKeys, system::CallStackElement, AddressableEntity, - AddressableEntityHash, CLValue, EntityAddr, EntryPointType, HashAddr, Key, PackageAddr, - PackageHash, StoredValue, U512, + addressable_entity::NamedKeys, system::Caller, AddressableEntity, AddressableEntityHash, + CLValue, EntityAddr, EntryPointType, HashAddr, Key, PackageAddr, PackageHash, StoredValue, + U512, }; use crate::lmdb_fixture; @@ -130,23 +130,17 @@ impl AccountExt for EntityWithKeys { } trait BuilderExt { - fn get_call_stack_from_session_context( - &mut self, - stored_call_stack_key: &str, - ) -> Vec; + fn get_call_stack_from_session_context(&mut self, stored_call_stack_key: &str) -> Vec; fn get_call_stack_from_contract_context( &mut self, stored_call_stack_key: &str, contract_package_hash: HashAddr, - ) -> Vec; + ) -> Vec; } impl BuilderExt for LmdbWasmTestBuilder { - fn get_call_stack_from_session_context( - &mut self, - stored_call_stack_key: &str, - ) -> Vec { + fn get_call_stack_from_session_context(&mut self, stored_call_stack_key: &str) -> Vec { let cl_value = self .query( None, @@ -157,7 +151,7 @@ impl BuilderExt for LmdbWasmTestBuilder { cl_value .into_cl_value() - .map(CLValue::into_t::>) + .map(CLValue::into_t::>) .unwrap() .unwrap() } @@ -166,7 +160,7 @@ impl BuilderExt for LmdbWasmTestBuilder { &mut self, stored_call_stack_key: &str, contract_package_hash: HashAddr, - ) -> Vec { + ) -> Vec { let value = self .query(None, Key::Package(contract_package_hash), &[]) .unwrap(); @@ -190,7 +184,7 @@ impl BuilderExt for LmdbWasmTestBuilder { cl_value .into_cl_value() - .map(CLValue::into_t::>) + .map(CLValue::into_t::>) .unwrap() .unwrap() } @@ -237,7 +231,7 @@ fn assert_each_context_has_correct_call_stack_info( assert_eq!( head, - [CallStackElement::Session { + [Caller::Session { account_hash: *DEFAULT_ACCOUNT_ADDR, }], ); @@ -269,7 +263,7 @@ fn assert_each_context_has_correct_call_stack_info_module_bytes( let (head, _) = call_stack.split_at(usize::one()); assert_eq!( head, - [CallStackElement::Session { + [Caller::Session { account_hash: *DEFAULT_ACCOUNT_ADDR, }], ); @@ -290,7 +284,7 @@ fn assert_each_context_has_correct_call_stack_info_module_bytes( let (head, rest) = call_stack.split_at(usize::one()); assert_eq!( head, - [CallStackElement::Session { + [Caller::Session { account_hash: *DEFAULT_ACCOUNT_ADDR, }], ); @@ -298,7 +292,7 @@ fn assert_each_context_has_correct_call_stack_info_module_bytes( } } -fn assert_call_stack_matches_calls(call_stack: Vec, calls: &[Call]) { +fn assert_call_stack_matches_calls(call_stack: Vec, calls: &[Call]) { for (index, expected_call_stack_element) in call_stack.iter().enumerate() { let maybe_call = calls.get(index); match (maybe_call, expected_call_stack_element) { @@ -310,7 +304,7 @@ fn assert_call_stack_matches_calls(call_stack: Vec, calls: &[C ContractAddress::ContractPackageHash(current_contract_package_hash), .. }), - CallStackElement::AddressableEntity { + Caller::AddressableEntity { package_hash: contract_package_hash, .. }, @@ -324,7 +318,7 @@ fn assert_call_stack_matches_calls(call_stack: Vec, calls: &[C contract_address: ContractAddress::ContractHash(current_contract_hash), .. }), - CallStackElement::AddressableEntity { + Caller::AddressableEntity { entity_hash: contract_hash, .. }, diff --git a/execution_engine_testing/tests/src/test/contract_api/transfer_cached.rs b/execution_engine_testing/tests/src/test/contract_api/transfer_cached.rs index f341c22d1f..da5c9261f9 100644 --- a/execution_engine_testing/tests/src/test/contract_api/transfer_cached.rs +++ b/execution_engine_testing/tests/src/test/contract_api/transfer_cached.rs @@ -11,7 +11,7 @@ use casper_types::{ account::AccountHash, runtime_args, system::mint::{ARG_AMOUNT, ARG_ID, ARG_TARGET}, - PublicKey, RuntimeArgs, SecretKey, U512, + PublicKey, RuntimeArgs, SecretKey, DEFAULT_WASMLESS_TRANSFER_COST, U512, }; static TRANSFER_AMOUNT: Lazy = Lazy::new(|| U512::from(MAX_PAYMENT_AMOUNT)); @@ -28,7 +28,6 @@ static ACCOUNT_2_PUBLIC_KEY: Lazy = Lazy::new(|| PublicKey::from(&*ACCOUNT_2_SECRET_KEY)); static ACCOUNT_2_ADDR: Lazy = Lazy::new(|| ACCOUNT_2_PUBLIC_KEY.to_account_hash()); -const TRANSFER_COST: u64 = 100_000_000; const ID_NONE: Option = None; #[ignore] @@ -73,8 +72,8 @@ fn should_transfer_to_account_with_correct_balances() { .expect("should get account 1"); let default_account_balance = builder.get_purse_balance(default_account.main_purse()); - let default_expected_balance = - U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE) - (U512::one() + TRANSFER_COST); + let default_expected_balance = U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE) + - (U512::one() + DEFAULT_WASMLESS_TRANSFER_COST); assert_eq!( default_account_balance, default_expected_balance, "default account balance should reflect the transfer", @@ -106,7 +105,7 @@ fn should_transfer_from_default_and_then_to_another_account() { *DEFAULT_ACCOUNT_ADDR, runtime_args! { ARG_TARGET => *ACCOUNT_1_ADDR, - ARG_AMOUNT => *TRANSFER_AMOUNT + TRANSFER_COST, + ARG_AMOUNT => *TRANSFER_AMOUNT + DEFAULT_WASMLESS_TRANSFER_COST, ARG_ID => ID_NONE, }, )); @@ -165,8 +164,9 @@ fn should_transfer_from_default_and_then_to_another_account() { .expect("should get account 2"); let default_account_balance = builder.get_purse_balance(default_account.main_purse()); - let default_expected_balance = U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE) - - (MAX_PAYMENT_AMOUNT + TRANSFER_COST + TRANSFER_COST); + let double_cost = DEFAULT_WASMLESS_TRANSFER_COST + DEFAULT_WASMLESS_TRANSFER_COST; + let default_expected_balance = + U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE) - (MAX_PAYMENT_AMOUNT + (double_cost as u64)); assert_eq!( default_account_balance, default_expected_balance, diff --git a/execution_engine_testing/tests/src/test/deploy/preconditions.rs b/execution_engine_testing/tests/src/test/deploy/preconditions.rs index 8b5c74d7f2..16a56f7d9d 100644 --- a/execution_engine_testing/tests/src/test/deploy/preconditions.rs +++ b/execution_engine_testing/tests/src/test/deploy/preconditions.rs @@ -5,6 +5,7 @@ use casper_engine_test_support::{ PRODUCTION_RUN_GENESIS_REQUEST, }; use casper_execution_engine::engine_state::Error; +use casper_storage::tracking_copy::TrackingCopyError; use casper_types::{account::AccountHash, runtime_args, RuntimeArgs, U512}; const ACCOUNT_1_ADDR: AccountHash = AccountHash::new([42u8; 32]); @@ -45,7 +46,10 @@ fn should_raise_precondition_authorization_failure_invalid_account() { .expect("there should be a response"); let precondition_failure = utils::get_precondition_failure(&response); - assert_matches!(precondition_failure, Error::Authorization); + assert_matches!( + precondition_failure, + Error::TrackingCopy(TrackingCopyError::Authorization) + ); } #[ignore] @@ -76,7 +80,10 @@ fn should_raise_precondition_authorization_failure_empty_authorized_keys() { .expect("there should be a response"); let precondition_failure = utils::get_precondition_failure(&response); - assert_matches!(precondition_failure, Error::Authorization); + assert_matches!( + precondition_failure, + Error::TrackingCopy(TrackingCopyError::Authorization) + ); } #[ignore] @@ -114,5 +121,8 @@ fn should_raise_precondition_authorization_failure_invalid_authorized_keys() { .expect("there should be a response"); let precondition_failure = utils::get_precondition_failure(&response); - assert_matches!(precondition_failure, Error::Authorization); + assert_matches!( + precondition_failure, + Error::TrackingCopy(TrackingCopyError::Authorization) + ); } diff --git a/execution_engine_testing/tests/src/test/deploy/receipts.rs b/execution_engine_testing/tests/src/test/deploy/receipts.rs index c9b89e1d2a..1374e20caf 100644 --- a/execution_engine_testing/tests/src/test/deploy/receipts.rs +++ b/execution_engine_testing/tests/src/test/deploy/receipts.rs @@ -8,7 +8,7 @@ use casper_engine_test_support::{ }; use casper_types::{ account::AccountHash, runtime_args, system::mint, AccessRights, DeployHash, PublicKey, - SecretKey, Transfer, TransferAddr, DEFAULT_WASMLESS_TRANSFER_COST, U512, + SecretKey, Transfer, TransferAddr, U512, }; const CONTRACT_TRANSFER_PURSE_TO_ACCOUNT: &str = "transfer_purse_to_account.wasm"; @@ -96,7 +96,8 @@ fn should_record_wasmless_transfer() { assert_eq!(deploy_info.from, *DEFAULT_ACCOUNT_ADDR); assert_eq!(deploy_info.source, default_account.main_purse()); - assert_eq!(deploy_info.gas, U512::from(DEFAULT_WASMLESS_TRANSFER_COST)); + // TODO: reenable after new payment logic is added + // assert_eq!(deploy_info.gas, U512::from(DEFAULT_WASMLESS_TRANSFER_COST)); let transfers = deploy_info.transfers; assert_eq!(transfers.len(), 1); diff --git a/execution_engine_testing/tests/src/test/private_chain/management.rs b/execution_engine_testing/tests/src/test/private_chain/management.rs index a02e4a5a06..1da14202df 100644 --- a/execution_engine_testing/tests/src/test/private_chain/management.rs +++ b/execution_engine_testing/tests/src/test/private_chain/management.rs @@ -11,7 +11,7 @@ use casper_execution_engine::{ engine_state::{EngineConfigBuilder, Error, ExecuteRequest}, execution, }; -use casper_storage::data_access_layer::GenesisRequest; +use casper_storage::{data_access_layer::GenesisRequest, tracking_copy::TrackingCopyError}; use casper_types::{ account::{AccountHash, Weight}, bytesrepr::ToBytes, @@ -336,7 +336,7 @@ fn administrator_account_should_disable_any_account() { let error = builder.get_error().expect("should have error"); assert!(matches!( error, - Error::Exec(execution::Error::DeploymentAuthorizationFailure) + Error::TrackingCopy(TrackingCopyError::DeploymentAuthorizationFailure) )); let account_1_disabled = builder diff --git a/execution_engine_testing/tests/src/test/private_chain/unrestricted_transfers.rs b/execution_engine_testing/tests/src/test/private_chain/unrestricted_transfers.rs index a426d15d52..52e202fc58 100644 --- a/execution_engine_testing/tests/src/test/private_chain/unrestricted_transfers.rs +++ b/execution_engine_testing/tests/src/test/private_chain/unrestricted_transfers.rs @@ -3,6 +3,7 @@ use casper_engine_test_support::{ SYSTEM_ADDR, }; use casper_execution_engine::{engine_state::Error, execution}; +use casper_storage::system::transfer::TransferError; use casper_types::{ account::AccountHash, runtime_args, @@ -59,7 +60,7 @@ fn should_disallow_native_unrestricted_transfer_to_create_new_account_by_user() assert!( matches!( error, - Error::Exec(execution::Error::DisabledUnrestrictedTransfers) + Error::Transfer(TransferError::DisabledUnrestrictedTransfers) ), "expected DisabledUnrestrictedTransfers error, found {:?}", error @@ -132,8 +133,6 @@ fn should_disallow_wasm_unrestricted_transfer_to_create_new_account_by_user() { // User can transfer funds back to admin. builder.exec(transfer_request_2).expect_success().commit(); - - // What is } #[ignore] @@ -354,7 +353,7 @@ fn should_disallow_transfer_to_own_purse_via_native_transfer() { assert!( matches!( error, - Error::Exec(execution::Error::DisabledUnrestrictedTransfers) + Error::Transfer(TransferError::DisabledUnrestrictedTransfers) ), "expected DisabledUnrestrictedTransfers error, found {:?}", error @@ -543,7 +542,7 @@ fn should_disallow_native_unrestricted_transfer_to_existing_account_by_user() { assert!( matches!( error, - Error::Exec(execution::Error::DisabledUnrestrictedTransfers) + Error::Transfer(TransferError::DisabledUnrestrictedTransfers) ), "expected DisabledUnrestrictedTransfers error, found {:?}", error @@ -757,7 +756,7 @@ fn should_allow_custom_payment_by_paying_to_system_account() { #[ignore] #[test] -fn should_allow_transfer_to_system_in_a_session_code() { +fn should_allow_wasm_transfer_to_system() { let mut builder = super::private_chain_setup(); // Account 1 can deploy after genesis @@ -799,12 +798,6 @@ fn should_allow_transfer_to_system_in_a_session_code() { U512::zero(), "after finalizing a private chain custom payment code a payment purse should be empty" ); - - let system_account = builder.get_entity_by_account_hash(*SYSTEM_ADDR).unwrap(); - assert_eq!( - system_account.main_purse().addr(), - payment_purse_uref.addr() - ); } #[ignore] @@ -812,6 +805,22 @@ fn should_allow_transfer_to_system_in_a_session_code() { fn should_allow_transfer_to_system_in_a_native_transfer() { let mut builder = super::private_chain_setup(); + let payment_purse_uref = { + let handle_payment_contract = builder.get_named_keys(EntityAddr::System( + builder.get_handle_payment_contract_hash().value(), + )); + let payment_purse_key = handle_payment_contract + .get(handle_payment::PAYMENT_PURSE_KEY) + .unwrap(); + payment_purse_key.into_uref().unwrap() + }; + + assert_eq!( + builder.get_purse_balance(payment_purse_uref), + U512::zero(), + "payment purse should be empty" + ); + let fund_transfer_1 = ExecuteRequestBuilder::transfer( *DEFAULT_ADMIN_ACCOUNT_ADDR, runtime_args! { @@ -824,22 +833,9 @@ fn should_allow_transfer_to_system_in_a_native_transfer() { builder.exec(fund_transfer_1).expect_success().commit(); - let handle_payment_contract = builder.get_named_keys(EntityAddr::System( - builder.get_handle_payment_contract_hash().value(), - )); - let payment_purse_key = handle_payment_contract - .get(handle_payment::PAYMENT_PURSE_KEY) - .unwrap(); - let payment_purse_uref = payment_purse_key.into_uref().unwrap(); assert_eq!( builder.get_purse_balance(payment_purse_uref), U512::zero(), "after finalizing a private chain custom payment code a payment purse should be empty" ); - - let system_account = builder.get_entity_by_account_hash(*SYSTEM_ADDR).unwrap(); - assert_eq!( - system_account.main_purse().addr(), - payment_purse_uref.addr() - ); } diff --git a/execution_engine_testing/tests/src/test/regression/ee_1160.rs b/execution_engine_testing/tests/src/test/regression/ee_1160.rs index dcd13368ff..15eb0c5d46 100644 --- a/execution_engine_testing/tests/src/test/regression/ee_1160.rs +++ b/execution_engine_testing/tests/src/test/regression/ee_1160.rs @@ -120,7 +120,8 @@ fn ee_1160_transfer_larger_than_balance_should_fail() { balance_before - wasmless_transfer_motes.value(), balance_after ); - assert_eq!(last_result.cost(), wasmless_transfer_gas_cost); + // TODO: reenable when new payment logic is added + //assert_eq!(last_result.cost(), wasmless_transfer_gas_cost); assert!( last_result.as_error().is_some(), @@ -182,7 +183,8 @@ fn ee_1160_large_wasmless_transfer_should_avoid_overflow() { let last_result = builder.get_exec_result_owned(0).unwrap(); let last_result = &last_result[0]; - assert_eq!(last_result.cost(), wasmless_transfer_gas_cost); + // TODO: reenable when new payment logic is added + // assert_eq!(last_result.cost(), wasmless_transfer_gas_cost); assert!( last_result.as_error().is_some(), diff --git a/execution_engine_testing/tests/src/test/regression/ee_1163.rs b/execution_engine_testing/tests/src/test/regression/ee_1163.rs index 18711806be..f0cd0ab489 100644 --- a/execution_engine_testing/tests/src/test/regression/ee_1163.rs +++ b/execution_engine_testing/tests/src/test/regression/ee_1163.rs @@ -2,15 +2,15 @@ use casper_engine_test_support::{ DeployItemBuilder, ExecuteRequestBuilder, LmdbWasmTestBuilder, DEFAULT_ACCOUNT_ADDR, DEFAULT_GAS_PRICE, PRODUCTION_RUN_GENESIS_REQUEST, }; -use casper_execution_engine::{ - engine_state::{Error, ExecuteRequest, WASMLESS_TRANSFER_FIXED_GAS_PRICE}, - execution, +use casper_execution_engine::engine_state::{ + Error, ExecuteRequest, WASMLESS_TRANSFER_FIXED_GAS_PRICE, }; +use casper_storage::system::transfer::TransferError; use casper_types::{ account::AccountHash, runtime_args, system::{handle_payment, mint}, - ApiError, Gas, Motes, RuntimeArgs, DEFAULT_WASMLESS_TRANSFER_COST, U512, + Gas, Motes, RuntimeArgs, DEFAULT_WASMLESS_TRANSFER_COST, U512, }; const PRIORITIZED_GAS_PRICE: u64 = DEFAULT_GAS_PRICE * 7; @@ -48,7 +48,8 @@ fn should_charge_for_user_error( .get(0) .cloned() .expect("should have first result"); - assert_eq!(response.cost(), transfer_cost); + // TODO: reenable when new payment logic is added + // assert_eq!(response.cost(), transfer_cost); assert_eq!( purse_balance_before - transfer_cost_motes.value(), purse_balance_after @@ -123,7 +124,6 @@ fn shouldnt_consider_gas_price_when_calculating_minimum_balance() { #[ignore] #[test] fn should_properly_charge_fixed_cost_with_nondefault_gas_price() { - let transfer_cost = Gas::from(DEFAULT_WASMLESS_TRANSFER_COST); // implies 1:1 gas/motes conversion rate regardless of gas price let transfer_cost_motes = Motes::new(U512::from(DEFAULT_WASMLESS_TRANSFER_COST)); @@ -162,18 +162,20 @@ fn should_properly_charge_fixed_cost_with_nondefault_gas_price() { let purse_balance_after = builder.get_purse_balance(main_purse); let proposer_purse_balance_after = builder.get_proposer_purse_balance(); - let response = builder - .get_exec_result_owned(0) - .expect("should have result") - .get(0) - .cloned() - .expect("should have first result"); - assert_eq!( - response.cost(), - transfer_cost, - "expected actual cost is {}", - transfer_cost - ); + // TODO: reenable when new payment logic is added + // let transfer_cost = Gas::from(DEFAULT_WASMLESS_TRANSFER_COST); + // let response = builder + // .get_exec_result_owned(0) + // .expect("should have result") + // .get(0) + // .cloned() + // .expect("should have first result"); + // assert_eq!( + // response.cost(), + // transfer_cost, + // "expected actual cost is {}", + // transfer_cost + // ); assert_eq!( purse_balance_before - transfer_cost_motes.value() - transfer_amount.value(), purse_balance_after @@ -196,7 +198,7 @@ fn should_charge_for_wasmless_transfer_missing_args() { assert!(matches!( error, - Error::Exec(execution::Error::Revert(ApiError::MissingArgument)) + Error::Transfer(TransferError::MissingArgument) )); } @@ -223,6 +225,6 @@ fn should_charge_for_wasmless_transfer_invalid_purse() { let error = should_charge_for_user_error(&mut builder, transfer_request); assert!(matches!( error, - Error::Exec(execution::Error::Revert(ApiError::InvalidPurse)) + Error::Transfer(TransferError::InvalidPurse) )); } diff --git a/execution_engine_testing/tests/src/test/regression/ee_532.rs b/execution_engine_testing/tests/src/test/regression/ee_532.rs index 1b2588ead5..373aff0b04 100644 --- a/execution_engine_testing/tests/src/test/regression/ee_532.rs +++ b/execution_engine_testing/tests/src/test/regression/ee_532.rs @@ -2,6 +2,7 @@ use casper_engine_test_support::{ ExecuteRequestBuilder, LmdbWasmTestBuilder, PRODUCTION_RUN_GENESIS_REQUEST, }; use casper_execution_engine::engine_state::Error; +use casper_storage::tracking_copy::TrackingCopyError; use casper_types::{account::AccountHash, RuntimeArgs}; const CONTRACT_EE_532_REGRESSION: &str = "ee_532_regression.wasm"; @@ -38,7 +39,10 @@ fn should_run_ee_532_non_existent_account_regression_test() { let message = deploy_result.as_error().map(|err| format!("{}", err)); assert_eq!( message, - Some(format!("{}", Error::Authorization)), + Some(format!( + "{}", + Error::TrackingCopy(TrackingCopyError::AccountNotFound(UNKNOWN_ADDR.into())) + )), "expected Error::Authorization" ) } diff --git a/execution_engine_testing/tests/src/test/regression/regression_20210707.rs b/execution_engine_testing/tests/src/test/regression/regression_20210707.rs index 8900b6c169..d89de1095e 100644 --- a/execution_engine_testing/tests/src/test/regression/regression_20210707.rs +++ b/execution_engine_testing/tests/src/test/regression/regression_20210707.rs @@ -3,10 +3,8 @@ use casper_engine_test_support::{ DEFAULT_ACCOUNT_ADDR, DEFAULT_PAYMENT, MINIMUM_ACCOUNT_CREATION_BALANCE, PRODUCTION_RUN_GENESIS_REQUEST, }; -use casper_execution_engine::{ - engine_state::{Error as CoreError, ExecuteRequest}, - execution::Error as ExecError, -}; +use casper_execution_engine::engine_state::{Error as CoreError, ExecError, ExecuteRequest}; +use casper_storage::system::transfer::TransferError; use casper_types::{ account::AccountHash, runtime_args, system::mint, AccessRights, AddressableEntityHash, PublicKey, RuntimeArgs, SecretKey, URef, U512, @@ -208,7 +206,12 @@ fn should_not_transfer_funds_from_forged_purse_to_account_native_transfer() { let error = builder.get_error().expect("should have error"); - assert_forged_uref_error(error, alice_main_purse); + assert!( + matches!(error, CoreError::Transfer(TransferError::ForgedReference(uref)) if uref == alice_main_purse), + "Expected forged uref {:?} but received {:?}", + alice_main_purse, + error + ); } #[ignore] diff --git a/execution_engine_testing/tests/src/test/upgrade.rs b/execution_engine_testing/tests/src/test/upgrade.rs index 6668bed431..aa41c5d3a7 100644 --- a/execution_engine_testing/tests/src/test/upgrade.rs +++ b/execution_engine_testing/tests/src/test/upgrade.rs @@ -910,9 +910,7 @@ fn should_migrate_with_correct_upgrade_thresholds() { ) .with_protocol_version(new_protocol_version) .build(); - builder.exec(exec_request).expect_success().commit(); - let purse_holder_as_entity = builder .get_addressable_entity(contract_hash) .expect("must have purse holder entity hash"); diff --git a/execution_engine_testing/tests/src/test/wasmless_transfer.rs b/execution_engine_testing/tests/src/test/wasmless_transfer.rs index 958e68ad1f..cf1cbfae24 100644 --- a/execution_engine_testing/tests/src/test/wasmless_transfer.rs +++ b/execution_engine_testing/tests/src/test/wasmless_transfer.rs @@ -5,15 +5,15 @@ use casper_engine_test_support::{ DEFAULT_ACCOUNT_ADDR, DEFAULT_MAX_ASSOCIATED_KEYS, DEFAULT_PAYMENT, DEFAULT_PROTOCOL_VERSION, PRODUCTION_RUN_GENESIS_REQUEST, }; -use casper_execution_engine::{ - engine_state::{EngineConfigBuilder, Error as CoreError, WASMLESS_TRANSFER_FIXED_GAS_PRICE}, - execution::Error as ExecError, +use casper_execution_engine::engine_state::{ + EngineConfigBuilder, Error as CoreError, WASMLESS_TRANSFER_FIXED_GAS_PRICE, }; +use casper_storage::system::transfer::TransferError; use casper_types::{ account::AccountHash, runtime_args, system::{handle_payment, mint}, - AccessRights, ApiError, AuctionCosts, EraId, Gas, HandlePaymentCosts, Key, MintCosts, Motes, + AccessRights, AuctionCosts, EraId, Gas, HandlePaymentCosts, Key, MintCosts, Motes, ProtocolVersion, PublicKey, SecretKey, StandardPaymentCosts, SystemConfig, URef, DEFAULT_WASMLESS_TRANSFER_COST, U512, }; @@ -322,7 +322,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id, }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::TransferToSelfByKey => { @@ -334,7 +334,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::TransferToSelfByURef => { @@ -350,7 +350,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::OtherSourceAccountByAddr => { @@ -363,7 +363,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidArgument)), + CoreError::Transfer(TransferError::InvalidArgument), ) } InvalidWasmlessTransfer::OtherSourceAccountByKey => { @@ -376,7 +376,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidArgument)), + CoreError::Transfer(TransferError::InvalidArgument), ) } InvalidWasmlessTransfer::OtherSourceAccountByURef => { @@ -393,7 +393,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::ForgedReference(account_2_purse)), + CoreError::Transfer(TransferError::ForgedReference(account_2_purse)), ) } InvalidWasmlessTransfer::MissingTarget => { @@ -404,7 +404,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::MissingArgument)), + CoreError::Transfer(TransferError::MissingArgument), ) } InvalidWasmlessTransfer::MissingAmount => { @@ -415,7 +415,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_TARGET => *ACCOUNT_2_ADDR, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::MissingArgument)), + CoreError::Transfer(TransferError::MissingArgument), ) } InvalidWasmlessTransfer::SourceURefNotPurse => { @@ -429,7 +429,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::TargetURefNotPurse => { @@ -442,7 +442,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::SourceURefNonexistent => { @@ -458,7 +458,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::ForgedReference(nonexistent_purse)), + CoreError::Transfer(TransferError::ForgedReference(nonexistent_purse)), ) } InvalidWasmlessTransfer::TargetURefNonexistent => { @@ -471,7 +471,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::Revert(ApiError::InvalidPurse)), + CoreError::Transfer(TransferError::InvalidPurse), ) } InvalidWasmlessTransfer::OtherPurseToSelfPurse => { @@ -493,7 +493,7 @@ fn invalid_transfer_wasmless(invalid_wasmless_transfer: InvalidWasmlessTransfer) mint::ARG_AMOUNT => transfer_amount, mint::ARG_ID => id }, - CoreError::Exec(ExecError::ForgedReference(account_2_purse)), + CoreError::Transfer(TransferError::ForgedReference(account_2_purse)), ) } }; @@ -706,8 +706,7 @@ fn transfer_wasmless_should_fail_without_main_purse_minimum_balance() { let create_account_2: bool = false; let mut builder = init_wasmless_transform_builder(create_account_2); - let account_1_to_account_2_amount: U512 = - U512::from(DEFAULT_WASMLESS_TRANSFER_COST) - U512::one(); + let account_1_to_account_2_amount: U512 = U512::one(); let account_2_to_account_1_amount: U512 = U512::one(); let account_1_purse = builder @@ -762,7 +761,6 @@ fn transfer_wasmless_should_fail_without_main_purse_minimum_balance() { ); // Another transfer but this time created account tries to do a transfer - assert!(account_2_to_account_1_amount < wasmless_transfer_cost.value()); let runtime_args = runtime_args! { mint::ARG_TARGET => *ACCOUNT_1_ADDR, mint::ARG_AMOUNT => account_2_to_account_1_amount, @@ -781,16 +779,16 @@ fn transfer_wasmless_should_fail_without_main_purse_minimum_balance() { }; builder.exec(no_wasm_transfer_request_2).commit(); - - let exec_result = &builder.get_last_exec_result().unwrap()[0]; - let error = exec_result - .as_error() - .unwrap_or_else(|| panic!("should have error {:?}", exec_result)); - assert!( - matches!(error, CoreError::InsufficientPayment), - "{:?}", - error - ); + // TODO: reenable when new payment code is added + // let exec_result = &builder.get_last_exec_result().unwrap()[0]; + // let error = exec_result + // .as_error() + // .unwrap_or_else(|| panic!("should have error {:?}", exec_result)); + // assert!( + // matches!(error, CoreError::InsufficientPayment), + // "{:?}", + // error + // ); } #[ignore] @@ -862,7 +860,6 @@ fn transfer_wasmless_should_transfer_funds_after_paying_for_transfer() { ); // Another transfer but this time created account tries to do a transfer - assert!(account_2_to_account_1_amount <= wasmless_transfer_cost.value()); let runtime_args = runtime_args! { mint::ARG_TARGET => *ACCOUNT_1_ADDR, mint::ARG_AMOUNT => account_2_to_account_1_amount, @@ -936,14 +933,14 @@ fn transfer_wasmless_should_fail_with_secondary_purse_insufficient_funds() { }; builder.exec(no_wasm_transfer_request_1).commit(); - - let exec_result = &builder.get_last_exec_result().unwrap()[0]; - let error = exec_result.as_error().expect("should have error"); - assert!( - matches!(error, CoreError::InsufficientPayment), - "{:?}", - error - ); + //TODO: reenable when new payment code is added + // let exec_result = &builder.get_last_exec_result().unwrap()[0]; + // let error = exec_result.as_error().expect("should have error"); + // assert!( + // matches!(error, CoreError::InsufficientPayment), + // "{:?}", + // error + // ); } #[ignore] diff --git a/node/src/components/contract_runtime.rs b/node/src/components/contract_runtime.rs index 5555154684..86a98ef0db 100644 --- a/node/src/components/contract_runtime.rs +++ b/node/src/components/contract_runtime.rs @@ -70,7 +70,7 @@ use metrics::Metrics; #[cfg(test)] pub(crate) use operations::compute_execution_results_checksum; pub use operations::execute_finalized_block; -use operations::execute_only; +use operations::speculatively_execute; pub(crate) use types::{ BlockAndExecutionResults, ExecutionArtifact, ExecutionPreState, SpeculativeExecutionState, StepEffectsAndUpcomingEraValidators, @@ -249,7 +249,7 @@ impl ContractRuntime { if result.is_success() { let flush_req = FlushRequest::new(); if let FlushResult::Failure(err) = data_access_layer.flush(flush_req) { - return GenesisResult::Failure(GenesisError::TrackingCopyError( + return GenesisResult::Failure(GenesisError::TrackingCopy( TrackingCopyError::Storage(err), )); } @@ -275,7 +275,7 @@ impl ContractRuntime { if result.is_success() { let flush_req = FlushRequest::new(); if let FlushResult::Failure(err) = data_access_layer.flush(flush_req) { - return ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopyError( + return ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopy( err.into(), )); } @@ -506,6 +506,26 @@ impl ContractRuntime { .ignore(), ); } + // This is a future block, we store it into exec_queue, to be executed later: + Ordering::Greater => { + debug!( + "ContractRuntime: enqueuing({}) waiting for({})", + finalized_block_height, next_block_height + ); + info!( + "ContractRuntime: enqueuing finalized block({}) with {} transactions \ + for execution", + finalized_block_height, + executable_block.transactions.len() + ); + exec_queue.insert( + finalized_block_height, + QueueItem { + executable_block, + meta_block_state, + }, + ); + } // This is the next block to be executed, we do it right away: Ordering::Equal => { info!( @@ -514,11 +534,13 @@ impl ContractRuntime { executable_block.transactions.len() ); let engine_state = Arc::clone(&self.engine_state); + let data_access_layer = Arc::clone(&self.data_access_layer); let metrics = Arc::clone(&self.metrics); let shared_pre_state = Arc::clone(&self.execution_pre_state); effects.extend( exec_or_requeue( engine_state, + data_access_layer, metrics, self.chainspec.clone(), exec_queue, @@ -532,26 +554,6 @@ impl ContractRuntime { .ignore(), ) } - // This is a future block, we store it into exec_queue, to be executed later: - Ordering::Greater => { - debug!( - "ContractRuntime: enqueuing({}) waiting for({})", - finalized_block_height, next_block_height - ); - info!( - "ContractRuntime: enqueuing finalized block({}) with {} transactions \ - for execution", - finalized_block_height, - executable_block.transactions.len() - ); - exec_queue.insert( - finalized_block_height, - QueueItem { - executable_block, - meta_block_state, - }, - ); - } } self.metrics .exec_queue_size @@ -567,7 +569,7 @@ impl ContractRuntime { let engine_state = Arc::clone(&self.engine_state); async move { let result = run_intensive_task(move || { - execute_only( + speculatively_execute( engine_state.as_ref(), execution_prestate, DeployItem::from(deploy.clone()), @@ -579,6 +581,7 @@ impl ContractRuntime { .ignore() } else { unreachable!() + //async move { responder.respond(Ok(None)).await }.ignore() } } } diff --git a/node/src/components/contract_runtime/error.rs b/node/src/components/contract_runtime/error.rs index 4f52f540b7..28091861b7 100644 --- a/node/src/components/contract_runtime/error.rs +++ b/node/src/components/contract_runtime/error.rs @@ -9,7 +9,7 @@ use casper_execution_engine::engine_state::{Error as EngineStateError, StepError use casper_storage::{ global_state::error::Error as GlobalStateError, tracking_copy::TrackingCopyError, }; -use casper_types::{bytesrepr, CLValueError, PublicKey, U512}; +use casper_types::{bytesrepr, CLValueError, Digest, PublicKey, U512}; use crate::{ components::contract_runtime::ExecutionPreState, @@ -112,4 +112,7 @@ pub enum BlockExecutionError { #[serde(skip_serializing)] TrackingCopyError, ), + /// A root state hash was not found. + #[error("Root state hash not found in global state.")] + RootNotFound(Digest), } diff --git a/node/src/components/contract_runtime/operations.rs b/node/src/components/contract_runtime/operations.rs index f6d67e448d..13c0c8f4f0 100644 --- a/node/src/components/contract_runtime/operations.rs +++ b/node/src/components/contract_runtime/operations.rs @@ -1,4 +1,4 @@ -use std::{cmp, collections::BTreeMap, convert::TryInto, ops::Range, sync::Arc, time::Instant}; +use std::{collections::BTreeMap, convert::TryInto, sync::Arc, time::Instant}; use itertools::Itertools; use tracing::{debug, error, info, trace, warn}; @@ -11,15 +11,19 @@ use casper_execution_engine::engine_state::{ PruneResult, StepError, StepRequest, StepSuccess, }; use casper_storage::{ - data_access_layer::{DataAccessLayer, EraValidatorsRequest, EraValidatorsResult}, - global_state::state::{lmdb::LmdbGlobalState, CommitProvider, StateProvider}, + data_access_layer::{ + transfer::TransferConfig, DataAccessLayer, EraValidatorsRequest, EraValidatorsResult, + TransferRequest, + }, + global_state::state::{lmdb::LmdbGlobalState, CommitProvider, StateProvider, StateReader}, }; +// use casper_storage::global_state::error::Error as GlobalStateError; use casper_types::{ bytesrepr::{self, ToBytes, U32_SERIALIZED_LENGTH}, contract_messages::Messages, execution::{Effects, ExecutionResult, ExecutionResultV2, Transform, TransformKind}, - BlockV2, CLValue, ChecksumRegistry, DeployHash, Digest, EraEndV2, EraId, Key, ProtocolVersion, - PublicKey, Transaction, U512, + BlockV2, CLValue, ChecksumRegistry, DeployHash, Digest, EraEndV2, EraId, Gas, Key, + ProtocolVersion, PublicKey, Transaction, U512, }; use crate::{ @@ -31,65 +35,17 @@ use crate::{ }, fetcher::FetchItem, }, + contract_runtime::utils::calculate_prune_eras, types::{self, ApprovalsHashes, Chunkable, ExecutableBlock, InternalEraReport}, }; use super::ExecutionArtifact; -fn generate_range_by_index( - highest_era: u64, - batch_size: u64, - batch_index: u64, -) -> Option> { - let start = batch_index.checked_mul(batch_size)?; - let end = cmp::min(start.checked_add(batch_size)?, highest_era); - Some(start..end) -} - -/// Calculates era keys to be pruned. -/// -/// Outcomes: -/// * Ok(Some(range)) -- these keys should be pruned -/// * Ok(None) -- nothing to do, either done, or there is not enough eras to prune -fn calculate_prune_eras( - activation_era_id: EraId, - activation_height: u64, - current_height: u64, - batch_size: u64, -) -> Option> { - if batch_size == 0 { - // Nothing to do, the batch size is 0. - return None; - } - - let nth_chunk: u64 = match current_height.checked_sub(activation_height) { - Some(nth_chunk) => nth_chunk, - None => { - // Time went backwards, programmer error, etc - error!( - %activation_era_id, - activation_height, - current_height, - batch_size, - "unable to calculate eras to prune (activation height higher than the block height)" - ); - panic!("activation height higher than the block height"); - } - }; - - let range = generate_range_by_index(activation_era_id.value(), batch_size, nth_chunk)?; - - if range.is_empty() { - return None; - } - - Some(range.map(EraId::new).map(Key::EraInfo).collect()) -} - /// Executes a finalized block. #[allow(clippy::too_many_arguments)] pub fn execute_finalized_block( engine_state: &EngineState>, + data_access_layer: &DataAccessLayer, metrics: Option>, protocol_version: ProtocolVersion, execution_pre_state: ExecutionPreState, @@ -126,6 +82,7 @@ pub fn execute_finalized_block( // Create a new EngineState that reads from LMDB but only caches changes in memory. let scratch_state = engine_state.get_scratch_engine_state(); + // let scratch_state = data_access_layer.get_scratch_engine_state(); // Pay out block rewards if let Some(rewards) = &executable_block.rewards { @@ -138,14 +95,61 @@ pub fn execute_finalized_block( )?; } - // WARNING: Do not change the order of `transactions` as it will result in a different root - // hash. - for txn in executable_block.transactions { - let deploy = match txn { - Transaction::Deploy(deploy) => deploy, + for transaction in executable_block.transactions { + let (deploy_hash, deploy) = match transaction { + Transaction::Deploy(deploy) => { + let deploy_hash = *deploy.hash(); + if deploy.is_transfer() { + // native transfers are routed to the data provider + let authorization_keys = deploy + .approvals() + .iter() + .map(|approval| approval.signer().to_account_hash()) + .collect(); + let transfer_req = TransferRequest::with_runtime_args( + TransferConfig::Unadministered, /* TODO: check chainspec & handle + * administered possibility */ + state_root_hash, + block_time, + protocol_version, + PublicKey::clone(&executable_block.proposer), + deploy_hash.into(), + deploy.header().account().to_account_hash(), + authorization_keys, + deploy.session().args().clone(), + U512::zero(), /* <-- this should be the native transfer cost from the + * chainspec */ + ); + // native transfer auto-commits + let transfer_result = data_access_layer.transfer(transfer_req); + trace!(?deploy_hash, ?transfer_result, "native transfer result"); + match EngineExecutionResult::from_transfer_result( + transfer_result, + Gas::new(U512::zero()), + ) { + Err(_) => return Err(BlockExecutionError::RootNotFound(state_root_hash)), + Ok(exec_result) => { + let ExecutionResultAndMessages { + execution_result, + messages, + } = ExecutionResultAndMessages::from(exec_result); + let versioned_execution_result = + ExecutionResult::from(execution_result); + execution_results.push(ExecutionArtifact::new( + deploy_hash, + deploy.header().clone(), + versioned_execution_result, + messages, + )); + } + } + continue; + } + (deploy_hash, deploy) + } Transaction::V1(_) => continue, }; - let deploy_hash = *deploy.hash(); + let deploy_header = deploy.header().clone(); let execute_request = ExecuteRequest::new( state_root_hash, @@ -155,17 +159,13 @@ pub fn execute_finalized_block( PublicKey::clone(&executable_block.proposer), ); - // TODO: this is currently working coincidentally because we are passing only one - // deploy_item per exec. The execution results coming back from the EE lack the - // mapping between deploy_hash and execution result, and this outer logic is - // enriching it with the deploy hash. If we were passing multiple deploys per exec - // the relation between the deploy and the execution results would be lost. let result = execute(&scratch_state, metrics.clone(), execute_request)?; trace!(?deploy_hash, ?result, "deploy execution result"); // As for now a given state is expected to exist. let (state_hash, execution_result, messages) = commit_execution_results( &scratch_state, + // data_access_layer, metrics.clone(), state_root_hash, deploy_hash, @@ -207,55 +207,59 @@ pub fn execute_finalized_block( // If the finalized block has an era report, run the auction contract and get the upcoming era // validators. - let maybe_step_effects_and_upcoming_era_validators = - if let Some(era_report) = &executable_block.era_report { - let StepSuccess { - post_state_hash: _, // ignore the post-state-hash returned from scratch - effects: step_effects, - } = commit_step( - &scratch_state, // engine_state - metrics, - protocol_version, - state_root_hash, - era_report.clone(), - executable_block.timestamp.millis(), - executable_block.era_id.successor(), - )?; - - state_root_hash = - engine_state.write_scratch_to_db(state_root_hash, scratch_state.into_inner())?; - - let era_validators_result = engine_state - .get_era_validators(EraValidatorsRequest::new(state_root_hash, protocol_version)); - - let upcoming_era_validators = match era_validators_result { - EraValidatorsResult::AuctionNotFound => { - panic!("auction not found"); - } - EraValidatorsResult::RootNotFound => { - panic!("root not found"); - } - EraValidatorsResult::ValueNotFound(msg) => { - panic!("validator snapshot not found: {}", msg); - } - EraValidatorsResult::Failure(tce) => { - return Err(BlockExecutionError::GetEraValidators(tce)); - } - EraValidatorsResult::Success { era_validators } => era_validators, - }; + let maybe_step_effects_and_upcoming_era_validators = if let Some(era_report) = + &executable_block.era_report + { + let StepSuccess { + post_state_hash: _, // ignore the post-state-hash returned from scratch + effects: step_effects, + } = commit_step( + &scratch_state, // engine_state + metrics, + protocol_version, + state_root_hash, + era_report.clone(), + executable_block.timestamp.millis(), + executable_block.era_id.successor(), + )?; - Some(StepEffectsAndUpcomingEraValidators { - step_effects, - upcoming_era_validators, - }) - } else { - // Finally, the new state-root-hash from the cumulative changes to global state is - // returned when they are written to LMDB. - state_root_hash = - engine_state.write_scratch_to_db(state_root_hash, scratch_state.into_inner())?; - None + state_root_hash = + engine_state.write_scratch_to_db(state_root_hash, scratch_state.into_inner())?; + + // state_root_hash = data_access_layer.write_scratch_to_db(state_root_hash, scratch_state)?; + + let era_validators_req = EraValidatorsRequest::new(state_root_hash, protocol_version); + let era_validators_result = data_access_layer.era_validators(era_validators_req); + + let upcoming_era_validators = match era_validators_result { + EraValidatorsResult::AuctionNotFound => { + panic!("auction not found"); + } + EraValidatorsResult::RootNotFound => { + panic!("root not found"); + } + EraValidatorsResult::ValueNotFound(msg) => { + panic!("validator snapshot not found: {}", msg); + } + EraValidatorsResult::Failure(tce) => { + return Err(BlockExecutionError::GetEraValidators(tce)); + } + EraValidatorsResult::Success { era_validators } => era_validators, }; + Some(StepEffectsAndUpcomingEraValidators { + step_effects, + upcoming_era_validators, + }) + } else { + // Finally, the new state-root-hash from the cumulative changes to global state is + // returned when they are written to LMDB. + state_root_hash = + engine_state.write_scratch_to_db(state_root_hash, scratch_state.into_inner())?; + // state_root_hash = data_access_layer.write_scratch_to_db(state_root_hash, scratch_state)?; + None + }; + // Flush once, after all deploys have been executed. engine_state.flush_environment()?; @@ -380,7 +384,23 @@ pub fn execute_finalized_block( )); let approvals_hashes = txn_ids.into_iter().map(|id| id.approvals_hash()).collect(); - let proof_of_checksum_registry = engine_state.get_checksum_registry_proof(state_root_hash)?; + + let proof_of_checksum_registry = match data_access_layer.tracking_copy(state_root_hash)? { + Some(tc) => match tc.reader().read_with_proof(&Key::ChecksumRegistry)? { + Some(proof) => proof, + None => { + return Err(BlockExecutionError::EngineState( + engine_state::Error::MissingChecksumRegistry, + )) + } + }, + None => { + return Err(BlockExecutionError::EngineState( + engine_state::Error::RootNotFound(state_root_hash), + )) + } + }; + let approvals_hashes = Box::new(ApprovalsHashes::new_v2( *block.hash(), approvals_hashes, @@ -398,6 +418,7 @@ pub fn execute_finalized_block( /// Commits the execution results. fn commit_execution_results( engine_state: &EngineState, + // data_access_layer: &DataAccessLayer, metrics: Option>, state_root_hash: Digest, deploy_hash: DeployHash, @@ -432,6 +453,8 @@ where } }; let new_state_root = commit_transforms(engine_state, metrics, state_root_hash, effects)?; + // let new_state_root = commit_transforms(data_access_layer, metrics, state_root_hash, + // effects)?; let ExecutionResultAndMessages { execution_result, messages, @@ -442,28 +465,32 @@ where fn commit_transforms( engine_state: &EngineState, + // data_access_layer: &DataAccessLayer, metrics: Option>, state_root_hash: Digest, effects: Effects, ) -> Result +// ) -> Result where S: StateProvider + CommitProvider, { trace!(?state_root_hash, ?effects, "commit"); let start = Instant::now(); let result = engine_state.commit_effects(state_root_hash, effects); + // let result = data_access_layer.commit(state_root_hash, effects); if let Some(metrics) = metrics { metrics.apply_effect.observe(start.elapsed().as_secs_f64()); } trace!(?result, "commit result"); result.map(Digest::from) + // result } /// Execute the transaction without committing the effects. /// Intended to be used for discovery operations on read-only nodes. /// /// Returns effects of the execution. -pub fn execute_only( +pub fn speculatively_execute( engine_state: &EngineState, execution_state: SpeculativeExecutionState, deploy: DeployItem, @@ -616,359 +643,3 @@ pub(crate) fn compute_execution_results_checksum<'a>( BlockExecutionError::FailedToComputeExecutionResultsChecksum(bytesrepr::Error::OutOfMemory) }) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn calculation_is_safe_with_invalid_input() { - assert_eq!(calculate_prune_eras(EraId::new(0), 0, 0, 0,), None); - assert_eq!(calculate_prune_eras(EraId::new(0), 0, 0, 5,), None); - assert_eq!(calculate_prune_eras(EraId::new(u64::MAX), 0, 0, 0,), None); - assert_eq!( - calculate_prune_eras(EraId::new(u64::MAX), 1, u64::MAX, u64::MAX), - None - ); - } - - #[test] - fn calculation_is_lazy() { - // NOTE: Range of EraInfos is lazy, so it does not consume memory, but getting the last - // batch out of u64::MAX of erainfos needs to iterate over all chunks. - assert!(calculate_prune_eras(EraId::new(u64::MAX), 0, u64::MAX, 100,).is_none(),); - assert_eq!( - calculate_prune_eras(EraId::new(u64::MAX), 1, 100, 100,) - .unwrap() - .len(), - 100 - ); - } - - #[test] - fn should_calculate_prune_eras() { - let activation_height = 50; - let current_height = 50; - const ACTIVATION_POINT_ERA_ID: EraId = EraId::new(5); - - // batch size 1 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 1 - ), - Some(vec![Key::EraInfo(EraId::new(0))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 1 - ), - Some(vec![Key::EraInfo(EraId::new(1))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 1 - ), - Some(vec![Key::EraInfo(EraId::new(2))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 3, - 1 - ), - Some(vec![Key::EraInfo(EraId::new(3))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 4, - 1 - ), - Some(vec![Key::EraInfo(EraId::new(4))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 5, - 1 - ), - None, - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 1), - None, - ); - - // batch size 2 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 2 - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)) - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 2 - ), - Some(vec![ - Key::EraInfo(EraId::new(2)), - Key::EraInfo(EraId::new(3)) - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 2 - ), - Some(vec![Key::EraInfo(EraId::new(4))]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 3, - 2 - ), - None - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 2), - None, - ); - - // batch size 3 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 3 - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)), - Key::EraInfo(EraId::new(2)), - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 3 - ), - Some(vec![ - Key::EraInfo(EraId::new(3)), - Key::EraInfo(EraId::new(4)), - ]) - ); - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 3 - ), - None - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 3), - None, - ); - - // batch size 4 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 4 - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)), - Key::EraInfo(EraId::new(2)), - Key::EraInfo(EraId::new(3)), - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 4 - ), - Some(vec![Key::EraInfo(EraId::new(4)),]) - ); - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 4 - ), - None - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 4), - None, - ); - - // batch size 5 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 5 - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)), - Key::EraInfo(EraId::new(2)), - Key::EraInfo(EraId::new(3)), - Key::EraInfo(EraId::new(4)), - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 5 - ), - None, - ); - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 5 - ), - None - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 5), - None, - ); - - // batch size 6 - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - 6 - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)), - Key::EraInfo(EraId::new(2)), - Key::EraInfo(EraId::new(3)), - Key::EraInfo(EraId::new(4)), - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - 6 - ), - None, - ); - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - 6 - ), - None - ); - assert_eq!( - calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 6), - None, - ); - - // batch size max - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height, - u64::MAX, - ), - Some(vec![ - Key::EraInfo(EraId::new(0)), - Key::EraInfo(EraId::new(1)), - Key::EraInfo(EraId::new(2)), - Key::EraInfo(EraId::new(3)), - Key::EraInfo(EraId::new(4)), - ]) - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 1, - u64::MAX, - ), - None, - ); - - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - current_height + 2, - u64::MAX, - ), - None - ); - assert_eq!( - calculate_prune_eras( - ACTIVATION_POINT_ERA_ID, - activation_height, - u64::MAX, - u64::MAX, - ), - None, - ); - } -} diff --git a/node/src/components/contract_runtime/rewards.rs b/node/src/components/contract_runtime/rewards.rs index 6139709574..ccdc1f6197 100644 --- a/node/src/components/contract_runtime/rewards.rs +++ b/node/src/components/contract_runtime/rewards.rs @@ -4,7 +4,8 @@ mod tests; use std::{collections::BTreeMap, ops::Range, sync::Arc}; use casper_storage::data_access_layer::{ - EraValidatorsRequest, RoundSeigniorageRateResult, TotalSupplyResult, + EraValidatorsRequest, RoundSeigniorageRateRequest, RoundSeigniorageRateResult, + TotalSupplyRequest, TotalSupplyResult, }; use futures::stream::{self, StreamExt as _, TryStreamExt as _}; @@ -32,6 +33,7 @@ impl ReactorEventT for T where T: Send + From + From Self { + Self { + protocol_version, + era_id: block.era_id, + height: block.height, + proposer: *block.proposer, + rewarded_signatures: block.rewarded_signatures, + state_root_hash: Digest::default(), + is_switch_block: block.era_report.is_some(), + is_genesis: block.era_id.is_genesis(), + } + } +} + #[derive(Debug)] pub(crate) struct RewardsInfo { eras_info: BTreeMap, @@ -78,7 +95,7 @@ pub enum RewardsError { } impl RewardsInfo { - pub async fn new_from_storage( + pub async fn new( effect_builder: EffectBuilder, protocol_version: ProtocolVersion, signature_rewards_max_delay: u64, @@ -114,15 +131,13 @@ impl RewardsInfo { "blocks fetched", ); - let eras_info = Self::create_eras_info( - effect_builder, - current_era_id, - protocol_version, - cited_blocks.iter(), - ) - .await?; + let eras_info = + Self::create_eras_info(effect_builder, current_era_id, cited_blocks.iter()).await?; - cited_blocks.push(executable_block.into()); + cited_blocks.push(CitedBlock::from_executable_block( + executable_block, + protocol_version, + )); Ok(RewardsInfo { eras_info, @@ -143,7 +158,6 @@ impl RewardsInfo { async fn create_eras_info( effect_builder: EffectBuilder, current_era_id: EraId, - protocol_version: ProtocolVersion, mut cited_blocks: impl Iterator, ) -> Result, RewardsError> { let oldest_block = cited_blocks.next(); @@ -165,13 +179,14 @@ impl RewardsInfo { .chain(cited_blocks.filter(|&block| block.is_switch_block)) .map(|block| { let state_root_hash = block.state_root_hash; + let protocol_version = block.protocol_version; let era = if block.is_switch_block { block.era_id.successor() } else { block.era_id }; - (era, state_root_hash) + (era, protocol_version, state_root_hash) }) .collect(); @@ -179,7 +194,7 @@ impl RewardsInfo { eras_and_state_root_hashes.len() + usize::from(oldest_block_is_genesis); let mut eras_info: BTreeMap<_, _> = stream::iter(eras_and_state_root_hashes) - .then(|(era_id, state_root_hash)| async move { + .then(|(era_id, protocol_version, state_root_hash)| async move { let era_validators_result = effect_builder .get_era_validators_from_contract_runtime(EraValidatorsRequest::new( state_root_hash, @@ -197,7 +212,10 @@ impl RewardsInfo { .ok_or_else(|| RewardsError::FailedToFetchEraValidators(state_root_hash))? .1; - let total_supply = match effect_builder.get_total_supply(state_root_hash).await { + let total_supply_request = + TotalSupplyRequest::new(state_root_hash, protocol_version); + let total_supply = match effect_builder.get_total_supply(total_supply_request).await + { TotalSupplyResult::RootNotFound | TotalSupplyResult::MintNotFound | TotalSupplyResult::ValueNotFound(_) @@ -207,8 +225,10 @@ impl RewardsInfo { TotalSupplyResult::Success { total_supply } => total_supply, }; + let seignorate_rate_request = + RoundSeigniorageRateRequest::new(state_root_hash, protocol_version); let seignorate_rate = match effect_builder - .get_round_seigniorage_rate(state_root_hash) + .get_round_seigniorage_rate(seignorate_rate_request) .await { RoundSeigniorageRateResult::RootNotFound @@ -342,9 +362,11 @@ pub(crate) async fn fetch_data_and_calculate_rewards_for_era "starting the rewards calculation" ); - if current_era_id.is_genesis() { - // Special case: genesis block does not yield any reward, because there is no block - // producer, and no previous blocks whose signatures are to be rewarded: + if current_era_id.is_genesis() + || current_era_id == chainspec.protocol_config.activation_point.era_id() + { + // Special case: genesis block and immediate switch blocks do not yield any reward, because + // there is no block producer, and no signatures from previous blocks to be rewarded: Ok(chainspec .network_config .accounts_config @@ -352,7 +374,7 @@ pub(crate) async fn fetch_data_and_calculate_rewards_for_era .map(|account| (account.public_key.clone(), U512::zero())) .collect()) } else { - let rewards_info = RewardsInfo::new_from_storage( + let rewards_info = RewardsInfo::new( effect_builder, chainspec.protocol_version(), chainspec.core_config.signature_rewards_max_delay, @@ -499,6 +521,7 @@ async fn collect_past_blocks_batched>( impl From for CitedBlock { fn from(block: Block) -> Self { Self { + protocol_version: block.protocol_version(), era_id: block.era_id(), height: block.height(), proposer: block.proposer().clone(), @@ -509,17 +532,3 @@ impl From for CitedBlock { } } } - -impl From for CitedBlock { - fn from(block: ExecutableBlock) -> Self { - Self { - era_id: block.era_id, - height: block.height, - proposer: *block.proposer, - rewarded_signatures: block.rewarded_signatures, - state_root_hash: Digest::default(), - is_switch_block: block.era_report.is_some(), - is_genesis: block.era_id.is_genesis(), - } - } -} diff --git a/node/src/components/contract_runtime/utils.rs b/node/src/components/contract_runtime/utils.rs index 8946223778..de32981f0e 100644 --- a/node/src/components/contract_runtime/utils.rs +++ b/node/src/components/contract_runtime/utils.rs @@ -17,10 +17,12 @@ use casper_execution_engine::engine_state::EngineState; use casper_storage::{ data_access_layer::DataAccessLayer, global_state::state::lmdb::LmdbGlobalState, }; -use casper_types::{Chainspec, EraId}; +use casper_types::{Chainspec, EraId, Key}; use once_cell::sync::Lazy; use std::{ + cmp, collections::{BTreeMap, HashMap}, + ops::Range, sync::{Arc, Mutex}, }; use tracing::{debug, error, info}; @@ -53,6 +55,7 @@ where #[allow(clippy::too_many_arguments)] pub(super) async fn exec_or_requeue( engine_state: Arc>>, + data_access_layer: Arc>, metrics: Arc, chainspec: Arc, mut exec_queue: ExecQueue, @@ -72,7 +75,6 @@ pub(super) async fn exec_or_requeue( { debug!("ContractRuntime: execute_finalized_block_or_requeue"); let contract_runtime_metrics = metrics.clone(); - let protocol_version = chainspec.protocol_version(); let activation_point = chainspec.protocol_config.activation_point; let prune_batch_size = chainspec.core_config.prune_batch_size; @@ -109,6 +111,7 @@ pub(super) async fn exec_or_requeue( debug!("ContractRuntime: execute_finalized_block"); execute_finalized_block( engine_state.as_ref(), + data_access_layer.as_ref(), Some(contract_runtime_metrics), protocol_version, current_pre_state, @@ -244,3 +247,409 @@ pub(super) async fn exec_or_requeue( .await; } } + +fn generate_range_by_index( + highest_era: u64, + batch_size: u64, + batch_index: u64, +) -> Option> { + let start = batch_index.checked_mul(batch_size)?; + let end = cmp::min(start.checked_add(batch_size)?, highest_era); + Some(start..end) +} + +/// Calculates era keys to be pruned. +/// +/// Outcomes: +/// * Ok(Some(range)) -- these keys should be pruned +/// * Ok(None) -- nothing to do, either done, or there is not enough eras to prune +pub(super) fn calculate_prune_eras( + activation_era_id: EraId, + activation_height: u64, + current_height: u64, + batch_size: u64, +) -> Option> { + if batch_size == 0 { + // Nothing to do, the batch size is 0. + return None; + } + + let nth_chunk: u64 = match current_height.checked_sub(activation_height) { + Some(nth_chunk) => nth_chunk, + None => { + // Time went backwards, programmer error, etc + error!( + %activation_era_id, + activation_height, + current_height, + batch_size, + "unable to calculate eras to prune (activation height higher than the block height)" + ); + panic!("activation height higher than the block height"); + } + }; + + let range = generate_range_by_index(activation_era_id.value(), batch_size, nth_chunk)?; + + if range.is_empty() { + return None; + } + + Some(range.map(EraId::new).map(Key::EraInfo).collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn calculation_is_safe_with_invalid_input() { + assert_eq!(calculate_prune_eras(EraId::new(0), 0, 0, 0,), None); + assert_eq!(calculate_prune_eras(EraId::new(0), 0, 0, 5,), None); + assert_eq!(calculate_prune_eras(EraId::new(u64::MAX), 0, 0, 0,), None); + assert_eq!( + calculate_prune_eras(EraId::new(u64::MAX), 1, u64::MAX, u64::MAX), + None + ); + } + + #[test] + fn calculation_is_lazy() { + // NOTE: Range of EraInfos is lazy, so it does not consume memory, but getting the last + // batch out of u64::MAX of erainfos needs to iterate over all chunks. + assert!(calculate_prune_eras(EraId::new(u64::MAX), 0, u64::MAX, 100,).is_none(),); + assert_eq!( + calculate_prune_eras(EraId::new(u64::MAX), 1, 100, 100,) + .unwrap() + .len(), + 100 + ); + } + + #[test] + fn should_calculate_prune_eras() { + let activation_height = 50; + let current_height = 50; + const ACTIVATION_POINT_ERA_ID: EraId = EraId::new(5); + + // batch size 1 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 1 + ), + Some(vec![Key::EraInfo(EraId::new(0))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 1 + ), + Some(vec![Key::EraInfo(EraId::new(1))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 1 + ), + Some(vec![Key::EraInfo(EraId::new(2))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 3, + 1 + ), + Some(vec![Key::EraInfo(EraId::new(3))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 4, + 1 + ), + Some(vec![Key::EraInfo(EraId::new(4))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 5, + 1 + ), + None, + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 1), + None, + ); + + // batch size 2 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 2 + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)) + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 2 + ), + Some(vec![ + Key::EraInfo(EraId::new(2)), + Key::EraInfo(EraId::new(3)) + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 2 + ), + Some(vec![Key::EraInfo(EraId::new(4))]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 3, + 2 + ), + None + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 2), + None, + ); + + // batch size 3 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 3 + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)), + Key::EraInfo(EraId::new(2)), + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 3 + ), + Some(vec![ + Key::EraInfo(EraId::new(3)), + Key::EraInfo(EraId::new(4)), + ]) + ); + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 3 + ), + None + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 3), + None, + ); + + // batch size 4 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 4 + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)), + Key::EraInfo(EraId::new(2)), + Key::EraInfo(EraId::new(3)), + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 4 + ), + Some(vec![Key::EraInfo(EraId::new(4)),]) + ); + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 4 + ), + None + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 4), + None, + ); + + // batch size 5 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 5 + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)), + Key::EraInfo(EraId::new(2)), + Key::EraInfo(EraId::new(3)), + Key::EraInfo(EraId::new(4)), + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 5 + ), + None, + ); + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 5 + ), + None + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 5), + None, + ); + + // batch size 6 + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + 6 + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)), + Key::EraInfo(EraId::new(2)), + Key::EraInfo(EraId::new(3)), + Key::EraInfo(EraId::new(4)), + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + 6 + ), + None, + ); + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + 6 + ), + None + ); + assert_eq!( + calculate_prune_eras(ACTIVATION_POINT_ERA_ID, activation_height, u64::MAX, 6), + None, + ); + + // batch size max + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height, + u64::MAX, + ), + Some(vec![ + Key::EraInfo(EraId::new(0)), + Key::EraInfo(EraId::new(1)), + Key::EraInfo(EraId::new(2)), + Key::EraInfo(EraId::new(3)), + Key::EraInfo(EraId::new(4)), + ]) + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 1, + u64::MAX, + ), + None, + ); + + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + current_height + 2, + u64::MAX, + ), + None + ); + assert_eq!( + calculate_prune_eras( + ACTIVATION_POINT_ERA_ID, + activation_height, + u64::MAX, + u64::MAX, + ), + None, + ); + } +} diff --git a/node/src/components/deploy_buffer/tests.rs b/node/src/components/deploy_buffer/tests.rs index 71d05c653c..c0383fb321 100644 --- a/node/src/components/deploy_buffer/tests.rs +++ b/node/src/components/deploy_buffer/tests.rs @@ -375,7 +375,7 @@ fn register_deploys_and_blocks() { .unwrap(); // try to register valid deploys - let num_valid_deploys: usize = rng.gen_range(50..500); + let num_valid_deploys: usize = rng.gen_range(50..300); let valid_deploys = create_valid_deploys(&mut rng, num_valid_deploys, DeployType::Random, None, None); valid_deploys diff --git a/node/src/components/rpc_server/rpcs/docs.rs b/node/src/components/rpc_server/rpcs/docs.rs index adf4d5aee7..ee744bc905 100644 --- a/node/src/components/rpc_server/rpcs/docs.rs +++ b/node/src/components/rpc_server/rpcs/docs.rs @@ -27,7 +27,7 @@ use super::{ use crate::effect::EffectBuilder; pub(crate) const DOCS_EXAMPLE_PROTOCOL_VERSION: ProtocolVersion = - ProtocolVersion::from_parts(1, 5, 3); + ProtocolVersion::from_parts(2, 0, 0); const DEFINITIONS_PATH: &str = "#/components/schemas/"; diff --git a/node/src/effect.rs b/node/src/effect.rs index 94416b1ee8..4fc5043f74 100644 --- a/node/src/effect.rs +++ b/node/src/effect.rs @@ -1992,11 +1992,10 @@ impl EffectBuilder { /// Returns the total supply from the given `root_hash`. /// /// This operation is read only. - pub(crate) async fn get_total_supply(self, state_hash: Digest) -> TotalSupplyResult + pub(crate) async fn get_total_supply(self, request: TotalSupplyRequest) -> TotalSupplyResult where REv: From, { - let request = TotalSupplyRequest::new(state_hash); self.make_request( move |responder| ContractRuntimeRequest::GetTotalSupply { request, responder }, QueueKind::ContractRuntime, @@ -2009,12 +2008,11 @@ impl EffectBuilder { /// This operation is read only. pub(crate) async fn get_round_seigniorage_rate( self, - state_hash: Digest, + request: RoundSeigniorageRateRequest, ) -> RoundSeigniorageRateResult where REv: From, { - let request = RoundSeigniorageRateRequest::new(state_hash); self.make_request( move |responder| ContractRuntimeRequest::GetRoundSeigniorageRate { request, responder }, QueueKind::ContractRuntime, diff --git a/resources/local/chainspec.toml.in b/resources/local/chainspec.toml.in index 7cc53c56da..1e757d342d 100644 --- a/resources/local/chainspec.toml.in +++ b/resources/local/chainspec.toml.in @@ -1,6 +1,6 @@ [protocol] # Protocol version. -version = '1.0.0' +version = '2.0.0' # Whether we need to clear latest blocks back to the switch block just before the activation point or not. hard_reset = false # This protocol version becomes active at this point. diff --git a/resources/production/chainspec.toml b/resources/production/chainspec.toml index 615760d9c9..b6cbc099da 100644 --- a/resources/production/chainspec.toml +++ b/resources/production/chainspec.toml @@ -1,6 +1,6 @@ [protocol] # Protocol version. -version = '1.5.3' +version = '2.0.0' # Whether we need to clear latest blocks back to the switch block just before the activation point or not. hard_reset = true # This protocol version becomes active at this point. diff --git a/resources/test/rpc_schema.json b/resources/test/rpc_schema.json index 5238644a39..ae85d59387 100644 --- a/resources/test/rpc_schema.json +++ b/resources/test/rpc_schema.json @@ -1,7 +1,7 @@ { "openrpc": "1.0.0-rc1", "info": { - "version": "1.5.3", + "version": "2.0.0", "title": "Client API of Casper Node", "description": "This describes the JSON-RPC 2.0 API of a node on the Casper network.", "contact": { @@ -116,7 +116,7 @@ "result": { "name": "account_put_deploy_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "deploy_hash": "5c9b3b099c1378aa8e4a5f07f59ff1fcdc69a83179427c7e67ae0377d94d93fa" } } @@ -246,7 +246,7 @@ "result": { "name": "account_put_transaction_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "transaction_hash": { "Version1": "6aaf4a54499e3757eb4be6967503dcc431e4623bf8bb57a14c1729a114a1aaa2" } @@ -333,7 +333,7 @@ "result": { "name": "info_get_deploy_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "deploy": { "hash": "5c9b3b099c1378aa8e4a5f07f59ff1fcdc69a83179427c7e67ae0377d94d93fa", "header": { @@ -494,7 +494,7 @@ "result": { "name": "info_get_transaction_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "transaction": { "Version1": { "hash": "6aaf4a54499e3757eb4be6967503dcc431e4623bf8bb57a14c1729a114a1aaa2", @@ -675,7 +675,7 @@ "result": { "name": "state_get_account_info_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "account": { "account_hash": "account-hash-e94daaff79c2ab8d9c31d9c3058d7d0a0dd31204a5638dc1451fa67b2e3fb88c", "named_keys": [ @@ -776,7 +776,7 @@ "result": { "name": "state_get_dictionary_item_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "dictionary_key": "dictionary-67518854aa916c97d4e53df8570c8217ccc259da2721b692102d76acd0ee8d1f", "stored_value": { "CLValue": { @@ -891,7 +891,7 @@ "result": { "name": "query_global_state_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "block_header": { "Version2": { "parent_hash": "0707070707070707070707070707070707070707070707070707070707070707", @@ -1025,7 +1025,7 @@ "result": { "name": "query_balance_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "balance": "123456" } } @@ -1065,7 +1065,7 @@ "result": { "name": "info_get_peers_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "peers": [ { "node_id": "tls:0101..0101", @@ -1200,7 +1200,7 @@ "address": "127.0.0.1:54321" } ], - "api_version": "1.5.3", + "api_version": "2.0.0", "build_version": "1.0.0-xxxxxxxxx@DEBUG", "chainspec_name": "casper-example", "starting_state_root_hash": "0000000000000000000000000000000000000000000000000000000000000000", @@ -1278,7 +1278,7 @@ "result": { "name": "info_get_validator_changes_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "changes": [ { "public_key": "01d9bf2148748a85c89da5aad8ee0b0fc2d105fd39d41a4c796536354f0ae2900c", @@ -1327,7 +1327,7 @@ "result": { "name": "info_get_chainspec_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "chainspec_bytes": { "chainspec_bytes": "2a2a", "maybe_genesis_accounts_bytes": null, @@ -1393,7 +1393,7 @@ "result": { "name": "chain_get_block_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "block_with_signatures": { "block": { "Version2": { @@ -1535,7 +1535,7 @@ "result": { "name": "chain_get_block_transfers_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "block_hash": "0707070707070707070707070707070707070707070707070707070707070707", "transfers": [ { @@ -1609,7 +1609,7 @@ "result": { "name": "chain_get_state_root_hash_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "state_root_hash": "0808080808080808080808080808080808080808080808080808080808080808" } } @@ -1698,7 +1698,7 @@ "result": { "name": "state_get_item_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "stored_value": { "CLValue": { "cl_type": "U64", @@ -1776,7 +1776,7 @@ "result": { "name": "state_get_balance_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "balance_value": "123456", "merkle_proof": "01000000006ef2e0949ac76e55812421f755abe129b6244fe7168b77f47a72536147614625016ef2e0949ac76e55812421f755abe129b6244fe7168b77f47a72536147614625000000003529cde5c621f857f75f3810611eb4af3f998caaa9d4a3413cf799f99c67db0307010000006ef2e0949ac76e55812421f755abe129b6244fe7168b77f47a7253614761462501010102000000006e06000000000074769d28aac597a36a03a932d4b43e4f10bf0403ee5c41dd035102553f5773631200b9e173e8f05361b681513c14e25e3138639eb03232581db7557c9e8dbbc83ce94500226a9a7fe4f2b7b88d5103a4fc7400f02bf89c860c9ccdd56951a2afe9be0e0267006d820fb5676eb2960e15722f7725f3f8f41030078f8b2e44bf0dc03f71b176d6e800dc5ae9805068c5be6da1a90b2528ee85db0609cc0fb4bd60bbd559f497a98b67f500e1e3e846592f4918234647fca39830b7e1e6ad6f5b7a99b39af823d82ba1873d000003000000010186ff500f287e9b53f823ae1582b1fa429dfede28015125fd233a31ca04d5012002015cc42669a55467a1fdf49750772bfc1aed59b9b085558eb81510e9b015a7c83b0301e3cf4a34b1db6bfa58808b686cb8fe21ebe0c1bcbcee522649d2b135fe510fe3" } @@ -1839,7 +1839,7 @@ "result": { "name": "chain_get_era_info_by_switch_block_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "era_summary": { "block_hash": "9ccc716f5f3c7ac238bf7aaad113c2add3586921a7966faffb3a5a253aa1d75e", "era_id": 42, @@ -1919,7 +1919,7 @@ "result": { "name": "state_get_auction_info_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "auction_state": { "state_root_hash": "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "block_height": 10, @@ -2014,7 +2014,7 @@ "result": { "name": "chain_get_era_summary_example_result", "value": { - "api_version": "1.5.3", + "api_version": "2.0.0", "era_summary": { "block_hash": "9ccc716f5f3c7ac238bf7aaad113c2add3586921a7966faffb3a5a253aa1d75e", "era_id": 42, @@ -7074,4 +7074,4 @@ } } } -} \ No newline at end of file +} diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index d8decfe847..abd69f436e 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -10,7 +10,7 @@ use casper_types::{ bytesrepr::{self, FromBytes}, contract_messages::{MessagePayload, MessageTopicOperation}, package::EntityVersion, - system::CallStackElement, + system::Caller, AddressableEntityHash, ApiError, BlockTime, CLTyped, CLValue, Key, PackageHash, Phase, RuntimeArgs, URef, BLAKE2B_DIGEST_LENGTH, BLOCKTIME_SERIALIZED_LENGTH, PHASE_SERIALIZED_LENGTH, }; @@ -385,7 +385,7 @@ pub(crate) fn read_host_buffer(size: usize) -> Result, ApiError> { } /// Returns the call stack. -pub fn get_call_stack() -> Vec { +pub fn get_call_stack() -> Vec { let (call_stack_len, result_size) = { let mut call_stack_len: usize = 0; let mut result_size: usize = 0; diff --git a/smart_contracts/contracts/client/non-standard-payment/src/main.rs b/smart_contracts/contracts/client/non-standard-payment/src/main.rs index 74da2f7734..bd1657df48 100644 --- a/smart_contracts/contracts/client/non-standard-payment/src/main.rs +++ b/smart_contracts/contracts/client/non-standard-payment/src/main.rs @@ -13,11 +13,12 @@ use casper_contract::{ use casper_types::{ api_error, bytesrepr::{self, FromBytes}, - ApiError, PublicKey, U512, + ApiError, RuntimeArgs, URef, U512, }; const ARG_AMOUNT: &str = "amount"; const ARG_SOURCE_UREF: &str = "source"; +const GET_PAYMENT_PURSE: &str = "get_payment_purse"; /// This logic is intended to be used as SESSION PAYMENT LOGIC /// Alternate payment logic that allows payment from a purse other than the executing [Account]'s @@ -33,17 +34,22 @@ pub extern "C" fn call() { } }; + // handle payment contract + let handle_payment_contract_hash = system::get_handle_payment(); + + // get payment purse for current execution + let payment_purse: URef = runtime::call_contract( + handle_payment_contract_hash, + GET_PAYMENT_PURSE, + RuntimeArgs::default(), + ); + // amount to transfer from named purse to payment purse let amount: U512 = runtime::get_named_arg(ARG_AMOUNT); // transfer amount from named purse to payment purse, which will be used to pay for execution - system::transfer_from_purse_to_account( - purse_uref, - PublicKey::System.to_account_hash(), - amount, - None, - ) - .unwrap_or_revert(); + system::transfer_from_purse_to_purse(purse_uref, payment_purse, amount, None) + .unwrap_or_revert(); } fn get_named_arg_if_exists(name: &str) -> Option { diff --git a/storage/src/data_access_layer.rs b/storage/src/data_access_layer.rs index a6c82239a3..6944116db9 100644 --- a/storage/src/data_access_layer.rs +++ b/storage/src/data_access_layer.rs @@ -1,10 +1,9 @@ -use casper_types::{execution::Effects, Digest, EraId}; - use crate::global_state::{ error::Error as GlobalStateError, state::{CommitProvider, StateProvider}, trie_store::operations::PruneResult, }; +use casper_types::{execution::Effects, Digest, EraId}; use crate::tracking_copy::TrackingCopy; @@ -19,6 +18,7 @@ mod protocol_upgrade; pub mod query; mod round_seigniorage; mod total_supply; +pub mod transfer; mod trie; pub use addressable_entity::{AddressableEntityRequest, AddressableEntityResult}; @@ -35,6 +35,7 @@ pub use protocol_upgrade::{ProtocolUpgradeRequest, ProtocolUpgradeResult}; pub use query::{QueryRequest, QueryResult}; pub use round_seigniorage::{RoundSeigniorageRateRequest, RoundSeigniorageRateResult}; pub use total_supply::{TotalSupplyRequest, TotalSupplyResult}; +pub use transfer::{TransferRequest, TransferResult}; pub use trie::{PutTrieRequest, PutTrieResult, TrieElement, TrieRequest, TrieResult}; pub struct Block { diff --git a/storage/src/data_access_layer/flush.rs b/storage/src/data_access_layer/flush.rs index 8be4bcc5ee..26fbeeae04 100644 --- a/storage/src/data_access_layer/flush.rs +++ b/storage/src/data_access_layer/flush.rs @@ -22,6 +22,6 @@ pub enum FlushResult { ManualSyncDisabled, /// Successfully flushed. Success, - /// Failed to get total supply. + /// Failed to flush. Failure(GlobalStateError), } diff --git a/storage/src/data_access_layer/round_seigniorage.rs b/storage/src/data_access_layer/round_seigniorage.rs index c7a02ab480..c19dd2d88b 100644 --- a/storage/src/data_access_layer/round_seigniorage.rs +++ b/storage/src/data_access_layer/round_seigniorage.rs @@ -1,23 +1,32 @@ use crate::tracking_copy::TrackingCopyError; -use casper_types::{Digest, U512}; +use casper_types::{Digest, ProtocolVersion, U512}; use num_rational::Ratio; /// Request to get the current round seigniorage rate. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct RoundSeigniorageRateRequest { state_hash: Digest, + protocol_version: ProtocolVersion, } impl RoundSeigniorageRateRequest { /// Create instance of RoundSeigniorageRateRequest. - pub fn new(state_hash: Digest) -> Self { - RoundSeigniorageRateRequest { state_hash } + pub fn new(state_hash: Digest, protocol_version: ProtocolVersion) -> Self { + RoundSeigniorageRateRequest { + state_hash, + protocol_version, + } } /// Returns state root hash. pub fn state_hash(&self) -> Digest { self.state_hash } + + /// Returns the protocol version. + pub fn protocol_version(&self) -> ProtocolVersion { + self.protocol_version + } } /// Represents a result of a `round_seigniorage_rate` request. diff --git a/storage/src/data_access_layer/total_supply.rs b/storage/src/data_access_layer/total_supply.rs index 2b837097fd..3f27168c99 100644 --- a/storage/src/data_access_layer/total_supply.rs +++ b/storage/src/data_access_layer/total_supply.rs @@ -1,22 +1,31 @@ use crate::tracking_copy::TrackingCopyError; -use casper_types::{Digest, U512}; +use casper_types::{Digest, ProtocolVersion, U512}; /// Request for total supply. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TotalSupplyRequest { state_hash: Digest, + protocol_version: ProtocolVersion, } impl TotalSupplyRequest { /// Creates an instance of TotalSupplyRequest. - pub fn new(state_hash: Digest) -> Self { - TotalSupplyRequest { state_hash } + pub fn new(state_hash: Digest, protocol_version: ProtocolVersion) -> Self { + TotalSupplyRequest { + state_hash, + protocol_version, + } } /// Returns state root hash. pub fn state_hash(&self) -> Digest { self.state_hash } + + /// Returns the protocol version. + pub fn protocol_version(&self) -> ProtocolVersion { + self.protocol_version + } } /// Represents a result of a `total_supply` request. diff --git a/storage/src/data_access_layer/transfer.rs b/storage/src/data_access_layer/transfer.rs new file mode 100644 index 0000000000..a64fcf0b90 --- /dev/null +++ b/storage/src/data_access_layer/transfer.rs @@ -0,0 +1,235 @@ +use std::collections::BTreeSet; + +use casper_types::{ + account::AccountHash, execution::Effects, Digest, ProtocolVersion, PublicKey, RuntimeArgs, + TransferAddr, U512, +}; + +use crate::system::transfer::{TransferArgs, TransferError}; + +/// Transfer details. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TransferRequestArgs { + Raw(RuntimeArgs), + Explicit(TransferArgs), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TransferConfig { + Administered { + administrative_accounts: BTreeSet, + allow_unrestricted_transfer: bool, + }, + Unadministered, +} + +impl TransferConfig { + /// Returns a new instance. + pub fn new( + administrative_accounts: BTreeSet, + allow_unrestricted_transfer: bool, + ) -> Self { + if administrative_accounts.is_empty() && allow_unrestricted_transfer { + TransferConfig::Unadministered + } else { + TransferConfig::Administered { + administrative_accounts, + allow_unrestricted_transfer, + } + } + } + + /// Does account hash belong to an administrative account? + pub fn is_administrator(&self, account_hash: &AccountHash) -> bool { + match self { + TransferConfig::Administered { + administrative_accounts, + .. + } => administrative_accounts.contains(account_hash), + TransferConfig::Unadministered => false, + } + } + + /// Administrative accounts, if any. + pub fn administrative_accounts(&self) -> BTreeSet { + match self { + TransferConfig::Administered { + administrative_accounts, + .. + } => administrative_accounts.clone(), + TransferConfig::Unadministered => BTreeSet::default(), + } + } + + /// Allow unrestricted transfers. + pub fn allow_unrestricted_transfers(&self) -> bool { + match self { + TransferConfig::Administered { + allow_unrestricted_transfer, + .. + } => *allow_unrestricted_transfer, + TransferConfig::Unadministered => true, + } + } + + /// Restricted transfer should be enforced. + pub fn enforce_transfer_restrictions(&self, account_hash: &AccountHash) -> bool { + !self.allow_unrestricted_transfers() && !self.is_administrator(account_hash) + } +} + +/// Request for motes transfer. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TransferRequest { + /// Config. + config: TransferConfig, + /// State root hash. + state_hash: Digest, + /// Block time represented as a unix timestamp. + block_time: u64, + /// Protocol version. + protocol_version: ProtocolVersion, + /// Public key of the proposer. + proposer: PublicKey, + /// Transaction hash. + transaction_hash: Digest, + /// Base account. + address: AccountHash, + /// List of authorizing accounts. + authorization_keys: BTreeSet, + /// Args. + args: TransferRequestArgs, + /// Cost. + cost: U512, +} + +impl TransferRequest { + /// Creates new request object. + #[allow(clippy::too_many_arguments)] + pub fn new( + config: TransferConfig, + state_hash: Digest, + block_time: u64, + protocol_version: ProtocolVersion, + proposer: PublicKey, + transaction_hash: Digest, + address: AccountHash, + authorization_keys: BTreeSet, + args: TransferArgs, + cost: U512, + ) -> Self { + let args = TransferRequestArgs::Explicit(args); + Self { + config, + state_hash, + block_time, + protocol_version, + proposer, + transaction_hash, + address, + authorization_keys, + args, + cost, + } + } + + /// Creates new request instance with runtime args. + #[allow(clippy::too_many_arguments)] + pub fn with_runtime_args( + config: TransferConfig, + state_hash: Digest, + block_time: u64, + protocol_version: ProtocolVersion, + proposer: PublicKey, + transaction_hash: Digest, // <-- TODO: this should probably be TransactionHash + address: AccountHash, + authorization_keys: BTreeSet, + args: RuntimeArgs, + cost: U512, + ) -> Self { + let args = TransferRequestArgs::Raw(args); + Self { + config, + state_hash, + block_time, + protocol_version, + proposer, + transaction_hash, + address, + authorization_keys, + args, + cost, + } + } + + pub fn config(&self) -> &TransferConfig { + &self.config + } + + /// Returns state root hash. + pub fn state_hash(&self) -> Digest { + self.state_hash + } + + /// Returns address. + pub fn address(&self) -> AccountHash { + self.address + } + + /// Returns authorization keys. + pub fn authorization_keys(&self) -> &BTreeSet { + &self.authorization_keys + } + + /// Returns protocol version. + pub fn protocol_version(&self) -> ProtocolVersion { + self.protocol_version + } + + /// Returns block time. + pub fn block_time(&self) -> u64 { + self.block_time + } + + /// Returns transaction hash. + pub fn transaction_hash(&self) -> Digest { + self.transaction_hash + } + + /// Returns proposer. + pub fn proposer(&self) -> &PublicKey { + &self.proposer + } + + /// The cost. + pub fn cost(&self) -> U512 { + self.cost + } + + /// Returns transfer args. + pub fn args(&self) -> &TransferRequestArgs { + &self.args + } + + /// Into args. + pub fn into_args(self) -> TransferRequestArgs { + self.args + } +} + +#[derive(Debug, Clone)] +pub enum TransferResult { + /// Invalid state root hash. + RootNotFound, + /// Transfer succeeded + Success { + /// List of transfers that happened during execution. + transfers: Vec, + /// State hash after transfer is committed to the global state. + post_state_hash: Digest, + /// Effects of transfer. + effects: Effects, + }, + /// Transfer failed + Failure(TransferError), +} diff --git a/storage/src/global_state/error.rs b/storage/src/global_state/error.rs index 1becf8b21e..88000dd321 100644 --- a/storage/src/global_state/error.rs +++ b/storage/src/global_state/error.rs @@ -2,7 +2,7 @@ use std::sync; use thiserror::Error; -use casper_types::{bytesrepr, Digest}; +use casper_types::{bytesrepr, Digest, Key}; use crate::global_state::{state::CommitError, trie::TrieRaw}; @@ -33,6 +33,10 @@ pub enum Error { /// Failed to put a trie node into global state because some of its children were missing. #[error("Failed to put a trie into global state because some of its children were missing")] MissingTrieNodeChildren(Digest, TrieRaw, Vec), + + /// Failed to prune listed keys. + #[error("Pruning attempt failed.")] + FailedToPrune(Vec), } impl From for Error { diff --git a/storage/src/global_state/state/lmdb.rs b/storage/src/global_state/state/lmdb.rs index 097719edef..3dcc616fd1 100644 --- a/storage/src/global_state/state/lmdb.rs +++ b/storage/src/global_state/state/lmdb.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::{collections::HashMap, ops::Deref, sync::Arc}; use lmdb::{DatabaseFlags, RwTransaction}; @@ -11,8 +12,8 @@ use casper_types::{ use crate::{ data_access_layer::{ - FlushRequest, FlushResult, PutTrieRequest, PutTrieResult, TrieElement, TrieRequest, - TrieResult, + DataAccessLayer, FlushRequest, FlushResult, PutTrieRequest, PutTrieResult, TrieElement, + TrieRequest, TrieResult, }, global_state::{ error::Error as GlobalStateError, @@ -29,7 +30,7 @@ use crate::{ lmdb::{LmdbTrieStore, ScratchTrieStore}, operations::{ keys_with_prefix, missing_children, prune, put_trie, read, read_with_proof, - PruneResult, ReadResult, + PruneResult as OpPruneResult, PruneResult, ReadResult, }, }, DEFAULT_MAX_DB_SIZE, DEFAULT_MAX_QUERY_DEPTH, DEFAULT_MAX_READERS, @@ -386,6 +387,45 @@ impl StateProvider for LmdbGlobalState { } } +impl DataAccessLayer { + /// Flushes the LMDB environment to disk when manual sync is enabled in the config.toml. + pub fn flush_environment(&self) -> Result<(), GlobalStateError> { + if self.state().environment().is_manual_sync_enabled() { + self.state().environment().sync()? + } + Ok(()) + } + + /// Provide a local cached-only version of engine-state. + pub fn get_scratch_engine_state(&self) -> ScratchGlobalState { + self.state().create_scratch() + } + + /// Writes state cached in an `EngineState` to LMDB. + pub fn write_scratch_to_db( + &self, + state_root_hash: Digest, + scratch_global_state: ScratchGlobalState, + ) -> Result { + let (stored_values, keys_to_prune) = scratch_global_state.into_inner(); + let post_state_hash = self + .state() + .put_stored_values(state_root_hash, stored_values)?; + if keys_to_prune.is_empty() { + return Ok(post_state_hash); + } + let prune_keys = keys_to_prune.iter().cloned().collect_vec(); + match self.state().prune_keys(post_state_hash, &prune_keys) { + Ok(result) => match result { + OpPruneResult::Pruned(post_state_hash) => Ok(post_state_hash), + OpPruneResult::DoesNotExist => Err(GlobalStateError::FailedToPrune(prune_keys)), + OpPruneResult::RootNotFound => Err(GlobalStateError::RootNotFound), + }, + Err(err) => Err(err), + } + } +} + /// Creates prepopulated LMDB global state instance that stores data in a temporary directory. As /// soon as the `TempDir` instance is dropped all the data stored will be removed from the disk as /// well. diff --git a/storage/src/global_state/state/mod.rs b/storage/src/global_state/state/mod.rs index 2ab29c58d9..69ebe7c875 100644 --- a/storage/src/global_state/state/mod.rs +++ b/storage/src/global_state/state/mod.rs @@ -6,20 +6,21 @@ pub mod lmdb; /// Lmdb implementation of global state with cache. pub mod scratch; -use std::{cell::RefCell, collections::HashMap, rc::Rc}; +use std::{cell::RefCell, collections::HashMap, convert::TryFrom, rc::Rc}; use tracing::{debug, error, warn}; use casper_types::{ - addressable_entity::EntityKindTag, + addressable_entity::{EntityKindTag, NamedKeys}, bytesrepr, execution::{Effects, Transform, TransformError, TransformInstruction, TransformKind}, system::{ auction::SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY, - mint::{ROUND_SEIGNIORAGE_RATE_KEY, TOTAL_SUPPLY_KEY}, + mint::{ARG_AMOUNT, ROUND_SEIGNIORAGE_RATE_KEY, TOTAL_SUPPLY_KEY}, AUCTION, MINT, }, - AddressableEntity, Digest, EntityAddr, Key, KeyTag, StoredValue, + Account, AddressableEntity, AddressableEntityHash, DeployHash, Digest, EntityAddr, Key, KeyTag, + Phase, PublicKey, RuntimeArgs, StoredValue, U512, }; #[cfg(test)] @@ -27,13 +28,14 @@ pub use self::lmdb::make_temporary_global_state; use crate::{ data_access_layer::{ - era_validators::EraValidatorsResult, AddressableEntityRequest, AddressableEntityResult, - BalanceRequest, BalanceResult, BidsRequest, BidsResult, EraValidatorsRequest, - ExecutionResultsChecksumRequest, ExecutionResultsChecksumResult, FlushRequest, FlushResult, - GenesisRequest, GenesisResult, ProtocolUpgradeRequest, ProtocolUpgradeResult, - PutTrieRequest, PutTrieResult, QueryRequest, QueryResult, RoundSeigniorageRateRequest, - RoundSeigniorageRateResult, TotalSupplyRequest, TotalSupplyResult, TrieRequest, TrieResult, - EXECUTION_RESULTS_CHECKSUM_NAME, + era_validators::EraValidatorsResult, + transfer::{TransferRequest, TransferRequestArgs, TransferResult}, + AddressableEntityRequest, AddressableEntityResult, BalanceRequest, BalanceResult, + BidsRequest, BidsResult, EraValidatorsRequest, ExecutionResultsChecksumRequest, + ExecutionResultsChecksumResult, FlushRequest, FlushResult, GenesisRequest, GenesisResult, + ProtocolUpgradeRequest, ProtocolUpgradeResult, PutTrieRequest, PutTrieResult, QueryRequest, + QueryResult, RoundSeigniorageRateRequest, RoundSeigniorageRateResult, TotalSupplyRequest, + TotalSupplyResult, TrieRequest, TrieResult, EXECUTION_RESULTS_CHECKSUM_NAME, }, global_state::{ error::Error as GlobalStateError, @@ -48,10 +50,11 @@ use crate::{ auction, auction::bidding::{BiddingRequest, BiddingResult}, genesis::{GenesisError, GenesisInstaller}, - mint::transfer::{TransferRequest, TransferResult}, + mint::{Mint, NativeMintRuntime}, protocol_upgrade::{ProtocolUpgradeError, ProtocolUpgrader}, + transfer::{NewTransferTargetMode, TransferError, TransferRuntimeArgsBuilder}, }, - tracking_copy::{TrackingCopy, TrackingCopyError, TrackingCopyExt}, + tracking_copy::{TrackingCopy, TrackingCopyEntityExt, TrackingCopyError, TrackingCopyExt}, }; use super::trie_store::operations::PruneResult; @@ -106,7 +109,7 @@ pub trait CommitProvider: StateProvider { Ok(Some(tc)) => Rc::new(RefCell::new(tc)), Ok(None) => return GenesisResult::Fatal("state uninitialized".to_string()), Err(err) => { - return GenesisResult::Failure(GenesisError::TrackingCopyError( + return GenesisResult::Failure(GenesisError::TrackingCopy( crate::tracking_copy::TrackingCopyError::Storage(err), )) } @@ -129,9 +132,9 @@ pub trait CommitProvider: StateProvider { post_state_hash, effects, }, - Err(err) => GenesisResult::Failure(GenesisError::TrackingCopyError( - TrackingCopyError::Storage(err), - )), + Err(err) => { + GenesisResult::Failure(GenesisError::TrackingCopy(TrackingCopyError::Storage(err))) + } } } @@ -141,7 +144,7 @@ pub trait CommitProvider: StateProvider { Ok(Some(tc)) => Rc::new(RefCell::new(tc)), Ok(None) => return ProtocolUpgradeResult::RootNotFound, Err(err) => { - return ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopyError( + return ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopy( TrackingCopyError::Storage(err), )) } @@ -162,11 +165,203 @@ pub trait CommitProvider: StateProvider { post_state_hash, effects, }, - Err(err) => ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopyError( + Err(err) => ProtocolUpgradeResult::Failure(ProtocolUpgradeError::TrackingCopy( TrackingCopyError::Storage(err), )), } } + + /// Direct transfer. + fn transfer(&self, request: TransferRequest) -> TransferResult { + let state_hash = request.state_hash(); + let tc = match self.tracking_copy(state_hash) { + Ok(Some(tc)) => Rc::new(RefCell::new(tc)), + Ok(None) => return TransferResult::RootNotFound, + Err(err) => { + return TransferResult::Failure(TransferError::TrackingCopy( + TrackingCopyError::Storage(err), + )) + } + }; + + let account_hash = request.address(); + let protocol_version = request.protocol_version(); + if let Err(tce) = tc + .borrow_mut() + .migrate_account(account_hash, protocol_version) + { + return TransferResult::Failure(tce.into()); + } + + let authorization_keys = request.authorization_keys(); + + let config = request.config(); + let administrative_accounts = config.administrative_accounts(); + + let runtime_args = match request.args() { + TransferRequestArgs::Raw(runtime_args) => runtime_args.clone(), + TransferRequestArgs::Explicit(transfer_args) => { + match RuntimeArgs::try_from(*transfer_args) { + Ok(runtime_args) => runtime_args, + Err(cve) => return TransferResult::Failure(TransferError::CLValue(cve)), + } + } + }; + + let remaining_spending_limit = match runtime_args.try_get_number(ARG_AMOUNT) { + Ok(amount) => amount, + Err(cve) => { + debug!("failed to derive remaining_spending_limit"); + return TransferResult::Failure(TransferError::CLValue(cve)); + } + }; + + let mut runtime_args_builder = TransferRuntimeArgsBuilder::new(runtime_args); + + let transfer_target_mode = match runtime_args_builder + .resolve_transfer_target_mode(protocol_version, Rc::clone(&tc)) + { + Ok(transfer_target_mode) => transfer_target_mode, + Err(error) => return TransferResult::Failure(error), + }; + + if config.enforce_transfer_restrictions(&account_hash) { + // We need to make sure that either the source or target is an admin. + match transfer_target_mode { + NewTransferTargetMode::ExistingAccount { + target_account_hash, + .. + } + | NewTransferTargetMode::CreateAccount(target_account_hash) => { + let is_target_system_account = + target_account_hash == PublicKey::System.to_account_hash(); + let is_target_administrator = config.is_administrator(&target_account_hash); + if !(is_target_system_account || is_target_administrator) { + // Transferring from normal account to a purse doesn't work. + return TransferResult::Failure( + TransferError::DisabledUnrestrictedTransfers, + ); + } + } + NewTransferTargetMode::PurseExists(_) => { + // We don't know who is the target and we can't simply reverse search + // account/contract that owns it. We also can't know if purse is owned exactly + // by one entity in the system. + return TransferResult::Failure(TransferError::DisabledUnrestrictedTransfers); + } + } + } + + let (entity, entity_addr) = match tc.borrow_mut().get_authorized_addressable_entity( + protocol_version, + account_hash, + authorization_keys, + &administrative_accounts, + ) { + Ok((entity, entity_hash)) => { + let entity_addr = + EntityAddr::new_with_tag(entity.entity_kind(), entity_hash.value()); + (entity, entity_addr) + } + Err(tce) => return TransferResult::Failure(TransferError::TrackingCopy(tce)), + }; + + let named_keys = match tc.borrow_mut().get_named_keys(entity_addr) { + Ok(named_keys) => named_keys, + Err(tce) => return TransferResult::Failure(TransferError::TrackingCopy(tce)), + }; + let access_rights = entity + .extract_access_rights(AddressableEntityHash::new(entity_addr.value()), &named_keys); + + let mut mint_provider = NativeMintRuntime::new( + config.clone(), + protocol_version, + Rc::clone(&tc), + account_hash, + entity.clone(), + named_keys.clone(), + access_rights, + remaining_spending_limit, + request.transaction_hash(), + Phase::Session, + ); + + match transfer_target_mode { + NewTransferTargetMode::ExistingAccount { .. } + | NewTransferTargetMode::PurseExists(_) => { + // Noop + } + NewTransferTargetMode::CreateAccount(account_hash) => { + let main_purse = match mint_provider.mint(U512::zero()) { + Ok(uref) => uref, + Err(mint_error) => { + return TransferResult::Failure(TransferError::Mint(mint_error)) + } + }; + // TODO: KARAN TO FIX: this should create a shiny new addressable entity instance, + // not create a legacy account and then uplift it. + let account = Account::create(account_hash, NamedKeys::new(), main_purse); + if let Err(tce) = tc + .borrow_mut() + .create_addressable_entity_from_account(account, protocol_version) + { + return TransferResult::Failure(tce.into()); + } + } + } + + let transfer_args = { + match runtime_args_builder.build(&entity, named_keys, protocol_version, Rc::clone(&tc)) + { + Ok(transfer_args) => transfer_args, + Err(error) => return TransferResult::Failure(error), + } + }; + if let Err(mint_error) = mint_provider.transfer( + transfer_args.to(), + transfer_args.source(), + transfer_args.target(), + transfer_args.amount(), + transfer_args.arg_id(), + ) { + return TransferResult::Failure(TransferError::Mint(mint_error)); + } + + let transfers = mint_provider.into_transfers(); + + { + // TODO: this block needs to be updated with version management for new style + // Transactions + let deploy_hash = DeployHash::new(request.transaction_hash()); + let deploy_info = casper_types::DeployInfo::new( + deploy_hash, + &transfers, + account_hash, + entity.main_purse(), + request.cost(), + ); + tc.borrow_mut().write( + Key::DeployInfo(deploy_hash), + StoredValue::DeployInfo(deploy_info), + ); + } + + let effects = tc.borrow_mut().effects(); + + // commit + match self.commit(state_hash, effects.clone()) { + Ok(post_state_hash) => TransferResult::Success { + transfers, + post_state_hash, + effects, + }, + Err(tce) => TransferResult::Failure(tce.into()), + } + } + + fn bidding(&self, _bid_request: BiddingRequest) -> BiddingResult { + unimplemented!() + } } /// A trait expressing operations over the trie. @@ -238,13 +433,20 @@ pub trait StateProvider { Err(err) => return EraValidatorsResult::Failure(TrackingCopyError::Storage(err)), }; - let query_request = match tc.get_system_contracts() { + let query_request = match tc.get_system_entity_registry() { Ok(scr) => match scr.get(AUCTION).copied() { - Some(auction_hash) => QueryRequest::new( - state_hash, - Key::addressable_entity_key(EntityKindTag::System, auction_hash), - vec![SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY.to_string()], - ), + Some(auction_hash) => { + let key = if request.protocol_version().value().major < 2 { + Key::Hash(auction_hash.value()) + } else { + Key::addressable_entity_key(EntityKindTag::System, auction_hash) + }; + QueryRequest::new( + state_hash, + key, + vec![SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY.to_string()], + ) + } None => return EraValidatorsResult::AuctionNotFound, }, Err(err) => return EraValidatorsResult::Failure(err), @@ -380,16 +582,17 @@ pub trait StateProvider { TrackingCopyError::CLValue(error), ); } - }; - }; - warn!( - %key, - type_name = %value.type_name(), - "expected a CLValue::Key or Account to be stored under account hash" - ); - return AddressableEntityResult::Failure( - TrackingCopyError::UnexpectedStoredValueVariant, - ); + } + } else { + warn!( + %key, + type_name = %value.type_name(), + "expected a CLValue::Key or Account to be stored under account hash" + ); + return AddressableEntityResult::Failure( + TrackingCopyError::UnexpectedStoredValueVariant, + ); + } } } } @@ -447,13 +650,16 @@ pub trait StateProvider { Err(err) => return TotalSupplyResult::Failure(TrackingCopyError::Storage(err)), }; - let query_request = match tc.get_system_contracts() { + let query_request = match tc.get_system_entity_registry() { Ok(scr) => match scr.get(MINT).copied() { - Some(mint_hash) => QueryRequest::new( - state_hash, - Key::addressable_entity_key(EntityKindTag::System, mint_hash), - vec![TOTAL_SUPPLY_KEY.to_string()], - ), + Some(mint_hash) => { + let key = if request.protocol_version().value().major < 2 { + Key::Hash(mint_hash.value()) + } else { + Key::addressable_entity_key(EntityKindTag::System, mint_hash) + }; + QueryRequest::new(state_hash, key, vec![TOTAL_SUPPLY_KEY.to_string()]) + } None => { error!("unexpected query failure; mint not found"); return TotalSupplyResult::MintNotFound; @@ -499,13 +705,20 @@ pub trait StateProvider { } }; - let query_request = match tc.get_system_contracts() { + let query_request = match tc.get_system_entity_registry() { Ok(scr) => match scr.get(MINT).copied() { - Some(mint_hash) => QueryRequest::new( - state_hash, - Key::addressable_entity_key(EntityKindTag::System, mint_hash), - vec![ROUND_SEIGNIORAGE_RATE_KEY.to_string()], - ), + Some(mint_hash) => { + let key = if request.protocol_version().value().major < 2 { + Key::Hash(mint_hash.value()) + } else { + Key::addressable_entity_key(EntityKindTag::System, mint_hash) + }; + QueryRequest::new( + state_hash, + key, + vec![ROUND_SEIGNIORAGE_RATE_KEY.to_string()], + ) + } None => { error!("unexpected query failure; mint not found"); return RoundSeigniorageRateResult::MintNotFound; @@ -554,14 +767,6 @@ pub trait StateProvider { root: Digest, keys_to_delete: &[Key], ) -> Result; - - fn transfer(&self, _transfer_request: TransferRequest) -> TransferResult { - unimplemented!() - } - - fn bidding(&self, _bid_request: BiddingRequest) -> BiddingResult { - unimplemented!() - } } /// Write multiple key/stored value pairs to the store in a single rw transaction. diff --git a/storage/src/system.rs b/storage/src/system.rs index c6614114b0..74623d6637 100644 --- a/storage/src/system.rs +++ b/storage/src/system.rs @@ -5,3 +5,4 @@ pub mod handle_payment; pub mod mint; pub mod protocol_upgrade; pub mod standard_payment; +pub mod transfer; diff --git a/storage/src/system/genesis.rs b/storage/src/system/genesis.rs index d3be24f443..fee953b703 100644 --- a/storage/src/system/genesis.rs +++ b/storage/src/system/genesis.rs @@ -123,7 +123,7 @@ pub enum GenesisError { /// Genesis process requires initial accounts. MissingGenesisAccounts, /// A tracking copy error. - TrackingCopyError(TrackingCopyError), + TrackingCopy(TrackingCopyError), } impl fmt::Display for GenesisError { @@ -262,10 +262,8 @@ where Ok(total_supply_uref.into()) } - fn create_handle_payment( - &self, - handle_payment_payment_purse: URef, - ) -> Result> { + fn create_handle_payment(&self) -> Result> { + let handle_payment_payment_purse = self.create_purse(U512::zero())?; let named_keys = { let mut named_keys = NamedKeys::new(); let named_key = Key::URef(handle_payment_payment_purse); @@ -563,11 +561,7 @@ where Ok(contract_hash) } - pub fn create_accounts( - &self, - total_supply_key: Key, - payment_purse_uref: URef, - ) -> Result<(), Box> { + pub fn create_accounts(&self, total_supply_key: Key) -> Result<(), Box> { let accounts = { let mut ret: Vec = self.config.accounts_iter().cloned().collect(); let system_account = GenesisAccount::system(); @@ -593,14 +587,7 @@ where for account in accounts { let account_starting_balance = account.balance().value(); - let main_purse = match account { - GenesisAccount::System - if self.config.administrative_accounts().next().is_some() => - { - payment_purse_uref - } - _ => self.create_purse(account_starting_balance)?, - }; + let main_purse = self.create_purse(account_starting_balance)?; self.store_addressable_entity( EntityKind::Account(account.account_hash()), @@ -872,16 +859,14 @@ where // Create mint let total_supply_key = self.create_mint()?; - let payment_purse_uref = self.create_purse(U512::zero())?; - // Create all genesis accounts - self.create_accounts(total_supply_key, payment_purse_uref)?; + self.create_accounts(total_supply_key)?; // Create the auction and setup the stake of all genesis validators. self.create_auction(total_supply_key)?; // Create handle payment - self.create_handle_payment(payment_purse_uref)?; + self.create_handle_payment()?; self.store_chainspec_registry(chainspec_registry)?; diff --git a/storage/src/system/mint.rs b/storage/src/system/mint.rs index 71783e08b3..7789746665 100644 --- a/storage/src/system/mint.rs +++ b/storage/src/system/mint.rs @@ -1,7 +1,7 @@ +mod mint_native; pub mod runtime_provider; pub mod storage_provider; pub mod system_provider; -pub mod transfer; use num_rational::Ratio; use num_traits::CheckedMul; @@ -10,7 +10,7 @@ use casper_types::{ account::AccountHash, system::{ mint::{Error, ROUND_SEIGNIORAGE_RATE_KEY, TOTAL_SUPPLY_KEY}, - CallStackElement, + Caller, }, Key, PublicKey, SystemEntityRegistry, URef, U512, }; @@ -20,6 +20,8 @@ use crate::system::mint::{ system_provider::SystemProvider, }; +pub use crate::system::mint::mint_native::NativeMintRuntime; + /// Mint trait. pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { /// Mint new token with given `initial_balance` balance. Returns new purse on success, otherwise @@ -80,7 +82,7 @@ pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { .ok_or(Error::ArithmeticOverflow)?; // update total supply - self.write(total_supply_uref, reduced_total_supply)?; + self.write_amount(total_supply_uref, reduced_total_supply)?; Ok(()) } @@ -103,13 +105,13 @@ pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { id: Option, ) -> Result<(), Error> { if !self.allow_unrestricted_transfers() { - let registry = match self.get_system_contract_registry() { + let registry = match self.get_system_entity_registry() { Ok(registry) => registry, Err(_) => SystemEntityRegistry::new(), }; - let immediate_caller = self.get_immediate_caller().cloned(); + let immediate_caller = self.get_immediate_caller(); match immediate_caller { - Some(CallStackElement::AddressableEntity { + Some(Caller::AddressableEntity { entity_hash: contract_hash, .. }) if registry.has_contract_hash(&contract_hash) => { @@ -117,20 +119,20 @@ pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { // transfer) } - Some(CallStackElement::Session { account_hash: _ }) + Some(Caller::Session { account_hash: _ }) if self.is_called_from_standard_payment() => { // Standard payment acts as a session without separate stack frame and calls // into mint's transfer. } - Some(CallStackElement::Session { account_hash }) + Some(Caller::Session { account_hash }) if account_hash == PublicKey::System.to_account_hash() => { // System calls a session code. } - Some(CallStackElement::Session { account_hash }) => { + Some(Caller::Session { account_hash }) => { // For example: a session using transfer host functions, or calling the mint's // entrypoint directly @@ -179,7 +181,7 @@ pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { } } - Some(CallStackElement::AddressableEntity { + Some(Caller::AddressableEntity { package_hash: _, entity_hash: _, }) => { @@ -198,9 +200,16 @@ pub trait Mint: RuntimeProvider + StorageProvider + SystemProvider { } if !source.is_readable() { + // TODO: I don't think we should check is_readable on the source or the target. + // is_writeable(), which is checked for below is what actually matters. + // ie it doesn't matter if the initiator can READ the balance, what matters is + // if they can transfer the token, which is controlled by having WRITE access. return Err(Error::InvalidAccessRights); } if !source.is_writeable() || !target.is_addable() { + // TODO: Similarly, I don't think we should enforce is addable on the target + // Unlike other uses of URefs (such as a counter), in this context the value represents + // a deposit of token. Generally, deposit of a desirable resource is permissive. return Err(Error::InvalidAccessRights); } let source_balance: U512 = match self.read_balance(source)? { diff --git a/storage/src/system/mint/mint_native.rs b/storage/src/system/mint/mint_native.rs new file mode 100644 index 0000000000..8c7c30198a --- /dev/null +++ b/storage/src/system/mint/mint_native.rs @@ -0,0 +1,300 @@ +use std::{cell::RefCell, rc::Rc}; +use tracing::error; + +use crate::{ + data_access_layer::transfer, + global_state::{error::Error as GlobalStateError, state::StateReader}, + system::{ + error::ProviderError, + mint::{ + runtime_provider::RuntimeProvider, storage_provider::StorageProvider, + system_provider::SystemProvider, Mint, + }, + }, + tracking_copy::{TrackingCopyEntityExt, TrackingCopyExt}, + AddressGenerator, TrackingCopy, +}; +use casper_types::{ + account::AccountHash, + addressable_entity::NamedKeys, + bytesrepr::{FromBytes, ToBytes}, + system::{mint::Error, Caller}, + AccessRights, AddressableEntity, CLTyped, CLValue, ContextAccessRights, DeployHash, Digest, + Key, Phase, ProtocolVersion, PublicKey, StoredValue, SystemEntityRegistry, Transfer, + TransferAddr, URef, U512, +}; + +pub struct NativeMintRuntime { + address_generator: AddressGenerator, + transfer_config: transfer::TransferConfig, + protocol_version: ProtocolVersion, + + tracking_copy: Rc>>, + address: AccountHash, + addressable_entity: AddressableEntity, + named_keys: NamedKeys, + access_rights: ContextAccessRights, + remaining_spending_limit: U512, + transfers: Vec, + transaction_hash: Digest, + phase: Phase, +} + +impl NativeMintRuntime +where + S: StateReader, +{ + #[allow(clippy::too_many_arguments)] + pub fn new( + transfer_config: transfer::TransferConfig, + protocol_version: ProtocolVersion, + tracking_copy: Rc>>, + address: AccountHash, + addressable_entity: AddressableEntity, + named_keys: NamedKeys, + access_rights: ContextAccessRights, + remaining_spending_limit: U512, + transaction_hash: Digest, + phase: Phase, + ) -> Self { + let address_generator = AddressGenerator::new(transaction_hash.as_ref(), phase); + let transfers = vec![]; + NativeMintRuntime { + address_generator, + transfer_config, + protocol_version, + + tracking_copy, + address, + addressable_entity, + named_keys, + access_rights, + remaining_spending_limit, + transfers, + transaction_hash, + phase, + } + } + + pub fn into_transfers(self) -> Vec { + self.transfers + } +} + +impl RuntimeProvider for NativeMintRuntime +where + S: StateReader, +{ + fn get_caller(&self) -> AccountHash { + self.address + } + + fn get_immediate_caller(&self) -> Option { + let caller = Caller::Session { + account_hash: PublicKey::System.to_account_hash(), + }; + Some(caller) + } + + fn is_called_from_standard_payment(&self) -> bool { + false + } + + fn get_system_entity_registry(&self) -> Result { + self.tracking_copy + .borrow_mut() + .get_system_entity_registry() + .map_err(|tce| { + error!(%tce, "unable to obtain system contract registry during transfer"); + ProviderError::SystemContractRegistry + }) + } + + fn read_addressable_entity_by_account_hash( + &mut self, + account_hash: AccountHash, + ) -> Result, ProviderError> { + match self + .tracking_copy + .borrow_mut() + .read_addressable_entity_by_account_hash(self.protocol_version, account_hash) + { + Ok(entity) => Ok(Some(entity)), + Err(tce) => { + error!(%tce, "error reading addressable entity by account hash"); + Err(ProviderError::AddressableEntityByAccountHash(account_hash)) + } + } + } + + fn get_phase(&self) -> Phase { + self.phase + } + + fn get_key(&self, name: &str) -> Option { + self.named_keys.get(name).cloned() + } + + fn get_approved_spending_limit(&self) -> U512 { + self.remaining_spending_limit + } + + fn sub_approved_spending_limit(&mut self, amount: U512) { + if let Some(res) = self.remaining_spending_limit.checked_sub(amount) { + self.remaining_spending_limit = res; + } else { + error!( + limit = %self.remaining_spending_limit, + spent = %amount, + "exceeded main purse spending limit" + ); + self.remaining_spending_limit = U512::zero(); + } + } + + fn get_main_purse(&self) -> URef { + self.addressable_entity.main_purse() + } + + fn is_administrator(&self, account_hash: &AccountHash) -> bool { + self.transfer_config.is_administrator(account_hash) + } + + fn allow_unrestricted_transfers(&self) -> bool { + self.transfer_config.allow_unrestricted_transfers() + } +} + +impl StorageProvider for NativeMintRuntime +where + S: StateReader, +{ + fn new_uref(&mut self, value: T) -> Result { + let cl_value: CLValue = CLValue::from_t(value).map_err(|_| Error::CLValue)?; + let uref = self + .address_generator + .new_uref(AccessRights::READ_ADD_WRITE); + self.access_rights.extend(&[uref]); + // we are creating this key now, thus we know it is a Key::URef and we grant the creator + // full permissions on it, thus we do not need to do validate key / validate uref access + // before storing it. + self.tracking_copy + .borrow_mut() + .write(Key::URef(uref), StoredValue::CLValue(cl_value)); + Ok(uref) + } + + fn read(&mut self, uref: URef) -> Result, Error> { + // check access rights on uref + if !self.access_rights.has_access_rights_to_uref(&uref) { + return Err(Error::ForgedReference); + } + let key = &Key::URef(uref); + let stored_value = match self.tracking_copy.borrow_mut().read(key) { + Ok(Some(stored_value)) => stored_value, + Ok(None) => return Ok(None), + Err(_) => return Err(Error::Storage), + }; + // by convention, we only store CLValues under Key::URef + if let StoredValue::CLValue(value) = stored_value { + // Only CLTyped instances should be stored as a CLValue. + let value = CLValue::into_t(value).map_err(|_| Error::CLValue)?; + Ok(Some(value)) + } else { + Err(Error::CLValue) + } + } + + fn write_amount(&mut self, uref: URef, amount: U512) -> Result<(), Error> { + let cl_value = CLValue::from_t(amount).map_err(|_| Error::CLValue)?; + // check access rights on uref + if !self.access_rights.has_access_rights_to_uref(&uref) { + return Err(Error::ForgedReference); + } + self.tracking_copy + .borrow_mut() + .write(Key::URef(uref), StoredValue::CLValue(cl_value)); + Ok(()) + } + + fn add(&mut self, uref: URef, value: T) -> Result<(), Error> { + let cl_value = CLValue::from_t(value).map_err(|_| Error::CLValue)?; + self.tracking_copy + .borrow_mut() + .add(Key::URef(uref), StoredValue::CLValue(cl_value)) + .map_err(|_| Error::Storage)?; + Ok(()) + } + + fn read_balance(&mut self, uref: URef) -> Result, Error> { + match self + .tracking_copy + .borrow_mut() + .get_purse_balance(Key::Balance(uref.addr())) + { + Ok(motes) => Ok(Some(motes.value())), + Err(_) => Err(Error::Storage), + } + } + + fn write_balance(&mut self, uref: URef, balance: U512) -> Result<(), Error> { + let cl_value = CLValue::from_t(balance).map_err(|_| Error::CLValue)?; + self.tracking_copy + .borrow_mut() + .write(Key::Balance(uref.addr()), StoredValue::CLValue(cl_value)); + Ok(()) + } + + fn add_balance(&mut self, uref: URef, value: U512) -> Result<(), Error> { + let cl_value = CLValue::from_t(value).map_err(|_| Error::CLValue)?; + self.tracking_copy + .borrow_mut() + .add(Key::Balance(uref.addr()), StoredValue::CLValue(cl_value)) + .map_err(|_| Error::Storage)?; + Ok(()) + } +} + +impl SystemProvider for NativeMintRuntime +where + S: StateReader, +{ + fn record_transfer( + &mut self, + maybe_to: Option, + source: URef, + target: URef, + amount: U512, + id: Option, + ) -> Result<(), Error> { + if self.phase != Phase::Session { + return Ok(()); + } + let transfer_addr = TransferAddr::new(self.address_generator.create_address()); + let key = Key::Transfer(transfer_addr); // <-- a new key variant needed to deal w/ versioned transaction hash + let transaction_hash = self.transaction_hash; + let transfer = { + // the below line is incorrect; new transaction hash is not currently supported here + // ...the transfer struct needs to be upgraded to TransactionHash + let deploy_hash = DeployHash::new(transaction_hash); + let from: AccountHash = self.get_caller(); + let fee: U512 = U512::zero(); + Transfer::new(deploy_hash, from, maybe_to, source, target, amount, fee, id) + }; + { + // im not sure why we need to collate these, but matching the existing impl + let transfers = &mut self.transfers; + transfers.push(transfer_addr); + } + + self.tracking_copy + .borrow_mut() + .write(key, StoredValue::Transfer(transfer)); + Ok(()) + } +} + +impl Mint for NativeMintRuntime where + S: StateReader +{ +} diff --git a/storage/src/system/mint/runtime_provider.rs b/storage/src/system/mint/runtime_provider.rs index e8abc4b74f..8ade0171ec 100644 --- a/storage/src/system/mint/runtime_provider.rs +++ b/storage/src/system/mint/runtime_provider.rs @@ -1,8 +1,7 @@ use crate::system::error::ProviderError; use casper_types::{ - account::AccountHash, - system::{mint::Error, CallStackElement}, - AddressableEntity, Key, Phase, SystemEntityRegistry, URef, U512, + account::AccountHash, system::Caller, AddressableEntity, Key, Phase, SystemEntityRegistry, + URef, U512, }; /// Provider of runtime host functionality. @@ -11,12 +10,12 @@ pub trait RuntimeProvider { fn get_caller(&self) -> AccountHash; /// This method should return the immediate caller of the current context. - fn get_immediate_caller(&self) -> Option<&CallStackElement>; + fn get_immediate_caller(&self) -> Option; fn is_called_from_standard_payment(&self) -> bool; /// Get system contract registry. - fn get_system_contract_registry(&self) -> Result; + fn get_system_entity_registry(&self) -> Result; fn read_addressable_entity_by_account_hash( &mut self, @@ -26,17 +25,14 @@ pub trait RuntimeProvider { /// Gets execution phase fn get_phase(&self) -> Phase; - /// This method should handle storing given [`Key`] under `name`. - fn put_key(&mut self, name: &str, key: Key) -> Result<(), Error>; - /// This method should handle obtaining a given named [`Key`] under a `name`. fn get_key(&self, name: &str) -> Option; /// Returns approved CSPR spending limit. fn get_approved_spending_limit(&self) -> U512; - /// Signal to host that `transferred` amount of tokens has been transferred. - fn sub_approved_spending_limit(&mut self, transferred: U512); + /// Signal to host that `amount` of tokens has been transferred. + fn sub_approved_spending_limit(&mut self, amount: U512); /// Returns main purse of the sender account. fn get_main_purse(&self) -> URef; diff --git a/storage/src/system/mint/storage_provider.rs b/storage/src/system/mint/storage_provider.rs index e8f6f666af..efedb9110f 100644 --- a/storage/src/system/mint/storage_provider.rs +++ b/storage/src/system/mint/storage_provider.rs @@ -12,8 +12,8 @@ pub trait StorageProvider { /// Read data from [`URef`]. fn read(&mut self, uref: URef) -> Result, Error>; - /// Write data under a [`URef`]. - fn write(&mut self, uref: URef, value: T) -> Result<(), Error>; + /// Write a [`U512`] amount under a [`URef`]. + fn write_amount(&mut self, uref: URef, amount: U512) -> Result<(), Error>; /// Add data to a [`URef`]. fn add(&mut self, uref: URef, value: T) -> Result<(), Error>; diff --git a/storage/src/system/mint/transfer.rs b/storage/src/system/mint/transfer.rs deleted file mode 100644 index ef6a9f20bb..0000000000 --- a/storage/src/system/mint/transfer.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::tracking_copy::TrackingCopyError; -use casper_types::{Digest, URef, U512}; - -/// Request for motes transfer. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct TransferRequest { - state_hash: Digest, - source: URef, - target: URef, - amount: U512, -} - -impl TransferRequest { - /// Creates new request object. - pub fn new(state_hash: Digest, source: URef, target: URef, amount: U512) -> Self { - TransferRequest { - state_hash, - source, - target, - amount, - } - } - - /// Returns state root hash. - pub fn state_hash(&self) -> Digest { - self.state_hash - } - - /// Returns source uref. - pub fn source(&self) -> URef { - self.source - } - - /// Returns target uref. - pub fn target(&self) -> URef { - self.target - } - - /// Returns amount. - pub fn amount(&self) -> U512 { - self.amount - } -} - -#[derive(Debug)] -pub enum TransferResult { - /// Invalid state root hash. - RootNotFound, - /// Transfer succeeded - Success, - /// Transfer failed - Failure, - /// Storage Error - StorageError(crate::global_state::error::Error), - /// Tracking Copy Error - TrackingCopyError(TrackingCopyError), -} diff --git a/storage/src/system/protocol_upgrade.rs b/storage/src/system/protocol_upgrade.rs index 22bce8ffdf..ed37c65e85 100644 --- a/storage/src/system/protocol_upgrade.rs +++ b/storage/src/system/protocol_upgrade.rs @@ -87,7 +87,7 @@ pub enum ProtocolUpgradeError { MissingSystemEntityHash(String), /// Tracking copy error. #[error("{0}")] - TrackingCopyError(crate::tracking_copy::TrackingCopyError), + TrackingCopy(crate::tracking_copy::TrackingCopyError), } impl From for ProtocolUpgradeError { @@ -98,7 +98,7 @@ impl From for ProtocolUpgradeError { impl From for ProtocolUpgradeError { fn from(err: crate::tracking_copy::TrackingCopyError) -> Self { - ProtocolUpgradeError::TrackingCopyError(err) + ProtocolUpgradeError::TrackingCopy(err) } } @@ -208,7 +208,8 @@ where fn system_contract_registry(&self) -> Result { debug!("system contract registry"); - let registry = if let Ok(registry) = self.tracking_copy.borrow_mut().get_system_contracts() + let registry = if let Ok(registry) = + self.tracking_copy.borrow_mut().get_system_entity_registry() { registry } else { @@ -802,18 +803,17 @@ where /// Handle bids migration. pub fn handle_bids_migration(&self) -> Result<(), ProtocolUpgradeError> { debug!("handle bids migration"); - let mut borrow = self.tracking_copy.borrow_mut(); - let existing_bid_keys = match borrow.get_keys(&KeyTag::Bid) { + let mut tc = self.tracking_copy.borrow_mut(); + let existing_bid_keys = match tc.get_keys(&KeyTag::Bid) { Ok(keys) => keys, - Err(err) => return Err(ProtocolUpgradeError::TrackingCopyError(err)), + Err(err) => return Err(ProtocolUpgradeError::TrackingCopy(err)), }; for key in existing_bid_keys { - if let Some(StoredValue::Bid(existing_bid)) = borrow - .get(&key) - .map_err(Into::::into)? + if let Some(StoredValue::Bid(existing_bid)) = + tc.get(&key).map_err(Into::::into)? { // prune away the original record, we don't need it anymore - borrow.prune(key); + tc.prune(key); if existing_bid.staked_amount().is_zero() { // the previous logic enforces unbonding all delegators of @@ -830,7 +830,7 @@ where let validator_public_key = existing_bid.validator_public_key(); let validator_bid_addr = BidAddr::from(validator_public_key.clone()); let validator_bid = ValidatorBid::from(*existing_bid.clone()); - borrow.write( + tc.write( validator_bid_addr.into(), StoredValue::BidKind(BidKind::Validator(Box::new(validator_bid))), ); @@ -846,7 +846,7 @@ where // unstaked, so technically we don't need to check for 0 balance here. // However, since it is low effort to check, doing it just to be sure. if !delegator.staked_amount().is_zero() { - borrow.write( + tc.write( delegator_bid_addr.into(), StoredValue::BidKind(BidKind::Delegator(Box::new(delegator))), ); @@ -869,7 +869,7 @@ where .tracking_copy .borrow_mut() .get(&highest_era_info_key) - .map_err(ProtocolUpgradeError::TrackingCopyError)?; + .map_err(ProtocolUpgradeError::TrackingCopy)?; match get_result { Some(stored_value @ StoredValue::EraInfo(_)) => { diff --git a/execution_engine/src/engine_state/transfer.rs b/storage/src/system/transfer.rs similarity index 68% rename from execution_engine/src/engine_state/transfer.rs rename to storage/src/system/transfer.rs index b9391faf00..f5b1aaa71e 100644 --- a/execution_engine/src/engine_state/transfer.rs +++ b/storage/src/system/transfer.rs @@ -1,16 +1,77 @@ use std::{cell::RefCell, convert::TryFrom, rc::Rc}; +use thiserror::Error; -use casper_storage::{ - global_state::{error::Error as GlobalStateError, state::StateReader}, - tracking_copy::{TrackingCopy, TrackingCopyExt}, -}; use casper_types::{ - account::AccountHash, addressable_entity::NamedKeys, system::mint, AccessRights, - AddressableEntity, ApiError, CLType, CLValueError, Key, ProtocolVersion, PublicKey, - RuntimeArgs, StoredValue, URef, U512, + account::AccountHash, + addressable_entity::NamedKeys, + bytesrepr::FromBytes, + system::{mint, mint::Error as MintError}, + AccessRights, AddressableEntity, CLType, CLTyped, CLValue, CLValueError, Key, ProtocolVersion, + RuntimeArgs, StoredValue, StoredValueTypeMismatch, URef, U512, }; -use crate::{engine_state::Error, execution::Error as ExecError}; +use crate::{ + global_state::{error::Error as GlobalStateError, state::StateReader}, + tracking_copy::{TrackingCopy, TrackingCopyEntityExt, TrackingCopyError, TrackingCopyExt}, +}; + +#[derive(Clone, Error, Debug)] +pub enum TransferError { + /// Invalid key variant. + #[error("Invalid key {0}")] + UnexpectedKeyVariant(Key), + /// Type mismatch error. + #[error("{}", _0)] + TypeMismatch(StoredValueTypeMismatch), + /// Forged reference error. + #[error("Forged reference: {}", _0)] + ForgedReference(URef), + /// Invalid access. + #[error("Invalid access rights: {}", required)] + InvalidAccess { + /// Required access rights of the operation. + required: AccessRights, + }, + /// Error converting a CLValue. + #[error("{0}")] + CLValue(CLValueError), + /// Invalid purse. + #[error("Invalid purse")] + InvalidPurse, + /// Invalid argument. + #[error("Invalid argument")] + InvalidArgument, + /// Missing argument. + #[error("Missing argument")] + MissingArgument, + /// Invalid purse. + #[error("Attempt to transfer amount 0")] + AttemptToTransferZero, + /// Invalid operation. + #[error("Invalid operation")] + InvalidOperation, + /// Failed to transfer tokens on a private chain. + #[error("Failed to transfer with unrestricted transfers disabled")] + DisabledUnrestrictedTransfers, + /// Tracking copy error. + #[error("{0}")] + TrackingCopy(TrackingCopyError), + /// Mint error. + #[error("{0}")] + Mint(MintError), +} + +impl From for TransferError { + fn from(gse: GlobalStateError) -> Self { + TransferError::TrackingCopy(TrackingCopyError::Storage(gse)) + } +} + +impl From for TransferError { + fn from(tce: TrackingCopyError) -> Self { + TransferError::TrackingCopy(tce) + } +} /// A target mode indicates if a native transfer's arguments will resolve to an existing purse, or /// will have to create a new account first. @@ -27,7 +88,7 @@ pub enum TransferTargetMode { /// A target mode indicates if a native transfer's arguments will resolve to an existing purse, or /// will have to create a new account first. #[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) enum NewTransferTargetMode { +pub enum NewTransferTargetMode { /// Native transfer arguments resolved into a transfer to an existing account. ExistingAccount { /// Existing account hash. @@ -44,7 +105,7 @@ pub(crate) enum NewTransferTargetMode { /// Mint's transfer arguments. /// /// A struct has a benefit of static typing, which is helpful while resolving the arguments. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TransferArgs { to: Option, source: URef, @@ -81,6 +142,11 @@ impl TransferArgs { self.source } + /// Returns `target` field. + pub fn target(&self) -> URef { + self.target + } + /// Returns `arg_id` field. pub fn arg_id(&self) -> Option { self.arg_id @@ -110,7 +176,7 @@ impl TryFrom for RuntimeArgs { /// State of a builder of a `TransferArgs`. /// -/// Purpose of this builder is to resolve native tranfer args into [`TransferTargetMode`] and a +/// Purpose of this builder is to resolve native transfer args into [`TransferTargetMode`] and a /// [`TransferArgs`] instance to execute actual token transfer on the mint contract. #[derive(Clone, Debug, PartialEq, Eq)] pub struct TransferRuntimeArgsBuilder { @@ -151,9 +217,10 @@ impl TransferRuntimeArgsBuilder { fn resolve_source_uref( &self, account: &AddressableEntity, - named_keys: NamedKeys, + named_keys: NamedKeys, /* TODO: consider passing in URef values inside named keys + * instead of entire named keys */ tracking_copy: Rc>>, - ) -> Result + ) -> Result where R: StateReader, { @@ -161,7 +228,7 @@ impl TransferRuntimeArgsBuilder { let arg_name = mint::ARG_SOURCE; match imputed_runtime_args.get(arg_name) { Some(cl_value) if *cl_value.cl_type() == CLType::URef => { - let uref: URef = cl_value.clone().into_t().map_err(Error::reverter)?; + let uref: URef = self.map_cl_value(cl_value)?; if account.main_purse().addr() == uref.addr() { return Ok(uref); @@ -177,26 +244,24 @@ impl TransferRuntimeArgsBuilder { if found_uref.is_writeable() { // it is a URef and caller has access but is it a purse URef? if !self.purse_exists(found_uref.to_owned(), tracking_copy) { - return Err(Error::reverter(ApiError::InvalidPurse)); + return Err(TransferError::InvalidPurse); } Ok(uref) } else { - Err(Error::Exec(ExecError::InvalidAccess { + Err(TransferError::InvalidAccess { required: AccessRights::WRITE, - })) + }) } } - Some(key) => Err(Error::Exec(ExecError::TypeMismatch( - casper_types::StoredValueTypeMismatch::new( - "Key::URef".to_string(), - key.type_string(), - ), + Some(key) => Err(TransferError::TypeMismatch(StoredValueTypeMismatch::new( + "Key::URef".to_string(), + key.type_string(), ))), - None => Err(Error::Exec(ExecError::ForgedReference(uref))), + None => Err(TransferError::ForgedReference(uref)), } } - Some(_) => Err(Error::reverter(ApiError::InvalidArgument)), + Some(_) => Err(TransferError::InvalidArgument), None => Ok(account.main_purse()), // if no source purse passed use account main purse } } @@ -207,17 +272,17 @@ impl TransferRuntimeArgsBuilder { /// * an existing purse [`URef`] /// * a 32-byte array, interpreted as an account hash /// * a [`Key::Account`], from which the account hash is extracted - /// * a [`PublicKey`], which is converted to an account hash + /// * a [`casper_types::PublicKey`], which is converted to an account hash /// /// If the "target" account hash is not existing, then a special variant is returned that /// indicates that the system has to create new account first. /// /// Returns [`NewTransferTargetMode`] with a resolved variant. - pub(super) fn resolve_transfer_target_mode( + pub fn resolve_transfer_target_mode( &mut self, protocol_version: ProtocolVersion, tracking_copy: Rc>>, - ) -> Result + ) -> Result where R: StateReader, { @@ -226,34 +291,30 @@ impl TransferRuntimeArgsBuilder { let account_hash = match imputed_runtime_args.get(arg_name) { Some(cl_value) if *cl_value.cl_type() == CLType::URef => { - let uref: URef = cl_value.clone().into_t().map_err(Error::reverter)?; + let uref = self.map_cl_value(cl_value)?; if !self.purse_exists(uref, tracking_copy) { - return Err(Error::reverter(ApiError::InvalidPurse)); + return Err(TransferError::InvalidPurse); } return Ok(NewTransferTargetMode::PurseExists(uref)); } Some(cl_value) if *cl_value.cl_type() == CLType::ByteArray(32) => { - let account_hash: AccountHash = - cl_value.clone().into_t().map_err(Error::reverter)?; - account_hash + self.map_cl_value(cl_value)? } Some(cl_value) if *cl_value.cl_type() == CLType::Key => { - let account_key: Key = cl_value.clone().into_t().map_err(Error::reverter)?; - + let account_key: Key = self.map_cl_value(cl_value)?; let account_hash: AccountHash = account_key .into_account() - .ok_or_else(|| Error::reverter(ApiError::Transfer))?; + .ok_or_else(|| TransferError::UnexpectedKeyVariant(account_key))?; account_hash } Some(cl_value) if *cl_value.cl_type() == CLType::PublicKey => { - let public_key: PublicKey = cl_value.clone().into_t().map_err(Error::reverter)?; - + let public_key = self.map_cl_value(cl_value)?; AccountHash::from(&public_key) } - Some(_) => return Err(Error::reverter(ApiError::InvalidArgument)), - None => return Err(Error::reverter(ApiError::MissingArgument)), + Some(_) => return Err(TransferError::InvalidArgument), + None => return Err(TransferError::MissingArgument), }; match tracking_copy @@ -275,38 +336,34 @@ impl TransferRuntimeArgsBuilder { /// Resolves amount. /// /// User has to specify "amount" argument that could be either a [`U512`] or a u64. - fn resolve_amount(&self) -> Result { + fn resolve_amount(&self) -> Result { let imputed_runtime_args = &self.inner; let amount = match imputed_runtime_args.get(mint::ARG_AMOUNT) { - Some(amount_value) if *amount_value.cl_type() == CLType::U512 => amount_value - .clone() - .into_t::() - .map_err(Error::reverter)?, + Some(amount_value) if *amount_value.cl_type() == CLType::U512 => { + self.map_cl_value(amount_value)? + } Some(amount_value) if *amount_value.cl_type() == CLType::U64 => { - let amount = amount_value - .clone() - .into_t::() - .map_err(Error::reverter)?; + let amount: u64 = self.map_cl_value(amount_value)?; U512::from(amount) } - Some(_) => return Err(Error::reverter(ApiError::InvalidArgument)), - None => return Err(Error::reverter(ApiError::MissingArgument)), + Some(_) => return Err(TransferError::InvalidArgument), + None => return Err(TransferError::MissingArgument), }; if amount.is_zero() { - return Err(Error::reverter(ApiError::Transfer)); + return Err(TransferError::AttemptToTransferZero); } Ok(amount) } - fn resolve_id(&self) -> Result, Error> { + fn resolve_id(&self) -> Result, TransferError> { let id_value = self .inner .get(mint::ARG_ID) - .ok_or_else(|| Error::reverter(ApiError::MissingArgument))?; - let id: Option = id_value.clone().into_t().map_err(Error::reverter)?; + .ok_or_else(|| TransferError::MissingArgument)?; + let id: Option = self.map_cl_value(id_value)?; Ok(id) } @@ -317,7 +374,7 @@ impl TransferRuntimeArgsBuilder { entity_named_keys: NamedKeys, protocol_version: ProtocolVersion, tracking_copy: Rc>>, - ) -> Result + ) -> Result where R: StateReader, { @@ -333,7 +390,7 @@ impl TransferRuntimeArgsBuilder { // 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)); + return Err(TransferError::InvalidOperation); } }; @@ -341,7 +398,7 @@ impl TransferRuntimeArgsBuilder { self.resolve_source_uref(from, entity_named_keys, Rc::clone(&tracking_copy))?; if source_uref.addr() == target_uref.addr() { - return Err(Error::reverter(ApiError::InvalidPurse)); + return Err(TransferError::InvalidPurse); } let amount = self.resolve_amount()?; @@ -356,4 +413,8 @@ impl TransferRuntimeArgsBuilder { arg_id: id, }) } + + fn map_cl_value(&self, cl_value: &CLValue) -> Result { + cl_value.clone().into_t().map_err(TransferError::CLValue) + } } diff --git a/storage/src/tracking_copy/error.rs b/storage/src/tracking_copy/error.rs index bc71d108fc..fc4138bbaa 100644 --- a/storage/src/tracking_copy/error.rs +++ b/storage/src/tracking_copy/error.rs @@ -168,9 +168,6 @@ pub enum Error { /// Invalid key #[error("Invalid key {0}")] UnexpectedKeyVariant(Key), - /// Failed to transfer tokens on a private chain. - #[error("Failed to transfer with unrestricted transfers disabled")] - DisabledUnrestrictedTransfers, /// Weight of all used associated keys does not meet entity's upgrade threshold. #[error("Deployment authorization failure")] UpgradeAuthorizationFailure, @@ -195,6 +192,9 @@ pub enum Error { /// Missing bid. #[error("Missing bid: {0}")] MissingBid(Key), + /// Not authorized. + #[error("Authorization error")] + Authorization, } impl Error { diff --git a/storage/src/tracking_copy/ext.rs b/storage/src/tracking_copy/ext.rs index 12012eef1c..0e59d5c98d 100644 --- a/storage/src/tracking_copy/ext.rs +++ b/storage/src/tracking_copy/ext.rs @@ -1,24 +1,13 @@ -use std::{ - collections::BTreeSet, - convert::{TryFrom, TryInto}, -}; +use std::{collections::BTreeSet, convert::TryInto}; -use crate::{ - global_state::{ - error::Error as GlobalStateError, state::StateReader, trie::merkle_proof::TrieMerkleProof, - }, - AddressGenerator, +use crate::global_state::{ + error::Error as GlobalStateError, state::StateReader, trie::merkle_proof::TrieMerkleProof, }; use casper_types::{ - account::AccountHash, - addressable_entity::{EntityKindTag, MessageTopics, NamedKeyAddr, NamedKeys}, - bytesrepr, - package::{EntityVersions, Groups, PackageStatus}, - AccessRights, AddressableEntity, AddressableEntityHash, ByteCode, ByteCodeAddr, ByteCodeHash, - CLValue, ChecksumRegistry, EntityAddr, EntityKind, EntryPoints, Key, KeyTag, Motes, Package, - PackageHash, Phase, ProtocolVersion, StoredValue, StoredValueTypeMismatch, - SystemEntityRegistry, URef, + account::AccountHash, addressable_entity::NamedKeys, ByteCode, ByteCodeAddr, ByteCodeHash, + CLValue, ChecksumRegistry, EntityAddr, Key, KeyTag, Motes, Package, PackageHash, StoredValue, + StoredValueTypeMismatch, SystemEntityRegistry, URef, }; use crate::tracking_copy::{TrackingCopy, TrackingCopyError}; @@ -28,29 +17,9 @@ pub trait TrackingCopyExt { /// The type for the returned errors. type Error; - /// Gets the entity hash for an account hash. - fn get_entity_hash_by_account_hash( - &mut self, - account_hash: AccountHash, - ) -> Result; - - /// Gets the entity for a given account by its account hash. - fn get_addressable_entity_by_account_hash( - &mut self, - protocol_version: ProtocolVersion, - account_hash: AccountHash, - ) -> Result; - /// Reads the entity key for a given account hash. fn read_account_key(&mut self, account_hash: AccountHash) -> Result; - /// Reads the entity by its account hash. - fn read_addressable_entity_by_account_hash( - &mut self, - protocol_version: ProtocolVersion, - account_hash: AccountHash, - ) -> Result; - /// Gets the purse balance key for a given purse. fn get_purse_balance_key(&self, purse_key: Key) -> Result; @@ -69,39 +38,20 @@ pub trait TrackingCopyExt { balance_key: Key, ) -> Result<(Motes, TrieMerkleProof), Self::Error>; - /// Gets an addressable entity by hash. - fn get_addressable_entity( - &mut self, - addressable_entity_hash: AddressableEntityHash, - ) -> Result; - /// Returns the collection of named keys for a given AddressableEntity. fn get_named_keys(&mut self, entity_addr: EntityAddr) -> Result; - /// Gets an entity by hash. - fn get_entity( - &mut self, - entity_hash: AddressableEntityHash, - ) -> Result<(AddressableEntity, bool), Self::Error>; - /// Gets a package by hash. fn get_package(&mut self, package_hash: PackageHash) -> Result; /// Gets the system contract registry. - fn get_system_contracts(&mut self) -> Result; + fn get_system_entity_registry(&mut self) -> Result; /// Gets the system checksum registry. fn get_checksum_registry(&mut self) -> Result, Self::Error>; /// Gets byte code by hash. fn get_byte_code(&mut self, byte_code_hash: ByteCodeHash) -> Result; - - /// Migrate the NamedKeys for a Contract or Account. - fn migrate_named_keys( - &mut self, - entity_addr: EntityAddr, - named_keys: NamedKeys, - ) -> Result<(), Self::Error>; } impl TrackingCopyExt for TrackingCopy @@ -110,111 +60,6 @@ where { type Error = TrackingCopyError; - fn get_entity_hash_by_account_hash( - &mut self, - account_hash: AccountHash, - ) -> Result { - let account_key = Key::Account(account_hash); - match self.get(&account_key)? { - Some(StoredValue::CLValue(cl_value)) => { - let entity_key = CLValue::into_t::(cl_value)?; - let entity_hash = AddressableEntityHash::try_from(entity_key) - .map_err(|_| TrackingCopyError::BytesRepr(bytesrepr::Error::Formatting))?; - - Ok(entity_hash) - } - Some(other) => Err(TrackingCopyError::TypeMismatch( - StoredValueTypeMismatch::new("CLValue".to_string(), other.type_name()), - )), - None => Err(TrackingCopyError::KeyNotFound(account_key)), - } - } - - fn get_addressable_entity_by_account_hash( - &mut self, - protocol_version: ProtocolVersion, - account_hash: AccountHash, - ) -> Result { - let account_key = Key::Account(account_hash); - - let contract_key = match self.get(&account_key)? { - Some(StoredValue::CLValue(contract_key_as_cl_value)) => { - CLValue::into_t::(contract_key_as_cl_value)? - } - Some(StoredValue::Account(account)) => { - // do a legacy account migration - let mut generator = - AddressGenerator::new(account.main_purse().addr().as_ref(), Phase::System); - - let byte_code_hash = ByteCodeHash::default(); - let entity_hash = AddressableEntityHash::new(generator.new_hash_address()); - let package_hash = PackageHash::new(generator.new_hash_address()); - - let entry_points = EntryPoints::new(); - - self.migrate_named_keys( - EntityAddr::Account(entity_hash.value()), - account.named_keys().clone(), - )?; - - let entity = AddressableEntity::new( - package_hash, - byte_code_hash, - entry_points, - protocol_version, - account.main_purse(), - account.associated_keys().clone().into(), - account.action_thresholds().clone().into(), - MessageTopics::default(), - EntityKind::Account(account_hash), - ); - - let access_key = generator.new_uref(AccessRights::READ_ADD_WRITE); - - let package = { - let mut package = Package::new( - access_key, - EntityVersions::default(), - BTreeSet::default(), - Groups::default(), - PackageStatus::Locked, - ); - package.insert_entity_version(protocol_version.value().major, entity_hash); - package - }; - - let entity_key = entity.entity_key(entity_hash); - - self.write(entity_key, StoredValue::AddressableEntity(entity.clone())); - self.write(package_hash.into(), package.into()); - - let contract_by_account = match CLValue::from_t(entity_key) { - Ok(cl_value) => cl_value, - Err(error) => return Err(TrackingCopyError::CLValue(error)), - }; - - self.write(account_key, StoredValue::CLValue(contract_by_account)); - - return Ok(entity); - } - - Some(other) => { - return Err(TrackingCopyError::TypeMismatch( - StoredValueTypeMismatch::new("Key".to_string(), other.type_name()), - )); - } - None => return Err(TrackingCopyError::KeyNotFound(account_key)), - }; - - match self.get(&contract_key)? { - Some(StoredValue::AddressableEntity(contract)) => Ok(contract), - Some(other) => Err(TrackingCopyError::TypeMismatch( - StoredValueTypeMismatch::new("Contract".to_string(), other.type_name()), - )), - None => Err(TrackingCopyError::KeyNotFound(contract_key)), - } - } - fn read_account_key(&mut self, account_hash: AccountHash) -> Result { let account_key = Key::Account(account_hash); match self.read(&account_key)? { @@ -226,14 +71,6 @@ where } } - fn read_addressable_entity_by_account_hash( - &mut self, - protocol_version: ProtocolVersion, - account_hash: AccountHash, - ) -> Result { - self.get_addressable_entity_by_account_hash(protocol_version, account_hash) - } - fn get_purse_balance_key(&self, purse_key: Key) -> Result { let balance_key: URef = purse_key .into_uref() @@ -298,49 +135,6 @@ where } } - fn get_addressable_entity( - &mut self, - entity_hash: AddressableEntityHash, - ) -> Result { - let package_kind_tag = if self.get_system_contracts()?.has_contract_hash(&entity_hash) { - EntityKindTag::System - } else { - EntityKindTag::SmartContract - }; - - let key = Key::addressable_entity_key(package_kind_tag, entity_hash); - - match self.read(&key)? { - Some(StoredValue::AddressableEntity(entity)) => Ok(entity), - Some(other) => Err(TrackingCopyError::TypeMismatch( - StoredValueTypeMismatch::new( - "AddressableEntity or Contract".to_string(), - other.type_name(), - ), - )), - None => Err(TrackingCopyError::KeyNotFound(key)), - } - } - - fn migrate_named_keys( - &mut self, - entity_addr: EntityAddr, - named_keys: NamedKeys, - ) -> Result<(), Self::Error> { - for (name, key) in named_keys.iter() { - let entry_key = { - let entry_addr = NamedKeyAddr::new_from_string(entity_addr, name.clone())?; - Key::NamedKey(entry_addr) - }; - - let cl_value = CLValue::from_t(*key).map_err(TrackingCopyError::CLValue)?; - - self.write(entry_key, StoredValue::CLValue(cl_value)) - } - - Ok(()) - } - fn get_named_keys(&mut self, entity_addr: EntityAddr) -> Result { let prefix = entity_addr .named_keys_prefix() @@ -430,29 +224,7 @@ where } } - fn get_entity( - &mut self, - entity_hash: AddressableEntityHash, - ) -> Result<(AddressableEntity, bool), Self::Error> { - let key = Key::contract_entity_key(entity_hash); - match self.read(&key)? { - Some(StoredValue::AddressableEntity(entity)) => Ok((entity, false)), - Some(other) => Err(Self::Error::TypeMismatch(StoredValueTypeMismatch::new( - "AddressableEntity".to_string(), - other.type_name(), - ))), - None => match self.read(&Key::Hash(entity_hash.value()))? { - Some(StoredValue::Contract(contract)) => Ok((contract.into(), true)), - Some(other) => Err(Self::Error::TypeMismatch(StoredValueTypeMismatch::new( - "Contract".to_string(), - other.type_name(), - ))), - None => Err(TrackingCopyError::KeyNotFound(key)), - }, - } - } - - fn get_system_contracts(&mut self) -> Result { + fn get_system_entity_registry(&mut self) -> Result { match self.get(&Key::SystemEntityRegistry)? { Some(StoredValue::CLValue(registry)) => { let registry: SystemEntityRegistry = diff --git a/storage/src/tracking_copy/ext_entity.rs b/storage/src/tracking_copy/ext_entity.rs new file mode 100644 index 0000000000..827a2c0d58 --- /dev/null +++ b/storage/src/tracking_copy/ext_entity.rs @@ -0,0 +1,376 @@ +use std::{collections::BTreeSet, convert::TryFrom}; + +use casper_types::{ + account::AccountHash, + addressable_entity::{ + ActionThresholds, AssociatedKeys, EntityKindTag, MessageTopics, NamedKeyAddr, + NamedKeyValue, NamedKeys, Weight, + }, + bytesrepr, + package::{EntityVersions, Groups, PackageStatus}, + AccessRights, Account, AddressableEntity, AddressableEntityHash, ByteCodeHash, CLValue, + EntityAddr, EntityKind, EntryPoints, Key, Package, PackageHash, Phase, ProtocolVersion, + StoredValue, StoredValueTypeMismatch, +}; + +use crate::{ + global_state::{error::Error as GlobalStateError, state::StateReader}, + tracking_copy::{TrackingCopy, TrackingCopyError, TrackingCopyExt}, + AddressGenerator, +}; + +/// Higher-level operations on the state via a `TrackingCopy`. +pub trait TrackingCopyEntityExt { + /// The type for the returned errors. + type Error; + + /// Gets an addressable entity by hash. + fn get_addressable_entity( + &mut self, + addressable_entity_hash: AddressableEntityHash, + ) -> Result; + + /// Gets the entity hash for an account hash. + fn get_entity_hash_by_account_hash( + &mut self, + account_hash: AccountHash, + ) -> Result; + + /// Gets the entity for a given account by its account hash. + fn get_addressable_entity_by_account_hash( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + ) -> Result; + + /// Reads the entity by its account hash. + fn read_addressable_entity_by_account_hash( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + ) -> Result; + + /// Get entity if authorized, else error. + fn get_authorized_addressable_entity( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + authorization_keys: &BTreeSet, + administrative_accounts: &BTreeSet, + ) -> Result<(AddressableEntity, AddressableEntityHash), Self::Error>; + + /// Migrate the NamedKeys for a Contract or Account. + fn migrate_named_keys( + &mut self, + entity_addr: EntityAddr, + named_keys: NamedKeys, + ) -> Result<(), Self::Error>; + + fn migrate_account( + &mut self, + account_hash: AccountHash, + protocol_version: ProtocolVersion, + ) -> Result<(), Self::Error>; + + fn create_addressable_entity_from_account( + &mut self, + account: Account, + protocol_version: ProtocolVersion, + ) -> Result<(), Self::Error>; +} + +impl TrackingCopyEntityExt for TrackingCopy +where + R: StateReader, +{ + type Error = TrackingCopyError; + + fn get_addressable_entity( + &mut self, + entity_hash: AddressableEntityHash, + ) -> Result { + let package_kind_tag = if self + .get_system_entity_registry()? + .has_contract_hash(&entity_hash) + { + EntityKindTag::System + } else { + EntityKindTag::SmartContract + }; + + let key = Key::addressable_entity_key(package_kind_tag, entity_hash); + + match self.read(&key)? { + Some(StoredValue::AddressableEntity(entity)) => Ok(entity), + Some(other) => Err(TrackingCopyError::TypeMismatch( + StoredValueTypeMismatch::new( + "AddressableEntity or Contract".to_string(), + other.type_name(), + ), + )), + None => Err(TrackingCopyError::KeyNotFound(key)), + } + } + + fn get_entity_hash_by_account_hash( + &mut self, + account_hash: AccountHash, + ) -> Result { + let account_key = Key::Account(account_hash); + match self.get(&account_key)? { + Some(StoredValue::CLValue(cl_value)) => { + let entity_key = CLValue::into_t::(cl_value)?; + let entity_hash = AddressableEntityHash::try_from(entity_key) + .map_err(|_| TrackingCopyError::BytesRepr(bytesrepr::Error::Formatting))?; + + Ok(entity_hash) + } + Some(other) => Err(TrackingCopyError::TypeMismatch( + StoredValueTypeMismatch::new("CLValue".to_string(), other.type_name()), + )), + None => Err(TrackingCopyError::KeyNotFound(account_key)), + } + } + + fn get_addressable_entity_by_account_hash( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + ) -> Result { + let account_key = Key::Account(account_hash); + + let contract_key = match self.get(&account_key)? { + Some(StoredValue::CLValue(contract_key_as_cl_value)) => { + CLValue::into_t::(contract_key_as_cl_value)? + } + Some(StoredValue::Account(account)) => { + // do a legacy account migration + let mut generator = + AddressGenerator::new(account.main_purse().addr().as_ref(), Phase::System); + + let byte_code_hash = ByteCodeHash::default(); + let entity_hash = AddressableEntityHash::new(generator.new_hash_address()); + let package_hash = PackageHash::new(generator.new_hash_address()); + + let entry_points = EntryPoints::new(); + + self.migrate_named_keys( + EntityAddr::Account(entity_hash.value()), + account.named_keys().clone(), + )?; + + let entity = AddressableEntity::new( + package_hash, + byte_code_hash, + entry_points, + protocol_version, + account.main_purse(), + account.associated_keys().clone().into(), + account.action_thresholds().clone().into(), + MessageTopics::default(), + EntityKind::Account(account_hash), + ); + + let access_key = generator.new_uref(AccessRights::READ_ADD_WRITE); + + let package = { + let mut package = Package::new( + access_key, + EntityVersions::default(), + BTreeSet::default(), + Groups::default(), + PackageStatus::Locked, + ); + package.insert_entity_version(protocol_version.value().major, entity_hash); + package + }; + + let entity_key = entity.entity_key(entity_hash); + + self.write(entity_key, StoredValue::AddressableEntity(entity.clone())); + self.write(package_hash.into(), package.into()); + + let contract_by_account = match CLValue::from_t(entity_key) { + Ok(cl_value) => cl_value, + Err(error) => return Err(TrackingCopyError::CLValue(error)), + }; + + self.write(account_key, StoredValue::CLValue(contract_by_account)); + + return Ok(entity); + } + + Some(other) => { + return Err(TrackingCopyError::TypeMismatch( + StoredValueTypeMismatch::new("Key".to_string(), other.type_name()), + )); + } + None => return Err(TrackingCopyError::KeyNotFound(account_key)), + }; + + match self.get(&contract_key)? { + Some(StoredValue::AddressableEntity(contract)) => Ok(contract), + Some(other) => Err(TrackingCopyError::TypeMismatch( + StoredValueTypeMismatch::new("Contract".to_string(), other.type_name()), + )), + None => Err(TrackingCopyError::KeyNotFound(contract_key)), + } + } + + fn read_addressable_entity_by_account_hash( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + ) -> Result { + self.get_addressable_entity_by_account_hash(protocol_version, account_hash) + } + + fn get_authorized_addressable_entity( + &mut self, + protocol_version: ProtocolVersion, + account_hash: AccountHash, + authorization_keys: &BTreeSet, + administrative_accounts: &BTreeSet, + ) -> Result<(AddressableEntity, AddressableEntityHash), Self::Error> { + let entity_record = + self.get_addressable_entity_by_account_hash(protocol_version, account_hash)?; + + let entity_hash = self.get_entity_hash_by_account_hash(account_hash)?; + + if !administrative_accounts.is_empty() + && administrative_accounts + .intersection(authorization_keys) + .next() + .is_some() + { + // Exit early if there's at least a single signature coming from an admin. + return Ok((entity_record, entity_hash)); + } + + // Authorize using provided authorization keys + if !entity_record.can_authorize(authorization_keys) { + return Err(Self::Error::Authorization); + } + + // Check total key weight against deploy threshold + if !entity_record.can_deploy_with(authorization_keys) { + return Err(Self::Error::DeploymentAuthorizationFailure); + } + + Ok((entity_record, entity_hash)) + } + + fn migrate_named_keys( + &mut self, + entity_addr: EntityAddr, + named_keys: NamedKeys, + ) -> 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) + } + + Ok(()) + } + + fn migrate_account( + &mut self, + account_hash: AccountHash, + protocol_version: ProtocolVersion, + ) -> Result<(), Self::Error> { + let key = Key::Account(account_hash); + let maybe_stored_value = self.read(&key)?; + + match maybe_stored_value { + Some(StoredValue::Account(account)) => { + self.create_addressable_entity_from_account(account, protocol_version) + } + Some(StoredValue::CLValue(_)) => Ok(()), + // This means the Account does not exist, which we consider to be + // an authorization error. As used by the node, this type of deploy + // will have already been filtered out, but for other EE use cases + // and testing it is reachable. + Some(_) => Err(Self::Error::UnexpectedStoredValueVariant), + None => Err(Self::Error::AccountNotFound(key)), + } + } + + fn create_addressable_entity_from_account( + &mut self, + account: Account, + protocol_version: ProtocolVersion, + ) -> Result<(), Self::Error> { + let account_hash = account.account_hash(); + + let mut generator = + AddressGenerator::new(account.main_purse().addr().as_ref(), Phase::System); + + let byte_code_hash = ByteCodeHash::default(); + let entity_hash = AddressableEntityHash::new(generator.new_hash_address()); + let package_hash = PackageHash::new(generator.new_hash_address()); + + let entry_points = EntryPoints::new(); + + let associated_keys = AssociatedKeys::from(account.associated_keys().clone()); + let action_thresholds = { + let account_threshold = account.action_thresholds().clone(); + ActionThresholds::new( + Weight::new(account_threshold.deployment.value()), + Weight::new(1u8), + Weight::new(account_threshold.key_management.value()), + ) + .map_err(Self::Error::SetThresholdFailure)? + }; + + let entity_addr = EntityAddr::new_account_entity_addr(entity_hash.value()); + + self.migrate_named_keys(entity_addr, account.named_keys().clone())?; + + let entity = AddressableEntity::new( + package_hash, + byte_code_hash, + entry_points, + protocol_version, + account.main_purse(), + associated_keys, + action_thresholds, + MessageTopics::default(), + EntityKind::Account(account_hash), + ); + + let access_key = generator.new_uref(AccessRights::READ_ADD_WRITE); + + let package = { + let mut package = Package::new( + access_key, + EntityVersions::default(), + BTreeSet::default(), + Groups::default(), + PackageStatus::Locked, + ); + package.insert_entity_version(protocol_version.value().major, entity_hash); + package + }; + + let entity_key: Key = entity.entity_key(entity_hash); + + self.write(entity_key, entity.into()); + self.write(package_hash.into(), package.into()); + let contract_by_account = match CLValue::from_t(entity_key) { + Ok(cl_value) => cl_value, + Err(err) => return Err(Self::Error::CLValue(err)), + }; + + self.write( + Key::Account(account_hash), + StoredValue::CLValue(contract_by_account), + ); + Ok(()) + } +} diff --git a/storage/src/tracking_copy/mod.rs b/storage/src/tracking_copy/mod.rs index 5d22148c9d..218b6c8cf7 100644 --- a/storage/src/tracking_copy/mod.rs +++ b/storage/src/tracking_copy/mod.rs @@ -4,6 +4,7 @@ mod byte_size; mod error; mod ext; +mod ext_entity; pub(self) mod meter; #[cfg(test)] mod tests; @@ -30,7 +31,9 @@ use casper_types::{ }; use self::meter::{heap_meter::HeapSize, Meter}; -pub use self::{error::Error as TrackingCopyError, ext::TrackingCopyExt}; +pub use self::{ + error::Error as TrackingCopyError, ext::TrackingCopyExt, ext_entity::TrackingCopyEntityExt, +}; /// Result of a query on a `TrackingCopy`. #[derive(Debug)] diff --git a/types/src/access_rights.rs b/types/src/access_rights.rs index 818ae3aa80..ad63a4ae05 100644 --- a/types/src/access_rights.rs +++ b/types/src/access_rights.rs @@ -7,6 +7,7 @@ use core::fmt::{self, Display, Formatter}; use bitflags::bitflags; #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -124,6 +125,7 @@ impl<'de> Deserialize<'de> for AccessRights { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> AccessRights { let mut result = AccessRights::NONE; diff --git a/types/src/account/account_hash.rs b/types/src/account/account_hash.rs index 1e4ff6d1a8..0d0e0a73f6 100644 --- a/types/src/account/account_hash.rs +++ b/types/src/account/account_hash.rs @@ -5,6 +5,7 @@ use core::{ }; #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -205,6 +206,7 @@ impl AsRef<[u8]> for AccountHash { #[derive(Debug)] pub struct TryFromSliceForAccountHashError(()); +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> AccountHash { AccountHash::new(rng.gen()) diff --git a/types/src/addressable_entity.rs b/types/src/addressable_entity.rs index dcf37e9547..44f22f0b16 100644 --- a/types/src/addressable_entity.rs +++ b/types/src/addressable_entity.rs @@ -1691,7 +1691,7 @@ impl AddressableEntity { self.entity_kind } - /// Is the given Package associated to an Account. + /// Is this an account? pub fn is_account_kind(&self) -> bool { matches!(self.entity_kind, EntityKind::Account(_)) } diff --git a/types/src/block.rs b/types/src/block.rs index 687a393e51..0d662e8608 100644 --- a/types/src/block.rs +++ b/types/src/block.rs @@ -11,12 +11,8 @@ mod json_compatibility; mod rewarded_signatures; mod rewards; mod signed_block_header; - -#[cfg(any(feature = "testing", test))] -mod test_block_builder { - pub mod test_block_v1_builder; - pub mod test_block_v2_builder; -} +#[cfg(any(all(feature = "std", feature = "testing"), test))] +mod test_block_builder; use alloc::{boxed::Box, vec::Vec}; use core::fmt::{self, Display, Formatter}; @@ -50,11 +46,8 @@ pub use json_compatibility::JsonBlockWithSignatures; pub use rewarded_signatures::{RewardedSignatures, SingleBlockRewardedSignatures}; pub use rewards::Rewards; pub use signed_block_header::{SignedBlockHeader, SignedBlockHeaderValidationError}; -#[cfg(any(feature = "testing", test))] -pub use test_block_builder::{ - test_block_v1_builder::TestBlockV1Builder, - test_block_v2_builder::TestBlockV2Builder as TestBlockBuilder, -}; +#[cfg(any(all(feature = "std", feature = "testing"), test))] +pub use test_block_builder::{TestBlockBuilder, TestBlockV1Builder}; #[cfg(feature = "json-schema")] static BLOCK: Lazy = Lazy::new(|| BlockV2::example().into()); diff --git a/types/src/block/test_block_builder.rs b/types/src/block/test_block_builder.rs new file mode 100644 index 0000000000..d514963781 --- /dev/null +++ b/types/src/block/test_block_builder.rs @@ -0,0 +1,5 @@ +mod test_block_v1_builder; +mod test_block_v2_builder; + +pub use test_block_v1_builder::TestBlockV1Builder; +pub use test_block_v2_builder::TestBlockV2Builder as TestBlockBuilder; diff --git a/types/src/block_time.rs b/types/src/block_time.rs index f278a36b87..935afa6e2b 100644 --- a/types/src/block_time.rs +++ b/types/src/block_time.rs @@ -29,6 +29,11 @@ impl BlockTime { pub fn saturating_sub(self, other: BlockTime) -> Self { BlockTime(self.0.saturating_sub(other.0)) } + + /// Returns inner value. + pub fn value(&self) -> u64 { + self.0 + } } impl From for u64 { diff --git a/types/src/chainspec.rs b/types/src/chainspec.rs index 9091348fd8..255b97c769 100644 --- a/types/src/chainspec.rs +++ b/types/src/chainspec.rs @@ -43,7 +43,6 @@ pub use fee_handling::FeeHandling; pub use genesis_config::{GenesisConfig, GenesisConfigBuilder}; #[cfg(any(feature = "testing", test))] pub use genesis_config::{DEFAULT_AUCTION_DELAY, DEFAULT_FEE_HANDLING, DEFAULT_REFUND_HANDLING}; - pub use global_state_update::{GlobalStateUpdate, GlobalStateUpdateConfig, GlobalStateUpdateError}; pub use highway_config::HighwayConfig; pub use network_config::NetworkConfig; diff --git a/types/src/chainspec/accounts_config/genesis.rs b/types/src/chainspec/accounts_config/genesis.rs index 08d601eefc..dae8d3fe18 100644 --- a/types/src/chainspec/accounts_config/genesis.rs +++ b/types/src/chainspec/accounts_config/genesis.rs @@ -1,18 +1,21 @@ #[cfg(feature = "datasize")] use datasize::DataSize; use num_traits::Zero; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, }; use serde::{Deserialize, Serialize}; +#[cfg(any(feature = "testing", test))] +use crate::SecretKey; use crate::{ account::AccountHash, bytesrepr, bytesrepr::{FromBytes, ToBytes, U8_SERIALIZED_LENGTH}, system::auction::DelegationRate, - Motes, PublicKey, SecretKey, + Motes, PublicKey, }; const TAG_LENGTH: usize = U8_SERIALIZED_LENGTH; @@ -80,6 +83,7 @@ impl GenesisValidator { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> GenesisValidator { let bonded_amount = Motes::new(rng.gen()); @@ -375,6 +379,7 @@ impl GenesisAccount { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> GenesisAccount { let mut bytes = [0u8; 32]; diff --git a/types/src/chainspec/genesis_config.rs b/types/src/chainspec/genesis_config.rs index 42c61dce0d..9890474faa 100644 --- a/types/src/chainspec/genesis_config.rs +++ b/types/src/chainspec/genesis_config.rs @@ -1,6 +1,8 @@ +#[cfg(any(feature = "testing", test))] use std::iter; use num_rational::Ratio; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -167,6 +169,7 @@ impl GenesisConfig { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> GenesisConfig { let count = rng.gen_range(1..10); diff --git a/types/src/chainspec/vm_config/auction_costs.rs b/types/src/chainspec/vm_config/auction_costs.rs index 2a6735155a..03b2199fc0 100644 --- a/types/src/chainspec/vm_config/auction_costs.rs +++ b/types/src/chainspec/vm_config/auction_costs.rs @@ -1,7 +1,11 @@ //! Costs of the auction system contract. #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -203,6 +207,7 @@ impl FromBytes for AuctionCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> AuctionCosts { AuctionCosts { diff --git a/types/src/chainspec/vm_config/chainspec_registry.rs b/types/src/chainspec/vm_config/chainspec_registry.rs index 21392a0643..39c7b2da7e 100644 --- a/types/src/chainspec/vm_config/chainspec_registry.rs +++ b/types/src/chainspec/vm_config/chainspec_registry.rs @@ -1,10 +1,12 @@ //! The registry of chainspec hash digests. +use std::{collections::BTreeMap, convert::TryFrom}; + +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, }; -use std::{collections::BTreeMap, convert::TryFrom}; use serde::{Deserialize, Serialize}; @@ -130,6 +132,7 @@ impl CLTyped for ChainspecRegistry { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> ChainspecRegistry { ChainspecRegistry { diff --git a/types/src/chainspec/vm_config/handle_payment_costs.rs b/types/src/chainspec/vm_config/handle_payment_costs.rs index 49f5370898..4a538b4c69 100644 --- a/types/src/chainspec/vm_config/handle_payment_costs.rs +++ b/types/src/chainspec/vm_config/handle_payment_costs.rs @@ -1,7 +1,11 @@ //! Costs of the `handle_payment` system contract. #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -80,6 +84,7 @@ impl FromBytes for HandlePaymentCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> HandlePaymentCosts { HandlePaymentCosts { diff --git a/types/src/chainspec/vm_config/host_function_costs.rs b/types/src/chainspec/vm_config/host_function_costs.rs index c5cda38640..ddb1c68de7 100644 --- a/types/src/chainspec/vm_config/host_function_costs.rs +++ b/types/src/chainspec/vm_config/host_function_costs.rs @@ -842,6 +842,7 @@ impl FromBytes for HostFunctionCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> HostFunctionCosts { HostFunctionCosts { diff --git a/types/src/chainspec/vm_config/message_limits.rs b/types/src/chainspec/vm_config/message_limits.rs index 9363515357..6f5d131fdf 100644 --- a/types/src/chainspec/vm_config/message_limits.rs +++ b/types/src/chainspec/vm_config/message_limits.rs @@ -1,6 +1,10 @@ #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -80,6 +84,7 @@ impl FromBytes for MessageLimits { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> MessageLimits { MessageLimits { diff --git a/types/src/chainspec/vm_config/mint_costs.rs b/types/src/chainspec/vm_config/mint_costs.rs index 90f0d75039..c32df42088 100644 --- a/types/src/chainspec/vm_config/mint_costs.rs +++ b/types/src/chainspec/vm_config/mint_costs.rs @@ -1,7 +1,11 @@ //! Costs of the mint system contract. #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -127,6 +131,7 @@ impl FromBytes for MintCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> MintCosts { MintCosts { diff --git a/types/src/chainspec/vm_config/opcode_costs.rs b/types/src/chainspec/vm_config/opcode_costs.rs index 5ad8c49c12..d87caf0c04 100644 --- a/types/src/chainspec/vm_config/opcode_costs.rs +++ b/types/src/chainspec/vm_config/opcode_costs.rs @@ -4,7 +4,11 @@ use datasize::DataSize; use derive_more::Add; use num_traits::Zero; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -96,6 +100,7 @@ impl Default for BrTableCost { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> BrTableCost { BrTableCost { @@ -321,6 +326,7 @@ impl FromBytes for ControlFlowCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> ControlFlowCosts { ControlFlowCosts { @@ -457,6 +463,7 @@ impl Default for OpcodeCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> OpcodeCosts { OpcodeCosts { diff --git a/types/src/chainspec/vm_config/standard_payment_costs.rs b/types/src/chainspec/vm_config/standard_payment_costs.rs index 618f7d6692..0669865edf 100644 --- a/types/src/chainspec/vm_config/standard_payment_costs.rs +++ b/types/src/chainspec/vm_config/standard_payment_costs.rs @@ -1,7 +1,11 @@ //! Costs of the standard payment system contract. #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::bytesrepr::{self, FromBytes, ToBytes}; @@ -45,6 +49,7 @@ impl FromBytes for StandardPaymentCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> StandardPaymentCosts { StandardPaymentCosts { pay: rng.gen() } diff --git a/types/src/chainspec/vm_config/storage_costs.rs b/types/src/chainspec/vm_config/storage_costs.rs index 0ce4e9ce6d..453c4b7872 100644 --- a/types/src/chainspec/vm_config/storage_costs.rs +++ b/types/src/chainspec/vm_config/storage_costs.rs @@ -3,7 +3,11 @@ use datasize::DataSize; use derive_more::Add; use num_traits::Zero; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::{ @@ -49,6 +53,7 @@ impl Default for StorageCosts { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> StorageCosts { StorageCosts { diff --git a/types/src/chainspec/vm_config/system_config.rs b/types/src/chainspec/vm_config/system_config.rs index d6f61677da..89c0922f2f 100644 --- a/types/src/chainspec/vm_config/system_config.rs +++ b/types/src/chainspec/vm_config/system_config.rs @@ -1,6 +1,10 @@ #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::{ @@ -9,7 +13,7 @@ use crate::{ }; /// Default gas cost for a wasmless transfer. -pub const DEFAULT_WASMLESS_TRANSFER_COST: u32 = 100_000_000; +pub const DEFAULT_WASMLESS_TRANSFER_COST: u32 = 0; // 100_000_000; TODO: reinstate when adding new payment logic /// Definition of costs in the system. /// @@ -91,6 +95,7 @@ impl Default for SystemConfig { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> SystemConfig { SystemConfig { diff --git a/types/src/chainspec/vm_config/wasm_config.rs b/types/src/chainspec/vm_config/wasm_config.rs index ab73b44b6c..fe432b1da6 100644 --- a/types/src/chainspec/vm_config/wasm_config.rs +++ b/types/src/chainspec/vm_config/wasm_config.rs @@ -1,7 +1,11 @@ //! Configuration of the Wasm execution engine. #[cfg(feature = "datasize")] use datasize::DataSize; -use rand::{distributions::Standard, prelude::*, Rng}; +#[cfg(any(feature = "testing", test))] +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use serde::{Deserialize, Serialize}; use crate::{ @@ -137,6 +141,7 @@ impl FromBytes for WasmConfig { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> WasmConfig { WasmConfig { diff --git a/types/src/contract_messages/topics.rs b/types/src/contract_messages/topics.rs index 9a41d3e39a..9bc593ca78 100644 --- a/types/src/contract_messages/topics.rs +++ b/types/src/contract_messages/topics.rs @@ -10,6 +10,7 @@ use core::fmt::{Debug, Display, Formatter}; #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -121,6 +122,7 @@ impl Debug for TopicNameHash { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> TopicNameHash { TopicNameHash(rng.gen()) diff --git a/types/src/lib.rs b/types/src/lib.rs index c33f85cc2f..bc8a035570 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -95,7 +95,7 @@ pub use block::{ FinalitySignatureId, RewardedSignatures, Rewards, SignedBlockHeader, SignedBlockHeaderValidationError, SingleBlockRewardedSignatures, }; -#[cfg(any(feature = "testing", test))] +#[cfg(any(all(feature = "std", feature = "testing"), test))] pub use block::{TestBlockBuilder, TestBlockV1Builder}; pub use block_time::{BlockTime, BLOCKTIME_SERIALIZED_LENGTH}; diff --git a/types/src/system.rs b/types/src/system.rs index e742b4d385..b4e39ad8e1 100644 --- a/types/src/system.rs +++ b/types/src/system.rs @@ -1,12 +1,12 @@ //! System modules, formerly known as "system contracts" pub mod auction; -mod call_stack_element; +mod caller; mod error; pub mod handle_payment; pub mod mint; pub mod standard_payment; mod system_contract_type; -pub use call_stack_element::{CallStackElement, CallStackElementTag}; +pub use caller::{Caller, CallerTag}; pub use error::Error; pub use system_contract_type::{SystemEntityType, AUCTION, HANDLE_PAYMENT, MINT, STANDARD_PAYMENT}; diff --git a/types/src/system/auction/bid_addr.rs b/types/src/system/auction/bid_addr.rs index 618b499468..fded546ff4 100644 --- a/types/src/system/auction/bid_addr.rs +++ b/types/src/system/auction/bid_addr.rs @@ -9,6 +9,7 @@ use alloc::vec::Vec; use core::fmt::{Debug, Display, Formatter}; #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -285,6 +286,7 @@ impl Debug for BidAddr { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> BidAddr { BidAddr::Validator(AccountHash::new(rng.gen())) diff --git a/types/src/system/call_stack_element.rs b/types/src/system/caller.rs similarity index 55% rename from types/src/system/call_stack_element.rs rename to types/src/system/caller.rs index df09eac361..b668af2aae 100644 --- a/types/src/system/call_stack_element.rs +++ b/types/src/system/caller.rs @@ -10,33 +10,24 @@ use crate::{ AddressableEntityHash, CLType, CLTyped, }; -/// Tag representing variants of CallStackElement for purposes of serialization. +/// Tag representing variants of CallerTag for purposes of serialization. #[derive(FromPrimitive, ToPrimitive)] #[repr(u8)] -pub enum CallStackElementTag { +pub enum CallerTag { /// Session tag. Session = 0, /// StoredContract tag. StoredContract, } -/// Represents the origin of a sub-call. +/// Identity of a calling entity. #[derive(Clone, Debug, PartialEq, Eq)] -pub enum CallStackElement { +pub enum Caller { /// Session Session { /// The account hash of the caller account_hash: AccountHash, }, - // /// Effectively an EntryPointType::Session - stored access to a session. - // StoredSession { - // /// The account hash of the caller - // account_hash: AccountHash, - // /// The package hash - // package_hash: PackageHash, - // /// The contract hash - // contract_hash: AddressableEntityHash, - // }, /// AddressableEntity AddressableEntity { /// The package hash @@ -46,54 +37,40 @@ pub enum CallStackElement { }, } -impl CallStackElement { - /// Creates a [`CallStackElement::Session`]. This represents a call into session code, and +impl Caller { + /// Creates a [`Caller::Session`]. This represents a call into session code, and /// should only ever happen once in a call stack. pub fn session(account_hash: AccountHash) -> Self { - CallStackElement::Session { account_hash } + Caller::Session { account_hash } } - /// Creates a [`'CallStackElement::StoredContract`]. This represents a call into a contract with + /// Creates a [`'Caller::StoredContract`]. This represents a call into a contract with /// `EntryPointType::Contract`. pub fn stored_contract( package_hash: PackageHash, contract_hash: AddressableEntityHash, ) -> Self { - CallStackElement::AddressableEntity { + Caller::AddressableEntity { package_hash, entity_hash: contract_hash, } } - // /// Creates a [`'CallStackElement::StoredSession`]. This represents a call into a contract - // with /// `EntryPointType::Session`. - // pub fn stored_session( - // account_hash: AccountHash, - // package_hash: PackageHash, - // contract_hash: AddressableEntityHash, - // ) -> Self { - // CallStackElement::StoredSession { - // account_hash, - // package_hash, - // contract_hash, - // } - // } - /// Gets the tag from self. - pub fn tag(&self) -> CallStackElementTag { + pub fn tag(&self) -> CallerTag { match self { - CallStackElement::Session { .. } => CallStackElementTag::Session, + Caller::Session { .. } => CallerTag::Session, - CallStackElement::AddressableEntity { .. } => CallStackElementTag::StoredContract, + Caller::AddressableEntity { .. } => CallerTag::StoredContract, } } /// Gets the [`AddressableEntityHash`] for both stored session and stored contract variants. pub fn contract_hash(&self) -> Option<&AddressableEntityHash> { match self { - CallStackElement::Session { .. } => None, + Caller::Session { .. } => None, - CallStackElement::AddressableEntity { + Caller::AddressableEntity { entity_hash: contract_hash, .. } => Some(contract_hash), @@ -101,16 +78,14 @@ impl CallStackElement { } } -impl ToBytes for CallStackElement { +impl ToBytes for Caller { fn to_bytes(&self) -> Result, bytesrepr::Error> { let mut result = bytesrepr::allocate_buffer(self)?; result.push(self.tag() as u8); match self { - CallStackElement::Session { account_hash } => { - result.append(&mut account_hash.to_bytes()?) - } + Caller::Session { account_hash } => result.append(&mut account_hash.to_bytes()?), - CallStackElement::AddressableEntity { + Caller::AddressableEntity { package_hash, entity_hash: contract_hash, } => { @@ -124,8 +99,8 @@ impl ToBytes for CallStackElement { fn serialized_length(&self) -> usize { U8_SERIALIZED_LENGTH + match self { - CallStackElement::Session { account_hash } => account_hash.serialized_length(), - CallStackElement::AddressableEntity { + Caller::Session { account_hash } => account_hash.serialized_length(), + Caller::AddressableEntity { package_hash, entity_hash: contract_hash, } => package_hash.serialized_length() + contract_hash.serialized_length(), @@ -133,20 +108,20 @@ impl ToBytes for CallStackElement { } } -impl FromBytes for CallStackElement { +impl FromBytes for Caller { fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> { let (tag, remainder): (u8, &[u8]) = FromBytes::from_bytes(bytes)?; - let tag = CallStackElementTag::from_u8(tag).ok_or(bytesrepr::Error::Formatting)?; + let tag = CallerTag::from_u8(tag).ok_or(bytesrepr::Error::Formatting)?; match tag { - CallStackElementTag::Session => { + CallerTag::Session => { let (account_hash, remainder) = AccountHash::from_bytes(remainder)?; - Ok((CallStackElement::Session { account_hash }, remainder)) + Ok((Caller::Session { account_hash }, remainder)) } - CallStackElementTag::StoredContract => { + CallerTag::StoredContract => { let (package_hash, remainder) = PackageHash::from_bytes(remainder)?; let (contract_hash, remainder) = AddressableEntityHash::from_bytes(remainder)?; Ok(( - CallStackElement::AddressableEntity { + Caller::AddressableEntity { package_hash, entity_hash: contract_hash, }, @@ -157,7 +132,7 @@ impl FromBytes for CallStackElement { } } -impl CLTyped for CallStackElement { +impl CLTyped for Caller { fn cl_type() -> CLType { CLType::Any } diff --git a/types/src/system/mint/error.rs b/types/src/system/mint/error.rs index df068bb33f..5c613989ff 100644 --- a/types/src/system/mint/error.rs +++ b/types/src/system/mint/error.rs @@ -154,6 +154,12 @@ pub enum Error { /// assert_eq!(22, Error::DisabledUnrestrictedTransfers as u8); DisabledUnrestrictedTransfers = 22, + /// Attempt to access a record using forged permissions. + /// ``` + /// # use casper_types::system::mint::Error; + /// assert_eq!(23, Error::ForgedReference as u8); + ForgedReference = 23, + #[cfg(test)] #[doc(hidden)] Sentinel, @@ -209,6 +215,7 @@ impl TryFrom for Error { d if d == Error::DisabledUnrestrictedTransfers as u8 => { Ok(Error::DisabledUnrestrictedTransfers) } + d if d == Error::ForgedReference as u8 => Ok(Error::ForgedReference), _ => Err(TryFromU8ForError(())), } } @@ -269,6 +276,7 @@ impl Display for Error { Error::DisabledUnrestrictedTransfers => { formatter.write_str("Disabled unrestricted transfers") } + Error::ForgedReference => formatter.write_str("Forged reference"), #[cfg(test)] Error::Sentinel => formatter.write_str("Sentinel error"), } diff --git a/types/src/transaction/deploy.rs b/types/src/transaction/deploy.rs index 6fd415beb4..88397c8282 100644 --- a/types/src/transaction/deploy.rs +++ b/types/src/transaction/deploy.rs @@ -150,6 +150,40 @@ pub struct Deploy { } impl Deploy { + /// Constructs a new signed `Deploy`. + #[cfg(any(all(feature = "std", feature = "testing"), test))] + #[allow(clippy::too_many_arguments)] + pub fn new( + timestamp: Timestamp, + ttl: TimeDiff, + gas_price: u64, + dependencies: Vec, + chain_name: String, + payment: ExecutableDeployItem, + session: ExecutableDeployItem, + secret_key: &SecretKey, + account: Option, + ) -> Deploy { + let account_and_secret_key = match account { + Some(account) => InitiatorAddrAndSecretKey::Both { + initiator_addr: InitiatorAddr::PublicKey(account), + secret_key, + }, + None => InitiatorAddrAndSecretKey::SecretKey(secret_key), + }; + + Deploy::build( + timestamp, + ttl, + gas_price, + dependencies, + chain_name, + payment, + session, + account_and_secret_key, + ) + } + /// Called by the `DeployBuilder` to construct a new `Deploy`. #[cfg(any(feature = "std", test))] #[allow(clippy::too_many_arguments)] @@ -345,6 +379,11 @@ impl Deploy { }) } + /// Returns `true` if this deploy is a native transfer. + pub fn is_transfer(&self) -> bool { + self.session.is_transfer() + } + /// Returns `Ok` if and only if: /// * the chain_name is correct, /// * the configured parameters are complied with at the given timestamp @@ -489,40 +528,6 @@ impl Deploy { &DEPLOY } - /// Constructs a new signed `Deploy`. - #[cfg(any(all(feature = "std", feature = "testing"), test))] - #[allow(clippy::too_many_arguments)] - pub fn new( - timestamp: Timestamp, - ttl: TimeDiff, - gas_price: u64, - dependencies: Vec, - chain_name: String, - payment: ExecutableDeployItem, - session: ExecutableDeployItem, - secret_key: &SecretKey, - account: Option, - ) -> Deploy { - let account_and_secret_key = match account { - Some(account) => InitiatorAddrAndSecretKey::Both { - initiator_addr: InitiatorAddr::PublicKey(account), - secret_key, - }, - None => InitiatorAddrAndSecretKey::SecretKey(secret_key), - }; - - Deploy::build( - timestamp, - ttl, - gas_price, - dependencies, - chain_name, - payment, - session, - account_and_secret_key, - ) - } - /// Returns a random `Deploy`. #[cfg(any(all(feature = "std", feature = "testing"), test))] pub fn random(rng: &mut TestRng) -> Self { diff --git a/types/src/transaction/transaction_v1/transaction_v1_body.rs b/types/src/transaction/transaction_v1/transaction_v1_body.rs index edc515df17..1c11f62853 100644 --- a/types/src/transaction/transaction_v1/transaction_v1_body.rs +++ b/types/src/transaction/transaction_v1/transaction_v1_body.rs @@ -6,7 +6,7 @@ use core::fmt::{self, Display, Formatter}; #[cfg(feature = "datasize")] use datasize::DataSize; -#[cfg(any(feature = "testing", test))] +#[cfg(any(all(feature = "std", feature = "testing"), test))] use rand::{Rng, RngCore}; #[cfg(feature = "json-schema")] use schemars::JsonSchema; @@ -21,7 +21,7 @@ use super::TransactionV1; #[cfg(any(feature = "std", test))] use super::{TransactionConfig, TransactionV1ConfigFailure}; use crate::bytesrepr::{self, FromBytes, ToBytes}; -#[cfg(any(feature = "testing", test))] +#[cfg(any(all(feature = "std", feature = "testing"), test))] use crate::{ bytesrepr::Bytes, testing::TestRng, PublicKey, TransactionInvocationTarget, TransactionRuntime, TransactionSessionKind, @@ -174,7 +174,7 @@ impl TransactionV1Body { } /// Returns a random `TransactionV1Body`. - #[cfg(any(feature = "testing", test))] + #[cfg(any(all(feature = "std", feature = "testing"), test))] pub fn random(rng: &mut TestRng) -> Self { match rng.gen_range(0..8) { 0 => { diff --git a/types/src/transfer.rs b/types/src/transfer.rs index 38dfe8f057..60f859aa7d 100644 --- a/types/src/transfer.rs +++ b/types/src/transfer.rs @@ -7,6 +7,7 @@ use core::{ #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -310,6 +311,7 @@ impl AsRef<[u8]> for TransferAddr { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> TransferAddr { TransferAddr::new(rng.gen()) diff --git a/types/src/uref.rs b/types/src/uref.rs index c24b2e85d5..29bd061b68 100644 --- a/types/src/uref.rs +++ b/types/src/uref.rs @@ -8,6 +8,7 @@ use core::{ #[cfg(feature = "datasize")] use datasize::DataSize; +#[cfg(any(feature = "testing", test))] use rand::{ distributions::{Distribution, Standard}, Rng, @@ -304,6 +305,7 @@ impl TryFrom for URef { } } +#[cfg(any(feature = "testing", test))] impl Distribution for Standard { fn sample(&self, rng: &mut R) -> URef { URef::new(rng.gen(), rng.gen())