Skip to content

Commit

Permalink
Some optimization (#116)
Browse files Browse the repository at this point in the history
* Change sudo to Alith

* Format

* Doc

* Security

* Security

* Opt

* Grammar
  • Loading branch information
AurevoirXavier authored Dec 8, 2022
1 parent 1da4e6c commit 222d968
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 27 deletions.
4 changes: 1 addition & 3 deletions node/src/chain_spec/crab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ pub fn genesis_config() -> ChainSpec {
session_keys(get_collator_keys_from_seed("Alice")),
)],
},
// no need to pass anything to aura, in fact it will panic if we do. Session will
// take care of this.
aura: Default::default(),
aura_ext: Default::default(),

Expand Down Expand Up @@ -258,7 +256,7 @@ fn testnet_genesis(
treasury: Default::default(),

// Utility stuff.
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(SUDO)) },
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(ALITH)) },
vesting: Default::default(),

// XCM stuff.
Expand Down
2 changes: 1 addition & 1 deletion node/src/chain_spec/darwinia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ fn testnet_genesis(
treasury: Default::default(),

// Utility stuff.
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(SUDO)) },
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(ALITH)) },
vesting: Default::default(),

// XCM stuff.
Expand Down
2 changes: 1 addition & 1 deletion node/src/chain_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ETHAN: &str = "0xFf64d3F6efE2317EE2807d223a0Bdc4c0c49dfDB";
const FAITH: &str = "0xC0F0f4ab324C46e55D02D0033343B4Be8A55532d";
const SUDO: &str = "0x2748def2f9c3cfbbb963002935bc6d2e1c36ce2e";

/// The default XCM version to set in genesis config.
// The default XCM version to set in genesis config.
const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION;

const PROTOCOL_ID: &str = "dar";
Expand Down
4 changes: 1 addition & 3 deletions node/src/chain_spec/pangolin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ pub fn genesis_config() -> ChainSpec {
session_keys(get_collator_keys_from_seed("Alice")),
)],
},
// no need to pass anything to aura, in fact it will panic if we do. Session will
// take care of this.
aura: Default::default(),
aura_ext: Default::default(),

Expand Down Expand Up @@ -253,7 +251,7 @@ fn testnet_genesis(
treasury: Default::default(),

// Utility stuff.
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(SUDO)) },
sudo: SudoConfig { key: Some(array_bytes::hex_n_into_unchecked(ALITH)) },
vesting: Default::default(),

