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
use std::collections::{BTreeMap, BTreeSet};
use parity_scale_codec::{Decode, Encode};
use sp_application_crypto::AppKey;
use sp_keystore::{CryptoStore, Error as KeystoreError, SyncCryptoStorePtr};
use super::{Statement, UncheckedSignedFullStatement};
use polkadot_primitives::v2::{
CandidateHash, CandidateReceipt, DisputeStatement, InvalidDisputeStatementKind, SessionIndex,
SigningContext, ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorSignature,
};
mod message;
pub use message::{DisputeMessage, Error as DisputeMessageCheckError, UncheckedDisputeMessage};
#[derive(Debug, Clone)]
pub struct SignedDisputeStatement {
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
session_index: SessionIndex,
}
#[derive(Debug, Clone)]
pub struct CandidateVotes {
pub candidate_receipt: CandidateReceipt,
pub valid: BTreeMap<ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)>,
pub invalid: BTreeMap<ValidatorIndex, (InvalidDisputeStatementKind, ValidatorSignature)>,
}
pub type ValidVoteData = (ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature));
pub type InvalidVoteData = (ValidatorIndex, (InvalidDisputeStatementKind, ValidatorSignature));
impl CandidateVotes {
pub fn voted_indices(&self) -> BTreeSet<ValidatorIndex> {
let mut keys: BTreeSet<_> = self.valid.keys().cloned().collect();
keys.extend(self.invalid.keys().cloned());
keys
}
}
impl SignedDisputeStatement {
pub fn new_unchecked_from_trusted_source(
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
) -> Self {
SignedDisputeStatement {
dispute_statement,
candidate_hash,
validator_public,
validator_signature,
session_index,
}
}
pub fn new_checked(
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
) -> Result<Self, ()> {
dispute_statement
.check_signature(&validator_public, candidate_hash, session_index, &validator_signature)
.map(|_| SignedDisputeStatement {
dispute_statement,
candidate_hash,
validator_public,
validator_signature,
session_index,
})
}
pub async fn sign_explicit(
keystore: &SyncCryptoStorePtr,
valid: bool,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
) -> Result<Option<Self>, KeystoreError> {
let dispute_statement = if valid {
DisputeStatement::Valid(ValidDisputeStatementKind::Explicit)
} else {
DisputeStatement::Invalid(InvalidDisputeStatementKind::Explicit)
};
let data = dispute_statement.payload_data(candidate_hash, session_index);
let signature = CryptoStore::sign_with(
&**keystore,
ValidatorId::ID,
&validator_public.clone().into(),
&data,
)
.await?;
let signature = match signature {
Some(sig) =>
sig.try_into().map_err(|_| KeystoreError::KeyNotSupported(ValidatorId::ID))?,
None => return Ok(None),
};
Ok(Some(Self {
dispute_statement,
candidate_hash,
validator_public,
validator_signature: signature,
session_index,
}))
}
pub fn statement(&self) -> &DisputeStatement {
&self.dispute_statement
}
pub fn candidate_hash(&self) -> &CandidateHash {
&self.candidate_hash
}
pub fn validator_public(&self) -> &ValidatorId {
&self.validator_public
}
pub fn validator_signature(&self) -> &ValidatorSignature {
&self.validator_signature
}
pub fn into_validator_signature(self) -> ValidatorSignature {
self.validator_signature
}
pub fn session_index(&self) -> SessionIndex {
self.session_index
}
pub fn from_backing_statement(
backing_statement: &UncheckedSignedFullStatement,
signing_context: SigningContext,
validator_public: ValidatorId,
) -> Result<Self, ()> {
let (statement_kind, candidate_hash) = match backing_statement.unchecked_payload() {
Statement::Seconded(candidate) => (
ValidDisputeStatementKind::BackingSeconded(signing_context.parent_hash),
candidate.hash(),
),
Statement::Valid(candidate_hash) => (
ValidDisputeStatementKind::BackingValid(signing_context.parent_hash),
*candidate_hash,
),
};
let dispute_statement = DisputeStatement::Valid(statement_kind);
Self::new_checked(
dispute_statement,
candidate_hash,
signing_context.session_index,
validator_public,
backing_statement.unchecked_signature().clone(),
)
}
}
#[derive(Clone, Encode, Decode, Debug)]
pub struct InvalidDisputeVote {
pub validator_index: ValidatorIndex,
pub signature: ValidatorSignature,
pub kind: InvalidDisputeStatementKind,
}
#[derive(Clone, Encode, Decode, Debug)]
pub struct ValidDisputeVote {
pub validator_index: ValidatorIndex,
pub signature: ValidatorSignature,
pub kind: ValidDisputeStatementKind,
}