Skip to content

Commit

Permalink
[E2E alternative backend]: Backend traits (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 authored Jul 31, 2023
1 parent bcbfa66 commit fdd164d
Show file tree
Hide file tree
Showing 26 changed files with 524 additions and 244 deletions.
2 changes: 1 addition & 1 deletion crates/e2e/macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn build_contract(path_to_cargo_toml: &str) -> String {
output_type: OutputType::HumanReadable,
skip_wasm_validation: false,
target: Target::Wasm,
..ExecuteArgs::default()
..Default::default()
};

match contract_build::execute(args) {
Expand Down
155 changes: 155 additions & 0 deletions crates/e2e/src/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
builders::CreateBuilderPartial,
CallBuilderFinal,
CallDryRunResult,
CallResult,
InstantiationResult,
UploadResult,
};
use ink_env::Environment;
use jsonrpsee::core::async_trait;
use pallet_contracts_primitives::ContractInstantiateResult;
use subxt::dynamic::Value;

/// Full E2E testing backend: combines general chain API and contract-specific operations.
#[async_trait]
pub trait E2EBackend<E: Environment>: ChainBackend + ContractsBackend<E> {}

/// General chain operations useful in contract testing.
#[async_trait]
pub trait ChainBackend {
/// Abstract type representing the entity that interacts with the chain.
type Actor: Send;
/// Identifier type for an actor.
type ActorId;
/// Balance type.
type Balance: Send;
/// Error type.
type Error;
/// Event log type.
type EventLog;

/// Generate a new actor's credentials and fund it with the given amount from the
/// `sender` actor.
async fn create_and_fund_account(
&mut self,
origin: &Self::Actor,
amount: Self::Balance,
) -> Self::Actor;

/// Returns the balance of `actor`.
async fn balance(&self, actor: Self::ActorId) -> Result<Self::Balance, Self::Error>;

/// Executes a runtime call `call_name` for the `pallet_name`.
/// The `call_data` is a `Vec<Value>`.
///
/// Note:
/// - `pallet_name` must be in camel case, for example `Balances`.
/// - `call_name` must be snake case, for example `force_transfer`.
/// - `call_data` is a `Vec<subxt::dynamic::Value>` that holds a representation of
/// some value.
///
/// Returns when the transaction is included in a block. The return value contains all
/// events that are associated with this transaction.
async fn runtime_call<'a>(
&mut self,
actor: &Self::Actor,
pallet_name: &'a str,
call_name: &'a str,
call_data: Vec<Value>,
) -> Result<Self::EventLog, Self::Error>;
}

/// Contract-specific operations.
#[async_trait]
pub trait ContractsBackend<E: Environment> {
/// Abstract type representing the entity that interacts with the chain.
type Actor;
/// Error type.
type Error;
/// Event log type.
type EventLog;

/// The function subsequently uploads and instantiates an instance of the contract.
///
/// This function extracts the metadata of the contract at the file path
/// `target/ink/$contract_name.contract`.
///
/// Calling this function multiple times should be idempotent, the contract is
/// newly instantiated each time using a unique salt. No existing contract
/// instance is reused!
async fn instantiate<Contract, Args: Send + scale::Encode, R>(
&mut self,
contract_name: &str,
caller: &Self::Actor,
constructor: CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<InstantiationResult<E, Self::EventLog>, Self::Error>;

/// Dry run contract instantiation.
async fn instantiate_dry_run<Contract, Args: Send + scale::Encode, R>(
&mut self,
contract_name: &str,
caller: &Self::Actor,
constructor: CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> ContractInstantiateResult<E::AccountId, E::Balance, ()>;

/// The function subsequently uploads and instantiates an instance of the contract.
///
/// This function extracts the Wasm of the contract for the specified contract.
///
/// Calling this function multiple times should be idempotent, the contract is
/// newly instantiated each time using a unique salt. No existing contract
/// instance is reused!
async fn upload(
&mut self,
contract_name: &str,
caller: &Self::Actor,
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<E, Self::EventLog>, Self::Error>;

/// Executes a `call` for the contract at `account_id`.
///
/// Returns when the transaction is included in a block. The return value
/// contains all events that are associated with this transaction.
async fn call<Args: Sync + scale::Encode, RetType: Send + scale::Decode>(
&mut self,
caller: &Self::Actor,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<CallResult<E, RetType, Self::EventLog>, Self::Error>
where
CallBuilderFinal<E, Args, RetType>: Clone;

/// Executes a dry-run `call`.
///
/// Returns the result of the dry run, together with the decoded return value of the
/// invoked message.
async fn call_dry_run<Args: Sync + scale::Encode, RetType: Send + scale::Decode>(
&mut self,
caller: &Self::Actor,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> CallDryRunResult<E, RetType>
where
CallBuilderFinal<E, Args, RetType>: Clone;
}
14 changes: 14 additions & 0 deletions crates/e2e/src/contract_results.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ink::codegen::ContractCallBuilder;
use ink_env::{
call::FromAccountId,
Expand Down
14 changes: 14 additions & 0 deletions crates/e2e/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use pallet_contracts_primitives::{
CodeUploadResult,
ContractExecResult,
Expand Down
16 changes: 11 additions & 5 deletions crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
html_favicon_url = "https://use.ink/crate-docs/favicon.png"
)]

mod backend;
mod builders;
mod client;
mod contract_results;
mod error;
pub mod events;
mod node_proc;
mod subxt_client;
mod xts;

pub use client::{
CallBuilderFinal,
Client,
Error,
pub use backend::{
ChainBackend,
ContractsBackend,
E2EBackend,
};
pub use contract_results::{
CallDryRunResult,
Expand All @@ -46,6 +47,11 @@ pub use node_proc::{
pub use sp_core::H256;
pub use sp_keyring::AccountKeyring;
pub use subxt;
pub use subxt_client::{
CallBuilderFinal,
Client,
Error,
};
pub use subxt_signer::sr25519::{
self,
dev::*,
Expand Down
Loading

0 comments on commit fdd164d

Please sign in to comment.