Unverified Commit 648283ee authored by Hero Bird's avatar Hero Bird Committed by GitHub

Add support for ext_terminate (#352)

* [core, lang] add support for ext_terminate

* [lang] apply rustfmt
parent d55fcd0e
Pipeline #83294 passed with stages
in 8 minutes and 50 seconds
......@@ -395,6 +395,25 @@ pub fn restore_contract<T>(
})
}
/// Terminates the existence of the currently executed smart contract.
///
/// This removes the calling account and transfers all remaining balance
/// to the given beneficiary.
///
/// # Note
///
/// 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 and results in a trap + rollback.
pub fn terminate_contract<T>(beneficiary: T::AccountId) -> !
where
T: EnvTypes,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnv::terminate_contract::<T>(instance, beneficiary)
})
}
/// Transfers value from the contract to the destination account ID.
///
/// # Note
......
......@@ -234,6 +234,15 @@ pub trait TypedEnv: Env {
) where
T: EnvTypes;
/// Terminates a smart contract.
///
/// # Note
///
/// For more details visit: [`ink_core::env::terminate_contract`]
fn terminate_contract<T>(&mut self, beneficiary: T::AccountId) -> !
where
T: EnvTypes;
/// Transfers value from the contract to the destination account ID.
///
/// # Note
......
......@@ -249,6 +249,13 @@ impl TypedEnv for EnvInstance {
unimplemented!("off-chain environment does not support contract instantiation")
}
fn terminate_contract<T>(&mut self, _beneficiary: T::AccountId) -> !
where
T: EnvTypes,
{
unimplemented!("off-chain environment does not support contract termination")
}
fn restore_contract<T>(
&mut self,
_account_id: T::AccountId,
......
......@@ -77,6 +77,7 @@ mod sys {
delta_ptr: u32,
delta_count: u32,
);
pub fn ext_terminate(beneficiary_ptr: u32, beneficiary_len: u32) -> !;
pub fn ext_dispatch_call(call_ptr: u32, call_len: u32);
......@@ -243,6 +244,10 @@ pub fn restore_to(
}
}
pub fn terminate(beneficiary: &[u8]) -> ! {
unsafe { sys::ext_terminate(beneficiary.as_ptr() as u32, beneficiary.len() as u32) }
}
pub fn dispatch_call(call: &[u8]) {
unsafe { sys::ext_dispatch_call(call.as_ptr() as u32, call.len() as u32) }
}
......
......@@ -330,6 +330,14 @@ impl TypedEnv for EnvInstance {
ext::restore_to(account_id, code_hash, rent_allowance, filtered_keys);
}
fn terminate_contract<T>(&mut self, beneficiary: T::AccountId) -> !
where
T: EnvTypes,
{
self.encode_into_buffer(beneficiary);
ext::terminate(&self.buffer[..]);
}
fn transfer<T>(&mut self, destination: T::AccountId, value: T::Balance) -> Result<()>
where
T: EnvTypes,
......
......@@ -263,6 +263,18 @@ where
env::restore_contract::<T>(account_id, code_hash, rent_allowance, filtered_keys)
}
/// Terminates the existence of a smart contract.
///
/// # Note
///
/// For more details visit: [`ink_core::env::terminate_contract`]
pub fn terminate_contract(self, beneficiary: T::AccountId) -> !
where
T: EnvTypes,
{
env::terminate_contract::<T>(beneficiary)
}
/// Transfers value from the contract to the destination account ID.
///
/// # Note
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment