Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Correct staked amount (#319)
Browse files Browse the repository at this point in the history
* Correct staked amount and withdraw expired unstaking automatically

* Fix compile

* Bump versions

* Add log

* Log

* Fix

* Fix migration

* Add more tests
  • Loading branch information
aurexav authored Mar 7, 2023
1 parent f33e2cb commit 2f0b7cd
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 187 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[profile.release]
panic = "unwind"

[profile.inc]
incremental = true
inherits = "release"

[workspace]
members = [
"core/*",
Expand Down
3 changes: 3 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ default_to_workspace = false
[env]
WASM_BUILD_WORKSPACE_HINT = "${CARGO_MAKE_WORKING_DIRECTORY}"

[env.inc]
CARGO_INCREMENTAL = 1

[tasks.fmt]
args = ["fmt"]
command = "cargo"
Expand Down
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,5 @@ pangoro-native = [
"pangoro-runtime",

# polkadot
"polkadot-cli/rococo-native",
"polkadot-cli/westend-native",
]
3 changes: 3 additions & 0 deletions runtime/pangolin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ runtime-benchmarks = [
fast-runtime = []

try-runtime = [
# crates.io
"array-bytes",

# cumulus
"cumulus-pallet-aura-ext/try-runtime",
"cumulus-pallet-dmp-queue/try-runtime",
Expand Down
2 changes: 1 addition & 1 deletion runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub const VERSION: sp_version::RuntimeVersion = sp_version::RuntimeVersion {
spec_name: sp_runtime::create_runtime_str!("Pangolin2"),
impl_name: sp_runtime::create_runtime_str!("DarwiniaOfficialRust"),
authoring_version: 0,
spec_version: 6_0_0_6,
spec_version: 6_0_0_7,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down
104 changes: 12 additions & 92 deletions runtime/pangolin/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,6 @@
// darwinia
#[allow(unused_imports)]
use crate::*;
// substrate
use codec::{Decode, Encode};
use frame_support::{
migration::{
get_storage_value, put_storage_value, storage_key_iter_with_suffix, take_storage_value,
},
Blake2_128Concat, StorageHasher,
};

#[derive(Encode, Decode)]
struct AssetAccount {
balance: Balance,
is_frozen: bool,
reason: ExistenceReason,
extra: (),
}
#[derive(Encode, Decode)]
enum ExistenceReason {
#[codec(index = 0)]
Consumer,
#[codec(index = 1)]
Sufficient,
#[codec(index = 2)]
DepositHeld(Balance),
#[codec(index = 3)]
DepositRefunded,
}
#[derive(Encode, Decode)]
struct AssetDetails {
owner: AccountId,
issuer: AccountId,
admin: AccountId,
freezer: AccountId,
supply: Balance,
deposit: Balance,
min_balance: Balance,
is_sufficient: bool,
accounts: u32,
sufficients: u32,
approvals: u32,
status: AssetStatus,
}
#[derive(Encode, Decode)]
enum AssetStatus {
Live,
Frozen,
Destroying,
}

const ASSETS: &[u8] = b"Assets";
const ASSETS_ASSET: &[u8] = b"Asset";
const ASSETS_ACCOUNT: &[u8] = b"Account";

pub struct CustomOnRuntimeUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
Expand All @@ -81,24 +29,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
let actual_accounts =
storage_key_iter_with_suffix::<AccountId, AssetAccount, Blake2_128Concat>(
ASSETS,
ASSETS_ACCOUNT,
&Blake2_128Concat::hash(&(AssetIds::PKton as u64).encode()),
)
.count();

let asset_detail = get_storage_value::<AssetDetails>(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::PKton as u64).encode()),
)
.unwrap();

assert_eq!(actual_accounts as u32, asset_detail.accounts);
assert_eq!(actual_accounts as u32, asset_detail.sufficients);

Ok(())
}

Expand All @@ -108,28 +38,18 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let actual_accounts =
storage_key_iter_with_suffix::<AccountId, AssetAccount, Blake2_128Concat>(
ASSETS,
ASSETS_ACCOUNT,
&Blake2_128Concat::hash(&(AssetIds::PKton as u64).encode()),
)
.count();
if let Some(mut asset_details) = take_storage_value::<AssetDetails>(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::PKton as u64).encode()),
) {
asset_details.accounts = actual_accounts as u32;
asset_details.sufficients = actual_accounts as u32;

put_storage_value(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::PKton as u64).encode()),
asset_details,
);
}
<darwinia_account_migration::Ledgers<Runtime>>::translate(
|k, mut v: darwinia_staking::Ledger<Runtime>| {
if let Some(ds) = <darwinia_account_migration::Deposits<Runtime>>::get(k) {
v.staked_ring -= ds.into_iter().map(|d| d.value).sum::<Balance>();
}

v.unstaking_ring.retain(|u| u.1 != 0);
v.unstaking_kton.retain(|u| u.1 != 0);

Some(v)
},
);

