Skip to content

Commit

Permalink
s-contract stage
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Dec 18, 2021
1 parent dbc777b commit 2ffc74f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
'node',
'pallets/naming',
'pallets/secrets',
'pallets/s-contract',
'runtime',
]
[profile.release]
Expand Down
80 changes: 44 additions & 36 deletions pallets/s-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

#[pallet::constant]
type CallLength: Get<u32>;
// type ForceOrigin: EnsureOrigin<Self::Origin>;
Expand Down Expand Up @@ -53,6 +53,7 @@ pub mod pallet {
pub enum Error<T> {
InvalideEncodedCall,
ContrtactIndexError,
CallIndexError,
}

#[pallet::call]
Expand All @@ -63,22 +64,27 @@ pub mod pallet {
origin: OriginFor<T>,
metadata: Vec<u8>,
contract_public_key: Vec<u8>,
constructor_call: EncodedCall,
initializatio_call: EncodedCall,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(Self::validate_encoded_call(constructor_call.clone()), Error::<T>::InvalideEncodedCall);

pallet_secret::Pallet::<T>::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::<T>::current_secret_id().saturating_sub(1);

// the first call
Self::insert_call(contract_index, 0, constructor_call);

Self::deposit_event(Event::<T>::ContractRegistered(contract_index));

Ok(())
// let who = ensure_signed(origin)?;

ensure!(Self::validate_call(initializatio_call.clone()), Error::<T>::InvalideEncodedCall);
match pallet_secrets::Pallet::<T>::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::<T>::current_secret_id().saturating_sub(1);

// the first call
Self::insert_call(contract_index, 0, initializatio_call);

Self::deposit_event(Event::<T>::ContractRegistered(contract_index));

Ok(())
},
Err(err) => Err(err)
}
}

#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(2, 3))]
Expand All @@ -88,16 +94,17 @@ pub mod pallet {
call: EncodedCall,
) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(Self::validate_encoded_call(constructor_call.clone()), Error::<T>::InvalideEncodedCall);

// the first call
Self::insert_call(contract_index, 0, constructor_call);

// ....

Self::deposit_event(Event::<T>::ContractRegistered(contract_index));
ensure!(Self::validate_call(call.clone()), Error::<T>::InvalideEncodedCall);

// 1. get the next call index
// 2. insert the call

Ok(())
if Self::insert_call(contract_index, 0, call) {
Self::deposit_event(Event::<T>::CallReceived(contract_index, 0));
Ok(())
} else {
Err(Error::<T>::CallIndexError.into())
}
}
}

Expand All @@ -110,20 +117,21 @@ pub mod pallet {

pub fn insert_call(
contract_index: SecretId,
call_index: 0,
call_index: CallIndex,
call: EncodedCall,
) {
) -> bool {

match <CallHistory<T>>::get(&contract_index) {
Some(history) => {
match history.len() as usize {
(call_index) => {
call_index => {
return true;
// insert the call
},
_ => Error::<T>::ContrtactIndexError
_ => false
}
},
None() => Error::<T>::ContrtactIndexError
None => false
}
}
}
Expand Down
1 change: 0 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ impl_runtime_apis! {
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
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)
Expand Down

0 comments on commit 2ffc74f

Please sign in to comment.