Skip to content

Commit

Permalink
feat: pallet_account_fix (#2427)
Browse files Browse the repository at this point in the history
* feat: pallet_account_fix

* debug: fmt

* debug: add try-runtime feature

* debug: change weight formula

* debug: bump spec version

* debug: for weight

* debug: weird

* debug: fix u64
  • Loading branch information
wangminqi committed Jan 29, 2024
1 parent f120d87 commit 17ce738
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
16 changes: 16 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
@@ -1,6 +1,7 @@
[workspace]
members = [
'node',
'pallets/account-fix',
'pallets/bridge',
'pallets/bridge-transfer',
'pallets/drop3',
Expand Down
44 changes: 44 additions & 0 deletions pallets/account-fix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
authors = ['Trust Computing GmbH <info@litentry.com>']
description = 'Pallet for temporary fix of onchain accountInfo'
edition = '2021'
homepage = 'https://litentry.com/'
license = 'GPL-3.0'
name = 'pallet-account-fix'
repository = 'https://github.com/litentry/litentry-parachain'
version = '0.1.0'

[dependencies]
# third-party dependencies
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }

# primitives
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }

# frame dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }

pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"codec/std",
"sp-std/std",
"sp-runtime/std",
"sp-io/std",
"sp-core/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
]
try-runtime = ["frame-support/try-runtime"]
92 changes: 92 additions & 0 deletions pallets/account-fix/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

//! A pallet for temporary fix of onchain accountInfo.
//! No storage for this pallet and it should be removed right after fixing.
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
pallet_prelude::*,
traits::{Currency, ReservableCurrency, StorageVersion},
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use sp_runtime::traits::StaticLookup;

use sp_std::vec::Vec;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + pallet_balances::Config {
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Change the admin account
/// similar to sudo.set_key, the old account will be supplied in event
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(20_000_000u64 * (who.len() as u64), 0))]
pub fn upgrade_accounts(
origin: OriginFor<T>,
who: Vec<T::AccountId>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
for i in &who {
frame_system::Pallet::<T>::inc_consumers(i)?;
}
// Do not pay a fee
Ok(Pays::No.into())
}

/// add some balance of an existing account
#[pallet::call_index(1)]
#[pallet::weight({10_000})]
pub fn set_balance(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
#[pallet::compact] add_free: BalanceOf<T>,
#[pallet::compact] add_reserved: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let who = T::Lookup::lookup(who)?;

let add_total = add_free + add_reserved;

// First we try to modify the account's balance to the forced balance.
T::Currency::deposit_into_existing(&who, add_total)?;
// Then do the reservation
T::Currency::reserve(&who, add_reserved)?;

Ok(().into())
}
}
}
4 changes: 4 additions & 0 deletions runtime/litentry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-
frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42", optional = true }

# Litentry pallets
pallet-account-fix = { path = "../../pallets/account-fix", default-features = false }
pallet-asset-manager = { path = "../../pallets/xcm-asset-manager", default-features = false }
pallet-bridge = { path = "../../pallets/bridge", default-features = false }
pallet-bridge-transfer = { path = "../../pallets/bridge-transfer", default-features = false }
Expand Down Expand Up @@ -137,6 +138,7 @@ runtime-benchmarks = [
"pallet-drop3/runtime-benchmarks",
"pallet-extrinsic-filter/runtime-benchmarks",
"cumulus-pallet-xcmp-queue/runtime-benchmarks",
"pallet-account-fix/runtime-benchmarks",
"pallet-asset-manager/runtime-benchmarks",
]
std = [
Expand All @@ -162,6 +164,7 @@ std = [
"orml-tokens/std",
"orml-traits/std",
"orml-xtokens/std",
"pallet-account-fix/std",
"pallet-asset-manager/std",
"pallet-aura/std",
"pallet-authorship/std",
Expand Down Expand Up @@ -219,6 +222,7 @@ try-runtime = [
"frame-try-runtime",
"orml-tokens/try-runtime",
"orml-xtokens/try-runtime",
"pallet-account-fix/try-runtime",
"pallet-asset-manager/try-runtime",
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
Expand Down
7 changes: 6 additions & 1 deletion runtime/litentry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("litentry-parachain"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9172,
spec_version: 9173,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -629,6 +629,10 @@ impl pallet_sudo::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_account_fix::Config for Runtime {
type Currency = Balances;
}

parameter_types! {
pub const ReservedXcmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
Expand Down Expand Up @@ -929,6 +933,7 @@ construct_runtime! {
AssetManager: pallet_asset_manager = 64,

// TMP
AccountFix: pallet_account_fix = 254,
Sudo: pallet_sudo = 255,
}
}
Expand Down

0 comments on commit 17ce738

Please sign in to comment.