// XCM stuff.
Expand Down
4 changes: 2 additions & 2 deletions node/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ pub async fn start_parachain_node(
keystore,
force_authoring,
slot_duration,
// We got around 500ms for proposing
// We got around 500ms for proposing.
block_proposal_slot_portion: cumulus_client_consensus_aura::SlotProportion::new(
1f32 / 24f32,
),
// And a maximum of 750ms if slots are skipped
// And a maximum of 750ms if slots are skipped.
max_block_proposal_slot_portion: Some(
cumulus_client_consensus_aura::SlotProportion::new(1f32 / 16f32),
),
Expand Down
25 changes: 22 additions & 3 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub mod pallet {
{
/// Override the [`frame_system::Config::RuntimeEvent`].
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Chain's ID, which is used for constructing the message. (follow EIP-712 SPEC)
#[pallet::constant]
type ChainId: Get<u64>;
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -143,7 +146,11 @@ pub mod pallet {
return InvalidTransaction::Custom(E_ACCOUNT_ALREADY_EXISTED).into();
}

let message = sr25519_signable_message(T::Version::get().spec_name.as_ref(), to);
let message = sr25519_signable_message(
T::ChainId::get(),
T::Version::get().spec_name.as_ref(),
to,
);

if verify_sr25519_signature(from, &message, signature) {
ValidTransaction::with_tag_prefix("account-migration")
Expand All @@ -160,8 +167,20 @@ pub mod pallet {
}
pub use pallet::*;

fn sr25519_signable_message(spec_name: &[u8], account_id_20: &AccountId20) -> Message {
hashing::blake2_256(&[spec_name, b"::account-migration", &account_id_20.0].concat())
fn sr25519_signable_message(
chain_id: u64,
spec_name: &[u8],
account_id_20: &AccountId20,
) -> Message {
hashing::blake2_256(
&[
&hashing::blake2_256(
&[&chain_id.to_le_bytes(), spec_name, b"::account-migration"].concat(),
),
account_id_20.0.as_slice(),
]
.concat(),
)
}

fn verify_sr25519_signature(
Expand Down
23 changes: 13 additions & 10 deletions pallet/account-migration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,35 @@ use sp_keyring::sr25519::Keyring;

#[test]
fn sr25519_signable_message_should_work() {
[b"Darwinia2".as_slice(), b"Crab2", b"Pangolin2"]
[(46_u64, b"Darwinia2".as_slice()), (44, b"Crab2"), (43, b"Pangolin2")]
.iter()
.zip([
[
81, 88, 182, 48, 229, 179, 23, 224, 117, 134, 146, 124, 106, 211, 130, 135, 68,
191, 187, 224, 116, 219, 61, 45, 126, 38, 77, 144, 214, 132, 173, 77,
75, 134, 66, 181, 153, 10, 7, 244, 225, 154, 100, 68, 239, 19, 129, 51, 181, 78,
66, 254, 167, 54, 211, 20, 171, 68, 160, 46, 216, 98, 9, 44,
],
[
253, 68, 12, 181, 246, 68, 61, 64, 106, 115, 48, 108, 235, 106, 2, 40, 115, 99, 84,
46, 106, 132, 116, 241, 65, 214, 128, 8, 88, 85, 137, 86,
171, 8, 180, 157, 214, 41, 236, 80, 127, 218, 216, 136, 239, 56, 153, 31, 128, 168,
154, 112, 70, 245, 19, 68, 53, 29, 49, 95, 238, 209, 238, 129,
],
[
227, 102, 33, 150, 94, 161, 161, 187, 83, 242, 232, 50, 184, 187, 169, 235, 148,
146, 90, 88, 85, 45, 61, 24, 224, 10, 33, 182, 177, 57, 219, 61,
251, 70, 107, 65, 22, 164, 1, 85, 114, 150, 161, 208, 235, 131, 15, 111, 154, 207,
193, 216, 110, 54, 58, 177, 15, 99, 104, 179, 13, 30, 55, 205,
],
])
.for_each(|(spec_name, message)| {
assert_eq!(sr25519_signable_message(spec_name, &Default::default()), message);
.for_each(|((chain_id, spec_name), message)| {
assert_eq!(
sr25519_signable_message(*chain_id, spec_name, &Default::default()),
message
);
});
}

#[test]
fn verify_sr25519_signature_should_work() {
Keyring::iter().enumerate().for_each(|(i, from)| {
let to = [i as _; 20];
let message = sr25519_signable_message(b"Darwinia2", &to.into());
let message = sr25519_signable_message(46, b"Darwinia2", &to.into());
let signature = from.sign(&message);

assert!(verify_sr25519_signature(&from.public().0.into(), &message, &signature));
Expand Down
11 changes: 10 additions & 1 deletion pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use core::fmt::Debug;
use codec::FullCodec;
// darwinia
use dc_inflation::TOTAL_SUPPLY;
use dc_types::{Balance, Moment};
use dc_types::{Balance, Moment, UNIT};
// substrate
use frame_support::{
log,
Expand Down Expand Up @@ -736,6 +736,15 @@ pub mod pallet {

return;
};

// TODO: add some tests in the core inflation,
// and get a more precise value/worst case.
if inflation > 1_000_000 * UNIT {
log::error!("[pallet::staking] it's impossible to mint over 1 million RING within a session according to current reward curve");

return;
}

let payout = T::PayoutFraction::get() * inflation;
let (total_points, reward_map) = <RewardPoints<T>>::get();
// Due to the `payout * percent` there might be some losses.
Expand Down
1 change: 1 addition & 0 deletions runtime/crab/src/pallets/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Runtime as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}
1 change: 1 addition & 0 deletions runtime/darwinia/src/pallets/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Runtime as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}
2 changes: 1 addition & 1 deletion runtime/pangolin/src/pallets/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl pallet_evm::Config for Runtime {
type BlockGasLimit = BlockGasLimit;
type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
type CallOrigin = pallet_evm::EnsureAddressRoot<AccountId>;
type ChainId = ConstU64<44>;
type ChainId = ConstU64<43>;
type Currency = Balances;
type FeeCalculator = FixedGasPrice;
type FindAuthor = FindAuthorTruncated<Aura>;
Expand Down
1 change: 1 addition & 0 deletions runtime/pangolin/src/pallets/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Runtime as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}
4 changes: 2 additions & 2 deletions tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ where
Ok(D::decode(&mut &*v)?)
}

twox128(pallet) + twox128(item) + blake2_256_concat(item_key)
// twox128(pallet) + twox128(item) + blake2_256_concat(item_key) -> blake2_256_concat(item_key)
fn get_blake2_128_concat_suffix(full_key: &str, item_key: &str) -> String {
full_key.trim_start_matches(item_key).into()
}

twox128(pallet) + twox128(item) + last 64 bytes
// twox128(pallet) + twox128(item) + blake2_256_concat(account_id_32) -> account_id_32
#[allow(unused)]
fn get_concat_suffix(full_key: &str, _: &str) -> String {
format!("0x{}", &full_key[full_key.len() - 64..])
Expand Down

0 comments on commit 222d968

Please sign in to comment.