1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#![allow(clippy::string_lit_as_bytes)]
#![allow(clippy::redundant_closure_call)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
#![cfg_attr(not(feature = "std"), no_std)]
//! Simple module for collecting signatures from organizational share groups
//! - this is a simple vote machine, similar to `vote-yesno` but without any
//! share-weighted threshold or counting complexity

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure}; // storage::IterableStorageDoubleMap
use frame_system::{self as system, ensure_signed};
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::prelude::*;
use util::{
    petition::{PetitionOutcome, PetitionSignature, PetitionState, PetitionView},
    traits::{
        Apply, Approved, ChainSudoPermissions, ChangeGroupMembership, GenerateUniqueID,
        GetFlatShareGroup, GetGroupSize, GetVoteOutcome, GroupMembership, IDIsAvailable,
        OpenPetition, OrganizationSupervisorPermissions, Rejected, SignPetition,
        SubGroupSupervisorPermissions, UpdatePetition, UpdatePetitionTerms,
    }, // RequestChanges
    uuid::UUID2,
};

/// Ipfs reference just is a type alias over a vector of bytes
pub type IpfsReference = Vec<u8>;

/// The organization identifier
pub type OrgId = u32;

/// The share group identifier
pub type ShareId = u32;

/// The petition identifier
pub type PetitionId = u32;

pub trait Trait: frame_system::Trait {
    type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

    /// Just used for permissions in this module
    type OrgData: GroupMembership<Self::AccountId>
        + ChainSudoPermissions<Self::AccountId>
        + OrganizationSupervisorPermissions<u32, Self::AccountId>;

    /// An instance of `SharesMembership`
    type ShareData: GetGroupSize<GroupId = UUID2>
        + GroupMembership<Self::AccountId, GroupId = UUID2>
        + SubGroupSupervisorPermissions<u32, u32, Self::AccountId>
        + ChangeGroupMembership<Self::AccountId>
        + GetFlatShareGroup<Self::AccountId>;
}

decl_event!(
    pub enum Event<T>
    where
        <T as frame_system::Trait>::AccountId,
    {
        /// Opener's account, Organization, Share Group, Petition Identifier, bool == IF_VETO_POWER_ENABLED
        NewPetitionStarted(AccountId, OrgId, ShareId, PetitionId),
        UserSignedPetition(PetitionId, AccountId, PetitionView<IpfsReference>, PetitionOutcome),
        /// Opener's account, Petition info, New Petition version
        PetitionUpdated(AccountId, OrgId, ShareId, PetitionId, u32),
    }
);

decl_error! {
    pub enum Error for Module<T: Trait> {
        /// Local Auths
        NotAuthorizedToCreatePetition,
        NotAuthorizedToUpdatePetition,
        /// The total electorate is less than the required support or required vetos to freeze
        PetitionDoesNotSatisfyCreationConstraints,
        CantSignBecauseAccountNotFoundInShareGroup,
        CannotSignIfPetitionStateDNE,
        CannotUpdateIfPetitionStateDNE,
        CannotGetStatusIfPetitionStateDNE,
    }
}

