Skip to content

Commit

Permalink
delegate_dependency api calls (#2076)
Browse files Browse the repository at this point in the history
* *WIP* add `delegate_dependency` env api calls

* Updated return types and moved docs

* unused imports

* Lazy delegate to

* Allow deprecated

* Initialise delegate_to hash in constructor

* Test example, adding `remove_code` extrinsic to e2e

* Docs

* CHANGELOG.md

* drink unimplemented

* Send it

* Fix comment

* Fix comment

* Fix comment

* Fix comment

* Fix comment

* Update integration-tests/upgradeable-contracts/delegator/delegatee2/Cargo.toml

Co-authored-by: Gherman <german@parity.io>

* Update crates/e2e/src/backend.rs

Co-authored-by: Gherman <german@parity.io>

* update to `lock/unlock` host fns

* #[allow(deprecated)] for _v1 calls

* Add link to lock up deposit

* doc comment as per review

* Update crates/e2e/src/backend.rs

Co-authored-by: Michael Müller <michi@parity.io>

* Update crates/e2e/src/backend.rs

Co-authored-by: Michael Müller <michi@parity.io>

* Update crates/env/src/api.rs

Co-authored-by: Michael Müller <michi@parity.io>

* Apply suggestions from code review

Co-authored-by: Michael Müller <michi@parity.io>

---------

Co-authored-by: Gherman <german@parity.io>
Co-authored-by: Michael Müller <michi@parity.io>
  • Loading branch information
3 people authored Feb 23, 2024
1 parent 1e46971 commit bc92b61
Show file tree
Hide file tree
Showing 20 changed files with 509 additions and 69 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Linter] Publish the linting crates on crates.io - [#2060](/~https://github.com/paritytech/ink/pull/2060)
- [E2E] Added `create_call_builder` for testing existing contracts - [#2075](/~https://github.com/paritytech/ink/pull/2075)
- `call_v2` cross-contract calls with additional limit parameters - [#2077](/~https://github.com/paritytech/ink/pull/2077)
- `delegate_dependency` api calls - [#2076](/~https://github.com/paritytech/ink/pull/2076)

### Changed
- `Mapping`: Reflect all possible failure cases in comments ‒ [#2079](/~https://github.com/paritytech/ink/pull/2079)
Expand Down
55 changes: 47 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const_env = { version = "0.1"}

# Substrate dependencies
pallet-contracts = { version = "27.0.0", default-features = false }
pallet-contracts-uapi = { package = "ink-pallet-contracts-uapi", version = "=6.0.0", default-features = false }
pallet-contracts-uapi = { package = "pallet-contracts-uapi-next", version = "=6.0.1", default-features = false }
sp-core = { version = "28.0.0", default-features = false }
sp-keyring = { version = "31.0.0", default-features = false }
sp-runtime = { version = "31.0.1", default-features = false }
Expand Down
35 changes: 33 additions & 2 deletions crates/e2e/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::Keypair;
use crate::{
backend_calls::{
InstantiateBuilder,
RemoveCodeBuilder,
UploadBuilder,
},
builders::CreateBuilderPartial,
Expand Down Expand Up @@ -159,6 +160,29 @@ pub trait ContractsBackend<E: Environment> {
UploadBuilder::new(self, contract_name, caller)
}

/// Start building a remove code call.
///
/// # Example
///
/// ```ignore
/// let contract = client
/// .remove_code(&ink_e2e::alice(), code_hash)
/// // Submit the call for on-chain execution.
/// .submit()
/// .await
/// .expect("remove failed");
/// ```
fn remove_code<'a>(
&'a mut self,
caller: &'a Keypair,
code_hash: E::Hash,
) -> RemoveCodeBuilder<E, Self>
where
Self: Sized + BuilderClient<E>,
{
RemoveCodeBuilder::new(self, caller, code_hash)
}

/// Start building a call using a builder pattern.
///
/// # Example
Expand Down Expand Up @@ -193,8 +217,8 @@ pub trait ContractsBackend<E: Environment> {

#[async_trait]
pub trait BuilderClient<E: Environment>: ContractsBackend<E> {
/// Executes a bare `call` for the contract at `account_id`. This function does
/// perform a dry-run, and user is expected to provide the gas limit.
/// Executes a bare `call` for the contract at `account_id`. This function does not
/// perform a dry-run, and the user is expected to provide the gas limit.
///
/// Use it when you want to have a more precise control over submitting extrinsic.
///
Expand Down Expand Up @@ -239,6 +263,13 @@ pub trait BuilderClient<E: Environment>: ContractsBackend<E> {
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<E, Self::EventLog>, Self::Error>;

/// Removes the code of the contract at `code_hash`.
async fn bare_remove_code(
&mut self,
caller: &Keypair,
code_hash: E::Hash,
) -> Result<Self::EventLog, Self::Error>;

/// Bare instantiate call. This function does not perform a dry-run,
/// and user is expected to provide the gas limit.
///
Expand Down
31 changes: 31 additions & 0 deletions crates/e2e/src/backend_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,37 @@ where
}
}

/// Allows to build an end-to-end remove code call using a builder pattern.
pub struct RemoveCodeBuilder<'a, E, B>
where
E: Environment,
B: BuilderClient<E>,
{
client: &'a mut B,
caller: &'a Keypair,
code_hash: E::Hash,
}

impl<'a, E, B> RemoveCodeBuilder<'a, E, B>
where
E: Environment,
B: BuilderClient<E>,
{
/// Initialize a remove code builder with essential values.
pub fn new(client: &'a mut B, caller: &'a Keypair, code_hash: E::Hash) -> Self {
Self {
client,
caller,
code_hash,
}
}

/// Submit the remove code extrinsic.
pub async fn submit(&mut self) -> Result<B::EventLog, B::Error> {
B::bare_remove_code(self.client, self.caller, self.code_hash).await
}
}

fn calculate_weight(
mut proof_size: u64,
mut ref_time: u64,
Expand Down
12 changes: 10 additions & 2 deletions crates/e2e/src/drink_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
#[async_trait]
impl<
AccountId: Clone + Send + Sync + From<[u8; 32]> + AsRef<[u8; 32]>,
Hash: Copy + From<[u8; 32]>,
Hash: Copy + Send + From<[u8; 32]>,
Config: SandboxConfig,
E: Environment<
AccountId = AccountId,
Expand Down Expand Up @@ -341,6 +341,14 @@ where
})
}

async fn bare_remove_code(
&mut self,
_caller: &Keypair,
_code_hash: E::Hash,
) -> Result<Self::EventLog, Self::Error> {
unimplemented!("drink! sandbox does not yet support remove_code")
}

async fn bare_call<Args: Sync + Encode + Clone, RetType: Send + Decode>(
&mut self,
caller: &Keypair,
Expand Down Expand Up @@ -417,7 +425,7 @@ where

impl<
AccountId: Clone + Send + Sync + From<[u8; 32]> + AsRef<[u8; 32]>,
Hash: Copy + From<[u8; 32]>,
Hash: Copy + Send + From<[u8; 32]>,
Config: SandboxConfig,
E: Environment<
AccountId = AccountId,
Expand Down
3 changes: 3 additions & 0 deletions crates/e2e/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub enum Error<DispatchError: fmt::Debug + fmt::Display> {
/// The `call` extrinsic failed.
#[error("Call extrinsic error: {0}")]
CallExtrinsic(DispatchError),
/// The `remove_code` extrinsic failed.
#[error("Remove code extrinsic error: {0}")]
RemoveCodeExtrinsic(DispatchError),
/// Error fetching account balance.
#[error("Fetching account Balance error: {0}")]
Balance(String),
Expand Down
33 changes: 29 additions & 4 deletions crates/e2e/src/subxt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ where
E::AccountId: Debug + Send + Sync,
E::Balance:
Clone + Debug + Send + Sync + From<u128> + scale::HasCompact + serde::Serialize,
E::Hash: Debug + Send + scale::Encode,
E::Hash: Debug + Send + Sync + scale::Encode,
{
async fn bare_instantiate<Contract: Clone, Args: Send + Sync + Encode + Clone, R>(
&mut self,
Expand Down Expand Up @@ -536,6 +536,30 @@ where
Ok(ret)
}

async fn bare_remove_code(
&mut self,
caller: &Keypair,
code_hash: E::Hash,
) -> Result<Self::EventLog, Self::Error> {
let tx_events = self.api.remove_code(caller, code_hash).await;

for evt in tx_events.iter() {
let evt = evt.unwrap_or_else(|err| {
panic!("unable to unwrap event: {err:?}");
});

if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error =
DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(|e| Error::Decoding(e.to_string()))?;
return Err(Error::RemoveCodeExtrinsic(dispatch_error))
}
}

Ok(tx_events)
}

async fn bare_call<Args: Sync + Encode + Clone, RetType: Send + Decode>(
&mut self,
caller: &Keypair,
Expand Down Expand Up @@ -571,7 +595,7 @@ where
if is_extrinsic_failed_event(&evt) {
let metadata = self.api.client.metadata();
let dispatch_error =
subxt::error::DispatchError::decode_from(evt.field_bytes(), metadata)
DispatchError::decode_from(evt.field_bytes(), metadata)
.map_err(|e| Error::Decoding(e.to_string()))?;
log_error(&format!("extrinsic for call failed: {dispatch_error}"));
return Err(Error::CallExtrinsic(dispatch_error))
Expand Down Expand Up @@ -660,13 +684,14 @@ where
C::Address: From<sr25519::PublicKey>,
C::Signature: From<sr25519::Signature>,
C::Address: Send + Sync,
<<C as subxt::Config>::ExtrinsicParams as subxt::config::ExtrinsicParams<C>>::OtherParams: Default + Send + Sync,
<<C as subxt::Config>::ExtrinsicParams as ExtrinsicParams<C>>::OtherParams:
Default + Send + Sync,

E: Environment,
E::AccountId: Debug + Send + Sync,
E::Balance:
Clone + Debug + Send + Sync + From<u128> + scale::HasCompact + serde::Serialize,
E::Hash: Debug + Send + scale::Encode,
E::Hash: Debug + Send + Sync + scale::Encode,
{
}

Expand Down
Loading

0 comments on commit bc92b61

Please sign in to comment.