Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain extension #662

Merged
merged 26 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
"contracts/snarcos-contract",
"contracts/snarcos-extension",
]
55 changes: 52 additions & 3 deletions bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ pub use frame_support::{
StorageValue,
};
use frame_support::{
log::error,
sp_runtime::Perquintill,
traits::{ConstU32, ConstU64, EqualPrivilegeOnly, SortedMembers, U128CurrencyToVote},
weights::constants::WEIGHT_PER_MILLIS,
PalletId,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
pub use pallet_balances::Call as BalancesCall;
use pallet_contracts::weights::WeightInfo;
use pallet_contracts::{
chain_extension::{
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom,
},
weights::WeightInfo,
};
use pallet_contracts_primitives::{
CodeUploadResult, ContractExecResult, ContractInstantiateResult, GetStorageResult,
};
Expand All @@ -51,7 +57,7 @@ use sp_runtime::{
OpaqueKeys, Verify,
},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature, RuntimeAppPublic,
ApplyExtrinsicResult, DispatchError, MultiSignature, RuntimeAppPublic,
};
pub use sp_runtime::{FixedPointNumber, Perbill, Permill};
use sp_staking::EraIndex;
Expand Down Expand Up @@ -640,6 +646,49 @@ impl pallet_utility::Config for Runtime {
type PalletsOrigin = OriginCaller;
}

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 {
41 => {
deuszx marked this conversation as resolved.
Show resolved Hide resolved
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"))
}
}
}

fn enabled() -> bool {
true
}
}

// Prints debug output of the `contracts` pallet to stdout if the node is started with `-lruntime::contracts=debug`.
const CONTRACTS_DEBUG_OUTPUT: bool = true;

Expand Down Expand Up @@ -672,7 +721,7 @@ impl pallet_contracts::Config for Runtime {
type DepositPerByte = DepositPerByte;
type WeightPrice = pallet_transaction_payment::Pallet<Self>;
type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
type ChainExtension = ();
type ChainExtension = AlephChainExtension;
type DeletionQueueDepth = DeletionQueueDepth;
type DeletionWeightLimit = DeletionWeightLimit;
type Schedule = Schedule;
Expand Down
Loading