Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes required for ink! integration #38

Merged
merged 8 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
shell: bash
run: |
rustup component add rust-src
cargo install cargo-contract --version 3.0.1
cargo install cargo-contract --version 3.2.0

- name: Run tests for examples
shell: bash
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.1.1"
version = "0.1.2"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand All @@ -43,4 +43,4 @@ sp-core = { version = "22.0.0" }

# Local dependencies

drink = { version = "0.1.1", path = "drink" }
drink = { version = "0.1.2", path = "drink" }
21 changes: 21 additions & 0 deletions drink/src/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub trait ChainApi {
fn build_block(&mut self) -> DrinkResult<u64>;

/// Build `n` empty blocks and return the new height.
///
/// # Arguments
///
/// * `n` - The number of blocks to build.
fn build_blocks(&mut self, n: u64) -> DrinkResult<u64> {
let mut last_block = None;
for _ in 0..n {
Expand All @@ -22,7 +26,19 @@ pub trait ChainApi {
}

/// Add tokens to an account.
///
/// # Arguments
///
/// * `address` - The address of the account to add tokens to.
/// * `amount` - The number of tokens to add.
fn add_tokens(&mut self, address: AccountId32, amount: u128);

/// Return the balance of an account.
///
/// # Arguments
///
/// * `address` - The address of the account to query.
fn balance(&mut self, address: &AccountId32) -> u128;
}

impl<R: Runtime> ChainApi for Sandbox<R> {
Expand All @@ -45,4 +61,9 @@ impl<R: Runtime> ChainApi for Sandbox<R> {
let _ = pallet_balances::Pallet::<R>::deposit_creating(&address, amount);
});
}

fn balance(&mut self, address: &AccountId32) -> u128 {
self.externalities
.execute_with(|| pallet_balances::Pallet::<R>::free_balance(address))
}
}
70 changes: 65 additions & 5 deletions drink/src/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,89 @@

use frame_support::{sp_runtime::AccountId32, weights::Weight};
use pallet_contracts::{CollectEvents, DebugInfo, Determinism};
use pallet_contracts_primitives::{Code, ContractExecResult, ContractInstantiateResult};
use pallet_contracts_primitives::{
Code, CodeUploadResult, ContractExecResult, ContractInstantiateResult,
};

use crate::{runtime::Runtime, EventRecordOf, Sandbox};

/// Interface for contract-related operations.
pub trait ContractApi<R: Runtime> {
/// Interface for `bare_instantiate` contract call.
///
/// # Arguments
///
/// * `contract_bytes` - The contract code.
/// * `value` - The number of tokens to be transferred to the contract.
/// * `data` - The input data to be passed to the contract (including constructor name).
/// * `salt` - The salt to be used for contract address derivation.
/// * `origin` - The sender of the contract call.
/// * `gas_limit` - The gas limit for the contract call.
/// * `storage_deposit_limit` - The storage deposit limit for the contract call.
#[allow(clippy::too_many_arguments)]
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
value: u128,
piotrMocz marked this conversation as resolved.
Show resolved Hide resolved
data: Vec<u8>,
salt: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractInstantiateResult<AccountId32, u128, EventRecordOf<R>>;

/// Interface for `bare_upload_code` contract call.
///
/// # Arguments
///
/// * `contract_bytes` - The contract code.
/// * `origin` - The sender of the contract call.
/// * `storage_deposit_limit` - The storage deposit limit for the contract call.
fn upload_contract(
&mut self,
contract_bytes: Vec<u8>,
origin: AccountId32,
storage_deposit_limit: Option<u128>,
) -> CodeUploadResult<<R as frame_system::Config>::Hash, u128>;

/// Interface for `bare_call` contract call.
///
/// # Arguments
///
/// * `address` - The address of the contract to be called.
/// * `value` - The number of tokens to be transferred to the contract.
/// * `data` - The input data to be passed to the contract (including message name).
/// * `origin` - The sender of the contract call.
/// * `gas_limit` - The gas limit for the contract call.
/// * `storage_deposit_limit` - The storage deposit limit for the contract call.
fn call_contract(
&mut self,
address: AccountId32,
value: u128,
data: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractExecResult<u128, EventRecordOf<R>>;
}

impl<R: Runtime> ContractApi<R> for Sandbox<R> {
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
value: u128,
data: Vec<u8>,
salt: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractInstantiateResult<AccountId32, u128, EventRecordOf<R>> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_instantiate(
origin,
0,
value,
gas_limit,
None,
storage_deposit_limit,
Code::Upload(contract_bytes),
data,
salt,
Expand All @@ -52,20 +94,38 @@ impl<R: Runtime> ContractApi<R> for Sandbox<R> {
})
}

fn upload_contract(
&mut self,
contract_bytes: Vec<u8>,
origin: AccountId32,
storage_deposit_limit: Option<u128>,
) -> CodeUploadResult<<R as frame_system::Config>::Hash, u128> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_upload_code(
origin,
contract_bytes,
storage_deposit_limit,
Determinism::Enforced,
)
})
}

fn call_contract(
&mut self,
address: AccountId32,
value: u128,
data: Vec<u8>,
origin: AccountId32,
gas_limit: Weight,
storage_deposit_limit: Option<u128>,
) -> ContractExecResult<u128, EventRecordOf<R>> {
self.externalities.execute_with(|| {
pallet_contracts::Pallet::<R>::bare_call(
origin,
address,
0,
value,
gas_limit,
None,
storage_deposit_limit,
data,
DebugInfo::UnsafeDebug,
CollectEvents::UnsafeCollect,
Expand Down
21 changes: 18 additions & 3 deletions drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::{
Sandbox, DEFAULT_ACTOR, DEFAULT_GAS_LIMIT,
};

const ZERO_TRANSFER: u128 = 0;
const DEFAULT_STORAGE_DEPOSIT_LIMIT: Option<u128> = None;

/// Session specific errors.
#[derive(Error, Debug)]
pub enum SessionError {
Expand Down Expand Up @@ -165,6 +168,11 @@ impl<R: Runtime> Session<R> {
&mut self.sandbox
}

/// Returns a reference for basic contracts API.
pub fn contracts_api(&mut self) -> &mut impl ContractApi<R> {
&mut self.sandbox
}

/// Deploys a contract with a given constructor, arguments and salt. In case of a success,
/// returns `self`.
pub fn deploy_and(
Expand Down Expand Up @@ -196,10 +204,12 @@ impl<R: Runtime> Session<R> {

let result = self.sandbox.deploy_contract(
contract_bytes,
ZERO_TRANSFER,
data,
salt,
self.actor.clone(),
self.gas_limit,
DEFAULT_STORAGE_DEPOSIT_LIMIT,
);

let ret = match &result.result {
Expand Down Expand Up @@ -272,9 +282,14 @@ impl<R: Runtime> Session<R> {
.clone(),
};

let result = self
.sandbox
.call_contract(address, data, self.actor.clone(), self.gas_limit);
let result = self.sandbox.call_contract(
address,
ZERO_TRANSFER,
data,
self.actor.clone(),
self.gas_limit,
DEFAULT_STORAGE_DEPOSIT_LIMIT,
);

let ret = match &result.result {
Ok(exec_result) if exec_result.did_revert() => Err(SessionError::CallReverted),
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.69"
channel = "1.70"