Skip to content

Commit

Permalink
Merge #2081
Browse files Browse the repository at this point in the history
2081: Expand Deploy Acceptor r=darthsiroftardis a=darthsiroftardis

Co-authored-by: Ed Hastings <ed@casperlabs.io>

CHANGELOG:

- Expanded the checks the `DeployAcceptor` makes on a single `Deploy`
     - Account related checks
          1. Checks if the `AccountHashes` associated with the `Deploy` are valid  associated keys
          2. Checks if the `Weight` of the `AccountHashes` meet the threshold to perform a deployment
          3. Checks if the balance of the `Account` in the case of a `Deploy` received by from a `Client` is sufficient 
     - ExecutableDeployItem
         1. Checks if the ExecutableDeployItem present in the session or payment fields are valid and present in the case of stored contracts and contract packages
 - Moved the cryptographic validity check of a `Deploy` to be the last step once all other prerequisites are met.
 - Expanded the test suite to cover the newly added checks
         
Closes #2085 


Co-authored-by: Karan Dhareshwar <karan@casperlabs.io>
  • Loading branch information
casperlabs-bors-ng[bot] and darthsiroftardis committed Sep 22, 2021
2 parents a87246d + c015eb1 commit 8516aa6
Show file tree
Hide file tree
Showing 13 changed files with 2,376 additions and 336 deletions.
163 changes: 163 additions & 0 deletions execution_engine/src/core/engine_state/executable_deploy_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,63 @@ const STORED_VERSIONED_CONTRACT_BY_HASH_TAG: u8 = 3;
const STORED_VERSIONED_CONTRACT_BY_NAME_TAG: u8 = 4;
const TRANSFER_TAG: u8 = 5;

/// Possible ways to identify the `ExecutableDeployItem`.
#[derive(
Clone, DataSize, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
pub enum ExecutableDeployItemIdentifier {
/// The deploy item is of the type [`ExecutableDeployItem::ModuleBytes`]
Module,
/// The deploy item is a variation of a stored contract.
Contract(ContractIdentifier),
/// The deploy item is a variation of a stored contract package.
Package(ContractPackageIdentifier),
/// The deploy item is a native transfer.
Transfer,
}

/// Possible ways to identify the contract object within an `ExecutableDeployItem`.
#[derive(
Clone, DataSize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
pub enum ContractIdentifier {
/// The contract object within the deploy item is identified by name.
Name(String),
/// The contract object within the deploy item is identified by its hash.
Hash(ContractHash),
}

/// Possible ways to identify the contract package object within an `ExecutableDeployItem`.
#[derive(
Clone, DataSize, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
pub enum ContractPackageIdentifier {
/// The stored contract package within the deploy item is identified by name.
Name {
/// Name of the contract package.
name: String,
/// The version specified in the deploy item.
version: Option<ContractVersion>,
},
/// The stored contract package within the deploy item is identified by hash.
Hash {
/// Hash of the contract package.
contract_package_hash: ContractPackageHash,
/// The version specified in the deploy item.
version: Option<ContractVersion>,
},
}

impl ContractPackageIdentifier {
/// Returns the version of the contract package specified in the deploy item.
pub fn version(&self) -> Option<ContractVersion> {
match self {
ContractPackageIdentifier::Name { version, .. } => *version,
ContractPackageIdentifier::Hash { version, .. } => *version,
}
}
}

/// Represents possible variants of an executable deploy.
#[derive(
Clone, DataSize, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
Expand Down Expand Up @@ -134,6 +191,72 @@ impl ExecutableDeployItem {
}
}

/// Returns the identifier of the ExecutableDeployItem.
pub fn identifier(&self) -> ExecutableDeployItemIdentifier {
match self {
ExecutableDeployItem::ModuleBytes { .. } => ExecutableDeployItemIdentifier::Module,
ExecutableDeployItem::StoredContractByHash { hash, .. } => {
ExecutableDeployItemIdentifier::Contract(ContractIdentifier::Hash(*hash))
}
ExecutableDeployItem::StoredContractByName { name, .. } => {
ExecutableDeployItemIdentifier::Contract(ContractIdentifier::Name(name.to_string()))
}
ExecutableDeployItem::StoredVersionedContractByHash { hash, version, .. } => {
ExecutableDeployItemIdentifier::Package(ContractPackageIdentifier::Hash {
contract_package_hash: *hash,
version: *version,
})
}
ExecutableDeployItem::StoredVersionedContractByName { name, version, .. } => {
ExecutableDeployItemIdentifier::Package(ContractPackageIdentifier::Name {
name: name.to_string(),
version: *version,
})
}
ExecutableDeployItem::Transfer { .. } => ExecutableDeployItemIdentifier::Transfer,
}
}

