Skip to content

Commit fc98c49

Browse files
authored
feat: DKG contract method for updating announce address (#6050)
* added new dkg execute methods for ownership transfer and announce address update * cherry-pick TestableNymContract for the dkg contract from #5091 * tests * schema fixes * removed old queued migrations
1 parent 92a88cd commit fc98c49

File tree

31 files changed

+1275
-158
lines changed

31 files changed

+1275
-158
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/client-libs/validator-client/src/nyxd/contract_traits/dkg_signing_client.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ pub trait DkgSigningClient {
136136
self.execute_dkg_contract(fee, req, "trigger DKG resharing".to_string(), vec![])
137137
.await
138138
}
139+
140+
async fn transfer_ownership(
141+
&self,
142+
transfer_to: String,
143+
fee: Option<Fee>,
144+
) -> Result<ExecuteResult, NyxdError> {
145+
let req = DkgExecuteMsg::TransferOwnership { transfer_to };
146+
147+
self.execute_dkg_contract(fee, req, "".to_string(), vec![])
148+
.await
149+
}
150+
async fn update_announce_address(
151+
&self,
152+
new_address: String,
153+
fee: Option<Fee>,
154+
) -> Result<ExecuteResult, NyxdError> {
155+
let req = DkgExecuteMsg::UpdateAnnounceAddress { new_address };
156+
157+
self.execute_dkg_contract(fee, req, "".to_string(), vec![])
158+
.await
159+
}
139160
}
140161

141162
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
@@ -168,6 +189,7 @@ where
168189
mod tests {
169190
use super::*;
170191
use crate::nyxd::contract_traits::tests::IgnoreValue;
192+
use nym_coconut_dkg_common::msg::ExecuteMsg;
171193

172194
// it's enough that this compiles and clippy is happy about it
173195
#[allow(dead_code)]
@@ -210,6 +232,12 @@ mod tests {
210232
DkgExecuteMsg::AdvanceEpochState {} => client.advance_dkg_epoch_state(None).ignore(),
211233
DkgExecuteMsg::TriggerReset {} => client.trigger_dkg_reset(None).ignore(),
212234
DkgExecuteMsg::TriggerResharing {} => client.trigger_dkg_resharing(None).ignore(),
235+
ExecuteMsg::TransferOwnership { transfer_to } => {
236+
client.transfer_ownership(transfer_to, None).ignore()
237+
}
238+
ExecuteMsg::UpdateAnnounceAddress { new_address } => {
239+
client.update_announce_address(new_address, None).ignore()
240+
}
213241
};
214242
}
215243
}

common/cosmwasm-smart-contracts/coconut-dkg/src/dealer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ use crate::types::{EncodedBTEPublicKeyWithProof, NodeIndex};
55
use cosmwasm_schema::cw_serde;
66
use cosmwasm_std::Addr;
77