// frame_support::weights::Weight::zero()
RuntimeBlockWeights::get().max_block
Expand Down
3 changes: 3 additions & 0 deletions runtime/pangoro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ runtime-benchmarks = [
fast-runtime = []

try-runtime = [
# crates.io
"array-bytes",

# cumulus
"cumulus-pallet-aura-ext/try-runtime",
"cumulus-pallet-dmp-queue/try-runtime",
Expand Down
2 changes: 1 addition & 1 deletion runtime/pangoro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub const VERSION: sp_version::RuntimeVersion = sp_version::RuntimeVersion {
spec_name: sp_runtime::create_runtime_str!("Pangoro2"),
impl_name: sp_runtime::create_runtime_str!("DarwiniaOfficialRust"),
authoring_version: 0,
spec_version: 6_0_0_3,
spec_version: 6_0_0_4,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down
142 changes: 56 additions & 86 deletions runtime/pangoro/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,84 +20,64 @@
#[allow(unused_imports)]
use crate::*;
// substrate
use codec::{Decode, Encode};
use frame_support::{
migration::{
get_storage_value, put_storage_value, storage_key_iter_with_suffix, take_storage_value,
},
Blake2_128Concat, StorageHasher,
};

#[derive(Encode, Decode)]
struct AssetAccount {
balance: Balance,
is_frozen: bool,
reason: ExistenceReason,
extra: (),
}
#[derive(Encode, Decode)]
enum ExistenceReason {
#[codec(index = 0)]
Consumer,
#[codec(index = 1)]
Sufficient,
#[codec(index = 2)]
DepositHeld(Balance),
#[codec(index = 3)]
DepositRefunded,
}
#[derive(Encode, Decode)]
struct AssetDetails {
owner: AccountId,
issuer: AccountId,
admin: AccountId,
freezer: AccountId,
supply: Balance,
deposit: Balance,
min_balance: Balance,
is_sufficient: bool,
accounts: u32,
sufficients: u32,
approvals: u32,
status: AssetStatus,
}
#[derive(Encode, Decode)]
enum AssetStatus {
Live,
Frozen,
Destroying,
}
#[allow(unused_imports)]
use frame_support::log;

const ASSETS: &[u8] = b"Assets";
const ASSETS_ASSET: &[u8] = b"Asset";
const ASSETS_ACCOUNT: &[u8] = b"Account";
#[cfg(feature = "try-runtime")]
const ERROR_ACCOUNT: &str = "0xa847fbb7ce32a41fbea2216c7073752bb13dd6bfae44bc0f726e020452c2105b";

pub struct CustomOnRuntimeUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
log::info!("Pre-check account: {ERROR_ACCOUNT}");

let a = array_bytes::hex_n_into_unchecked::<_, sp_runtime::AccountId32, 32>(ERROR_ACCOUNT);

assert_eq!(
AccountMigration::ledger_of(&a).unwrap(),
darwinia_staking::Ledger::<Runtime> {
staked_ring: 3_190_000_000_000_000_000_000,
staked_kton: 9_000_000_000_000_000,
staked_deposits: frame_support::BoundedVec::truncate_from(vec![0, 1, 2]),
unstaking_ring: frame_support::BoundedVec::truncate_from(vec![(
10_000_000_000_000_000_000,
0
)]),
unstaking_kton: frame_support::BoundedVec::truncate_from(vec![(
1_000_000_000_000_000,
0
)]),
unstaking_deposits: frame_support::BoundedVec::default()
}
);

Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
let actual_accounts =
storage_key_iter_with_suffix::<AccountId, AssetAccount, Blake2_128Concat>(
ASSETS,
ASSETS_ACCOUNT,
&Blake2_128Concat::hash(&(AssetIds::OKton as u64).encode()),
)
.count();
log::info!("Post-check account: {ERROR_ACCOUNT}");

let asset_detail = get_storage_value::<AssetDetails>(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::OKton as u64).encode()),
)
.unwrap();
let a = array_bytes::hex_n_into_unchecked::<_, sp_runtime::AccountId32, 32>(ERROR_ACCOUNT);

assert_eq!(
AccountMigration::ledger_of(&a).unwrap(),
darwinia_staking::Ledger::<Runtime> {
staked_ring: 3_190_000_000_000_000_000_000
- AccountMigration::deposit_of(&a)
.unwrap()
.into_iter()
.map(|d| d.value)
.sum::<Balance>(),
staked_kton: 9_000_000_000_000_000,
staked_deposits: frame_support::BoundedVec::truncate_from(vec![0, 1, 2]),
unstaking_ring: frame_support::BoundedVec::default(),
unstaking_kton: frame_support::BoundedVec::default(),
unstaking_deposits: frame_support::BoundedVec::default()
}
);

assert_eq!(actual_accounts as u32, asset_detail.accounts);
assert_eq!(actual_accounts as u32, asset_detail.sufficients);
Ok(())
}

Expand All @@ -107,28 +87,18 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let actual_accounts =
storage_key_iter_with_suffix::<AccountId, AssetAccount, Blake2_128Concat>(
ASSETS,
ASSETS_ACCOUNT,
&Blake2_128Concat::hash(&(AssetIds::OKton as u64).encode()),
)
.count();
if let Some(mut asset_details) = take_storage_value::<AssetDetails>(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::OKton as u64).encode()),
) {
asset_details.accounts = actual_accounts as u32;
asset_details.sufficients = actual_accounts as u32;
<darwinia_account_migration::Ledgers<Runtime>>::translate(
|k, mut v: darwinia_staking::Ledger<Runtime>| {
if let Some(ds) = <darwinia_account_migration::Deposits<Runtime>>::get(k) {
v.staked_ring -= ds.into_iter().map(|d| d.value).sum::<Balance>();
}

put_storage_value(
ASSETS,
ASSETS_ASSET,
&Blake2_128Concat::hash(&(AssetIds::OKton as u64).encode()),
asset_details,
);
}
v.unstaking_ring.retain(|u| u.1 != 0);
v.unstaking_kton.retain(|u| u.1 != 0);

Some(v)
},
);

// frame_support::weights::Weight::zero()
RuntimeBlockWeights::get().max_block
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub type Result<T> = anyhow::Result<T>;
fn main() -> Result<()> {
std::env::set_var(
"RUST_LOG",
std::env::var("RUST_LOG").unwrap_or("state_processor=info".into()),
std::env::var("RUST_LOG").unwrap_or_else(|_| "state_processor=info".into()),
);
pretty_env_logger::init();

Expand Down
Loading

0 comments on commit 2f0b7cd

Please sign in to comment.