51b511b27e
WIP; rebasing Another branch squash Squashing the v3 branch changing min pledge amounts logic for adding new nymnode into the contract converting mixnode/gateway bonding into nym-node bonding logic for migrating gateways into nymnodes ibid for mixnodes further nym-node work + fixed most existing unit tests forbid nymnode migration with pending cost params changes preassign nodeid for gateways changing role assignment and epoch progression changing role assignment and epoch progression optional custom http port logic for unbonding a nym-node updating Delegation struct logic for increasing pledge of either mixnode or nymnode logic for decreasing pledge of either mixnode or a nym node logic for changing cost params of either mixnode or a nym node wip initialise nymnodes storage fixing transaction tests fixed naive family tests reward-compatibility related works resolving delegation events introduced rewarded set metadata another iteration of restoring old tests updated rewarding part of nym-api parking the branch unparking the branch wip purged families added 'ExitGateway' role passing explicit work factor for rewarding function remove legacy layers storage wip: node description queries added announced ports to self-described api step1 in gruelling journey of adding node_id to gateways ensure epoch work never goes above 1.0 changed active set to contain role distribution [theoretically] sending rewarding messages for the new rewarded set [theoretically] assigning new rewarded set reimplementing more nym-api features remove legacy types re-implement legacy network monitor restoring further routes + minor refactor of NodeStatusCache skimmed routes now return legacy nodes alongside nym-nodes seemingly restored all functionalities in nym-api removing more legacy things from the contract initial contract cleanup added nym-api endpoints to return generic annotations regardless of type updated simulator to use new rewarding parameters more contract cleanup made existing mixnet contract tests compile extra validation of nym-node bonding parameters fixed additional compilation issues fixed nym-api v3 database migration failure added additional nym-node contract queries updated the schema made additional delegation/rewards queries compatible with both legacy mixnodes and nym-nodes fixing existing unit tests in mixnet contract wip resolved first batch of 500 compiler errors re-deprecating routes making wallet's rust backend compile fixed non-determinism in contract + nym-api build fixes to the build populating cotracts-cache with nym-nodes data more missing nymnodes queries temp mixnet contract methods + restored result submission in nym-api allow deprecated routes submitting correct results for mixnode results removed deprecated re-export of AxumAppState and removed smurf naming moved axum modules into support::http cleaning up nym-api warnings determine entry gateways before exits exposed transaction to update nym-node config missing memo for updating node config new routes added routes to swagger and fixed relative paths fixed some macro derivations added nym-node commands to nym-cli
238 lines
7.6 KiB
Rust
238 lines
7.6 KiB
Rust
use cosmwasm_std::{Addr, Api, Storage, Uint128};
|
|
use cosmwasm_std::{Coin, Order};
|
|
use cw_storage_plus::{Item, Map};
|
|
use mixnet_contract_common::{IdentityKey, NodeId};
|
|
use vesting_contract_common::account::VestingAccountStorageKey;
|
|
use vesting_contract_common::{Account, PledgeData, VestingContractError};
|
|
|
|
pub(crate) type BlockTimestampSecs = u64;
|
|
|
|
/// Counter for the unique, monotonically increasing storage key id for the vesting account data.
|
|
pub const KEY: Item<'_, VestingAccountStorageKey> = Item::new("key");
|
|
|
|
/// Storage map containing vesting account information associated with particular owner address.
|
|
pub const ACCOUNTS: Map<'_, Addr, Account> = Map::new("acc");
|
|
|
|
/// Storage map containing information about amount of tokens associated with particular vesting account
|
|
/// that are currently present in the contract (and have not been withdrawn or staked in the mixnet contract)
|
|
// note: this assumes I understood the intent behind this correctly
|
|
const BALANCES: Map<'_, VestingAccountStorageKey, Uint128> = Map::new("blc");
|
|
|
|
/// Storage map containing information about amount of tokens withdrawn from the contract by a particular vesting account.
|
|
const WITHDRAWNS: Map<'_, VestingAccountStorageKey, Uint128> = Map::new("wthd");
|
|
|
|
/// Storage map containing information about amount of tokens pledged towards bonding mixnodes
|
|
/// in the mixnet contract using a particular vesting account.
|
|
const BOND_PLEDGES: Map<'_, VestingAccountStorageKey, PledgeData> = Map::new("bnd");
|
|
|
|
/// Storage map containing information about amount of tokens pledged towards bonding gateways
|
|
/// in the mixnet contract using a particular vesting account.
|
|
const GATEWAY_PLEDGES: Map<'_, VestingAccountStorageKey, PledgeData> = Map::new("gtw");
|
|
|
|
/// Old, pre-v2 migration, storage map that used to contain information about tokens delegated
|
|
/// towards particular mixnodes in the mixnet contract with given vesting account.
|
|
/// It should be completely empty.
|
|
pub const _OLD_DELEGATIONS: Map<
|
|
'_,
|
|
(VestingAccountStorageKey, IdentityKey, BlockTimestampSecs),
|
|
Uint128,
|
|
> = Map::new("dlg");
|
|
|
|
/// Storage map containing information about tokens delegated towards particular mixnodes
|
|
/// in the mixnet contract with given vesting account.
|
|
pub const DELEGATIONS: Map<'_, (VestingAccountStorageKey, NodeId, BlockTimestampSecs), Uint128> =
|
|
Map::new("dlg_v2");
|
|
|
|
/// Explicit contract admin that is allowed, among other things, to create new vesting accounts.
|
|
pub const ADMIN: Item<'_, Addr> = Item::new("adm");
|
|
|
|
/// Address of the mixnet contract.
|
|
pub const MIXNET_CONTRACT_ADDRESS: Item<'_, Addr> = Item::new("mix");
|
|
|
|
/// The denomination of coin used for staking.
|
|
pub const MIX_DENOM: Item<'_, String> = Item::new("den");
|
|
|
|
pub fn save_delegation(
|
|
key: (VestingAccountStorageKey, NodeId, BlockTimestampSecs),
|
|
amount: Uint128,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
let existing_delegation_amount = if let Some(delegation) = DELEGATIONS.may_load(storage, key)? {
|
|
delegation
|
|
} else {
|
|
Uint128::zero()
|
|
};
|
|
let new_delegations_amount = existing_delegation_amount + amount;
|
|
DELEGATIONS.save(storage, key, &new_delegations_amount)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn remove_delegation(
|
|
key: (VestingAccountStorageKey, NodeId, BlockTimestampSecs),
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
DELEGATIONS.remove(storage, key);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load_delegation_timestamps(
|
|
prefix: (VestingAccountStorageKey, NodeId),
|
|
storage: &dyn Storage,
|
|
) -> Result<Vec<BlockTimestampSecs>, VestingContractError> {
|
|
let block_timestamps = DELEGATIONS
|
|
.prefix(prefix)
|
|
.keys(storage, None, None, Order::Ascending)
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
Ok(block_timestamps)
|
|
}
|
|
|
|
pub fn count_subdelegations_for_mix(
|
|
prefix: (VestingAccountStorageKey, NodeId),
|
|
storage: &dyn Storage,
|
|
) -> u32 {
|
|
DELEGATIONS
|
|
.prefix(prefix)
|
|
.keys(storage, None, None, Order::Ascending)
|
|
.count() as u32
|
|
}
|
|
|
|
pub fn load_withdrawn(
|
|
key: VestingAccountStorageKey,
|
|
storage: &dyn Storage,
|
|
) -> Result<Uint128, VestingContractError> {
|
|
Ok(WITHDRAWNS
|
|
.may_load(storage, key)
|
|
.unwrap_or(None)
|
|
.unwrap_or_else(Uint128::zero))
|
|
}
|
|
|
|
pub fn load_balance(
|
|
key: VestingAccountStorageKey,
|
|
storage: &dyn Storage,
|
|
) -> Result<Uint128, VestingContractError> {
|
|
Ok(BALANCES
|
|
.may_load(storage, key)
|
|
.unwrap_or(None)
|
|
.unwrap_or_else(Uint128::zero))
|
|
}
|
|
|
|
pub fn save_balance(
|
|
key: VestingAccountStorageKey,
|
|
value: Uint128,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
BALANCES.save(storage, key, &value)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn save_withdrawn(
|
|
key: VestingAccountStorageKey,
|
|
value: Uint128,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
WITHDRAWNS.save(storage, key, &value)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load_bond_pledge(
|
|
key: VestingAccountStorageKey,
|
|
storage: &dyn Storage,
|
|
) -> Result<Option<PledgeData>, VestingContractError> {
|
|
Ok(BOND_PLEDGES.may_load(storage, key).unwrap_or(None))
|
|
}
|
|
|
|
pub fn remove_bond_pledge(
|
|
key: VestingAccountStorageKey,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
BOND_PLEDGES.remove(storage, key);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn save_bond_pledge(
|
|
key: VestingAccountStorageKey,
|
|
value: &PledgeData,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
BOND_PLEDGES.save(storage, key, value)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn decrease_bond_pledge(
|
|
key: VestingAccountStorageKey,
|
|
amount: Coin,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
let mut existing = BOND_PLEDGES.load(storage, key)?;
|
|
if existing.amount.amount <= amount.amount {
|
|
// this shouldn't be possible!
|
|
// (but check for it anyway... just in case)
|
|
return Err(VestingContractError::InvalidBondPledgeReduction {
|
|
current: existing.amount,
|
|
decrease_by: amount,
|
|
});
|
|
}
|
|
existing.amount.amount -= amount.amount;
|
|
save_bond_pledge(key, &existing, storage)
|
|
}
|
|
|
|
pub fn load_gateway_pledge(
|
|
key: VestingAccountStorageKey,
|
|
storage: &dyn Storage,
|
|
) -> Result<Option<PledgeData>, VestingContractError> {
|
|
Ok(GATEWAY_PLEDGES.may_load(storage, key).unwrap_or(None))
|
|
}
|
|
|
|
pub fn save_gateway_pledge(
|
|
key: VestingAccountStorageKey,
|
|
value: &PledgeData,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
GATEWAY_PLEDGES.save(storage, key, value)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn remove_gateway_pledge(
|
|
key: VestingAccountStorageKey,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
GATEWAY_PLEDGES.remove(storage, key);
|
|
Ok(())
|
|
}
|
|
|
|
pub fn save_account(
|
|
account: &Account,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
ACCOUNTS.save(storage, account.owner_address(), account)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load_account(
|
|
address: Addr,
|
|
storage: &dyn Storage,
|
|
) -> Result<Option<Account>, VestingContractError> {
|
|
Ok(ACCOUNTS.may_load(storage, address).unwrap_or(None))
|
|
}
|
|
|
|
pub fn delete_account(
|
|
address: Addr,
|
|
storage: &mut dyn Storage,
|
|
) -> Result<(), VestingContractError> {
|
|
ACCOUNTS.remove(storage, address);
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_account(address: Addr, storage: &dyn Storage) -> Result<Account, VestingContractError> {
|
|
load_account(address.clone(), storage)?
|
|
.ok_or_else(|| VestingContractError::NoAccountForAddress(address.into_string()))
|
|
}
|
|
|
|
pub fn account_from_address(
|
|
address: &str,
|
|
storage: &dyn Storage,
|
|
api: &dyn Api,
|
|
) -> Result<Account, VestingContractError> {
|
|
validate_account(api.addr_validate(address)?, storage)
|
|
}
|