Skip to content

Commit

Permalink
Chain extension (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 authored Oct 20, 2022
1 parent 4df6103 commit fab52b8
Show file tree
Hide file tree
Showing 10 changed files with 1,667 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ exclude = [
"fork-off",
"benches/payout-stakers",
"bin/cliain",
"bin/runtime/src/chain_extension/snarcos-extension",
"contracts/snarcos-contract",
]
49 changes: 49 additions & 0 deletions bin/runtime/src/chain_extension/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use frame_support::log::error;
use pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig,
};
use sp_core::crypto::UncheckedFrom;
use sp_runtime::DispatchError;

use crate::Runtime;

pub const SNARCOS_CHAIN_EXT: u32 = 41;

pub struct AlephChainExtension;
impl ChainExtension<Runtime> for AlephChainExtension {
fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
match func_id {
SNARCOS_CHAIN_EXT => {
use pallet_snarcos::{Error, Pallet as Snarcos};

// The argument-passing-mode doesn't matter for now. All the data to runtime call
// are mocked now.
let mut env = env.buf_in_buf_out();
// After benchmarking is merged and `pallet_snarcos::WeightInfo` is available,
// use real weight here.
env.charge_weight(41)?;

match Snarcos::<Runtime>::bare_store_key([0u8; 4], [0u8; 8].to_vec()) {
// In case `DispatchResultWithPostInfo` was returned (or some simpler
// equivalent for `bare_store_key`), we could adjust weight. However, for the
// storing key action it doesn't make sense.
Ok(_) => {
// Return status code for success.
Ok(RetVal::Converging(0))
}
Err(Error::<Runtime>::VerificationKeyTooLong) => Ok(RetVal::Converging(1)),
Err(Error::<Runtime>::IdentifierAlreadyInUse) => Ok(RetVal::Converging(2)),
// Unknown error.
_ => Ok(RetVal::Converging(3)),
}
}
_ => {
error!("Called an unregistered `func_id`: {}", func_id);
Err(DispatchError::Other("Unimplemented func_id"))
}
}
}
}
Loading

0 comments on commit fab52b8

Please sign in to comment.