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
#![cfg_attr(not(feature = "std"), no_std)]
use sp_runtime::RuntimeDebug;
use codec::{Decode, Encode};
use sp_std::vec::Vec;
#[cfg(test)]
mod tests;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type Call: From<Call<Self>>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
pub fn run_event(origin: OriginFor<T>, id: Vec<u8>) -> DispatchResultWithPostInfo {
let _ = ensure_signed(origin)?;
<Participants<T>>::kill();
<CurrentEventId<T>>::mutate(move |event_id| *event_id = id);
Ok(().into())
}
#[pallet::weight(0)]
pub fn enlist_participants(
origin: OriginFor<T>,
participants: Vec<EnlistedParticipant>,
) -> DispatchResultWithPostInfo {
let _ = ensure_signed(origin)?;
if validate_participants_parallel(&<CurrentEventId<T>>::get(), &participants[..]) {
for participant in participants {
<Participants<T>>::append(participant.account);
}
}
Ok(().into())
}
}
#[pallet::storage]
#[pallet::getter(fn participants)]
pub(super) type Participants<T: Config> = StorageValue<_, Vec<Vec<u8>>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn get_current_event_id)]
pub(super) type CurrentEventId<T: Config> = StorageValue<_, Vec<u8>, ValueQuery>;
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct EnlistedParticipant {
pub account: Vec<u8>,
pub signature: Vec<u8>,
}
impl EnlistedParticipant {
fn verify(&self, event_id: &[u8]) -> bool {
use sp_core::Public;
use sp_runtime::traits::Verify;
use std::convert::TryFrom;
match sp_core::sr25519::Signature::try_from(&self.signature[..]) {
Ok(signature) => {
let public = sp_core::sr25519::Public::from_slice(self.account.as_ref());
signature.verify(event_id, &public)
},
_ => false,
}
}
}
fn validate_participants_parallel(event_id: &[u8], participants: &[EnlistedParticipant]) -> bool {
fn spawn_verify(data: Vec<u8>) -> Vec<u8> {
let stream = &mut &data[..];
let event_id = Vec::<u8>::decode(stream).expect("Failed to decode");
let participants = Vec::<EnlistedParticipant>::decode(stream).expect("Failed to decode");
for participant in participants {
if !participant.verify(&event_id) {
return false.encode()
}
}
true.encode()
}
let mut async_payload = Vec::new();
event_id.encode_to(&mut async_payload);
participants[..participants.len() / 2].encode_to(&mut async_payload);
let handle = sp_tasks::spawn(spawn_verify, async_payload);
let mut result = true;
for participant in &participants[participants.len() / 2 + 1..] {
if !participant.verify(event_id) {
result = false;
break
}
}
bool::decode(&mut &handle.join()[..]).expect("Failed to decode result") && result
}