Skip to content

Commit

Permalink
Formating code
Browse files Browse the repository at this point in the history
  • Loading branch information
kostiask committed Jan 25, 2024
1 parent 304bf67 commit e766032
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 70 deletions.
15 changes: 6 additions & 9 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::{sync::Arc, time::Duration};

use codec::Encode;
use qmc_runtime::{self, opaque::Block, RuntimeApi};
use sc_client_api::{Backend, BlockBackend, ExecutorProvider};
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
Expand All @@ -12,7 +13,6 @@ use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_runtime::offchain::{OffchainStorage, STORAGE_PREFIX};
use codec::Encode;

// Our native executor instance.
pub struct ExecutorDispatch;
Expand Down Expand Up @@ -236,14 +236,11 @@ pub async fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceE
})
};

match config.qrng_api_url {
Some(ref qrng) => {
let tmp = qrng.encode();
if let Some(mut storage) = backend.offchain_storage() {
storage.set(STORAGE_PREFIX, b"qrng-api-url", &tmp);
};
},
_ => {}
if let Some(ref qrng) = config.qrng_api_url {
let tmp = qrng.encode();
if let Some(mut storage) = backend.offchain_storage() {
storage.set(STORAGE_PREFIX, b"qrng-api-url", &tmp);
};
};

let runner_port = config.runner_port.unwrap().to_le_bytes();
Expand Down
4 changes: 1 addition & 3 deletions pallets/ocw-psk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ pub mod pallet {

if number_of_block_for_restart_node == Some(0) {
let entropy = match <randao::Pallet<T>>::get_secret(current_block_number) {
Ok(secret) => {
T::Hashing::hash(&secret.to_le_bytes())
},
Ok(secret) => T::Hashing::hash(&secret.to_le_bytes()),
Err(err) => {
log::info!(
"[OCW-PSK] There is no random number for this block: {:?}",
Expand Down
99 changes: 44 additions & 55 deletions pallets/ocw-randao/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::alloc::string::ToString;
pub use pallet::*;
use scale_info::prelude::string::String;
use serde::{Deserialize, Deserializer};
use sp_core::{Decode, Encode, Hasher};
use sp_io::offchain::timestamp;
use sp_runtime::offchain::{http::Request, Duration};
use sp_runtime::offchain::{http::Request, storage::StorageValueRef, Duration};
use sp_std::{str, vec::Vec};
use scale_info::prelude::string::String;
use codec::alloc::string::ToString;
use sp_runtime::offchain::storage::StorageValueRef;

use crate::Error::{DeserializeError, HttpFetchError, GetNumberQRNGError};
use crate::Error::{DeserializeError, GetNumberQRNGError, HttpFetchError};

const ONCHAIN_COMMITS: &[u8] = b"ocw-randao::commits";
const ONCHAIN_REVEALS: &[u8] = b"ocw-randao::reveals";
Expand All @@ -28,19 +27,18 @@ struct QRNGResponse {
}

pub fn de_string_to_bytes<'de, D>(de: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(de)?;
Ok(s.as_bytes().to_vec())
}
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(de)?;
Ok(s.as_bytes().to_vec())
}

#[frame_support::pallet]
pub mod pallet {
use frame_support::{pallet_prelude::*, traits::Randomness};
use frame_system::pallet_prelude::BlockNumberFor;


use super::*;

#[pallet::pallet]
Expand Down Expand Up @@ -99,15 +97,13 @@ pub mod pallet {
.expect("[OCW-RANDAO] Vector length doesn't match the target array");

let random_num = match Self::get_random_numner_from_qrng() {
Ok(secret) => {
secret
},
Ok(secret) => secret,
Err(err) => {
log::error!("[OCW-RANDAO] Failed to get qrng rundom number. {:?}", err);
let (random_seed, _) = T::Randomness::random(&b"PSK creator chosing"[..]);
let random_number = <u64>::decode(&mut random_seed.as_ref())
.expect("[OCW-RANDAO] secure hashes should always be bigger than u32; qed");
random_number
.expect("[OCW-RANDAO] secure hashes should always be bigger than u32; qed");
random_number
}
};

Expand Down Expand Up @@ -176,7 +172,6 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {

#[deny(clippy::clone_double_ref)]
fn derived_key(block_number: u64, prefix: &[u8]) -> Vec<u8> {
block_number.using_encoded(|encoded_bn| {
Expand All @@ -191,48 +186,42 @@ impl<T: Config> Pallet<T> {

fn get_random_numner_from_qrng() -> Result<u64, Error<T>> {
let storage_qrng_api_url = StorageValueRef::persistent(b"qrng-api-url");
let qrng_api_url = match storage_qrng_api_url.get::<_>() {
Ok(Some(bytes)) => {
match String::from_utf8(bytes) {
Ok(url) => {
url
},
Err(err) => {
log::error!("[OCW-RANDAO] Failed to convert bytes to string: {:?}", err);
return Err(GetNumberQRNGError)
}
}
}
Ok(None) => {
"".to_string()
}
let qrng_api_url = match storage_qrng_api_url.get::<_>() {
Ok(Some(bytes)) => match String::from_utf8(bytes) {
Ok(url) => url,
Err(err) => {
log::error!(
"[OCW-RANDAO] Error occurred while fetching RPC port from storage. {:?}",
err
);
return Err(GetNumberQRNGError)
log::error!("[OCW-RANDAO] Failed to convert bytes to string: {:?}", err);
return Err(GetNumberQRNGError);
}
};
},
Ok(None) => "".to_string(),
Err(err) => {
log::error!(
"[OCW-RANDAO] Error occurred while fetching RPC port from storage. {:?}",
err
);
return Err(GetNumberQRNGError);
}
};

let qrng_data = match Self::fetch_qrng_data(&qrng_api_url) {
Ok(qrng_data) => qrng_data,
Err(err) => {
log::error!("[OCW-RANDAO] Failed to fetch qrng data. {:?}", err);
return Err(GetNumberQRNGError)
}
};
let qrng_data = match Self::fetch_qrng_data(&qrng_api_url) {
Ok(qrng_data) => qrng_data,
Err(err) => {
log::error!("[OCW-RANDAO] Failed to fetch qrng data. {:?}", err);
return Err(GetNumberQRNGError);
}
};

let random_num_vec = match Self::parse_qrng_data(&qrng_data) {
Ok(qrng_data) => qrng_data.result,
Err(err) => {
log::error!("[OCW-RANDAO] Failed to parse qrng response. {:?}", err);
return Err(GetNumberQRNGError)
}
};
let random_num_vec = match Self::parse_qrng_data(&qrng_data) {
Ok(qrng_data) => qrng_data.result,
Err(err) => {
log::error!("[OCW-RANDAO] Failed to parse qrng response. {:?}", err);
return Err(GetNumberQRNGError);
}
};

let bytes: [u8; 8] = random_num_vec[0..8].try_into().unwrap();
let random_num = u64::from_le_bytes(bytes);
let bytes: [u8; 8] = random_num_vec[0..8].try_into().unwrap();
let random_num = u64::from_le_bytes(bytes);
Ok(random_num)
}

Expand Down
7 changes: 4 additions & 3 deletions pallets/randao/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ pub mod pallet {
match Self::create_and_raw_unsigned(block_num, commit_balkline, commit_deadline) {
Ok(()) => {
log::info!(
"[RANDAO] Successful created a campaign for the block {:?}",
block_num
)},
"[RANDAO] Successful created a campaign for the block {:?}",
block_num
)
}
Err(err) => log::info!(
"[RANDAO] Failed to create a campaign for the block {:?} : {:?}",
block_num,
Expand Down

0 comments on commit e766032

Please sign in to comment.