33
44use crate :: api:: common:: refund_to;
55use crate :: config:: Config ;
6- use crate :: dto_mapping:: IntoInterfaceType ;
6+ use crate :: dto_mapping:: { IntoContractType , IntoInterfaceType } ;
77use crate :: errors:: { Error , InvalidParameters , InvalidState } ;
88use crate :: state:: ProtocolContractState ;
99use 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