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

Update rand-extension example #793

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion crates/storage/src/collections/bitvec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 15 additions & 2 deletions examples/rand-extension/runtime/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# 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;
}
```
46 changes: 28 additions & 18 deletions examples/rand-extension/runtime/chain-extension-example.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
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))
Expand Down