decl_storage! {
    trait Store for Module<T: Trait> as VotePetition {
        /// For efficient UID generation inside this module
        PetitionIdNonce get(fn petition_id_nonce): PetitionId;
        /// The current state of a petition
        pub PetitionStates get(fn petition_states): map
            hasher(opaque_blake2_256) PetitionId => Option<PetitionState<IpfsReference, T::BlockNumber>>;

        /// The signatures of participants in the petition
        pub SignatureLogger get(fn signature_logger): double_map
            hasher(opaque_blake2_256) PetitionId,
            hasher(opaque_blake2_256) T::AccountId => Option<PetitionSignature<T::AccountId, IpfsReference>>;
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        type Error = Error<T>;
        fn deposit_event() = default;

        #[weight = 0]
        pub fn create_petition(
            origin,
            organization: OrgId,
            share_id: ShareId,
            topic: Option<IpfsReference>,
            required_support: u32,
            required_against: Option<u32>,
            ends: Option<T::BlockNumber>,
        ) -> DispatchResult {
            let caller = ensure_signed(origin)?;
            let authentication: bool = Self::check_if_sudo_account(&caller)
                || Self::check_if_organization_supervisor_account(organization, &caller)
                || Self::check_if_organization_share_supervisor_account(organization, share_id, &caller);
            ensure!(authentication, Error::<T>::NotAuthorizedToCreatePetition);
            // create and open the petition
            let petition_id = Self::open_petition(
                organization,
                share_id,
                topic,
                required_support,
                required_against,
                ends,
            )?;
            Self::deposit_event(RawEvent::NewPetitionStarted(caller, organization, share_id, petition_id));
            Ok(())
        }
        #[weight = 0]
        pub fn direct_sign_petition(
            origin,
            petition_id: PetitionId,
            view: PetitionView<IpfsReference>,
        ) -> DispatchResult {
            let signer = ensure_signed(origin)?;
            let outcome = Self::sign_petition(
                petition_id,
                signer.clone(),
                view.clone(),
            )?;
            Self::deposit_event(RawEvent::UserSignedPetition(petition_id, signer, view, outcome));
            Ok(())
        }
        #[weight = 0]
        pub fn supervisor_update_petition(
            origin,
            organization: OrgId,
            share_id: ShareId,
            petition_id: PetitionId,
            new_topic: IpfsReference,
        ) -> DispatchResult {
            let caller = ensure_signed(origin)?;
            let authentication: bool = Self::check_if_sudo_account(&caller)
                || Self::check_if_organization_supervisor_account(organization, &caller)
                || Self::check_if_organization_share_supervisor_account(organization, share_id, &caller);
            ensure!(authentication, Error::<T>::NotAuthorizedToUpdatePetition);

            // TODO: get the outcome and store it, frozen for the old version

            Self::update_petition(petition_id, new_topic)?;
            // Self::deposit_event(RawEvent::PetitionUpdated(caller, organization, share_id, petition_id));
            Ok(())
        }
    }
}

impl<T: Trait> Module<T> {
    // $$$ AUTH CHECKS $$$
    fn check_if_sudo_account(who: &T::AccountId) -> bool {
        <<T as Trait>::OrgData as ChainSudoPermissions<<T as frame_system::Trait>::AccountId>>::is_sudo_key(who)
    }
    fn check_if_organization_supervisor_account(organization: OrgId, who: &T::AccountId) -> bool {
        <<T as Trait>::OrgData as OrganizationSupervisorPermissions<
            u32,
            <T as frame_system::Trait>::AccountId,
        >>::is_organization_supervisor(organization, who)
    }
    fn check_if_organization_share_supervisor_account(
        organization: OrgId,
        share_id: ShareId,
        who: &T::AccountId,
    ) -> bool {
        <<T as Trait>::ShareData as SubGroupSupervisorPermissions<
            u32,
            u32,
            <T as frame_system::Trait>::AccountId,
        >>::is_sub_group_supervisor(organization, share_id, who)
    }
    // fn check_if_account_is_member_in_organization(
    //     organization: OrgId,
    //     account: &T::AccountId,
    // ) -> bool {
    //     <<T as Trait>::OrgData as OrganizationMembership<<T as frame_system::Trait>::AccountId>>::is_member_of_organization(organization, account)
    // }
    fn check_if_account_is_in_share_group(
        organization: OrgId,
        share_id: ShareId,
        account: &T::AccountId,
    ) -> bool {
        let prefix = UUID2::new(organization, share_id);
        <<T as Trait>::ShareData as GroupMembership<<T as frame_system::Trait>::AccountId>>::is_member_of_group(prefix, account)
    }
}

impl<T: Trait> IDIsAvailable<PetitionId> for Module<T> {
    fn id_is_available(id: PetitionId) -> bool {
        <PetitionStates<T>>::get(id).is_none()
    }
}