8+
pub type BlockHeight = u64;
9+
pub type TransactionIndex = u32;
10+
11+
#[cw_serde]
12+
pub struct OwnershipTransfer {
13+
pub node_index: NodeIndex,
14+
pub from: Addr,
15+
pub to: Addr,
16+
}
17+
818
#[cw_serde]
919
pub struct DealerDetails {
1020
pub address: Addr,

common/cosmwasm-smart-contracts/coconut-dkg/src/msg.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ pub enum ExecuteMsg {
7373
TriggerReset {},
7474

7575
TriggerResharing {},
76+
77+
/// Transfers ownership of the epoch dealer to another address.
78+
/// This assumes off-chain hand-over of keys
79+
TransferOwnership {
80+
transfer_to: String,
81+
},
82+
83+
/// Update announce address of this signer
84+
UpdateAnnounceAddress {
85+
new_address: String,
86+
},
7687
}
7788

7889
#[cw_serde]

common/cosmwasm-smart-contracts/contracts-common-testing/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ rand_chacha = { workspace = true }
2020
rand = { workspace = true }
2121
cw-multi-test = { workspace = true }
2222

23+
nym-contracts-common = { path = "../contracts-common" }
24+
2325
[lints]
2426
workspace = true

common/cosmwasm-smart-contracts/contracts-common-testing/src/helpers.rs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,98 @@
33

44
use cosmwasm_std::testing::{message_info, MockApi, MockQuerier, MockStorage};
55
use cosmwasm_std::{
6-
coins, Addr, BankMsg, CosmosMsg, Empty, Env, MemoryStorage, MessageInfo, Order, OwnedDeps,
7-
Response, StdResult, Storage,
6+
coins, Addr, BankMsg, CosmosMsg, Decimal, Empty, Env, MemoryStorage, MessageInfo, Order,
7+
OwnedDeps, Response, StdResult, Storage,
88
};
99
use cw_storage_plus::{KeyDeserialize, Map, Prefix, PrimaryKey};
10+
use nym_contracts_common::events::may_find_attribute;
1011
use rand::{RngCore, SeedableRng};
1112
use rand_chacha::ChaCha20Rng;
1213
use serde::de::DeserializeOwned;
1314
use serde::Serialize;
15+
use std::fmt::Debug;
16+
use std::str::FromStr;
1417

1518
pub const TEST_DENOM: &str = "unym";
1619
pub const TEST_PREFIX: &str = "n";
1720

21+
pub trait FindAttribute {
22+
fn attribute<E, S>(&self, event_type: E, attribute: &str) -> String
23+
where
24+
E: Into<Option<S>>,
25+
S: Into<String>;
26+
27+
fn any_attribute(&self, attribute: &str) -> String {
28+
self.attribute::<_, String>(None, attribute)
29+
}
30+
31+
fn any_parsed_attribute<T>(&self, attribute: &str) -> T
32+
where
33+
T: FromStr,
34+
<T as FromStr>::Err: Debug,
35+
{
36+
self.parsed_attribute::<_, String, T>(None, attribute)
37+
}
38+
39+
fn parsed_attribute<E, S, T>(&self, event_type: E, attribute: &str) -> T
40+
where
41+
E: Into<Option<S>>,
42+
S: Into<String>,
43+
T: FromStr,
44+
<T as FromStr>::Err: Debug;
45+
46+
fn decimal<E, S>(&self, event_type: E, attribute: &str) -> Decimal
47+
where
48+
E: Into<Option<S>>,
49+
S: Into<String>,
50+
{
51+
self.parsed_attribute(event_type, attribute)
52+
}
53+
}
54+
55+
#[track_caller]
56+
pub fn find_attribute<S: Into<String>>(
57+
event_type: Option<S>,
58+
attribute: &str,
59+
response: &Response,
60+
) -> String {
61+
let event_type = event_type.map(Into::into);
62+
for event in &response.events {
63+
if let Some(typ) = &event_type {
64+
if &event.ty != typ {
65+
continue;
66+
}
67+
}
68+
if let Some(attr) = may_find_attribute(event, attribute) {
69+
return attr;
70+
}
71+
}
72+
// this is only used in tests so panic here is fine
73+
panic!("did not find the attribute")
74+
}
75+
76+
impl FindAttribute for Response {
77+
fn attribute<E, S>(&self, event_type: E, attribute: &str) -> String
78+
where
79+
E: Into<Option<S>>,
80+
S: Into<String>,
81+
{
82+
find_attribute(event_type.into(), attribute, self)
83+
}
84+
85+
fn parsed_attribute<E, S, T>(&self, event_type: E, attribute: &str) -> T
86+
where
87+
E: Into<Option<S>>,
88+
S: Into<String>,
89+
T: FromStr,
90+
<T as FromStr>::Err: Debug,
91+
{
92+
find_attribute(event_type.into(), attribute, self)
93+
.parse()
94+
.unwrap()
95+
}
96+
}
97+
1898
pub fn mock_api() -> MockApi {
1999
MockApi::default().with_prefix(TEST_PREFIX)
20100
}

common/cosmwasm-smart-contracts/contracts-common-testing/src/tester/basic_traits.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use crate::{ContractTester, TestableNymContract};
55
use cosmwasm_std::testing::{message_info, mock_env};
66
use cosmwasm_std::{
7-
from_json, Addr, Coin, ContractInfo, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
8-
Storage, Timestamp,
7+
from_json, Addr, BlockInfo, Coin, ContractInfo, Deps, DepsMut, Env, MessageInfo, Response,
8+
StdResult, Storage, Timestamp,
99
};
1010
use cw_multi_test::{next_block, AppResponse, Executor};
1111
use serde::de::DeserializeOwned;
@@ -62,6 +62,8 @@ pub trait ContractOpts {
6262
coins: &[Coin],
6363
message: Self::ExecuteMsg,
6464
) -> Result<Response, Self::ContractError>;
65+
66+
fn unchecked_contract_address<D: TestableNymContract>(&self) -> Addr;
6567
}
6668

