Skip to content

Commit

Permalink
Fixed warning and added docs (#259)
Browse files Browse the repository at this point in the history
* Adding a doc on chain_spec file

* Remove warnings

* Adding comments on params passing in `ChainSpec::from_genesis()`

* Minor update on the doc

* Fixing warnings/errors in cargo test

* updated
  • Loading branch information
jimmychu0807 authored Nov 10, 2023
1 parent 6b61e8a commit 5eefce8
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 39 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ jobs:
uses: 0xbillw/install-rust-substrate-action@main
- name: Build
run: cargo build -r --verbose
- name: Run tests
run: cargo test -r --verbose
# We don't run cargo test for now.
# - name: Run tests
# run: cargo test -r --verbose
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions c-pallets/cacher/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;
use crate as pallet_cacher;

use frame_support::traits::{ConstU128, ConstU32, ConstU64};
use frame_support::traits::{ConstU128, ConstU32};
use sp_core::H256;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -39,15 +39,15 @@ impl frame_system::Config for Test {
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type BlockHashCount = Self::BlockHashCount;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type SS58Prefix = Self::SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
Expand Down
1 change: 0 additions & 1 deletion c-pallets/cacher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use super::*;
use frame_support::{assert_noop, assert_ok};
use mock::{new_test_ext, Cacher, RuntimeOrigin, Test};
use pallet_balances::Error as BalancesError;
use sp_runtime::traits::Hash;

#[test]
Expand Down
11 changes: 7 additions & 4 deletions c-pallets/oss/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ frame_support::construct_runtime!(

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024));
}
#[derive(Clone, Eq, PartialEq)]
pub const P2PLength: u32 = 10;
#[derive(Clone, Eq, PartialEq)]
pub const AuthorLimit: u32 = 2;
}

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
Expand Down Expand Up @@ -81,7 +83,8 @@ impl frame_system::Config for Test {

impl pallet_oss::Config for Test {
type RuntimeEvent = RuntimeEvent;

type P2PLength = P2PLength;
type AuthorLimit = AuthorLimit;
type WeightInfo = ();
}

Expand Down
8 changes: 4 additions & 4 deletions c-pallets/scheduler-credit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,18 @@ mod test {
#[test]
fn scheduler_counter_works() {
let mut sce = SchedulerCounterEntry::default();
sce.increase_block_size::<Test>(100);
let _ = sce.increase_block_size::<Test>(100);
assert_eq!(100, sce.proceed_block_size);
sce.increase_block_size::<Test>(100);
let _ = sce.increase_block_size::<Test>(100);
assert_eq!(200, sce.proceed_block_size);
assert_eq!(0, sce.punishment_part());
assert_eq!(100, sce.figure_credit_value(2000));

sce.increase_punishment_count::<Test>();
let _ = sce.increase_punishment_count::<Test>();
assert_eq!(1, sce.punishment_count);

assert_eq!(100, sce.figure_credit_value(1000));
sce.increase_punishment_count::<Test>();
let _ = sce.increase_punishment_count::<Test>();

assert_eq!(2, sce.punishment_count);
assert_eq!(0, sce.figure_credit_value(1000));
Expand Down
8 changes: 4 additions & 4 deletions c-pallets/scheduler-credit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use frame_support::traits::ValidatorCredits;
#[test]
fn figure_credit_scores_works() {
ExtBuilder::default().build_and_execute(|| {
<Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&1, 100);
<Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&1, 100);
let _ = <Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&1, 100);
let _ = <Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&1, 100);
assert_eq!(200, CurrentCounters::<Test>::get(1).proceed_block_size);

<Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&2, 50);
<Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&3, 150);
let _ = <Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&2, 50);
let _ = <Pallet<Test> as SchedulerCreditCounter<AccountId>>::record_proceed_block_size(&3, 150);