impl<T: Trait> GenerateUniqueID<PetitionId> for Module<T> {
    fn generate_unique_id() -> PetitionId {
        let mut petition_id_nonce = PetitionIdNonce::get() + 1u32;
        while !Self::id_is_available(petition_id_nonce) {
            petition_id_nonce += 1u32;
        }
        PetitionIdNonce::put(petition_id_nonce);
        petition_id_nonce
    }
}

impl<T: Trait> GetVoteOutcome for Module<T> {
    type VoteId = PetitionId;
    type Outcome = PetitionOutcome; // could be more contextful

    fn get_vote_outcome(vote_id: Self::VoteId) -> Result<Self::Outcome, DispatchError> {
        // get the petition state
        let petition_state = <PetitionStates<T>>::get(vote_id)
            .ok_or(Error::<T>::CannotGetStatusIfPetitionStateDNE)?;
        // TODO: lazy update that only makes insertion if outcome changes
        let outcome_check = Self::check_petition_outcome(petition_state.clone())?;
        let updated_petition_state = petition_state.set_outcome(outcome_check);
        <PetitionStates<T>>::insert(vote_id, updated_petition_state);
        Ok(outcome_check)
    }
}

impl<T: Trait> OpenPetition<IpfsReference, T::BlockNumber> for Module<T> {
    fn open_petition(
        organization: u32,
        share_id: u32,
        topic: Option<IpfsReference>,
        required_support: u32,
        require_against: Option<u32>,
        duration: Option<T::BlockNumber>,
    ) -> Result<Self::VoteId, DispatchError> {
        // get the total size of the electorate
        let prefix = UUID2::new(organization, share_id);
        let total_electorate = <<T as Trait>::ShareData as GetGroupSize>::get_size_of_group(prefix);
        let ends: Option<T::BlockNumber> = if let Some(time_after_now) = duration {
            let current_time = <frame_system::Module<T>>::block_number();
            Some(current_time + time_after_now)
        } else {
            None
        };
        // returns an error if total_electorate < required_support
        let new_petition_state = PetitionState::new(
            topic,
            prefix.into(),
            required_support,
            require_against,
            total_electorate,
            ends,
        )
        .ok_or(Error::<T>::PetitionDoesNotSatisfyCreationConstraints)?;
        // insert petition state
        let petition_id = Self::generate_unique_id();
        <PetitionStates<T>>::insert(petition_id, new_petition_state);
        Ok(petition_id)
    }
    // why do we need this? because we only have context for total_electorate in this method,
    // not outside of it so we can't just pass total_electorate into `open_petition`
    fn open_unanimous_approval_petition(
        organization: u32,
        share_id: u32,
        topic: Option<IpfsReference>,
        duration: Option<T::BlockNumber>,
    ) -> Result<Self::VoteId, DispatchError> {
        // get the total size of the electorate
        let prefix = UUID2::new(organization, share_id);
        let total_electorate = <<T as Trait>::ShareData as GetGroupSize>::get_size_of_group(prefix);
        let ends: Option<T::BlockNumber> = if let Some(time_after_now) = duration {
            let current_time = <frame_system::Module<T>>::block_number();
            Some(current_time + time_after_now)
        } else {
            None
        };
        // returns an error if total_electorate < required_support || total_electorate < required_vetos_to_freeze
        let new_petition_state = PetitionState::new(
            topic,
            prefix.into(),
            total_electorate,
            None,
            total_electorate,
            ends,
        )
        .ok_or(Error::<T>::PetitionDoesNotSatisfyCreationConstraints)?;
        // insert petition state
        let petition_id = Self::generate_unique_id();
        <PetitionStates<T>>::insert(petition_id, new_petition_state);
        Ok(petition_id)
    }
}

