From 44d59ff8c2db874682e7bccd7ae8b32accd2a279 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 14 Jan 2022 15:50:57 +0000 Subject: [PATCH 1/3] Bump @openzeppelin/contracts in /contracts/basic-bandwidth-generation (#1034) Bumps [@openzeppelin/contracts](https://github.com/OpenZeppelin/openzeppelin-contracts) from 4.4.1 to 4.4.2. - [Release notes](https://github.com/OpenZeppelin/openzeppelin-contracts/releases) - [Changelog](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/CHANGELOG.md) - [Commits](https://github.com/OpenZeppelin/openzeppelin-contracts/compare/v4.4.1...v4.4.2) --- updated-dependencies: - dependency-name: "@openzeppelin/contracts" dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- contracts/basic-bandwidth-generation/package-lock.json | 6 +++--- contracts/basic-bandwidth-generation/package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/basic-bandwidth-generation/package-lock.json b/contracts/basic-bandwidth-generation/package-lock.json index 110d9ce2e9..eba9f64f04 100644 --- a/contracts/basic-bandwidth-generation/package-lock.json +++ b/contracts/basic-bandwidth-generation/package-lock.json @@ -1556,9 +1556,9 @@ } }, "@openzeppelin/contracts": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.4.1.tgz", - "integrity": "sha512-o+pHCf/yMLSlV5MkDQEzEQL402i6SoRnktru+0rdSxVEFZcTzzGhZCAtZjUFyKGazMSv1TilzMg+RbED1N8XHQ==" + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.4.2.tgz", + "integrity": "sha512-NyJV7sJgoGYqbtNUWgzzOGW4T6rR19FmX1IJgXGdapGPWsuMelGJn9h03nos0iqfforCbCB0iYIR0MtIuIFLLw==" }, "@openzeppelin/test-helpers": { "version": "0.5.15", diff --git a/contracts/basic-bandwidth-generation/package.json b/contracts/basic-bandwidth-generation/package.json index 910d7a4541..3f98a678a2 100644 --- a/contracts/basic-bandwidth-generation/package.json +++ b/contracts/basic-bandwidth-generation/package.json @@ -12,7 +12,7 @@ "dependencies": { "@nomiclabs/hardhat-truffle5": "^2.0.2", "@nomiclabs/hardhat-web3": "^2.0.0", - "@openzeppelin/contracts": "^4.4.1", + "@openzeppelin/contracts": "^4.4.2", "@openzeppelin/test-helpers": "^0.5.15", "dotenv": "^10.0.0", "find-config": "^1.0.0" From 8fb54dd4e74b58cc7331966cef25bfd0c4a5b6ab Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 14 Jan 2022 20:57:51 +0100 Subject: [PATCH 2/3] Feature/downcast reward estimation (#1031) * Downcast u128 to u64 * fmt * Fix status * fmt --- validator-api/src/node_status_api/routes.rs | 34 ++++++++++++------- validator-api/src/rewarding/error.rs | 6 ++++ validator-api/src/rewarding/mod.rs | 15 ++++---- .../validator-api-requests/src/models.rs | 6 ++-- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/validator-api/src/node_status_api/routes.rs b/validator-api/src/node_status_api/routes.rs index b4a9530566..986c0d5919 100644 --- a/validator-api/src/node_status_api/routes.rs +++ b/validator-api/src/node_status_api/routes.rs @@ -131,18 +131,28 @@ pub(crate) async fn get_mixnode_reward_estimation( .await .map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))?; - let (estimated_total_node_reward, estimated_operator_reward, estimated_delegators_reward) = - epoch_reward_params.estimate_reward(&bond, uptime.u8(), status.is_active()); - - Ok(Json(RewardEstimationResponse { - estimated_total_node_reward, - estimated_operator_reward, - estimated_delegators_reward, - current_epoch_start: current_epoch.start_unix_timestamp(), - current_epoch_end: current_epoch.end_unix_timestamp(), - current_epoch_uptime: uptime.u8(), - as_at, - })) + match epoch_reward_params.estimate_reward(&bond, uptime.u8(), status.is_active()) { + Ok(( + estimated_total_node_reward, + estimated_operator_reward, + estimated_delegators_reward, + )) => { + let reponse = RewardEstimationResponse { + estimated_total_node_reward, + estimated_operator_reward, + estimated_delegators_reward, + current_epoch_start: current_epoch.start_unix_timestamp(), + current_epoch_end: current_epoch.end_unix_timestamp(), + current_epoch_uptime: uptime.u8(), + as_at, + }; + Ok(Json(reponse)) + } + Err(e) => Err(ErrorResponse::new( + e.to_string(), + Status::InternalServerError, + )), + } } else { Err(ErrorResponse::new( "mixnode bond not found", diff --git a/validator-api/src/rewarding/error.rs b/validator-api/src/rewarding/error.rs index 347fa07ad6..4f20a878aa 100644 --- a/validator-api/src/rewarding/error.rs +++ b/validator-api/src/rewarding/error.rs @@ -23,6 +23,12 @@ pub(crate) enum RewardingError { #[error("Failed to query the smart contract - {0}")] ValidatorClientError(ValidatorClientError), + + #[error("Error downcasting u128 -> u64")] + DowncastingError { + #[from] + source: std::num::TryFromIntError, + }, } impl From for RewardingError { diff --git a/validator-api/src/rewarding/mod.rs b/validator-api/src/rewarding/mod.rs index 74222bb16b..fc67a7bba8 100644 --- a/validator-api/src/rewarding/mod.rs +++ b/validator-api/src/rewarding/mod.rs @@ -58,7 +58,7 @@ impl EpochRewardParams { node: &MixNodeBond, uptime: u8, in_active_set: bool, - ) -> (u128, u128, u128) { + ) -> Result<(u64, u64, u64), RewardingError> { let node_reward_params = NodeRewardParams::new( self.period_reward_pool, self.rewarded_set_size.into(), @@ -76,14 +76,15 @@ impl EpochRewardParams { let delegators_reward = node.reward_delegation(node.total_delegation().amount, &node_reward_params); - ( + Ok(( total_node_reward .reward() - .checked_to_num() - .unwrap_or_default(), - operator_reward, - delegators_reward, - ) + .checked_to_num::() + .unwrap_or_default() + .try_into()?, + operator_reward.try_into()?, + delegators_reward.try_into()?, + )) } } diff --git a/validator-api/validator-api-requests/src/models.rs b/validator-api/validator-api-requests/src/models.rs index fddd64d788..a7e5640837 100644 --- a/validator-api/validator-api-requests/src/models.rs +++ b/validator-api/validator-api-requests/src/models.rs @@ -35,9 +35,9 @@ pub struct MixnodeStatusResponse { #[derive(Clone, Copy, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))] pub struct RewardEstimationResponse { - pub estimated_total_node_reward: u128, - pub estimated_operator_reward: u128, - pub estimated_delegators_reward: u128, + pub estimated_total_node_reward: u64, + pub estimated_operator_reward: u64, + pub estimated_delegators_reward: u64, pub current_epoch_start: i64, pub current_epoch_end: i64, From 56d36d2c46a458c5a36d45b3f39f3146c2ebaf45 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 14 Jan 2022 20:59:40 +0100 Subject: [PATCH 3/3] Migrate to cw-storage-plus 0.11.1 (#1035) --- Cargo.lock | 4 ++-- contracts/Cargo.lock | 24 +++++++++---------- contracts/mixnet/Cargo.toml | 6 ++--- contracts/mixnet/src/delegations/storage.rs | 8 +++---- contracts/mixnet/src/rewards/helpers.rs | 4 ++-- contracts/mixnet/src/rewards/queries.rs | 6 ++--- contracts/mixnet/src/rewards/storage.rs | 5 ++-- contracts/mixnet/src/rewards/transactions.rs | 12 +++++----- contracts/vesting/Cargo.toml | 2 +- .../src/vesting/account/delegating_account.rs | 4 ++-- contracts/vesting/src/vesting/account/mod.rs | 4 ++-- .../src/vesting/account/vesting_account.rs | 2 +- nym-wallet/Cargo.lock | 4 ++-- 13 files changed, 42 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3af75358c..fde6d7bcee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1255,9 +1255,9 @@ dependencies = [ [[package]] name = "cw-storage-plus" -version = "0.10.3" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3b8b840947313c1a1cccf056836cd79a60b4526bdcd6582995be37dc97be4ae" +checksum = "7d7ee1963302b0ac2a9d42fe0faec826209c17452bfd36fbfd9d002a88929261" dependencies = [ "cosmwasm-std", "schemars", diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index f374cb6199..6654a6ff25 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -245,9 +245,9 @@ dependencies = [ [[package]] name = "cosmwasm-crypto" -version = "1.0.0-beta3" +version = "1.0.0-beta4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a380b87642204557629c9b72988c47b55fbfe6d474960adba56b22331504956a" +checksum = "f903ebbabc0d4880dbc76148efb8be8fc29fa4bf294c440c3d70da1c8bcafff7" dependencies = [ "digest 0.9.0", "ed25519-zebra", @@ -258,9 +258,9 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.0.0-beta3" +version = "1.0.0-beta4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "866713b2fe13f23038c7d8824c3059d1f28dd94685fb406d1533c4eeeefeefae" +checksum = "832bebef577ecb394603de8e2bf0de429b74aa364e17dec18e15ce37e71b0cae" dependencies = [ "syn", ] @@ -277,9 +277,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.0.0-beta3" +version = "1.0.0-beta4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8dbb9939b31441dfa9af3ec9740c8a24d585688401eff1b6b386abb7ad0d10a8" +checksum = "6238c45840cc9de5a39f0f619e3a4f7c38c5d2c6ac9e3e4d72751ee045e6d7da" dependencies = [ "base64", "cosmwasm-crypto", @@ -293,9 +293,9 @@ dependencies = [ [[package]] name = "cosmwasm-storage" -version = "1.0.0-beta3" +version = "1.0.0-beta4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4a4e55f0d64fed54cd2202301b8d466af8de044589247dabd77a4222f52f749" +checksum = "f472c0bfd584a4510702537d0b65c5331095e76099586a12f749fd69afda9c5e" dependencies = [ "cosmwasm-std", "serde", @@ -394,9 +394,9 @@ dependencies = [ [[package]] name = "cw-storage-plus" -version = "0.10.3" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3b8b840947313c1a1cccf056836cd79a60b4526bdcd6582995be37dc97be4ae" +checksum = "7d7ee1963302b0ac2a9d42fe0faec826209c17452bfd36fbfd9d002a88929261" dependencies = [ "cosmwasm-std", "schemars", @@ -1202,9 +1202,9 @@ dependencies = [ [[package]] name = "serde-json-wasm" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50eef3672ec8fa45f3457fd423ba131117786784a895548021976117c1ded449" +checksum = "042ac496d97e5885149d34139bad1d617192770d7eb8f1866da2317ff4501853" dependencies = [ "serde", ] diff --git a/contracts/mixnet/Cargo.toml b/contracts/mixnet/Cargo.toml index 5fc7aba1f0..d1ca3066fe 100644 --- a/contracts/mixnet/Cargo.toml +++ b/contracts/mixnet/Cargo.toml @@ -20,9 +20,9 @@ mixnet-contract-common = { path = "../../common/cosmwasm-smart-contracts/mixnet- config = { path = "../../common/config"} vesting-contract = { path = "../vesting" } -cosmwasm-std = "1.0.0-beta3" -cosmwasm-storage = "1.0.0-beta3" -cw-storage-plus = "0.10.3" +cosmwasm-std = "1.0.0-beta4" +cosmwasm-storage = "1.0.0-beta4" +cw-storage-plus = "0.11.1" bs58 = "0.4.0" schemars = "0.8" diff --git a/contracts/mixnet/src/delegations/storage.rs b/contracts/mixnet/src/delegations/storage.rs index 8e4f3cedcb..11316efd79 100644 --- a/contracts/mixnet/src/delegations/storage.rs +++ b/contracts/mixnet/src/delegations/storage.rs @@ -17,9 +17,9 @@ pub(crate) const DELEGATION_PAGE_DEFAULT_LIMIT: u32 = 250; type PrimaryKey = Vec; pub(crate) struct DelegationIndex<'a> { - pub(crate) owner: MultiIndex<'a, (Addr, PrimaryKey), Delegation>, + pub(crate) owner: MultiIndex<'a, Addr, Delegation>, - pub(crate) mixnode: MultiIndex<'a, (IdentityKey, PrimaryKey), Delegation>, + pub(crate) mixnode: MultiIndex<'a, IdentityKey, Delegation>, } impl<'a> IndexList for DelegationIndex<'a> { @@ -46,12 +46,12 @@ impl<'a> IndexList for DelegationIndex<'a> { pub(crate) fn delegations<'a>() -> IndexedMap<'a, PrimaryKey, Delegation, DelegationIndex<'a>> { let indexes = DelegationIndex { owner: MultiIndex::new( - |d, pk| (d.owner.clone(), pk), + |d| d.owner.clone(), DELEGATION_PK_NAMESPACE, DELEGATION_OWNER_IDX_NAMESPACE, ), mixnode: MultiIndex::new( - |d, pk| (d.node_identity.clone(), pk), + |d| d.node_identity.clone(), DELEGATION_PK_NAMESPACE, DELEGATION_MIXNODE_IDX_NAMESPACE, ), diff --git a/contracts/mixnet/src/rewards/helpers.rs b/contracts/mixnet/src/rewards/helpers.rs index c363cb5e39..e532c948f5 100644 --- a/contracts/mixnet/src/rewards/helpers.rs +++ b/contracts/mixnet/src/rewards/helpers.rs @@ -64,7 +64,7 @@ pub(crate) fn update_rewarding_status( if let Some(next_start) = next_start { storage::REWARDING_STATUS.save( storage, - (rewarding_interval_nonce.into(), mix_identity), + (rewarding_interval_nonce, mix_identity), &RewardingStatus::PendingNextDelegatorPage(PendingDelegatorRewarding { running_results: rewarding_results, next_start, @@ -74,7 +74,7 @@ pub(crate) fn update_rewarding_status( } else { storage::REWARDING_STATUS.save( storage, - (rewarding_interval_nonce.into(), mix_identity), + (rewarding_interval_nonce, mix_identity), &RewardingStatus::Complete(rewarding_results), )?; } diff --git a/contracts/mixnet/src/rewards/queries.rs b/contracts/mixnet/src/rewards/queries.rs index a52f74dcf0..d9f82ef7b8 100644 --- a/contracts/mixnet/src/rewards/queries.rs +++ b/contracts/mixnet/src/rewards/queries.rs @@ -19,10 +19,8 @@ pub(crate) fn query_rewarding_status( mix_identity: IdentityKey, rewarding_interval_nonce: u32, ) -> StdResult { - let status = storage::REWARDING_STATUS.may_load( - deps.storage, - (rewarding_interval_nonce.into(), mix_identity), - )?; + let status = storage::REWARDING_STATUS + .may_load(deps.storage, (rewarding_interval_nonce, mix_identity))?; Ok(MixnodeRewardingStatusResponse { status }) } diff --git a/contracts/mixnet/src/rewards/storage.rs b/contracts/mixnet/src/rewards/storage.rs index 7f8e1765c9..838b68f092 100644 --- a/contracts/mixnet/src/rewards/storage.rs +++ b/contracts/mixnet/src/rewards/storage.rs @@ -4,11 +4,12 @@ use crate::error::ContractError; use config::defaults::TOTAL_SUPPLY; use cosmwasm_std::{StdResult, Storage, Uint128}; -use cw_storage_plus::{Item, Map, U32Key}; +use cw_storage_plus::{Item, Map}; use mixnet_contract_common::{IdentityKey, RewardingStatus}; pub(crate) const REWARD_POOL: Item = Item::new("pool"); -pub(crate) const REWARDING_STATUS: Map<(U32Key, IdentityKey), RewardingStatus> = Map::new("rm"); +// TODO: Do we need a migration for this? +pub(crate) const REWARDING_STATUS: Map<(u32, IdentityKey), RewardingStatus> = Map::new("rm"); // approximately 1 day (assuming 5s per block) pub(crate) const MINIMUM_BLOCK_AGE_FOR_REWARDING: u64 = 17280; diff --git a/contracts/mixnet/src/rewards/transactions.rs b/contracts/mixnet/src/rewards/transactions.rs index 438ad9504d..5a838239d0 100644 --- a/contracts/mixnet/src/rewards/transactions.rs +++ b/contracts/mixnet/src/rewards/transactions.rs @@ -201,7 +201,7 @@ pub(crate) fn try_reward_next_mixnode_delegators( match storage::REWARDING_STATUS.may_load( deps.storage, - (rewarding_interval_nonce.into(), mix_identity.clone()), + (rewarding_interval_nonce, mix_identity.clone()), )? { None => { // we haven't called 'regular' try_reward_mixnode, i.e. the operator itself @@ -271,7 +271,7 @@ pub(crate) fn try_reward_mixnode( // check if the mixnode hasn't been rewarded in this rewarding interval already match storage::REWARDING_STATUS.may_load( deps.storage, - (rewarding_interval_nonce.into(), mix_identity.clone()), + (rewarding_interval_nonce, mix_identity.clone()), )? { None => (), Some(RewardingStatus::Complete(_)) => { @@ -304,7 +304,7 @@ pub(crate) fn try_reward_mixnode( if current_bond.block_height + storage::MINIMUM_BLOCK_AGE_FOR_REWARDING > env.block.height { storage::REWARDING_STATUS.save( deps.storage, - (rewarding_interval_nonce.into(), mix_identity.clone()), + (rewarding_interval_nonce, mix_identity.clone()), &RewardingStatus::Complete(Default::default()), )?; @@ -320,7 +320,7 @@ pub(crate) fn try_reward_mixnode( if params.uptime() == 0 { storage::REWARDING_STATUS.save( deps.storage, - (rewarding_interval_nonce.into(), mix_identity.clone()), + (rewarding_interval_nonce, mix_identity.clone()), &RewardingStatus::Complete(Default::default()), )?; @@ -1257,7 +1257,7 @@ pub mod tests { // it's all correctly saved match storage::REWARDING_STATUS - .load(deps.as_ref().storage, (1.into(), node_identity)) + .load(deps.as_ref().storage, (1u32, node_identity)) .unwrap() { RewardingStatus::Complete(result) => assert_eq!( @@ -1635,7 +1635,7 @@ pub mod tests { .idx .mixnode .prefix(node_identity.clone()) - .range(deps.as_ref().storage, None, None, Order::Ascending) + .range_raw(deps.as_ref().storage, None, None, Order::Ascending) { let (primary_key, delegation) = delegation.unwrap(); let delegator_reward = Uint128::new(delegation.amount.amount.u128() - base_delegation); diff --git a/contracts/vesting/Cargo.toml b/contracts/vesting/Cargo.toml index 069d31ae3e..dd61e3c325 100644 --- a/contracts/vesting/Cargo.toml +++ b/contracts/vesting/Cargo.toml @@ -19,7 +19,7 @@ vesting-contract-common = { path = "../../common/cosmwasm-smart-contracts/vestin config = { path = "../../common/config" } cosmwasm-std = { version = "1.0.0-beta3"} -cw-storage-plus = { version = "0.10.3", features = ["iterator"] } +cw-storage-plus = { version = "0.11.1", features = ["iterator"] } schemars = "0.8" serde = { version = "1.0.103", default-features = false, features = ["derive"] } diff --git a/contracts/vesting/src/vesting/account/delegating_account.rs b/contracts/vesting/src/vesting/account/delegating_account.rs index c39ae4a870..845279f355 100644 --- a/contracts/vesting/src/vesting/account/delegating_account.rs +++ b/contracts/vesting/src/vesting/account/delegating_account.rs @@ -111,8 +111,8 @@ impl DelegatingAccount for Account { // Iterate over keys matching the prefix and remove them from the map let block_times = delegations - .prefix_de(mix_bytes) - .keys_de(storage, None, None, Order::Ascending) + .prefix(mix_bytes) + .keys(storage, None, None, Order::Ascending) // Scan will blow up on first error .scan((), |_, x| x.ok()) .collect::>(); diff --git a/contracts/vesting/src/vesting/account/mod.rs b/contracts/vesting/src/vesting/account/mod.rs index 2ce20122ee..b10e60f81e 100644 --- a/contracts/vesting/src/vesting/account/mod.rs +++ b/contracts/vesting/src/vesting/account/mod.rs @@ -196,8 +196,8 @@ impl Account { let key = self.delegations_key(); let delegations: Map<(&[u8], u64), Uint128> = Map::new(&key); delegations - .prefix_de(mix.as_bytes()) - .keys_de(storage, None, None, Order::Ascending) + .prefix(mix.as_bytes()) + .keys(storage, None, None, Order::Ascending) // Scan will blow up on first error .scan((), |_, x| x.ok()) .collect::>() diff --git a/contracts/vesting/src/vesting/account/vesting_account.rs b/contracts/vesting/src/vesting/account/vesting_account.rs index 00ffdac957..8ffc6f6852 100644 --- a/contracts/vesting/src/vesting/account/vesting_account.rs +++ b/contracts/vesting/src/vesting/account/vesting_account.rs @@ -119,7 +119,7 @@ impl VestingAccount for Account { let delegations: Map<(&[u8], u64), Uint128> = Map::new(&delegations_key); let delegations_keys = delegations - .keys_de(storage, None, None, Order::Ascending) + .keys(storage, None, None, Order::Ascending) .scan((), |_, x| x.ok()) .filter(|(_mix, block_time)| *block_time < start_time) .map(|(mix, block_time)| (mix, block_time)) diff --git a/nym-wallet/Cargo.lock b/nym-wallet/Cargo.lock index 3be5369708..0c30eae3c5 100644 --- a/nym-wallet/Cargo.lock +++ b/nym-wallet/Cargo.lock @@ -954,9 +954,9 @@ dependencies = [ [[package]] name = "cw-storage-plus" -version = "0.10.3" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3b8b840947313c1a1cccf056836cd79a60b4526bdcd6582995be37dc97be4ae" +checksum = "7d7ee1963302b0ac2a9d42fe0faec826209c17452bfd36fbfd9d002a88929261" dependencies = [ "cosmwasm-std", "schemars",