From 55dc1ca31429be6b3d8f22c155faea4eb6b7fce2 Mon Sep 17 00:00:00 2001 From: Justin Frevert Date: Mon, 22 Nov 2021 05:02:55 -0500 Subject: [PATCH 1/4] Add argument to rand-extension example --- examples/rand-extension/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/rand-extension/lib.rs b/examples/rand-extension/lib.rs index d6033fd82f8..ed67a0db26f 100755 --- a/examples/rand-extension/lib.rs +++ b/examples/rand-extension/lib.rs @@ -15,7 +15,7 @@ pub trait FetchRandom { /// Note: this gives the operation a corresponding `func_id` (1101 in this case), /// and the chain-side chain extension will get the `func_id` to do further operations. #[ink(extension = 1101, returns_result = false)] - fn fetch_random() -> [u8; 32]; + fn fetch_random(subject: [u8; 32]) -> [u8; 32]; } #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] @@ -88,9 +88,9 @@ mod rand_extension { /// Update the value from the runtimes random source. #[ink(message)] - pub fn update(&mut self) -> Result<(), RandomReadErr> { + pub fn update(&mut self, subject: [u8; 32]) -> Result<(), RandomReadErr> { // Get the on-chain random seed - let new_random = self.env().extension().fetch_random()?; + let new_random = self.env().extension().fetch_random(subject)?; self.value = new_random; // Emit the `RandomUpdated` event when the random seed // is successfully fetched. From 2d4c3388a2690350483ebf3b44047ff4ae68fe46 Mon Sep 17 00:00:00 2001 From: Justin Frevert Date: Mon, 22 Nov 2021 05:50:12 -0500 Subject: [PATCH 2/4] add appropriate runtime and test example changes --- examples/rand-extension/lib.rs | 2 +- examples/rand-extension/runtime/chain-extension-example.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/rand-extension/lib.rs b/examples/rand-extension/lib.rs index ed67a0db26f..aede5ffcabe 100755 --- a/examples/rand-extension/lib.rs +++ b/examples/rand-extension/lib.rs @@ -146,7 +146,7 @@ mod rand_extension { assert_eq!(rand_extension.get(), [0; 32]); // when - rand_extension.update().expect("update must work"); + rand_extension.update([0_u8; 32]).expect("update must work"); // then assert_eq!(rand_extension.get(), [1; 32]); diff --git a/examples/rand-extension/runtime/chain-extension-example.rs b/examples/rand-extension/runtime/chain-extension-example.rs index 9aa9022d19f..ac6ccf81119 100644 --- a/examples/rand-extension/runtime/chain-extension-example.rs +++ b/examples/rand-extension/runtime/chain-extension-example.rs @@ -29,7 +29,8 @@ impl ChainExtension for FetchRandomExtension { match func_id { 1101 => { let mut env = env.buf_in_buf_out(); - let random_seed = crate::RandomnessCollectiveFlip::random_seed().0; + let arg: [u8; 32] = env.read_as()?; + let random_seed = crate::RandomnessCollectiveFlip::random(&arg).0; let random_slice = random_seed.encode(); trace!( target: "runtime", From f82538fec6c48561af139492014c6cc1106afef7 Mon Sep 17 00:00:00 2001 From: Justin Frevert Date: Tue, 23 Nov 2021 17:01:55 -0500 Subject: [PATCH 3/4] Clarify rand-extension comment wrt subject argument --- examples/rand-extension/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/rand-extension/lib.rs b/examples/rand-extension/lib.rs index aede5ffcabe..32b1d6f0a1f 100755 --- a/examples/rand-extension/lib.rs +++ b/examples/rand-extension/lib.rs @@ -86,7 +86,8 @@ mod rand_extension { Self::new(Default::default()) } - /// Update the value from the runtimes random source. + /// Seed a random value by passing some known argument `subject` to the runtime's random + /// source. Then, update the current value with the new random value #[ink(message)] pub fn update(&mut self, subject: [u8; 32]) -> Result<(), RandomReadErr> { // Get the on-chain random seed From d8ae73f7de1459add5582562d2a12f33914d4bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Wed, 24 Nov 2021 06:44:34 +0100 Subject: [PATCH 4/4] Update examples/rand-extension/lib.rs --- examples/rand-extension/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/rand-extension/lib.rs b/examples/rand-extension/lib.rs index 32b1d6f0a1f..21cd84e3098 100755 --- a/examples/rand-extension/lib.rs +++ b/examples/rand-extension/lib.rs @@ -86,8 +86,9 @@ mod rand_extension { Self::new(Default::default()) } - /// Seed a random value by passing some known argument `subject` to the runtime's random - /// source. Then, update the current value with the new random value + /// Seed a random value by passing some known argument `subject` to the runtime's + /// random source. Then, update the current `value` stored in this contract with the + /// new random value. #[ink(message)] pub fn update(&mut self, subject: [u8; 32]) -> Result<(), RandomReadErr> { // Get the on-chain random seed