impl<T: Trait> SignPetition<T::AccountId, IpfsReference> for Module<T> {
    type Petition = PetitionState<IpfsReference, T::BlockNumber>;
    type SignerView = PetitionView<IpfsReference>;
    fn check_petition_outcome(petition: Self::Petition) -> Result<Self::Outcome, DispatchError> {
        let current_block: T::BlockNumber = <frame_system::Module<T>>::block_number();
        let past_ends: bool = if let Some(end_time) = petition.ends() {
            end_time <= current_block
        } else {
            true
        };
        // define new outcome based on any changes to the petition state
        let new_outcome = if petition.approved() && past_ends && !petition.rejected() {
            PetitionOutcome::Approved
        } else if petition.rejected() && past_ends && petition.approved() {
            PetitionOutcome::Rejected
        } else if petition.approved() && !past_ends {
            PetitionOutcome::ApprovedButWaitingForTimeToExpire
        } else if petition.rejected() && !past_ends {
            PetitionOutcome::RejectedButWaitingForTimeToExpire
        } else {
            // default
            PetitionOutcome::VotingWithNoOutcomeYet
        };
        Ok(new_outcome)
    }
    fn sign_petition(
        petition_id: PetitionId,
        signer: T::AccountId,
        view: PetitionView<IpfsReference>,
    ) -> Result<Self::Outcome, DispatchError> {
        // get the petition state
        let petition_state = <PetitionStates<T>>::get(petition_id)
            .ok_or(Error::<T>::CannotSignIfPetitionStateDNE)?;
        // check that the signer can sign the petition
        let org_share = petition_state.voter_id_reqs();
        let authentication: bool =
            Self::check_if_account_is_in_share_group(org_share.0, org_share.1, &signer);
        ensure!(
            authentication,
            Error::<T>::CantSignBecauseAccountNotFoundInShareGroup
        );
        // calculate new petition state based on a change in views
        let new_petition_state =
            if let Some(current_signature) = <SignatureLogger<T>>::get(petition_id, &signer) {
                match (current_signature.view(), view.clone()) {
                    (PetitionView::Assent(_), PetitionView::Dissent(_)) => {
                        let ps1 = petition_state.revoke_assent();
                        ps1.add_dissent()
                    }
                    // instead of veto_to_freeze
                    (PetitionView::Assent(_), PetitionView::Veto(_)) => {
                        let ps1 = petition_state.revoke_assent();
                        ps1.add_veto()
                    }
                    (PetitionView::Dissent(_), PetitionView::Assent(_)) => {
                        let ps1 = petition_state.revoke_dissent();
                        ps1.add_assent()
                    }
                    (PetitionView::Dissent(_), PetitionView::Veto(_)) => {
                        let ps1 = petition_state.revoke_dissent();
                        ps1.add_veto()
                    }
                    (PetitionView::Veto(_), PetitionView::Assent(_)) => {
                        let ps1 = petition_state.revoke_veto();
                        ps1.add_assent()
                    }
                    (PetitionView::Veto(_), PetitionView::Dissent(_)) => {
                        let ps1 = petition_state.revoke_veto();
                        ps1.add_dissent()
                    }
                    // no comment or the same view has no impact on petition state
                    _ => petition_state,
                }
            } else {
                petition_state.apply(view.clone())
            };
        let petition_outcome = Self::check_petition_outcome(new_petition_state.clone())?;
        let updated_petition_state = new_petition_state.set_outcome(petition_outcome);
        <PetitionStates<T>>::insert(petition_id, updated_petition_state);
        let new_signature = PetitionSignature::new(signer.clone(), view);
        <SignatureLogger<T>>::insert(petition_id, &signer, new_signature);
        Ok(petition_outcome)
    }
}

