Skip to content

Commit

Permalink
new encoding schema
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Jun 1, 2022
1 parent 6a0ae50 commit 921f61f
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 550 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
'crates/skw-blockchain-pallets/pallet-s-contract',
'crates/skw-blockchain-pallets/pallet-registry',
'crates/skw-blockchain-pallets/pallet-parentchain',
'crates/skw-blockchain-pallets/pallet-s-account',
'crates/skw-blockchain-runtime',
'crates/skw-blockchain-pallets/primitives',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ benchmarks! {
Registry::<T>::register_secret_keeper(RawOrigin::Signed(caller.clone()).into(), public_key, vec![0, 0, 0, 0, 0, 0])?;
}: register_running_shard(RawOrigin::Signed(caller.clone()), 0)
verify { }

register_user_public_key {
let caller: T::AccountId = whitelisted_caller();
let public_key = decode_hex_uncompressed(PUBLIC_KEY);
}: register_user_public_key(RawOrigin::Signed(caller.clone()), public_key)
verify { }
}

impl_benchmark_test_suite!(
Expand Down
27 changes: 25 additions & 2 deletions crates/skw-blockchain-pallets/pallet-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ pub mod pallet {
#[pallet::getter(fn expiration_of)]
pub(super) type Expiration<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, T::BlockNumber>;

/// identity publlic key of each secret keepers, used to receive the secret
/// identity key of each secret keepers, used to receive the secret
#[pallet::storage]
#[pallet::getter(fn public_key_of)]
pub(super) type PublicKey<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, [u8; 32]>;


#[pallet::storage]
#[pallet::getter(fn user_public_key_of)]
pub(super) type UserPublicKey<T: Config> = StorageMap<_, Twox64Concat,
T::AccountId, [u8; 32]>;

/// members of each shard
#[pallet::storage]
#[pallet::getter(fn shard_members_of)]
Expand Down Expand Up @@ -210,6 +215,24 @@ pub mod pallet {

Ok(())
}

/// register a user's public key
// #[pallet::weight(<T as pallet::Config>::WeightInfo::register_user_public_key())]
#[pallet::weight(0)]
pub fn register_user_public_key(
origin: OriginFor<T>,
public_key: Vec<u8>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let pk = compress_hex_key(&public_key);
let bounded_pk: [u8; 32] = pk.try_into().map_err(|_| Error::<T>::InvalidPublicKey)?;

<UserPublicKey<T>>::insert(&who, bounded_pk);

Ok(())
}

}

impl<T: Config> Pallet<T> {
Expand Down
14 changes: 14 additions & 0 deletions crates/skw-blockchain-pallets/pallet-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,17 @@ fn is_beacon_turn_test_signle_keeper() {

});
}