/// Returns the identifier of the contract present in the deploy item, if present.
pub fn contract_identifier(&self) -> Option<ContractIdentifier> {
match self {
ExecutableDeployItem::ModuleBytes { .. }
| ExecutableDeployItem::StoredVersionedContractByHash { .. }
| ExecutableDeployItem::StoredVersionedContractByName { .. }
| ExecutableDeployItem::Transfer { .. } => None,

ExecutableDeployItem::StoredContractByName { name, .. } => {
Some(ContractIdentifier::Name(name.to_string()))
}
ExecutableDeployItem::StoredContractByHash { hash, .. } => {
Some(ContractIdentifier::Hash(*hash))
}
}
}

/// Returns the identifier of the contract package present in the deploy item, if present.
pub fn contract_package_identifier(&self) -> Option<ContractPackageIdentifier> {
match self {
ExecutableDeployItem::ModuleBytes { .. }
| ExecutableDeployItem::StoredContractByHash { .. }
| ExecutableDeployItem::StoredContractByName { .. }
| ExecutableDeployItem::Transfer { .. } => None,

ExecutableDeployItem::StoredVersionedContractByName { name, version, .. } => {
Some(ContractPackageIdentifier::Name {
name: name.clone(),
version: *version,
})
}
ExecutableDeployItem::StoredVersionedContractByHash { hash, version, .. } => {
Some(ContractPackageIdentifier::Hash {
contract_package_hash: *hash,
version: *version,
})
}
}
}

/// Returns the runtime arguments.
pub fn args(&self) -> &RuntimeArgs {
match self {
Expand All @@ -151,6 +274,46 @@ impl ExecutableDeployItem {
matches!(self, ExecutableDeployItem::Transfer { .. })
}

/// Checks if the deploy item is a contract identified by its name.
pub fn is_by_name(&self) -> bool {
matches!(
self,
ExecutableDeployItem::StoredVersionedContractByName { .. }
) || matches!(self, ExecutableDeployItem::StoredContractByName { .. })
}

/// Returns the name of the contract or contract package,
/// if the deploy item is identified by name.
pub fn by_name(&self) -> Option<String> {
match self {
ExecutableDeployItem::StoredContractByName { name, .. }
| ExecutableDeployItem::StoredVersionedContractByName { name, .. } => {
Some(name.clone())
}
ExecutableDeployItem::ModuleBytes { .. }
| ExecutableDeployItem::StoredContractByHash { .. }
| ExecutableDeployItem::StoredVersionedContractByHash { .. }
| ExecutableDeployItem::Transfer { .. } => None,
}
}

/// Checks if the deploy item is a stored contract.
pub fn is_stored_contract(&self) -> bool {
matches!(self, ExecutableDeployItem::StoredContractByHash { .. })
|| matches!(self, ExecutableDeployItem::StoredContractByName { .. })
}

/// Checks if the deploy item is a stored contract package.
pub fn is_stored_contract_package(&self) -> bool {
matches!(
self,
ExecutableDeployItem::StoredVersionedContractByHash { .. }
) || matches!(
self,
ExecutableDeployItem::StoredVersionedContractByName { .. }
)
}

/// Returns all the details necessary for execution.
/// This object is generated based on information provided by
/// [`ExecutableDeployItem`].
Expand Down
2 changes: 1 addition & 1 deletion execution_engine/src/core/engine_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub use self::{
engine_config::{EngineConfig, DEFAULT_MAX_QUERY_DEPTH},
era_validators::{GetEraValidatorsError, GetEraValidatorsRequest},
error::Error,
executable_deploy_item::ExecutableDeployItem,
executable_deploy_item::{ExecutableDeployItem, ExecutableDeployItemIdentifier},
execute_request::ExecuteRequest,
execution::Error as ExecError,
execution_result::{ExecutionResult, ExecutionResults, ForcedTransferResult},
Expand Down
Loading

0 comments on commit 8516aa6

Please sign in to comment.