// impl<T: Trait> RequestChanges<T::AccountId, IpfsReference> for Module<T> {
//     fn request_changes(
//         petition_id: PetitionId,
//         signer: T::AccountId,
//         justification: IpfsReference,
//     ) -> Result<Option<Self::Outcome>, DispatchError> {
//         // get the petition state
//         let petition_state = <PetitionStates<T>>::get(prefix.one_two(), petition_id)
//             .ok_or(Error::<T>::CannotVetoIfPetitionStateDNE)?;
//         let new_outcome: Option<PetitionOutcome> = if <VetoPower<T>>::get(prefix, &signer).is_some()
//         {
//             // overwrites any existing veto by default, this could be better
//             let new_veto = VetoContext::requested_changes(justification);
//             <VetoPower<T>>::insert(prefix, &signer, new_veto);
//             let new_petition_state = petition_state.veto_to_freeze();
//             <PetitionStates<T>>::insert(prefix.one_two(), petition_id, new_petition_state);
//             PetitionOutcomes::insert(prefix.one_two(), petition_id, PetitionOutcome::FrozenByVeto);
//             Some(PetitionOutcome::FrozenByVeto)
//         } else {
//             return Err(Error::<T>::NotAuthorizedToVetoPetition.into());
//         };
//         // return outcome
//         Ok(new_outcome)
//     }
//     fn accept_changes(
//         petition_id: u32,
//         signer: T::AccountId,
//     ) -> Result<Option<Self::Outcome>, DispatchError> {
//         let prefix = UUID3::new(organization, share_id, petition_id);
//         // get the petition state
//         let petition_state = <PetitionStates<T>>::get(prefix.one_two(), petition_id)
//             .ok_or(Error::<T>::CannotUnVetoIfPetitionStateDNE)?;
//         let new_outcome: Option<PetitionOutcome> = if <VetoPower<T>>::get(prefix, &signer).is_some()
//         {
//             // overwrites any existing veto by default, this could be better
//             let revoke_veto = VetoContext::accept_changes();
//             <VetoPower<T>>::insert(prefix, &signer, revoke_veto);
//             if Self::get_those_who_invoked_veto(organization, share_id, petition_id).is_none() {
//                 // only call revoke veto to unfreeze() if there are no vetos left after we inserted the revocation
//                 let new_petition_state = petition_state.revoke_veto_to_unfreeze();
//                 <PetitionStates<T>>::insert(
//                     prefix.one_two(),
//                     petition_id,
//                     new_petition_state.clone(),
//                 );
//                 let current_block: T::BlockNumber = <frame_system::Module<T>>::block_number();
//                 let past_ends: bool = if let Some(end_time) = new_petition_state.ends() {
//                     end_time <= current_block
//                 } else {
//                     true
//                 };
//                 // define new outcome based on any changes to the petition state
//                 let new_new_outcome =
//                     if new_petition_state.approved() && past_ends && !new_petition_state.rejected()
//                     {
//                         PetitionOutcome::Approved
//                     } else if new_petition_state.rejected()
//                         && past_ends
//                         && new_petition_state.approved()
//                     {
//                         PetitionOutcome::Rejected
//                     } else if new_petition_state.approved() && !past_ends {
//                         PetitionOutcome::ApprovedButWaiting
//                     } else if new_petition_state.rejected() && !past_ends {
//                         PetitionOutcome::RejectedButWaiting
//                     } else {
//                         // default
//                         PetitionOutcome::VotingWithNoOutcomeYet
//                     };
//                 PetitionOutcomes::insert(prefix.one_two(), petition_id, new_new_outcome);
//                 Some(new_new_outcome)
//             } else {
//                 None // no change in the outcome so this is equivalent to Some(PetitionOutcome::FrozenByVeto)
//             }
//         } else {
//             return Err(Error::<T>::NotAuthorizedToVetoPetition.into());
//         };
//         // return outcome
//         Ok(new_outcome)
//     }
// }

impl<T: Trait> UpdatePetition<T::AccountId, IpfsReference> for Module<T> {
    fn update_petition(petition_id: PetitionId, new_topic: IpfsReference) -> DispatchResult {
        // get the petition state
        let petition_state = <PetitionStates<T>>::get(petition_id)
            .ok_or(Error::<T>::CannotUpdateIfPetitionStateDNE)?;
        // update the petition
        let new_petition = petition_state.update_petition_terms(new_topic);
        <PetitionStates<T>>::insert(petition_id, new_petition);
        Ok(())
    }
}