Unverified Commit 4f229fc3 authored by Michael Müller's avatar Michael Müller Committed by GitHub
Browse files

Update `rand-extension` example (#793)

* Update `rand-extension` example

* Fix nightly clippy error
parent aeca1695
Pipeline #138893 failed with stages
in 6 minutes and 49 seconds
......@@ -268,7 +268,7 @@ impl Bitvec {
if value {
// If `value` is `true` set its first bit to `1`.
bits256.set(0);
debug_assert_eq!(bits256.get(0), true);
debug_assert!(bits256.get(0));
};
self.bits.push(bits256);
*self.len += 1;
......
# Chain-side Extension
Use this as an implementation of the trait `ChainExtension` in Substrate
and use it as the associated type `ChainExtension` of the trait `pallet_contracts::Config`.
To integrate this example into Substrate you need to do two things:
* Use the code in [`chain-extension-example.rs`](./chain-extension-example.rs) as an implementation for
the trait `ChainExtension` in Substrate.
You can just copy/paste the content of that file into e.g. your `runtime/src/lib.rs`.
* Use the implementation as the associated type `ChainExtension` of the trait
`pallet_contracts::Config`:
```rust
impl pallet_contracts::Config for Runtime {
type ChainExtension = FetchRandomExtension;
}
```
use codec::{Encode, Decode};
use frame_support::debug::{error, native};
use frame_support::traits::Randomness;
use codec::Encode;
use frame_support::log::{
error,
trace,
};
use pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, UncheckedFrom,
ChainExtension,
Environment,
Ext,
InitState,
RetVal,
SysConfig,
UncheckedFrom,
};
use sp_runtime::DispatchError;
use sp_std::vec::Vec;
/// contract extension for `FetchRandom`
/// Contract extension for `FetchRandom`
pub struct FetchRandomExtension;
impl ChainExtension for FetchRandomExtension {
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]>,
impl ChainExtension<Runtime> for FetchRandomExtension {
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 {
1101 => {
let mut env = env.buf_in_buf_out();
let random_seed: [u8; 32] = super::RandomnessCollectiveFlip::random_seed().0;
let random_seed = crate::RandomnessCollectiveFlip::random_seed().0;
let random_slice = random_seed.encode();
native::trace!(
trace!(
target: "runtime",
"[ChainExtension]|call|func_id:{:}",
func_id
);
env.write(&random_slice, false, None)
.map_err(|_| DispatchError::Other("ChainExtension failed to call random"))?;
env.write(&random_slice, false, None).map_err(|_| {
DispatchError::Other("ChainExtension failed to call random")
})?;
}
_ => {
error!("call an unregistered `func_id`, func_id:{:}", func_id);
return Err(DispatchError::Other("Unimplemented func_id"));
error!("Called an unregistered `func_id`: {:}", func_id);
return Err(DispatchError::Other("Unimplemented func_id"))
}
}
Ok(RetVal::Converging(0))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment