Skip to content

Commit 2b81e4d

Browse files
committed
chore: use UpdateId and Keyset from dtos
1 parent 949588c commit 2b81e4d

25 files changed

Lines changed: 316 additions & 266 deletions

crates/contract/src/api/attestation.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ mod tests {
10281028
let mut contract = MpcContract::init_running(
10291029
domains.clone(),
10301030
1,
1031-
keyset.clone(),
1031+
(&keyset).into_dto_type(),
10321032
(&parameters).into_dto_type(),
10331033
None,
10341034
)
@@ -1149,9 +1149,14 @@ mod tests {
11491149
attempt: AttemptId::new(),
11501150
}],
11511151
);
1152-
let mut contract =
1153-
MpcContract::init_running(domains, 1, keyset, (&parameters).into_dto_type(), None)
1154-
.unwrap();
1152+
let mut contract = MpcContract::init_running(
1153+
domains,
1154+
1,
1155+
(&keyset).into_dto_type(),
1156+
(&parameters).into_dto_type(),
1157+
None,
1158+
)
1159+
.unwrap();
11551160

11561161
// Expire the last participant's attestation so a kickout drops the set to 4.
11571162
let participant_list: Vec<_> = participants.participants().to_vec();

crates/contract/src/api/foreign_chain_support.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ mod tests {
946946
let mut env = Environment::new(None, Some(operator4.clone()), None);
947947
env.set_pk(new_signer_near_pk);
948948
contract
949-
.conclude_node_migration(&keyset)
949+
.conclude_node_migration((&keyset).into_dto_type())
950950
.expect("migration should succeed");
951951

952952
// Then: all 4 chains available — no manual recompute needed.
@@ -1214,9 +1214,14 @@ mod tests {
12141214
let parameters =
12151215
GovernanceThresholdParameters::new(gen_participants(4), GovernanceThreshold::new(3))
12161216
.unwrap();
1217-
let mut contract =
1218-
MpcContract::init_running(domains, 1, keyset, (&parameters).into_dto_type(), None)
1219-
.unwrap();
1217+
let mut contract = MpcContract::init_running(
1218+
domains,
1219+
1,
1220+
(&keyset).into_dto_type(),
1221+
(&parameters).into_dto_type(),
1222+
None,
1223+
)
1224+
.unwrap();
12201225
let participants = participant_account_ids(&contract);
12211226
whitelist_chain(&mut contract, dtos::ForeignChain::Bitcoin);
12221227

@@ -1276,9 +1281,14 @@ mod tests {
12761281
let parameters =
12771282
GovernanceThresholdParameters::new(gen_participants(4), GovernanceThreshold::new(3))
12781283
.unwrap();
1279-
let mut contract =
1280-
MpcContract::init_running(domains, 2, keyset, (&parameters).into_dto_type(), None)
1281-
.unwrap();
1284+
let mut contract = MpcContract::init_running(
1285+
domains,
1286+
2,
1287+
(&keyset).into_dto_type(),
1288+
(&parameters).into_dto_type(),
1289+
None,
1290+
)
1291+
.unwrap();
12821292
let participants = participant_account_ids(&contract);
12831293
whitelist_chain(&mut contract, dtos::ForeignChain::Bitcoin);
12841294

crates/contract/src/api/governance.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,14 @@ mod tests {
292292
attempt: AttemptId::new(),
293293
}],
294294
);
295-
let contract =
296-
MpcContract::init_running(domains, 1, keyset, (&parameters).into_dto_type(), None)
297-
.unwrap();
295+
let contract = MpcContract::init_running(
296+
domains,
297+
1,
298+
(&keyset).into_dto_type(),
299+
(&parameters).into_dto_type(),
300+
None,
301+
)
302+
.unwrap();
298303
(contract, participants, first_participant_id, domain_id)
299304
}
300305

crates/contract/src/api/lifecycle.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ impl MpcContract {
8686
pub fn init_running(
8787
domains: Vec<DomainConfig>,
8888
next_domain_id: u64,
89-
keyset: Keyset,
89+
keyset: dtos::Keyset,
9090
parameters: dtos::GovernanceThresholdParameters,
9191
init_config: Option<dtos::InitConfig>,
9292
) -> Result<Self, Error> {
93+
let keyset: Keyset = keyset.try_into_contract_type()?;
9394
let parameters: GovernanceThresholdParameters = parameters.try_into_contract_type()?;
9495
// Log participant count and hash - full parameters exceed NEAR's 16KB log limit at ~100 participants
9596
let params_hash = env::sha256_array(borsh::to_vec(&parameters).unwrap());

crates/contract/src/api/node_migration.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! migration to new hardware, and updating a participant's URL.
33
44
use crate::api::common::require_deposit;
5-
use crate::dto_mapping::IntoContractType;
5+
use crate::dto_mapping::{IntoContractType, TryIntoContractType};
66
use crate::errors::{self, Error, InvalidParameters, InvalidState};
77
use crate::primitives::key_state::Keyset;
88
use crate::primitives::participants::ParticipantInfo;
@@ -174,7 +174,7 @@ impl MpcContract {
174174
/// - [`NodeMigrationError::AccountPublicKeyMismatch`](crate::errors::NodeMigrationError::AccountPublicKeyMismatch): if caller’s public key does not match the expected destination node
175175
/// - [`InvalidParameters::InvalidTeeRemoteAttestation`]: if destination node’s TEE quote is invalid
176176
#[handle_result]
177-
pub fn conclude_node_migration(&mut self, keyset: &Keyset) -> Result<(), Error> {
177+
pub fn conclude_node_migration(&mut self, keyset: dtos::Keyset) -> Result<(), Error> {
178178
let account_id = Self::assert_caller_is_signer();
179179
let signer_pk = env::signer_account_pk();
180180
log!(
@@ -194,10 +194,12 @@ impl MpcContract {
194194
.into());
195195
}
196196

197+
// Converted after the participant/state validation so those errors take precedence.
198+
let keyset: Keyset = keyset.try_into_contract_type()?;
197199
let expected_keyset = &running_state.keyset;
198-
if expected_keyset != keyset {
200+
if *expected_keyset != keyset {
199201
return Err(errors::NodeMigrationError::KeysetMismatch {
200-
found: keyset.clone(),
202+
found: keyset,
201203
expected: expected_keyset.clone(),
202204
}
203205
.into());
@@ -310,6 +312,7 @@ pub const MINIMUM_NODE_MANAGEMENT_DEPOSIT: NearToken =
310312
mod tests {
311313
use super::*;
312314
use crate::api::test_utils::NUM_DOMAINS;
315+
use crate::dto_mapping::IntoInterfaceType;
313316
use crate::errors::NodeMigrationError;
314317
use crate::primitives::participants::{ParticipantId, Participants};
315318
use crate::primitives::test_utils::{
@@ -929,7 +932,7 @@ mod tests {
929932
test_env.set_signer(&self.signer_account_id);
930933
test_env.set_pk(self.signer_account_pk.clone());
931934

932-
let res = contract.conclude_node_migration(keyset);
935+
let res = contract.conclude_node_migration(keyset.into_dto_type());
933936

934937
if let Some(check) = &self.expected_error_check {
935938
let err = res.unwrap_err();

crates/contract/src/api/test_utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ pub(crate) fn basic_setup_with_protocol(
139139
let parameters =
140140
GovernanceThresholdParameters::new(gen_participants(4), GovernanceThreshold::new(3))
141141
.unwrap();
142-
let contract =
143-
MpcContract::init_running(domains, 1, keyset, (&parameters).into_dto_type(), None).unwrap();
142+
let contract = MpcContract::init_running(
143+
domains,
144+
1,
145+
(&keyset).into_dto_type(),
146+
(&parameters).into_dto_type(),
147+
None,
148+
)
149+
.unwrap();
144150
(context, contract, sk)
145151
}
146152

crates/contract/src/api/update.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::api::common::refund_to;
55
use crate::config::Config;
6-
use crate::dto_mapping::IntoInterfaceType;
6+
use crate::dto_mapping::{IntoContractType, IntoInterfaceType};
77
use crate::errors::{Error, InvalidParameters, InvalidState};
88
use crate::state::ProtocolContractState;
99
use crate::update::{ProposedUpdates, Update, UpdateId};
@@ -19,7 +19,7 @@ impl MpcContract {
1919
pub fn propose_update(
2020
&mut self,
2121
#[serializer(borsh)] args: ProposeUpdateArgs,
22-
) -> Result<UpdateId, Error> {
22+
) -> Result<dtos::UpdateId, Error> {
2323
// Only voters can propose updates:
2424
let proposer = self.voter_or_panic();
2525
let payload_bytes =
@@ -56,22 +56,23 @@ impl MpcContract {
5656
refund_to(&proposer, diff);
5757
}
5858

59-
Ok(id)
59+
Ok(id.into_dto_type())
6060
}
6161

62-
/// Vote for a proposed update given the [`UpdateId`] of the update.
62+
/// Vote for a proposed update, given the id returned by [`Self::propose_update`].
6363
///
6464
/// Returns `Ok(true)` if the amount of voters surpassed the threshold and the update was
6565
/// executed. Returns `Ok(false)` if the amount of voters did not surpass the threshold.
6666
/// Returns [`Error`] if the update was not found or if the voter is not a participant
6767
/// in the protocol.
6868
#[handle_result]
69-
pub fn vote_update(&mut self, id: UpdateId) -> Result<bool, Error> {
69+
pub fn vote_update(&mut self, id: dtos::UpdateId) -> Result<bool, Error> {
7070
log!(
7171
"vote_update: signer={}, id={:?}",
7272
env::signer_account_id(),
7373
id,
7474
);
75+
let id: UpdateId = id.into_contract_type();
7576

7677
let ProtocolContractState::Running(running_state) = &self.protocol_state else {
7778
env::panic_str("protocol must be in running state");
@@ -195,10 +196,10 @@ mod tests {
195196
fn propose_and_vote(
196197
contract: &mut MpcContract,
197198
update: Update,
198-
expected_update_id: u64,
199+
expected_update_id: UpdateId,
199200
) -> Vec<dtos::AccountId> {
200201
let update_id = contract.proposed_updates.propose(update.clone());
201-
assert_eq!(update_id.0, expected_update_id);
202+
assert_eq!(update_id, expected_update_id);
202203
// generate two accounts for voting
203204
let account_id_0 = gen_account_id();
204205
let account_id_1 = gen_account_id();
@@ -220,14 +221,14 @@ mod tests {
220221
/// Used to convert BTreeMap-based [`ProposedUpdates`] into a sortable vector format for assertions.
221222
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
222223
struct TestUpdate {
223-
update_id: u64,
224+
update_id: dtos::UpdateId,
224225
update_hash: dtos::UpdateHash,
225226
votes: Vec<dtos::AccountId>,
226227
}
227228

228229
impl TestUpdate {
229230
fn from_proposed_updates(
230-
update_id: u64,
231+
update_id: dtos::UpdateId,
231232
update_hash: dtos::UpdateHash,
232233
proposed_updates: &dtos::ProposedUpdates,
233234
) -> Self {
@@ -245,14 +246,17 @@ mod tests {
245246
}
246247
}
247248

248-
fn propose_and_vote_code(expected_update_id: u64, contract: &mut MpcContract) -> TestUpdate {
249+
fn propose_and_vote_code(
250+
expected_update_id: UpdateId,
251+
contract: &mut MpcContract,
252+
) -> TestUpdate {
249253
let code: [u8; 1000] = std::array::from_fn(|_| rand::random());
250254
let hash = Sha256::digest(code);
251255
let update = Update::Contract(code.into());
252256
let expected_update_hash = dtos::UpdateHash::Code(hash.into());
253257
let expected_votes = propose_and_vote(contract, update, expected_update_id);
254258
TestUpdate {
255-
update_id: expected_update_id,
259+
update_id: expected_update_id.into_dto_type(),
256260
update_hash: expected_update_hash,
257261
votes: expected_votes,
258262
}
@@ -290,18 +294,18 @@ mod tests {
290294
assert_eq!(empty_result.updates, BTreeMap::new());
291295

292296
// Propose and vote for code update
293-
let code_update_id = 0;
297+
let code_update_id = UpdateId(0);
294298
let mut code_update = propose_and_vote_code(code_update_id, &mut contract);
295299

296300
// Propose and vote for config update
297301
let mut config_update = {
298302
let update_config = dummy_config(1);
299303
let config_hash = Sha256::digest(serde_json::to_vec(&update_config).unwrap());
300304
let config_update_obj = Update::Config(update_config.clone());
301-
let config_update_id = 1;
305+
let config_update_id = UpdateId(1);
302306
let config_votes = propose_and_vote(&mut contract, config_update_obj, config_update_id);
303307
TestUpdate {
304-
update_id: config_update_id,
308+
update_id: config_update_id.into_dto_type(),
305309
update_hash: dtos::UpdateHash::Config(config_hash.into()),
306310
votes: config_votes,
307311
}
@@ -364,9 +368,8 @@ mod tests {
364368
let mut contract = MpcContract::new_from_protocol_state(protocol_contract_state);
365369

366370
// Propose and vote for code update
367-
let update_id_u64 = 0;
368-
let test_update = propose_and_vote_code(update_id_u64, &mut contract);
369-
let update_id = UpdateId::from(update_id_u64);
371+
let update_id = UpdateId(0);
372+
let test_update = propose_and_vote_code(update_id, &mut contract);
370373

371374
for (account_id, _, _) in participants.participants() {
372375
contract
@@ -376,7 +379,10 @@ mod tests {
376379
let proposed_updates = contract.proposed_updates();
377380
assert_eq!(proposed_updates.updates.len(), 1);
378381
assert_eq!(
379-
*proposed_updates.updates.get(&update_id.0).unwrap(),
382+
*proposed_updates
383+
.updates
384+
.get(&update_id.into_dto_type())
385+
.unwrap(),
380386
test_update.update_hash
381387
);
382388

@@ -386,7 +392,7 @@ mod tests {
386392
let actual_voters: Vec<_> = proposed_updates
387393
.votes
388394
.iter()
389-
.filter(|&(_, &uid)| uid == update_id.0)
395+
.filter(|&(_, &uid)| uid == update_id.into_dto_type())
390396
.map(|(voter, _)| voter.clone())
391397
.collect();
392398
assert_eq!(actual_voters.len(), expected_voters.len());
@@ -411,7 +417,7 @@ mod tests {
411417
let actual_voters: Vec<_> = res
412418
.votes
413419
.iter()
414-
.filter(|&(_, &uid)| uid == update_id.0)
420+
.filter(|&(_, &uid)| uid == update_id.into_dto_type())
415421
.map(|(voter, _)| voter.clone())
416422
.collect();
417423
assert_eq!(actual_voters.len(), test_update.votes.len());
@@ -429,7 +435,7 @@ mod tests {
429435
let mut contract = MpcContract::new_from_protocol_state(protocol_contract_state);
430436

431437
// Propose and vote for code update
432-
let update_id = 0;
438+
let update_id = UpdateId(0);
433439
let test_update = propose_and_vote_code(update_id, &mut contract);
434440

435441
let mut rng = rand::rngs::StdRng::seed_from_u64(42);
@@ -521,7 +527,7 @@ mod tests {
521527
.build()
522528
);
523529
// then: threshold not met (need 2 valid votes, have only 1)
524-
assert!(!contract.vote_update(update_id).unwrap());
530+
assert!(!contract.vote_update(update_id.into_dto_type()).unwrap());
525531

526532
// given: a 2nd participant vote is added
527533
contract
@@ -536,7 +542,7 @@ mod tests {
536542
.build()
537543
);
538544
// then: threshold met (have 2 valid votes, need 2)
539-
assert!(contract.vote_update(update_id).unwrap());
545+
assert!(contract.vote_update(update_id.into_dto_type()).unwrap());
540546
}
541547

542548
#[test]
@@ -608,9 +614,8 @@ mod tests {
608614
MpcContract::new_from_protocol_state(ProtocolContractState::Running(running_state));
609615

610616
// propose_and_vote_code adds 2 non-participant votes.
611-
let update_id_u64 = 0;
612-
let _ = propose_and_vote_code(update_id_u64, &mut contract);
613-
let update_id: UpdateId = update_id_u64.into();
617+
let update_id = UpdateId(0);
618+
let _ = propose_and_vote_code(update_id, &mut contract);
614619

615620
// Add votes from 2 current participants.
616621
let participants = participants.participants();
@@ -654,9 +659,8 @@ mod tests {
654659
let mut contract =
655660
MpcContract::new_from_protocol_state(ProtocolContractState::Running(running_state));
656661

657-
let update_id_u64 = 0;
658-
let test_update = propose_and_vote_code(update_id_u64, &mut contract);
659-
let update_id: UpdateId = update_id_u64.into();
662+
let update_id = UpdateId(0);
663+
let test_update = propose_and_vote_code(update_id, &mut contract);
660664
let non_participants: HashSet<AccountId> = test_update.votes.iter().cloned().collect();
661665

662666
let participants = participants.participants();

0 commit comments

Comments
 (0)