From 2ffc74fb107e3c85bf07035487eba98c117ba14c Mon Sep 17 00:00:00 2001 From: Song Zhou Date: Sat, 18 Dec 2021 04:44:55 +0000 Subject: [PATCH] s-contract stage --- Cargo.lock | 20 +++++++++ Cargo.toml | 1 + pallets/s-contract/src/lib.rs | 80 +++++++++++++++++++---------------- runtime/src/lib.rs | 1 - 4 files changed, 65 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae1c31f..10e9206 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3766,6 +3766,26 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-s-contract" +version = "3.0.0" +dependencies = [ + "env_logger", + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "pallet-balances", + "pallet-secrets", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", + "test-env-log", +] + [[package]] name = "pallet-secrets" version = "3.0.0" diff --git a/Cargo.toml b/Cargo.toml index 09f4dcb..d886722 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ 'node', 'pallets/naming', 'pallets/secrets', + 'pallets/s-contract', 'runtime', ] [profile.release] diff --git a/pallets/s-contract/src/lib.rs b/pallets/s-contract/src/lib.rs index f6a258d..2d3bdac 100644 --- a/pallets/s-contract/src/lib.rs +++ b/pallets/s-contract/src/lib.rs @@ -1,12 +1,12 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; pub use pallet::*; +use pallet_secrets; +// #[cfg(test)] +// mod tests; -#[cfg(test)] -mod tests; - -#[cfg(test)] -mod mock; +// #[cfg(test)] +// mod mock; // #[cfg(feature = "runtime-benchmarks")] // mod benchmarking; @@ -22,9 +22,9 @@ pub mod pallet { use super::*; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + pallet_secrets::Config { type Event: From> + IsType<::Event>; - + #[pallet::constant] type CallLength: Get; // type ForceOrigin: EnsureOrigin; @@ -53,6 +53,7 @@ pub mod pallet { pub enum Error { InvalideEncodedCall, ContrtactIndexError, + CallIndexError, } #[pallet::call] @@ -63,22 +64,27 @@ pub mod pallet { origin: OriginFor, metadata: Vec, contract_public_key: Vec, - constructor_call: EncodedCall, + initializatio_call: EncodedCall, ) -> DispatchResult { - let who = ensure_signed(origin)?; - ensure!(Self::validate_encoded_call(constructor_call.clone()), Error::::InvalideEncodedCall); - - pallet_secret::Pallet::::register_secret_contract(origin, metedata, contract_public_key); - - // get the lastest secretId - 1 -> it belongs to the secret we have just created - let contract_index = pallet_seceret::Pallet::::current_secret_id().saturating_sub(1); - - // the first call - Self::insert_call(contract_index, 0, constructor_call); - - Self::deposit_event(Event::::ContractRegistered(contract_index)); - - Ok(()) + // let who = ensure_signed(origin)?; + + ensure!(Self::validate_call(initializatio_call.clone()), Error::::InvalideEncodedCall); + match pallet_secrets::Pallet::::register_secret_contract( + origin, metadata, contract_public_key + ) { + Ok(()) => { + // get the lastest secretId - 1 -> it belongs to the secret we have just created + let contract_index = pallet_secrets::Pallet::::current_secret_id().saturating_sub(1); + + // the first call + Self::insert_call(contract_index, 0, initializatio_call); + + Self::deposit_event(Event::::ContractRegistered(contract_index)); + + Ok(()) + }, + Err(err) => Err(err) + } } #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(2, 3))] @@ -88,16 +94,17 @@ pub mod pallet { call: EncodedCall, ) -> DispatchResult { let who = ensure_signed(origin)?; - ensure!(Self::validate_encoded_call(constructor_call.clone()), Error::::InvalideEncodedCall); - - // the first call - Self::insert_call(contract_index, 0, constructor_call); - - // .... - - Self::deposit_event(Event::::ContractRegistered(contract_index)); + ensure!(Self::validate_call(call.clone()), Error::::InvalideEncodedCall); + + // 1. get the next call index + // 2. insert the call - Ok(()) + if Self::insert_call(contract_index, 0, call) { + Self::deposit_event(Event::::CallReceived(contract_index, 0)); + Ok(()) + } else { + Err(Error::::CallIndexError.into()) + } } } @@ -110,20 +117,21 @@ pub mod pallet { pub fn insert_call( contract_index: SecretId, - call_index: 0, + call_index: CallIndex, call: EncodedCall, - ) { + ) -> bool { match >::get(&contract_index) { Some(history) => { match history.len() as usize { - (call_index) => { + call_index => { + return true; // insert the call }, - _ => Error::::ContrtactIndexError + _ => false } }, - None() => Error::::ContrtactIndexError + None => false } } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 1480b50..8a197c3 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -512,7 +512,6 @@ impl_runtime_apis! { add_benchmark!(params, batches, frame_system, SystemBench::); add_benchmark!(params, batches, pallet_balances, Balances); add_benchmark!(params, batches, pallet_timestamp, Timestamp); - add_benchmark!(params, batches, pallet_template, TemplateModule); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } Ok(batches)