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

Add example which uses ext_transfer + ext_terminate #554

Merged
merged 20 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions crates/env/src/engine/off_chain/db/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ impl AccountsDb {
},
);
}

/// Removes an account.
pub fn remove_account<T>(&mut self, account_id: T::AccountId)
where
T: Environment,
{
self.accounts.remove(&OffAccountId::new(&account_id));
}
}

/// An account within the chain.
Expand Down
42 changes: 35 additions & 7 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl EnvBackend for EnvInstance {
impl EnvInstance {
fn transfer_impl<T>(
&mut self,
destination: T::AccountId,
destination: &T::AccountId,
value: T::Balance,
) -> Result<()>
where
Expand All @@ -220,18 +220,46 @@ impl EnvInstance {
}
let dst_value = self
.accounts
.get_or_create_account::<T>(&destination)
.get_or_create_account::<T>(destination)
.balance::<T>()?;
self.accounts
.get_account_mut::<T>(&src_id)
.expect("account of executed contract must exist")
.set_balance::<T>(src_value - value)?;
self.accounts
.get_account_mut::<T>(&destination)
.get_account_mut::<T>(destination)
.expect("the account must exist already or has just been created")
.set_balance::<T>(dst_value + value)?;
Ok(())
}

// Remove the calling account and transfer remaining balance.
//
// This function never returns. Either the termination was successful and the
// execution of the destroyed contract is halted. Or it failed during the termination
// which is considered fatal.
fn terminate_contract_impl<T>(&mut self, beneficiary: T::AccountId) -> !
where
T: Environment,
{
// Send the remaining balance to the beneficiary
let all: T::Balance = self.balance::<T>().expect("could not decode balance");
self.transfer_impl::<T>(&beneficiary, all)
.expect("transfer did not work ");

// Remove account
let contract_id = self.account_id::<T>().expect("could not decode account id");
self.accounts.remove_account::<T>(contract_id);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the on-chain impl would do here is set a tombstone with a code hash and remove storage. AFAIU both is not easily achievable with our current off-chain env, hence I left it out here for the moment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe provide a small dev. note comment explaining exactly this?

// Encode the result of the termination and panic with it.
// This enables testing for the proper result and makes sure this
// method returns `Never`.
let res = crate::test::ContractTerminationResult::<T::AccountId, T::Balance> {
beneficiary,
transferred: all,
};
panic!(scale::Encode::encode(&res));
}
}

impl TypedEnvBackend for EnvInstance {
Expand Down Expand Up @@ -349,7 +377,7 @@ impl TypedEnvBackend for EnvInstance {
T: Environment,
Args: scale::Encode,
{
unimplemented!("off-chain environment does not support contract invokation")
unimplemented!("off-chain environment does not support contract invocation")
}

fn eval_contract<T, Args, R>(
Expand All @@ -375,11 +403,11 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("off-chain environment does not support contract instantiation")
}

fn terminate_contract<T>(&mut self, _beneficiary: T::AccountId) -> !
fn terminate_contract<T>(&mut self, beneficiary: T::AccountId) -> !
where
T: Environment,
{
unimplemented!("off-chain environment does not support contract termination")
self.terminate_contract_impl::<T>(beneficiary)
}

fn restore_contract<T>(
Expand All @@ -398,7 +426,7 @@ impl TypedEnvBackend for EnvInstance {
where
T: Environment,
{
self.transfer_impl::<T>(destination, value)
self.transfer_impl::<T>(&destination, value)
}

fn random<T>(&mut self, subject: &[u8]) -> Result<T::Hash>
Expand Down
55 changes: 55 additions & 0 deletions crates/env/src/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,58 @@ where
Ok(callee)
})
}

/// The result of a successful contract termination.
#[derive(scale::Encode, scale::Decode)]
pub struct ContractTerminationResult<AccountId, Balance>
where
AccountId: scale::Codec,
Balance: scale::Codec,
{
/// The beneficiary account who received the remaining value in the contract.
pub beneficiary: AccountId,
/// The value which was transferred to the `beneficiary`.
pub transferred: Balance,
}

/// Tests if a contract terminates successfully after `self.env().terminate()`
/// has been called.
///
/// # Usage
///
/// The macro is used like this:
///
/// ```no_compile
/// let should_terminate = move || your_contract.fn_which_should_terminate();
/// ink_env::assert_contract_termination!(
/// should_terminate,
/// expected_beneficiary,
/// expected_value_transferred_to_beneficiary
/// );
/// ```
///
/// See `examples/lock-until` for a complete usage example.
#[cfg(feature = "std")]
#[macro_export]
macro_rules! assert_contract_termination {
(
$should_terminate:tt,
$beneficiary:expr,
$balance:expr
) => {
use std::panic;

let value_any = panic::catch_unwind($should_terminate)
.expect_err("contract did not terminate");
let encoded_input: &Vec<u8> =
value_any.downcast_ref::<Vec<u8>>().expect("must work");
let info: ink_env::test::ContractTerminationResult<AccountId, Balance> =
scale::Decode::decode(&mut &encoded_input[..]).expect("must work");

let expected_beneficiary: AccountId = $beneficiary;
assert_eq!(info.beneficiary, expected_beneficiary);

let expected_balance: Balance = $balance;
assert_eq!(info.transferred, expected_balance);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very clever and creative solution! might be acceptable. however, needs further macro hygiene improvements since this is going to be exposed to the users.

};
}
2 changes: 1 addition & 1 deletion crates/env/src/engine/off_chain/typed_encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<M> TypedEncoded<M> {
}
}

