Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
seal: Remove ext_dispatch_call and ext_get_runtime_storage (#6464)
Browse files Browse the repository at this point in the history
Those are way too hard to audit and make only sense with specific
chains. They shouldn't be in the core API.
  • Loading branch information
athei authored Jun 24, 2020
1 parent 971e52f commit 19b4b70
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 649 deletions.
1 change: 0 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ impl pallet_contracts::Trait for Runtime {
type Time = Timestamp;
type Randomness = RandomnessCollectiveFlip;
type Currency = Balances;
type Call = Call;
type Event = Event;
type DetermineContractAddress = pallet_contracts::SimpleAddressDeterminer<Runtime>;
type TrieIdGenerator = pallet_contracts::TrieIdFromParentCounter<Runtime>;
Expand Down
14 changes: 0 additions & 14 deletions frame/contracts/fixtures/dispatch_call.wat

This file was deleted.

15 changes: 0 additions & 15 deletions frame/contracts/fixtures/dispatch_call_then_trap.wat

This file was deleted.

74 changes: 0 additions & 74 deletions frame/contracts/fixtures/get_runtime_storage.wat

This file was deleted.

4 changes: 2 additions & 2 deletions frame/contracts/fixtures/restoration.wat
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@

;; Code hash of SET_RENT
(data (i32.const 264)
"\c2\1c\41\10\a5\22\d8\59\1c\4c\77\35\dd\2d\bf\a1"
"\13\0b\50\93\76\9b\92\31\97\b7\c5\74\26\aa\38\2a"
"\ab\d6\58\65\1e\83\6e\4a\18\0d\f2\6d\bc\42\ba\e9"
"\3d\64\76\e5\30\5b\33\46\bb\4d\43\99\38\21\ee\32"
)

;; Rent allowance
Expand Down
19 changes: 12 additions & 7 deletions frame/contracts/fixtures/set_rent.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(import "env" "ext_dispatch_call" (func $ext_dispatch_call (param i32 i32)))
(import "env" "ext_transfer" (func $ext_transfer (param i32 i32 i32 i32) (result i32)))
(import "env" "ext_set_storage" (func $ext_set_storage (param i32 i32 i32)))
(import "env" "ext_clear_storage" (func $ext_clear_storage (param i32)))
(import "env" "ext_set_rent_allowance" (func $ext_set_rent_allowance (param i32 i32)))
Expand All @@ -23,11 +23,13 @@
)
)

;; transfer 50 to ALICE
;; transfer 50 to CHARLIE
(func $call_2
(call $ext_dispatch_call
(i32.const 68)
(i32.const 11)
(call $assert
(i32.eq
(call $ext_transfer (i32.const 68) (i32.const 8) (i32.const 76) (i32.const 8))
(i32.const 0)
)
)
)

Expand Down Expand Up @@ -96,6 +98,9 @@
;; Encoding of 10 in balance
(data (i32.const 0) "\28")

;; Encoding of call transfer 50 to CHARLIE
(data (i32.const 68) "\00\00\03\00\00\00\00\00\00\00\C8")
;; encoding of Charlies's account id
(data (i32.const 68) "\03")

;; encoding of 50 balance
(data (i32.const 76) "\32")
)
48 changes: 8 additions & 40 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use frame_support::{
};

pub type AccountIdOf<T> = <T as frame_system::Trait>::AccountId;
pub type CallOf<T> = <T as Trait>::Call;
pub type MomentOf<T> = <<T as Trait>::Time as Time>::Moment;
pub type SeedOf<T> = <T as frame_system::Trait>::Hash;
pub type BlockNumberOf<T> = <T as frame_system::Trait>::BlockNumber;
Expand Down Expand Up @@ -151,9 +150,6 @@ pub trait Ext {
input_data: Vec<u8>,
) -> ExecResult;

/// Notes a call dispatch.
fn note_dispatch_call(&mut self, call: CallOf<Self::T>);

/// Restores the given destination contract sacrificing the current one.
///
/// Since this function removes the self contract eagerly, if succeeded, no further actions should
Expand Down Expand Up @@ -274,23 +270,11 @@ impl<T: Trait> Token<T> for ExecFeeToken {
}
}

#[cfg_attr(any(feature = "std", test), derive(PartialEq, Eq, Clone))]
#[derive(sp_runtime::RuntimeDebug)]
pub enum DeferredAction<T: Trait> {
DispatchRuntimeCall {
/// The account id of the contract who dispatched this call.
origin: T::AccountId,
/// The call to dispatch.
call: <T as Trait>::Call,
},
}

