Skip to content

Commit 794cfad

Browse files
authored
Feature/cosmwasm plus storage (#924)
* Upgraded code to be cosmwasm 1.0-beta.2 compatible * Added cw-storage-plus dependency * Experimentally replaced storage for config and layers with cw plus Item * The same for main mixnode storage * Usingn IndexedMap for mixnodes * Split delegations from mixnodes into separate module * MixnodeIndex on Addr directly * Moved namespace values to constants * Outdated comment * [ci skip] Generate TS types * Removed redundant identity index on mixnodes * IndexMap for gateways storage * Moved total delegation into a Map * Compiling contract code after delegation storage upgrades Tests dont compile yet and neither, I would assume, the client code * Delegation type cleanup * Client fixes * Migrated delegation tests + fixed them * Moved Rewarding Status to rewards * Reward pool * Rewarding status migrated * Made clippy happier * Added explorer API to default workspace members * Updated delegation types in explorer-api * Fixed tauri wallet Co-authored-by: jstuczyn <jstuczyn@users.noreply.github.com>
1 parent c9315b5 commit 794cfad

File tree

38 files changed

+1474
-1757
lines changed

38 files changed

+1474
-1757
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ default-members = [
6262
"service-providers/network-requester",
6363
"mixnode",
6464
"validator-api",
65+
"explorer-api",
6566
]
6667

6768
exclude = ["explorer", "contracts", "tokenomics-py"]

common/client-libs/validator-client/src/client.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ use mixnet_contract::ContractSettingsParams;
1010

1111
use crate::{validator_api, ValidatorClientError};
1212
use coconut_interface::{BlindSignRequestBody, BlindedSignatureResponse, VerificationKeyResponse};
13-
use mixnet_contract::{GatewayBond, MixNodeBond};
13+
use mixnet_contract::{Delegation, GatewayBond, MixNodeBond};
1414
#[cfg(feature = "nymd-client")]
1515
use mixnet_contract::{
16-
MixnetContractVersion, MixnodeRewardingStatusResponse, RawDelegationData,
17-
RewardingIntervalResponse,
16+
MixnetContractVersion, MixnodeRewardingStatusResponse, RewardingIntervalResponse,
1817
};
1918
use url::Url;
2019

@@ -312,9 +311,7 @@ impl<C> Client<C> {
312311
Ok(delegations)
313312
}
314313

315-
pub async fn get_all_nymd_mixnode_delegations(
316-
&self,
317-
) -> Result<Vec<mixnet_contract::UnpackedDelegation<RawDelegationData>>, ValidatorClientError>
314+
pub async fn get_all_network_delegations(&self) -> Result<Vec<Delegation>, ValidatorClientError>
318315
where
319316
C: CosmWasmClient + Sync,
320317
{
@@ -323,7 +320,7 @@ impl<C> Client<C> {
323320
loop {
324321
let mut paged_response = self
325322
.nymd
326-
.get_all_mix_delegations_paged(
323+
.get_all_network_delegations_paged(
327324
start_after.take(),
328325
self.mixnode_delegations_page_limit,
329326
)
@@ -340,10 +337,10 @@ impl<C> Client<C> {
340337
Ok(delegations)
341338
}
342339

343-
pub async fn get_all_nymd_reverse_mixnode_delegations(
340+
pub async fn get_all_delegator_delegations(
344341
&self,
345342
delegation_owner: &cosmrs::AccountId,
346-
) -> Result<Vec<mixnet_contract::IdentityKey>, ValidatorClientError>
343+
) -> Result<Vec<Delegation>, ValidatorClientError>
347344
where
348345
C: CosmWasmClient + Sync,
349346
{
@@ -352,13 +349,13 @@ impl<C> Client<C> {
352349
loop {
353350
let mut paged_response = self
354351
.nymd
355-
.get_reverse_mix_delegations_paged(
356-
mixnet_contract::Addr::unchecked(delegation_owner.as_ref()),
352+
.get_delegator_delegations_paged(
353+
delegation_owner.to_string(),
357354
start_after.take(),
358355
self.mixnode_delegations_page_limit,
359356
)
360357
.await?;
361-
delegations.append(&mut paged_response.delegated_nodes);
358+
delegations.append(&mut paged_response.delegations);
362359

363360
if let Some(start_after_res) = paged_response.start_next_after {
364361
start_after = Some(start_after_res)
@@ -370,28 +367,6 @@ impl<C> Client<C> {
370367
Ok(delegations)
371368
}
372369

373-
pub async fn get_all_nymd_mixnode_delegations_of_owner(
374-
&self,
375-
delegation_owner: &cosmrs::AccountId,
376-
) -> Result<Vec<mixnet_contract::Delegation>, ValidatorClientError>
377-
where
378-
C: CosmWasmClient + Sync,
379-
{
380-
let mut delegations = Vec::new();
381-
for node_identity in self
382-
.get_all_nymd_reverse_mixnode_delegations(delegation_owner)
383-
.await?
384-
{
385-
let delegation = self
386-
.nymd
387-
.get_mix_delegation(node_identity, delegation_owner)
388-
.await?;
389-
delegations.push(delegation);
390-
}
391-
392-
Ok(delegations)
393-
}
394-
395370
pub async fn blind_sign(
396371
&self,
397372
request_body: &BlindSignRequestBody,

common/client-libs/validator-client/src/nymd/mod.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use cosmrs::rpc::endpoint::broadcast;
1313
use cosmrs::rpc::{Error as TendermintRpcError, HttpClientUrl};
1414
use cosmwasm_std::{Coin, Uint128};
1515
use mixnet_contract::{
16-
Addr, ContractSettingsParams, Delegation, ExecuteMsg, Gateway, GatewayOwnershipResponse,
17-
IdentityKey, LayerDistribution, MixNode, MixOwnershipResponse, MixnetContractVersion,
18-
MixnodeRewardingStatusResponse, PagedAllDelegationsResponse, PagedGatewayResponse,
19-
PagedMixDelegationsResponse, PagedMixnodeResponse, PagedReverseMixDelegationsResponse,
20-
QueryMsg, RawDelegationData, RewardingIntervalResponse,
16+
ContractSettingsParams, Delegation, ExecuteMsg, Gateway, GatewayOwnershipResponse, IdentityKey,
17+
LayerDistribution, MixNode, MixOwnershipResponse, MixnetContractVersion,
18+
MixnodeRewardingStatusResponse, PagedAllDelegationsResponse, PagedDelegatorDelegationsResponse,
19+
PagedGatewayResponse, PagedMixDelegationsResponse, PagedMixnodeResponse, QueryMsg,
20+
RewardingIntervalResponse,
2121
};
2222
use serde::Serialize;
2323
use std::collections::HashMap;
@@ -313,7 +313,7 @@ impl<C> NymdClient<C> {
313313
C: CosmWasmClient + Sync,
314314
{
315315
let request = QueryMsg::OwnsMixnode {
316-
address: Addr::unchecked(address.as_ref()),
316+
address: address.to_string(),
317317
};
318318
let response: MixOwnershipResponse = self
319319
.client
@@ -328,7 +328,7 @@ impl<C> NymdClient<C> {
328328
C: CosmWasmClient + Sync,
329329
{
330330
let request = QueryMsg::OwnsGateway {
331-
address: Addr::unchecked(address.as_ref()),
331+
address: address.to_string(),
332332
};
333333
let response: GatewayOwnershipResponse = self
334334
.client
@@ -375,14 +375,13 @@ impl<C> NymdClient<C> {
375375
pub async fn get_mix_delegations_paged(
376376
&self,
377377
mix_identity: IdentityKey,
378-
// I really hate mixing cosmwasm and cosmos-sdk types here...
379-
start_after: Option<Addr>,
378+
start_after: Option<String>,
380379
page_limit: Option<u32>,
381380
) -> Result<PagedMixDelegationsResponse, NymdError>
382381
where
383382
C: CosmWasmClient + Sync,
384383
{
385-
let request = QueryMsg::GetMixDelegations {
384+
let request = QueryMsg::GetMixnodeDelegations {
386385
mix_identity: mix_identity.to_owned(),
387386
start_after,
388387
limit: page_limit,
@@ -393,16 +392,15 @@ impl<C> NymdClient<C> {
393392
}
394393

395394
/// Gets list of all mixnode delegations on particular page.
396-
pub async fn get_all_mix_delegations_paged(
395+
pub async fn get_all_network_delegations_paged(
397396
&self,
398-
// I really hate mixing cosmwasm and cosmos-sdk types here...
399-
start_after: Option<Vec<u8>>,
397+
start_after: Option<(IdentityKey, String)>,
400398
page_limit: Option<u32>,
401-
) -> Result<PagedAllDelegationsResponse<RawDelegationData>, NymdError>
399+
) -> Result<PagedAllDelegationsResponse, NymdError>
402400
where
403401
C: CosmWasmClient + Sync,
404402
{
405-
let request = QueryMsg::GetAllMixDelegations {
403+
let request = QueryMsg::GetAllNetworkDelegations {
406404
start_after,
407405
limit: page_limit,
408406
};
@@ -412,17 +410,17 @@ impl<C> NymdClient<C> {
412410
}
413411

414412
/// Gets list of all the mixnodes on which a particular address delegated.
415-
pub async fn get_reverse_mix_delegations_paged(
413+
pub async fn get_delegator_delegations_paged(
416414
&self,
417-
delegation_owner: Addr,
415+
delegator: String,
418416
start_after: Option<IdentityKey>,
419417
page_limit: Option<u32>,
420-
) -> Result<PagedReverseMixDelegationsResponse, NymdError>
418+
) -> Result<PagedDelegatorDelegationsResponse, NymdError>
421419
where
422420
C: CosmWasmClient + Sync,
423421
{
424-
let request = QueryMsg::GetReverseMixDelegations {
425-
delegation_owner,
422+
let request = QueryMsg::GetDelegatorDelegations {
423+
delegator,
426424
start_after,
427425
limit: page_limit,
428426
};
@@ -432,17 +430,17 @@ impl<C> NymdClient<C> {
432430
}
433431

434432
/// Checks value of delegation of given client towards particular mixnode.
435-
pub async fn get_mix_delegation(
433+
pub async fn get_delegation_details(
436434
&self,
437435
mix_identity: IdentityKey,
438436
delegator: &AccountId,
439437
) -> Result<Delegation, NymdError>
440438
where
441439
C: CosmWasmClient + Sync,
442440
{
443-
let request = QueryMsg::GetMixDelegation {
441+
let request = QueryMsg::GetDelegationDetails {
444442
mix_identity,
445-
address: Addr::unchecked(delegator.as_ref()),
443+
delegator: delegator.to_string(),
446444
};
447445
self.client
448446
.query_contract_smart(self.contract_address()?, &request)
Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
// due to code generated by JsonSchema
25
#![allow(clippy::field_reassign_with_default)]
36

@@ -7,58 +10,44 @@ use schemars::JsonSchema;
710
use serde::{Deserialize, Serialize};
811
use std::fmt::Display;
912

10-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
11-
pub struct UnpackedDelegation<T> {
13+
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)]
14+
pub struct Delegation {
1215
pub owner: Addr,
1316
pub node_identity: IdentityKey,
14-
pub delegation_data: T,
17+
pub amount: Coin,
18+
pub block_height: u64,
1519
}
1620

17-
impl<T> UnpackedDelegation<T> {
18-
pub fn new(owner: Addr, node_identity: IdentityKey, delegation_data: T) -> Self {
19-
UnpackedDelegation {
21+
impl Delegation {
22+
pub fn new(owner: Addr, node_identity: IdentityKey, amount: Coin, block_height: u64) -> Self {
23+
Delegation {
2024
owner,
2125
node_identity,
22-
delegation_data,
23-
}
24-
}
25-
}
26-
27-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
28-
pub struct RawDelegationData {
29-
pub amount: Uint128,
30-
pub block_height: u64,
31-
}
32-
33-
impl RawDelegationData {
34-
pub fn new(amount: Uint128, block_height: u64) -> Self {
35-
RawDelegationData {
3626
amount,
3727
block_height,
3828
}
3929
}
40-
}
4130

42-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
43-
pub struct Delegation {
44-
owner: Addr,
45-
amount: Coin,
46-
block_height: u64,
47-
}
31+
// TODO: change that to use .joined_key() and return Vec<u8>
32+
pub fn storage_key(&self) -> (IdentityKey, Addr) {
33+
(self.node_identity(), self.owner())
34+
}
4835

49-
impl Delegation {
50-
pub fn new(owner: Addr, amount: Coin, block_height: u64) -> Self {
51-
Delegation {
52-
owner,
53-
amount,
54-
block_height,
36+
pub fn increment_amount(&mut self, amount: Uint128, at_height: Option<u64>) {
37+
self.amount.amount += amount;
38+
if let Some(at_height) = at_height {
39+
self.block_height = at_height;
5540
}
5641
}
5742

5843
pub fn amount(&self) -> &Coin {
5944
&self.amount
6045
}
6146

47+
pub fn node_identity(&self) -> IdentityKey {
48+
self.node_identity.clone()
49+
}
50+
6251
pub fn owner(&self) -> Addr {
6352
self.owner.clone()
6453
}
@@ -72,65 +61,56 @@ impl Display for Delegation {
7261
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7362
write!(
7463
f,
75-
"{} {} delegated by {} at block {}",
76-
self.amount.amount, self.amount.denom, self.owner, self.block_height
64+
"{} delegated towards {} by {} at block {}",
65+
self.amount, self.node_identity, self.owner, self.block_height
7766
)
7867
}
7968
}
8069

8170
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
8271
pub struct PagedMixDelegationsResponse {
83-
pub node_identity: IdentityKey,
8472
pub delegations: Vec<Delegation>,
85-
pub start_next_after: Option<Addr>,
73+
pub start_next_after: Option<String>,
8674
}
8775

8876
impl PagedMixDelegationsResponse {
89-
pub fn new(
90-
node_identity: IdentityKey,
91-
delegations: Vec<Delegation>,
92-
start_next_after: Option<Addr>,
93-
) -> Self {
77+
pub fn new(delegations: Vec<Delegation>, start_next_after: Option<Addr>) -> Self {
9478
PagedMixDelegationsResponse {
95-
node_identity,
9679
delegations,
97-
start_next_after,
80+
start_next_after: start_next_after.map(|s| s.to_string()),
9881
}
9982
}
10083
}
10184

10285
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
103-
pub struct PagedReverseMixDelegationsResponse {
104-
pub delegation_owner: Addr,
105-
pub delegated_nodes: Vec<IdentityKey>,
86+
pub struct PagedDelegatorDelegationsResponse {
87+
pub delegations: Vec<Delegation>,
10688
pub start_next_after: Option<IdentityKey>,
10789
}
10890

109-
impl PagedReverseMixDelegationsResponse {
110-
pub fn new(
111-
delegation_owner: Addr,
112-
delegated_nodes: Vec<IdentityKey>,
113-
start_next_after: Option<IdentityKey>,
114-
) -> Self {
115-
PagedReverseMixDelegationsResponse {
116-
delegation_owner,
117-
delegated_nodes,
91+
impl PagedDelegatorDelegationsResponse {
92+
pub fn new(delegations: Vec<Delegation>, start_next_after: Option<IdentityKey>) -> Self {
93+
PagedDelegatorDelegationsResponse {
94+
delegations,
11895
start_next_after,
11996
}
12097
}
12198
}
12299

123100
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
124-
pub struct PagedAllDelegationsResponse<T> {
125-
pub delegations: Vec<UnpackedDelegation<T>>,
126-
pub start_next_after: Option<Vec<u8>>,
101+
pub struct PagedAllDelegationsResponse {
102+
pub delegations: Vec<Delegation>,
103+
pub start_next_after: Option<(IdentityKey, String)>,
127104
}
128105

129-
impl<T> PagedAllDelegationsResponse<T> {
130-
pub fn new(delegations: Vec<UnpackedDelegation<T>>, start_next_after: Option<Vec<u8>>) -> Self {
106+
impl PagedAllDelegationsResponse {
107+
pub fn new(
108+
delegations: Vec<Delegation>,
109+
start_next_after: Option<(IdentityKey, Addr)>,
110+
) -> Self {
131111
PagedAllDelegationsResponse {
132112
delegations,
133-
start_next_after,
113+
start_next_after: start_next_after.map(|(id, addr)| (id, addr.to_string())),
134114
}
135115
}
136116
}

common/mixnet-contract/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub const MIXNODE_DELEGATORS_PAGE_LIMIT: usize = 250;
1212

1313
pub use cosmwasm_std::{Addr, Coin};
1414
pub use delegation::{
15-
Delegation, PagedAllDelegationsResponse, PagedMixDelegationsResponse,
16-
PagedReverseMixDelegationsResponse, RawDelegationData, UnpackedDelegation,
15+
Delegation, PagedAllDelegationsResponse, PagedDelegatorDelegationsResponse,
16+
PagedMixDelegationsResponse,
1717
};
1818
pub use gateway::{Gateway, GatewayBond, GatewayOwnershipResponse, PagedGatewayResponse};
1919
pub use mixnode::{Layer, MixNode, MixNodeBond, MixOwnershipResponse, PagedMixnodeResponse};

0 commit comments

Comments
 (0)