/// Creates a new typed-encoded ininitialized by `value` of type `T`.
/// Creates a new typed-encoded initialized by `value` of type `T`.
pub fn new<T>(value: &T) -> Self
where
T: scale::Encode + 'static,
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/ir/src/ir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ where
{
let (ink_attrs, other_attrs) = ir::partition_attributes(attrs)?;
let normalized = ir::InkAttribute::from_expanded(ink_attrs).map_err(|err| {
err.into_combine(format_err!(parent_span, "at this invokation",))
err.into_combine(format_err!(parent_span, "at this invocation",))
})?;
normalized.ensure_first(is_valid_first).map_err(|err| {
err.into_combine(format_err!(
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/ir/src/ir/item/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl TryFrom<syn::ItemStruct> for Event {
}
let normalized =
ir::InkAttribute::from_expanded(ink_attrs).map_err(|err| {
err.into_combine(format_err!(field_span, "at this invokation",))
err.into_combine(format_err!(field_span, "at this invocation",))
})?;
if !matches!(normalized.first().kind(), ir::AttributeArgKind::Topic) {
return Err(format_err!(
Expand Down
4 changes: 2 additions & 2 deletions crates/lang/ir/src/ir/item_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl ItemImpl {
if !ink_attrs.is_empty() {
let normalized =
ir::InkAttribute::from_expanded(ink_attrs).map_err(|err| {
err.into_combine(format_err!(impl_block_span, "at this invokation",))
err.into_combine(format_err!(impl_block_span, "at this invocation",))
})?;
if normalized
.ensure_first(&ir::AttributeArgKind::Implementation)
Expand Down Expand Up @@ -295,7 +295,7 @@ impl TryFrom<syn::ItemImpl> for ItemImpl {
if !ink_attrs.is_empty() {
let normalized =
ir::InkAttribute::from_expanded(ink_attrs).map_err(|err| {
err.into_combine(format_err!(impl_block_span, "at this invokation",))
err.into_combine(format_err!(impl_block_span, "at this invocation",))
})?;
normalized.ensure_no_conflicts(|arg| {
!matches!(arg.kind(), ir::AttributeArgKind::Implementation | ir::AttributeArgKind::Namespace(_))
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! parse, analyze and generate code for ink! smart contracts.
//!
//! The entry point for every ink! smart contract is the [`Contract`](`crate::ir::Contract`)
//! with its [`Config`](`crate::ir::Config`) provided in the initial invokation at
//! with its [`Config`](`crate::ir::Config`) provided in the initial invocation at
//! `#[ink::contract(... configuration ...)]`.
//!
//! The ink! IR tries to stay close to the original Rust syntactic structure.
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ where
ink_env::gas_left::<T>().expect("couldn't decode gas left")
}

/// Returns the timstamp of the current block.
/// Returns the timestamp of the current block.
///
/// # Note
///
Expand Down
9 changes: 9 additions & 0 deletions examples/lock-until/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
36 changes: 36 additions & 0 deletions examples/lock-until/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "lock_until"
version = "3.0.0-rc1"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
ink_primitives = { version = "3.0.0-rc1", path = "../../crates/primitives", default-features = false }
ink_metadata = { version = "3.0.0-rc1", path = "../../crates/metadata", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0.0-rc1", path = "../../crates/env", default-features = false }
ink_storage = { version = "3.0.0-rc1", path = "../../crates/storage", default-features = false }
ink_lang = { version = "3.0.0-rc1", path = "../../crates/lang", default-features = false }

scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.4", default-features = false, features = ["derive"], optional = true }


[lib]
name = "lock_until"
path = "lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info",
"scale-info/std",
]
ink-as-dependency = []
Loading