pub struct ExecutionContext<'a, T: Trait + 'a, V, L> {
pub caller: Option<&'a ExecutionContext<'a, T, V, L>>,
pub self_account: T::AccountId,
pub self_trie_id: Option<TrieId>,
pub depth: usize,
pub deferred: Vec<DeferredAction<T>>,
pub config: &'a Config<T>,
pub vm: &'a V,
pub loader: &'a L,
Expand All @@ -314,7 +298,6 @@ where
self_trie_id: None,
self_account: origin,
depth: 0,
deferred: Vec::new(),
config: &cfg,
vm: &vm,
loader: &loader,
Expand All @@ -331,7 +314,6 @@ where
self_trie_id: trie_id,
self_account: dest,
depth: self.depth + 1,
deferred: Vec::new(),
config: self.config,
vm: self.vm,
loader: self.loader,
Expand Down Expand Up @@ -532,21 +514,14 @@ where
where F: FnOnce(&mut ExecutionContext<T, V, L>) -> ExecResult
{
use frame_support::storage::TransactionOutcome::*;
let (output, deferred) = {
let mut nested = self.nested(dest, trie_id);
let output = frame_support::storage::with_transaction(|| {
let output = func(&mut nested);
match output {
Ok(ref rv) if rv.is_success() => Commit(output),
_ => Rollback(output),
}
})?;
(output, nested.deferred)
};
if output.is_success() {
self.deferred.extend(deferred);
}
Ok(output)
let mut nested = self.nested(dest, trie_id);
frame_support::storage::with_transaction(|| {
let output = func(&mut nested);
match output {
Ok(ref rv) if rv.is_success() => Commit(output),
_ => Rollback(output),
}
})
}

/// Returns whether a contract, identified by address, is currently live in the execution
Expand Down Expand Up @@ -767,13 +742,6 @@ where
self.ctx.call(to.clone(), value, gas_meter, input_data)
}

fn note_dispatch_call(&mut self, call: CallOf<Self::T>) {
self.ctx.deferred.push(DeferredAction::DispatchRuntimeCall {
origin: self.ctx.self_account.clone(),
call,
});
}

fn restore_to(
&mut self,
dest: AccountIdOf<Self::T>,
Expand Down
44 changes: 6 additions & 38 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,13 @@ use sp_runtime::{
},
RuntimeDebug,
};
use frame_support::dispatch::{
PostDispatchInfo, DispatchResult, Dispatchable, DispatchResultWithPostInfo
};
use frame_support::{
Parameter, decl_module, decl_event, decl_storage, decl_error,
parameter_types, IsSubType, storage::child::ChildInfo,
decl_module, decl_event, decl_storage, decl_error,
parameter_types, storage::child::ChildInfo,
dispatch::{DispatchResult, DispatchResultWithPostInfo},
traits::{OnUnbalanced, Currency, Get, Time, Randomness},
};
use frame_support::traits::{OnUnbalanced, Currency, Get, Time, Randomness};
use frame_support::weights::GetDispatchInfo;
use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root};
use frame_system::{self as system, ensure_signed, ensure_root};
use pallet_contracts_primitives::{RentProjection, ContractAccessError};
use frame_support::weights::Weight;

Expand Down Expand Up @@ -321,12 +318,6 @@ pub trait Trait: frame_system::Trait {
/// The currency in which fees are paid and contract balances are held.
type Currency: Currency<Self::AccountId>;

/// The outer call dispatch type.
type Call:
Parameter +
Dispatchable<PostInfo=PostDispatchInfo, Origin=<Self as frame_system::Trait>::Origin> +
IsSubType<Module<Self>, Self> + GetDispatchInfo;

/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

Expand Down Expand Up @@ -644,30 +635,7 @@ impl<T: Trait> Module<T> {
let vm = WasmVm::new(&cfg.schedule);
let loader = WasmLoader::new(&cfg.schedule);
let mut ctx = ExecutionContext::top_level(origin.clone(), &cfg, &vm, &loader);

let result = func(&mut ctx, gas_meter);

// Execute deferred actions.
ctx.deferred.into_iter().for_each(|deferred| {
use self::exec::DeferredAction::*;
match deferred {
DispatchRuntimeCall {
origin: who,
call,
} => {
let info = call.get_dispatch_info();
let result = call.dispatch(RawOrigin::Signed(who.clone()).into());
let post_info = match result {
Ok(post_info) => post_info,
Err(err) => err.post_info,
};
gas_meter.refund(post_info.calc_unspent(&info));
Self::deposit_event(RawEvent::Dispatched(who, result.is_ok()));
}
}
});

result
func(&mut ctx, gas_meter)
}
}

Expand Down
Loading

0 comments on commit 19b4b70

Please sign in to comment.