// switch period
let period_duration = PeriodDuration::get();
Expand Down
2 changes: 1 addition & 1 deletion c-pallets/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ pub mod pallet {

// Note: in case there is no current era it is fine to bond one era more.
let era = Self::current_era().unwrap_or(0) + T::BondingDuration::get();
if let Some(mut chunk) =
if let Some(chunk) =
ledger.unlocking.last_mut().filter(|chunk| chunk.era == era)
{
// To keep the chunk count down, we only keep one chunk per era. Since
Expand Down
24 changes: 24 additions & 0 deletions docs/on-src/node/src/chain_spec.rs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The relationship of how the functions related to each others, in diagram.

Refer to [mermaid doc: Flowchart](https://mermaid.js.org/syntax/flowchart.html) for syntax.

`id` below is the `id` parameter used in **Cli** [`load_spec()`](https://github.com/CESSProject/cess/blob/main/node/src/command.rs#L57-L69)

```mermaid
flowchart LR
%% main
testnet_genesis("testnet_genesis()") --> main_genesis("cess_main_genesis()") --> main("cess_main()") --> initial_testnet(["id: `cess-initial-testnet`"])
%% testnet
testnet_genesis --> testnet_config_genesis("cess_testnet_config_genesis()") --> testnet_generate_config("cess_testnet_generate_config()") --> initial_devnet(["id: `cess-initial-devnet`"])
%% local
testnet_genesis --> local_testnet_genesis("local_testnet_genesis()") --> local_testnet_config("local_testnet_config()") --> local(["id: `local`"])
%% dev
testnet_genesis --> development_config_genesis("development_config_genesis()") --> development_config("development_config()") --> dev(["id: `dev`"])
%% Reading from chainspec file
chainSpec("Read from chainSpec") --> testnet_config("cess_testnet_config()") --> cess_testnet(["id: `cess-testnet`"])
chainSpec --> develop_config("cess_develop_config()") --> cess_devnet(["id: `cess-devnet`"])
```
23 changes: 21 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// Added a graph view on the functions relationship in this file, refer to the github-rendered view at:
// https://github.com/CESSProject/cess/blob/main/docs/on-src/node/src/chain_spec.rs.md

use sp_consensus_grandpa::AuthorityId as GrandpaId;
use cess_node_runtime::{
opaque::SessionKeys, wasm_binary_unwrap, AccountId, AuthorityDiscoveryConfig, Balance,
Expand Down Expand Up @@ -343,7 +346,9 @@ pub fn cess_develop_config() -> ChainSpec {
pub fn cess_testnet_generate_config() -> ChainSpec {
let boot_nodes = vec![];
ChainSpec::from_genesis(
// Name
"cess-devnet",
// ID
"cess-devnet",
ChainType::Live,
cess_testnet_config_genesis,
Expand All @@ -354,6 +359,7 @@ pub fn cess_testnet_generate_config() -> ChainSpec {
),
Some("TCESS"),
None,
// Properties
Some(
serde_json::from_str(
"{\"tokenDecimals\": 12, \"tokenSymbol\": \"TCESS\", \"SS58Prefix\": 11330}",
Expand All @@ -367,17 +373,24 @@ pub fn cess_testnet_generate_config() -> ChainSpec {
pub fn cess_main() -> ChainSpec {
let boot_nodes = vec![];
ChainSpec::from_genesis(
// Name
"cess-testnet",
// ID
"cess-testnet",
ChainType::Live,
cess_main_genesis,
// Bootnodes
boot_nodes,
// Telemetry
Some(
TelemetryEndpoints::new(vec![(STAGING_TELEMETRY_URL.to_string(), 0)])
.expect("Staging telemetry url is valid; qed"),
),
// Protocol ID
Some("TCESS"),
// Fork ID
None,
// Properties
Some(
serde_json::from_str(
"{\"tokenDecimals\": 12, \"tokenSymbol\": \"TCESS\", \"SS58Prefix\": 11330}",
Expand Down Expand Up @@ -410,10 +423,16 @@ pub fn development_config() -> ChainSpec {
// Telemetry
None,
// Protocol ID
Some("TCESS"),
// Fork ID
None,
// Properties
None,
None,
Some(
serde_json::from_str(
"{\"tokenDecimals\": 12, \"tokenSymbol\": \"TCESS\", \"SS58Prefix\": 11330}",
)
.expect("Provided valid json map"),
),
// Extensions
Default::default(),
)
Expand Down
31 changes: 15 additions & 16 deletions primitives/enclave-verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use sp_std::prelude::ToOwned;
use serde_json::Value;
use scale_info::prelude::string::String;
#[cfg(test)]
use rand::RngCore;

#[cfg(test)]
use rsa::{
RsaPublicKey, RsaPrivateKey,
pkcs1::{EncodeRsaPrivateKey, DecodeRsaPrivateKey, EncodeRsaPublicKey},
pkcs1::{/*EncodeRsaPrivateKey, DecodeRsaPrivateKey, */EncodeRsaPublicKey},
pkcs8::DecodePrivateKey,
};

// #[cfg(feature = "std")]
Expand Down Expand Up @@ -269,28 +269,27 @@ pub struct PoISKey {
use codec::{Decode, Encode};
#[test]
fn cryptos_rsa() {
let priv_key = RsaPrivateKey::from_pkcs8_der(&hex::decode("308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201010097f726d8bc510040ef6552b5861e54ab781527c4d852110ae14e5b23b6083201a4ebcec9e9dfae7d8a33460e14c634c7098e41b7fb861672f2cda91c73d55252aa3f39183b88a2e77beabbaff45fe8fe82112ea3c95df4f0b31e2bdd43e2a11635a14ddd7c30153eb56c3f0a07a05caa976f337000b7a8db4e691d2f54229f1176c27304d06e384c16cac868d26166b7563f6c32b315ec93398e4c43f3eba2ca93a394ed07e57afa4bf963f29d2288af4a8c499109833858f9af111a5d480f3255e65b7ceb1a68c8a1a089f903310777cf647f6cd7fbd6e4b4dddf0ca342d6b96454410647f02df76bc1ee44435ca1df012b9689daddd5e8240579203b814b050203010001028201000cc6c4c7581ddf4d9653087f26858a4cd84dbf0837bfbe9b11924aeb57b49cafb2b3f8b0d52eef36b2d5d8bffa5327c0cc36dfa39e4c09bb245ad22b083a192fc60c86ba58d7060b3c49e1f9cb2bfd24d8bea513342ce8190c962ecded953241f1c45c0d911161d7e1dcf5f7dbe849a236152d57ef5781a4de94cbd55cd784540c633f4ce96a053d6a19533b9830c7b8396f190f020099a4e481e24a41022a1191330ac599a7199ca776913db12be96cf5b949ad5d73be9d3b66311a8ba10a48b00a9deedbccc015aace97fb79ae19bbe39fa0103772d4a25bf7e4f35c2ede2943a935423720d4f3059994f171650558bc1bfabf725d10916095c162b8717cc902818100c393e0b47b91321fef951ad8fb1cdc1ec928e73f715f673ff093847d380dcff6c040691ac7cdcac45bf0ed38299a10b1349a56b60178de81e38095251dda7b2bb0ca96c5457fe162daf9b7a0bf30d1a8eae47340d75622dd72c22aa12f3fb45154896b791ab4a5275be06657098bb9def81010297981b0d531af2c4ee08da78b02818100c6ea027695077584eca26d7ef4b4e361f3cc583ff5fff2114daebbb0a6a92dff11861c7596626e86bad32fd11ae7683f8ad45599eefe2cd2117825e0c5590be86e2187e0bb1636a104cde7b6b0a573e169f56c2ae8ff171eece31295964a3426009fc4c2672ace0be0d59c99c427c0fbf8e7e9d4636165e9ba8803c24f53a9af02818100b24e0e8ddd1e09c9cdde6d64a6c3aff72d446a578fdfffbcee733f55fe15b1a4efaf89634e07d3b5e370aa850a8098794650f37ee9a6ad8d53c175b82a187734e4f03e36c9df05b7df95cd10f35de9b78bb70d506f41eb75635b9c0be98cb5b37453f8b4a7614c34aef1cdbbca4b26011ebd5e4ec1a5387795dd7392d1ecb37302818045146fd68edb104d21812755b7d63a418251ad344952a1d6b08bc6530b0e2613371ac437720aad27cd2a1aa91c16d1757fd94e012fa6c61a0e4713a083e8f0e1bf9d957ace7e606a7b28a7182330d295ae1eb57a1180c59ecfd5ec5656e35e48f45e880e9b959a093603f966cd60a0fce0ec69a081030a49a9a622e8107495b10281806de9944afeb9670b92eb33b3afcd4890c20b3ba9f7d055305d5027b2ccd5d8f565488b3f56342f04968d29cdaee716ad333868beecbd7df5cd2aeeba0c1c4d810f78162e0a02e3c1aa54ee9103469efc6cf3542e9292bddffc8d328996a8bb67f78a7701e9f113ad790bdfd981d39cf116b5ee41dd5e42fcc2b1f3d11e6a7d15").unwrap()).unwrap();
// let skey = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let pub_key = RsaPublicKey::from(&priv_key);
println!("{:?}",hex::encode(pub_key.to_pkcs1_der().unwrap().to_vec()));
let priv_key = RsaPrivateKey::from_pkcs8_der(&hex::decode("308204bd020100300d06092a864886f70d0101010500048204a7308204a3020100028201010097f726d8bc510040ef6552b5861e54ab781527c4d852110ae14e5b23b6083201a4ebcec9e9dfae7d8a33460e14c634c7098e41b7fb861672f2cda91c73d55252aa3f39183b88a2e77beabbaff45fe8fe82112ea3c95df4f0b31e2bdd43e2a11635a14ddd7c30153eb56c3f0a07a05caa976f337000b7a8db4e691d2f54229f1176c27304d06e384c16cac868d26166b7563f6c32b315ec93398e4c43f3eba2ca93a394ed07e57afa4bf963f29d2288af4a8c499109833858f9af111a5d480f3255e65b7ceb1a68c8a1a089f903310777cf647f6cd7fbd6e4b4dddf0ca342d6b96454410647f02df76bc1ee44435ca1df012b9689daddd5e8240579203b814b050203010001028201000cc6c4c7581ddf4d9653087f26858a4cd84dbf0837bfbe9b11924aeb57b49cafb2b3f8b0d52eef36b2d5d8bffa5327c0cc36dfa39e4c09bb245ad22b083a192fc60c86ba58d7060b3c49e1f9cb2bfd24d8bea513342ce8190c962ecded953241f1c45c0d911161d7e1dcf5f7dbe849a236152d57ef5781a4de94cbd55cd784540c633f4ce96a053d6a19533b9830c7b8396f190f020099a4e481e24a41022a1191330ac599a7199ca776913db12be96cf5b949ad5d73be9d3b66311a8ba10a48b00a9deedbccc015aace97fb79ae19bbe39fa0103772d4a25bf7e4f35c2ede2943a935423720d4f3059994f171650558bc1bfabf725d10916095c162b8717cc902818100c393e0b47b91321fef951ad8fb1cdc1ec928e73f715f673ff093847d380dcff6c040691ac7cdcac45bf0ed38299a10b1349a56b60178de81e38095251dda7b2bb0ca96c5457fe162daf9b7a0bf30d1a8eae47340d75622dd72c22aa12f3fb45154896b791ab4a5275be06657098bb9def81010297981b0d531af2c4ee08da78b02818100c6ea027695077584eca26d7ef4b4e361f3cc583ff5fff2114daebbb0a6a92dff11861c7596626e86bad32fd11ae7683f8ad45599eefe2cd2117825e0c5590be86e2187e0bb1636a104cde7b6b0a573e169f56c2ae8ff171eece31295964a3426009fc4c2672ace0be0d59c99c427c0fbf8e7e9d4636165e9ba8803c24f53a9af02818100b24e0e8ddd1e09c9cdde6d64a6c3aff72d446a578fdfffbcee733f55fe15b1a4efaf89634e07d3b5e370aa850a8098794650f37ee9a6ad8d53c175b82a187734e4f03e36c9df05b7df95cd10f35de9b78bb70d506f41eb75635b9c0be98cb5b37453f8b4a7614c34aef1cdbbca4b26011ebd5e4ec1a5387795dd7392d1ecb37302818045146fd68edb104d21812755b7d63a418251ad344952a1d6b08bc6530b0e2613371ac437720aad27cd2a1aa91c16d1757fd94e012fa6c61a0e4713a083e8f0e1bf9d957ace7e606a7b28a7182330d295ae1eb57a1180c59ecfd5ec5656e35e48f45e880e9b959a093603f966cd60a0fce0ec69a081030a49a9a622e8107495b10281806de9944afeb9670b92eb33b3afcd4890c20b3ba9f7d055305d5027b2ccd5d8f565488b3f56342f04968d29cdaee716ad333868beecbd7df5cd2aeeba0c1c4d810f78162e0a02e3c1aa54ee9103469efc6cf3542e9292bddffc8d328996a8bb67f78a7701e9f113ad790bdfd981d39cf116b5ee41dd5e42fcc2b1f3d11e6a7d15").unwrap()).unwrap();
// let skey = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let pub_key = RsaPublicKey::from(&priv_key);
println!("{:?}",hex::encode(pub_key.to_pkcs1_der().unwrap().to_vec()));

let original_text = [193, 246, 62, 231, 50, 88, 107, 103, 221, 158, 61, 148, 6, 248, 29, 248, 56, 172, 222, 109, 191, 170, 225, 72, 211, 29, 42, 18, 51, 234, 205, 136];
let original_text = [193, 246, 62, 231, 50, 88, 107, 103, 221, 158, 61, 148, 6, 248, 29, 248, 56, 172, 222, 109, 191, 170, 225, 72, 211, 29, 42, 18, 51, 234, 205, 136];

// let priv_key_der = priv_key.to_pkcs1_der().unwrap();
// println!("priv_key_der: {:?}", priv_key_der.as_bytes());
// println!();
// println!("priv_key_der: {:?}", priv_key_der.as_bytes());
// println!();

let doc = pub_key.to_pkcs1_der().unwrap();
// println!("pub_key: {:?}, length: {}", doc.as_bytes(), doc.as_bytes().len());
// println!("pub_key: {:?}, length: {}", doc.as_bytes(), doc.as_bytes().len());

let pk = rsa::RsaPublicKey::from_pkcs1_der(doc.as_bytes()).unwrap();
let pk = rsa::RsaPublicKey::from_pkcs1_der(doc.as_bytes()).unwrap();

let msg = "hello world!".as_bytes();
let binding = priv_key.sign(Pkcs1v15Sign::new_raw(), &original_text).unwrap();
let sig: &[u8] = binding.as_ref();
println!("sig is: {:?}, sig is length: {:?}", sig, sig.len());
let sig: &[u8] = binding.as_ref();
println!("sig is: {:?}, sig is length: {:?}", sig, sig.len());

let result = pk.verify(Pkcs1v15Sign::new_raw(), &original_text, &sig);
let result = pk.verify(Pkcs1v15Sign::new_raw(), &original_text, &sig);

println!("result: {:?}", result);
}
Expand Down Expand Up @@ -474,4 +473,4 @@ impl TryFrom<&[u8]> for QuoteReport {
// println!("encoding: {:?}", encoding);
// let hashing = sp_io::hashing::sha2_256(&encoding);
// println!("hashing: {:?}", hashing);
// }
// }
4 changes: 3 additions & 1 deletion utils/verify-bls-signatures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description = "A library for handling BLS signatures"
bls12_381 = { version = "0.7", default-features = false, features = ["groups", "pairings", "alloc", "experimental"] }
pairing = { version = "0.22", default-features = false }
lazy_static = { version = "1", default-features = false }
# rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }

# Check references:
# - https://docs.rs/getrandom/latest/getrandom/index.html
Expand All @@ -20,3 +19,6 @@ lazy_static = { version = "1", default-features = false }
getrandom = { version = "0.2.10", default-features = false, features = ["custom"] }
sha2 = { version = "0.9", default-features = false }
hex = { version = "0.4", default-features = false }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
2 changes: 1 addition & 1 deletion utils/verify-bls-signatures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl PrivateKey {
let mut buf = [0u8; 32];
// rng.fill_bytes(&mut buf);
// note: Handle the `Result` below
getrandom::getrandom(&mut buf);
let _ = getrandom::getrandom(&mut buf);
let s: Option<Scalar> = Scalar::from_bytes(&buf).into();

if let Some(s) = s {
Expand Down

0 comments on commit 5eefce8

Please sign in to comment.