@@ -160,7 +160,7 @@ pub struct BallotMeta {
160160}
161161
162162impl BallotMeta {
163- fn saturating_num_choices ( & self ) -> u32 {
163+ pub ( crate ) fn saturating_num_choices ( & self ) -> u32 {
164164 self . motions
165165 . iter ( )
166166 . fold ( 0 , |a, m| a. saturating_add ( m. choices . len ( ) as u32 ) )
@@ -399,37 +399,14 @@ pub mod pallet {
399399 meta : BallotMeta ,
400400 rcv : bool ,
401401 ) -> DispatchResult {
402- // Ensure origin is a permissioned agent, that `ca_id` exists, that its a notice, and the date invariant.
403- let agent = <ExternalAgents < T > >:: ensure_perms ( origin, ca_id. asset_id ) ?;
404- let ca = <CA < T > >:: ensure_ca_exists ( ca_id) ?;
405- ensure ! (
406- matches!( ca. kind, CAKind :: IssuerNotice ) ,
407- Error :: <T >:: CANotNotice
408- ) ;
409- Self :: ensure_range_invariant ( & ca, range) ?;
410-
411- // Ensure CA doesn't have a ballot yet.
412- ensure ! (
413- !TimeRanges :: <T >:: contains_key( ca_id) ,
414- Error :: <T >:: AlreadyExists
415- ) ;
416-
417- // Compute number-of-choices-in-motion cache.
418- let choices = Self :: derive_motion_num_choices ( & meta. motions ) ?;
419- Self :: ensure_meta_lengths_limited ( & meta) ?;
402+ // Ensure that the caller is a permissioned agent
403+ let caller_did = ExternalAgents :: < T > :: ensure_perms ( origin, ca_id. asset_id ) ?;
420404
421- // Charge protocol fee.
422- T :: ProtocolFee :: charge_fee ( ProtocolOp :: CorporateBallotAttachBallot ) ?;
405+ let motion_choices = Self :: validate_ballot_creation_rules ( ca_id, range, & meta) ?;
423406
424- // Commit to storage.
425- MotionNumChoices :: < T > :: insert ( ca_id, choices) ;
426- TimeRanges :: < T > :: insert ( ca_id, range) ;
427- Metas :: < T > :: insert ( ca_id, meta. clone ( ) ) ;
428- RCV :: < T > :: insert ( ca_id, rcv) ;
407+ Self :: unverified_create_ballot ( caller_did, ca_id, motion_choices, range, meta, rcv) ?;
429408
430- // Emit event.
431- Self :: deposit_event ( Event :: Created ( agent, ca_id, range, meta, rcv) ) ;
432- Ok ( ( ) . into ( ) )
409+ Ok ( ( ) )
433410 }
434411
435412 /// Cast `votes` in the ballot attached to the CA identified by `ca_id`.
@@ -719,6 +696,60 @@ pub mod pallet {
719696}
720697
721698impl < T : Config > Pallet < T > {
699+ /// Returns the number-of-choices-in-motion if all rules for creating a ballot are satisfied. Otherwise, an error.
700+ pub ( crate ) fn validate_ballot_creation_rules (
701+ ca_id : CAId ,
702+ ballot_time_range : BallotTimeRange ,
703+ ballot_meta : & BallotMeta ,
704+ ) -> Result < Vec < u16 > , DispatchError > {
705+ let corporate_action = CA :: < T > :: ensure_ca_exists ( ca_id) ?;
706+
707+ ensure ! (
708+ corporate_action. kind == CAKind :: IssuerNotice ,
709+ Error :: <T >:: CANotNotice
710+ ) ;
711+
712+ Self :: ensure_range_invariant ( & corporate_action, ballot_time_range) ?;
713+
714+ // Ensure CA doesn't have a ballot yet
715+ ensure ! (
716+ !TimeRanges :: <T >:: contains_key( ca_id) ,
717+ Error :: <T >:: AlreadyExists
718+ ) ;
719+
720+ let motion_choices = Self :: derive_motion_num_choices ( & ballot_meta. motions ) ?;
721+ Self :: ensure_meta_lengths_limited ( ballot_meta) ?;
722+
723+ Ok ( motion_choices)
724+ }
725+
726+ /// Charges the protocol fee and creates a ballot.
727+ pub ( crate ) fn unverified_create_ballot (
728+ caller_id : IdentityId ,
729+ ca_id : CAId ,
730+ motion_choices : Vec < u16 > ,
731+ ballot_time_range : BallotTimeRange ,
732+ ballot_meta : BallotMeta ,
733+ rcv : bool ,
734+ ) -> DispatchResult {
735+ T :: ProtocolFee :: charge_fee ( ProtocolOp :: CorporateBallotAttachBallot ) ?;
736+
737+ MotionNumChoices :: < T > :: insert ( ca_id, motion_choices) ;
738+ TimeRanges :: < T > :: insert ( ca_id, ballot_time_range) ;
739+ Metas :: < T > :: insert ( ca_id, ballot_meta. clone ( ) ) ;
740+ RCV :: < T > :: insert ( ca_id, rcv) ;
741+
742+ Self :: deposit_event ( Event :: Created (
743+ caller_id,
744+ ca_id,
745+ ballot_time_range,
746+ ballot_meta,
747+ rcv,
748+ ) ) ;
749+
750+ Ok ( ( ) )
751+ }
752+
722753 /// Ensure the ballot hasn't started and remove it.
723754 pub ( crate ) fn remove_ballot_base (
724755 agent : EventDid ,
0 commit comments