From e006f2f4a4cca8cb62dc3b571ae2972be861b38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Fri, 2 Sep 2022 14:45:11 +0100 Subject: [PATCH] Fixes to vesting-related migration --- contracts/mixnet/src/queued_migrations.rs | 34 ++++------- contracts/vesting/src/contract.rs | 4 +- contracts/vesting/src/storage.rs | 6 +- contracts/vesting/src/vesting/account/mod.rs | 64 ++++++++++---------- 4 files changed, 47 insertions(+), 61 deletions(-) diff --git a/contracts/mixnet/src/queued_migrations.rs b/contracts/mixnet/src/queued_migrations.rs index 608c6f017d..95249f7c1e 100644 --- a/contracts/mixnet/src/queued_migrations.rs +++ b/contracts/mixnet/src/queued_migrations.rs @@ -56,6 +56,8 @@ fn migrate_operator( fn is_proxy_vesting(proxy: Option<&Addr>, vesting_contract: &str) -> bool { if let Some(proxy) = proxy { + // bypass for my local network where I just imported contract state to fresh contract + // return true; if proxy.as_ref() == vesting_contract { return true; } @@ -132,30 +134,18 @@ fn migrate_delegator( .expect("failed to serialize mixnode migration msg"); response.messages.push(SubMsg::new(wasm_msg)); } else { - let mut to_address = delegation.owner.to_string(); - let mut make_bank_msg = true; // if the specified proxy matches the vesting contract address -> treat it as "proper" undelegation // and send tokens back there - if let Some(proxy) = delegation.proxy { - to_address = proxy.to_string(); - if proxy == vesting_contract { - make_bank_msg = false; - let vesting_track = VestingContractExecuteMsg::TrackUndelegation { - owner: delegation.owner.to_string(), - mix_identity: delegation.node_identity, - amount: delegation.amount.clone(), - }; - let wasm_msg = wasm_execute( - vesting_contract, - &vesting_track, - vec![delegation.amount.clone()], - )?; - response.messages.push(SubMsg::new(wasm_msg)); - } - } - - // otherwise just send tokens back to the user / different proxy - if make_bank_msg { + if is_proxy_vesting(delegation.proxy.as_ref(), vesting_contract) { + let vesting_track = VestingContractExecuteMsg::TrackUndelegation { + owner: delegation.owner.to_string(), + mix_identity: delegation.node_identity.clone(), + amount: delegation.amount.clone(), + }; + let wasm_msg = wasm_execute(vesting_contract, &vesting_track, vec![delegation.amount])?; + response.messages.push(SubMsg::new(wasm_msg)); + } else { + let to_address = delegation.proxy.unwrap_or(delegation.owner).into_string(); let return_tokens = BankMsg::Send { to_address, amount: vec![delegation.amount], diff --git a/contracts/vesting/src/contract.rs b/contracts/vesting/src/contract.rs index b64394cfc0..45cca06014 100644 --- a/contracts/vesting/src/contract.rs +++ b/contracts/vesting/src/contract.rs @@ -9,7 +9,7 @@ use crate::traits::{ }; use crate::vesting::{populate_vesting_periods, Account}; use cosmwasm_std::{ - coin, entry_point, to_binary, BankMsg, Coin, Deps, DepsMut, Env, MessageInfo, Order, + coin, entry_point, to_binary, BankMsg, Coin, Deps, DepsMut, Env, Event, MessageInfo, Order, QueryResponse, Response, StdResult, Timestamp, Uint128, }; use cw_storage_plus::Bound; @@ -63,7 +63,7 @@ fn update_delegation_to_v2( // this MUST succeed since we know this delegation was created via vesting contract... let account = account_from_address(&owner, deps.storage, deps.api)?; - let storage_prefix = (account.storage_key(), node_identity); + let storage_prefix = (account.storage_key(), node_identity.clone()); let old_data = OLD_DELEGATIONS .prefix(storage_prefix.clone()) .range(deps.storage, None, None, Order::Ascending) diff --git a/contracts/vesting/src/storage.rs b/contracts/vesting/src/storage.rs index 597b23bc9f..15a68e878b 100644 --- a/contracts/vesting/src/storage.rs +++ b/contracts/vesting/src/storage.rs @@ -52,10 +52,8 @@ pub fn remove_delegation( key: (u32, IdentityKey, BlockTimestampSecs), storage: &mut dyn Storage, ) -> Result<(), ContractError> { - Err(ContractError::MaintenanceMode) - // - // DELEGATIONS.remove(storage, key); - // Ok(()) + OLD_DELEGATIONS.remove(storage, key); + Ok(()) } pub fn delete_account(address: &Addr, storage: &mut dyn Storage) -> Result<(), ContractError> { diff --git a/contracts/vesting/src/vesting/account/mod.rs b/contracts/vesting/src/vesting/account/mod.rs index 5ea3e2f5c0..73141ef3bb 100644 --- a/contracts/vesting/src/vesting/account/mod.rs +++ b/contracts/vesting/src/vesting/account/mod.rs @@ -3,7 +3,7 @@ use crate::errors::ContractError; use crate::storage::{ load_balance, load_bond_pledge, load_gateway_pledge, load_withdrawn, remove_bond_pledge, remove_delegation, remove_gateway_pledge, save_account, save_balance, save_bond_pledge, - save_gateway_pledge, save_withdrawn, BlockTimestampSecs, DELEGATIONS, KEY, + save_gateway_pledge, save_withdrawn, BlockTimestampSecs, DELEGATIONS, KEY, OLD_DELEGATIONS, }; use cosmwasm_std::{Addr, Coin, Order, Storage, Timestamp, Uint128}; use cw_storage_plus::Bound; @@ -212,38 +212,36 @@ impl Account { mix: &str, storage: &mut dyn Storage, ) -> Result<(), ContractError> { - Err(ContractError::MaintenanceMode) - // - // let limit = 50; - // let mut start_after = None; - // let mut block_heights = Vec::new(); - // let mut prev_len = 0; - // // TODO: Test this - // loop { - // block_heights.extend( - // DELEGATIONS - // .prefix((self.storage_key(), mix.to_string())) - // .keys(storage, start_after, None, Order::Ascending) - // .take(limit) - // .filter_map(|key| key.ok()), - // ); - // - // if prev_len == block_heights.len() { - // break; - // } - // - // prev_len = block_heights.len(); - // - // start_after = block_heights.last().map(|last| Bound::exclusive(*last)); - // if start_after.is_none() { - // break; - // } - // } - // - // for block_height in block_heights { - // remove_delegation((self.storage_key(), mix.to_string(), block_height), storage)?; - // } - // Ok(()) + let limit = 50; + let mut start_after = None; + let mut block_heights = Vec::new(); + let mut prev_len = 0; + // TODO: Test this + loop { + block_heights.extend( + OLD_DELEGATIONS + .prefix((self.storage_key(), mix.to_string())) + .keys(storage, start_after, None, Order::Ascending) + .take(limit) + .filter_map(|key| key.ok()), + ); + + if prev_len == block_heights.len() { + break; + } + + prev_len = block_heights.len(); + + start_after = block_heights.last().map(|last| Bound::exclusive(*last)); + if start_after.is_none() { + break; + } + } + + for block_height in block_heights { + remove_delegation((self.storage_key(), mix.to_string(), block_height), storage)?; + } + Ok(()) } pub fn total_delegations_for_mix(