#[test]
fn insert_pk_for_user() {

use skw_blockchain_primitives::compress_hex_key;
new_test_ext().execute_with(|| {
System::set_block_number(1);

let public_key = decode_hex_uncompressed(PUBLIC_KEY).unwrap();
assert_ok!( Registry::register_user_public_key( Origin::signed(ALICE), public_key.clone()) );

assert!( Registry::user_public_key_of(&ALICE).unwrap().to_vec() == compress_hex_key(&public_key.clone()));
});
}
17 changes: 11 additions & 6 deletions crates/skw-blockchain-pallets/pallet-s-account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ scale-info = { version = "2.0.1", default-features = false, features = ["derive"
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22"}
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22" }
frame-benchmarking = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22", optional = true }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22" }

[dev-dependencies]
pallet-balances= { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22"}
pallet-balances= { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22"}
pallet-treasury= { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22"}
sp-core = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22" }
sp-io = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22" }
sp-runtime = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.22" }
Expand All @@ -40,10 +42,13 @@ runtime-benchmarks = [
'frame-system/runtime-benchmarks',
]
std = [
'codec/std',
'scale-info/std',
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ['frame-support/try-runtime']
9 changes: 5 additions & 4 deletions crates/skw-blockchain-pallets/pallet-s-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ pub mod pallet {
Currency, ExistenceRequirement::KeepAlive
}, Twox64Concat
};
use frame_support::sp_runtime::traits::AccountIdConversion;
use frame_system::pallet_prelude::*;
use super::*;
use sp_std::vec::Vec;

use skw_blockchain_primitives::{ShardId, CallIndex};
use skw_blockchain_primitives::{ShardId};
pub type BalanceOf<T> = pallet_treasury::BalanceOf<T>;

#[pallet::config]
Expand Down Expand Up @@ -80,7 +81,7 @@ pub mod pallet {
ensure_root(origin.clone())?;

let encoded_call = Self::build_account_creation_call(&account, shard_id);
pallet_s_contract::Pallet::<T>::push_call(origin, shard_id, encoded_call)?;
pallet_s_contract::Pallet::<T>::force_push_call(origin, shard_id, encoded_call)?;

Self::deposit_event(Event::<T>::EnclaveAccountCreated(account, shard_id));
Ok(())
Expand All @@ -94,7 +95,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

let treasury = Self::account_id();
let treasury = T::PalletId::get().into_account();
T::Currency::transfer(&who, &treasury, T::ReservationRequirement::get(), KeepAlive)?;
let encoded_call = Self::build_account_creation_call(&who, shard_id);
pallet_s_contract::Pallet::<T>::push_call(origin, shard_id, encoded_call)?;
Expand All @@ -115,7 +116,7 @@ pub mod pallet {
encrypted_egress: false,

transaction_action: b"create_account".to_vec(),
receiver: account.to_string().to_vec(),
receiver: account.to_string().as_bytes().to_vec(),

// an arb amount of offchain runtime gas token for transacrtions
amount: Some(T::DefaultFaucet::get()),
Expand Down
97 changes: 36 additions & 61 deletions crates/skw-blockchain-pallets/pallet-s-account/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@
use crate as pallet_s_account;
use pallet_s_contract;

use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64},
};
use sp_runtime::{
Perbill, Permill,
};

use frame_system as system;
use frame_support::{traits::{ConstU16, ConstU32, ConstU64, GenesisBuild}, PalletId};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
PalletId,
traits::{BlakeTwo256, IdentityLookup}, Permill,
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand All @@ -29,26 +20,39 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Treasury: pallet_treasury::{Pallet, Call, Storage, Config, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Event<T>},
Treasury: pallet_treasury::{Pallet, Call, Storage, Event<T>},
Secrets: pallet_secrets::{Pallet, Call, Storage, Event<T>},
SAccount: pallet_s_account::{Pallet, Call, Storage, Event<T>},
SContract: pallet_s_contract::{Pallet, Call, Storage, Event<T>},
}
);

parameter_types! {
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = u64;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
}

frame_support::parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
pub const Burn: Permill = Permill::from_percent(50);
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub const SContractPalletId: PalletId = PalletId(*b"scontrac");
}
impl pallet_treasury::Config for Test {
type PalletId = TreasuryPalletId;
type Currency = Balance;
type ApproveOrigin = frame_system::EnsureRoot<u128>;
type RejectOrigin = frame_system::EnsureRoot<u128>;
type Currency = Balances;
type ApproveOrigin = frame_system::EnsureRoot<u64>;
type RejectOrigin = frame_system::EnsureRoot<u64>;
type Event = Event;
type OnSlash = Treasury;
type OnSlash = ();
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ConstU64<1>;
type ProposalBondMaximum = ();
Expand All @@ -60,24 +64,7 @@ impl pallet_treasury::Config for Test {
type MaxApprovals = ConstU32<100>;
}

impl pallet_balances::Config for Test {
type Balance = u64;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
}

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

impl system::Config for Test {
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
Expand All @@ -92,56 +79,44 @@ impl system::Config for Test {
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
}

parameter_types! {
pub const IPFSCIDLength: u32 = 46;
pub const MaxActiveShards: u64 = 0;
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl pallet_secrets::Config for Test {
type WeightInfo = ();
type Event = Event;
type IPFSCIDLength = IPFSCIDLength;
}

parameter_types! {
pub const MaxCallLength: u32 = 512;
pub const MinContractNameLength: u32 = 1;
pub const MaxContractNameLength: u32 = 32;
type IPFSCIDLength = ConstU32<46>;
}

impl pallet_s_contract::Config for Test {
type WeightInfo = ();
type Event = Event;
type MaxCallLength = MaxCallLength;
type MinContractNameLength = MinContractNameLength;
type MaxContractNameLength = MaxContractNameLength;
}

parameter_types! {
pub const ReservationRequirement: u32 = 1;
type MaxCallLength = ConstU32<100_1000>;
type MinContractNameLength = ConstU32<1>;
type MaxContractNameLength = ConstU32<32>;
type MaxCallPerBlock = ConstU32<1_000>;
type SContractRoot = SContractPalletId;
}

impl pallet_s_account::Config for Test {
type WeightInfo = ();
type Event = Event;
type Currency = Balances;
type ReservationRequirement = ReservationRequirement;
type ReservationRequirement = ConstU64<1>;
type DefaultFaucet = ConstU32<1_000>;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 20)],
}.assimilate_storage(&mut t).unwrap();
Expand Down
Loading

0 comments on commit 921f61f

Please sign in to comment.