6769
impl<C> ContractOpts for ContractTester<C>
@@ -130,14 +132,47 @@ where
130132

131133
C::execute()(self.deps_mut(), env, info, message)
132134
}
135+
136+
fn unchecked_contract_address<D: TestableNymContract>(&self) -> Addr {
137+
self.unchecked_contract_address::<D>()
138+
}
133139
}
134140

135141
pub trait ChainOpts: ContractOpts {
136142
fn set_contract_balance(&mut self, balance: Coin);
137143

138-
fn next_block(&mut self);
144+
fn update_block<F: Fn(&mut BlockInfo)>(&mut self, action: F);
145+
fn set_to_epoch(&mut self) {
146+
self.set_block_time(Timestamp::from_seconds(0))
147+
}
148+
149+
fn set_to_genesis(&mut self) {
150+
self.update_block(|block| {
151+
block.height = 1;
152+
})
153+
}
154+
155+
fn next_block(&mut self) {
156+
self.update_block(next_block)
157+
}
158+
159+
fn advance_day_of_blocks(&mut self) {
160+
self.update_block(|block| {
161+
block.time = block.time.plus_seconds(24 * 60 * 60);
162+
block.height += 17280;
163+
})
164+
}
139165

140-
fn set_block_time(&mut self, time: Timestamp);
166+
fn advance_time_by(&mut self, delta_secs: u64) {
167+
self.update_block(|block| {
168+
block.time = block.time.plus_seconds(delta_secs);
169+
block.height += 1
170+
})
171+
}
172+
173+
fn set_block_time(&mut self, time: Timestamp) {
174+
self.update_block(|b| b.time = time)
175+
}
141176

142177
fn execute_msg(
143178
&mut self,
@@ -186,12 +221,9 @@ where
186221
)
187222
.unwrap();
188223
}
189-
fn next_block(&mut self) {
190-
self.app.update_block(next_block)
191-
}
192224

193-
fn set_block_time(&mut self, time: Timestamp) {
194-
self.app.update_block(|b| b.time = time)
225+
fn update_block<F: Fn(&mut BlockInfo)>(&mut self, action: F) {
226+
self.app.update_block(action)
195227
}
196228

197229
fn execute_msg(

common/cosmwasm-smart-contracts/contracts-common-testing/src/tester/extensions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ use cosmwasm_std::{
1111
};
1212
use cw_multi_test::Executor;
1313
use cw_storage_plus::{Key, Path, PrimaryKey};
14-
use rand::RngCore;
1514
use rand_chacha::ChaCha20Rng;
1615
use serde::de::DeserializeOwned;
1716
use serde::Serialize;
1817
use std::any::type_name;
1918
use std::ops::Deref;
2019

20+
pub use rand::prelude::*;
21+
2122
pub trait StorageReader {
2223
fn common_key(&self, key: CommonStorageKeys) -> Option<&[u8]>;
2324

0 commit comments

Comments
 (0)