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
use alloc::vec::Vec;
use core::{
convert::{TryFrom, TryInto},
fmt::{self, Display, Formatter},
result,
};
use crate::{
bytesrepr::{self, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
CLType, CLTyped,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(test, derive(strum::EnumIter))]
#[repr(u8)]
pub enum Error {
MissingKey = 0,
InvalidKeyVariant = 1,
MissingValue = 2,
Serialization = 3,
TransferToBidPurse = 4,
InvalidAmount = 5,
BidNotFound = 6,
ValidatorNotFound = 7,
DelegatorNotFound = 8,
Storage = 9,
Bonding = 10,
Unbonding = 11,
ReleaseFounderStake = 12,
GetBalance = 13,
InvalidContext = 14,
ValidatorFundsLocked = 15,
InvalidCaller = 16,
InvalidPublicKey = 17,
BondNotFound = 18,
CreatePurseFailed = 19,
UnbondTooLarge = 20,
BondTooSmall = 21,
MissingDelegations = 22,
MismatchedEraValidators = 23,
MintReward = 24,
InvalidValidatorSlotsValue = 25,
MintReduceTotalSupply = 26,
TransferToDelegatorPurse = 27,
ValidatorRewardTransfer = 28,
DelegatorRewardTransfer = 29,
WithdrawDelegatorReward = 30,
WithdrawValidatorReward = 31,
TransferToUnbondingPurse = 32,
RecordEraInfo = 33,
CLValue = 34,
MissingSeigniorageRecipients = 35,
Transfer = 36,
DelegationRateTooLarge = 37,
DelegatorFundsLocked = 38,
ArithmeticOverflow = 39,
#[doc(hidden)]
GasLimit,
}
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Error::MissingKey => formatter.write_str("Missing key"),
Error::InvalidKeyVariant => formatter.write_str("Invalid key variant"),
Error::MissingValue => formatter.write_str("Missing value"),
Error::Serialization => formatter.write_str("Serialization error"),
Error::TransferToBidPurse => formatter.write_str("Transfer to bid purse error"),
Error::InvalidAmount => formatter.write_str("Invalid amount"),
Error::BidNotFound => formatter.write_str("Bid not found"),
Error::ValidatorNotFound => formatter.write_str("Validator not found"),
Error::DelegatorNotFound => formatter.write_str("Delegator not found"),
Error::Storage => formatter.write_str("Storage error"),
Error::Bonding => formatter.write_str("Bonding error"),
Error::Unbonding => formatter.write_str("Unbonding error"),
Error::ReleaseFounderStake => formatter.write_str("Unable to release founder stake"),
Error::GetBalance => formatter.write_str("Unable to get purse balance"),
Error::InvalidContext => formatter.write_str("Invalid context"),
Error::ValidatorFundsLocked => formatter.write_str("Validator's funds are locked"),
Error::InvalidCaller => formatter.write_str("Function must be called by system account"),
Error::InvalidPublicKey => formatter.write_str("Supplied public key does not match caller's public key or has no associated account"),
Error::BondNotFound => formatter.write_str("Validator's bond not found"),
Error::CreatePurseFailed => formatter.write_str("Unable to create purse"),
Error::UnbondTooLarge => formatter.write_str("Unbond is too large"),
Error::BondTooSmall => formatter.write_str("Bond is too small"),
Error::MissingDelegations => formatter.write_str("Validators has not received any delegations"),
Error::MismatchedEraValidators => formatter.write_str("Mismatched era validator sets to distribute rewards"),
Error::MintReward => formatter.write_str("Failed to mint rewards"),
Error::InvalidValidatorSlotsValue => formatter.write_str("Invalid number of validator slots"),
Error::MintReduceTotalSupply => formatter.write_str("Failed to reduce total supply"),
Error::TransferToDelegatorPurse => formatter.write_str("Transfer to delegators purse error"),
Error::ValidatorRewardTransfer => formatter.write_str("Reward transfer to validator error"),
Error::DelegatorRewardTransfer => formatter.write_str("Rewards transfer to delegator error"),
Error::WithdrawDelegatorReward => formatter.write_str("Withdraw delegator reward error"),
Error::WithdrawValidatorReward => formatter.write_str("Withdraw validator reward error"),
Error::TransferToUnbondingPurse => formatter.write_str("Transfer to unbonding purse error"),
Error::RecordEraInfo => formatter.write_str("Record era info error"),
Error::CLValue => formatter.write_str("CLValue error"),
Error::MissingSeigniorageRecipients => formatter.write_str("Missing seigniorage recipients for given era"),
Error::Transfer => formatter.write_str("Transfer error"),
Error::DelegationRateTooLarge => formatter.write_str("Delegation rate too large"),
Error::DelegatorFundsLocked => formatter.write_str("Delegator's funds are locked"),
Error::ArithmeticOverflow => formatter.write_str("Arithmetic overflow"),
Error::GasLimit => formatter.write_str("GasLimit"),
}
}
}
impl CLTyped for Error {
fn cl_type() -> CLType {
CLType::U8
}
}
#[doc(hidden)]
#[derive(Debug, PartialEq, Eq)]
pub struct TryFromU8ForError(());
#[doc(hidden)]
impl TryFrom<u8> for Error {
type Error = TryFromU8ForError;
fn try_from(value: u8) -> result::Result<Self, Self::Error> {
match value {
d if d == Error::MissingKey as u8 => Ok(Error::MissingKey),
d if d == Error::InvalidKeyVariant as u8 => Ok(Error::InvalidKeyVariant),
d if d == Error::MissingValue as u8 => Ok(Error::MissingValue),
d if d == Error::Serialization as u8 => Ok(Error::Serialization),
d if d == Error::TransferToBidPurse as u8 => Ok(Error::TransferToBidPurse),
d if d == Error::InvalidAmount as u8 => Ok(Error::InvalidAmount),
d if d == Error::BidNotFound as u8 => Ok(Error::BidNotFound),
d if d == Error::ValidatorNotFound as u8 => Ok(Error::ValidatorNotFound),
d if d == Error::DelegatorNotFound as u8 => Ok(Error::DelegatorNotFound),
d if d == Error::Storage as u8 => Ok(Error::Storage),
d if d == Error::Bonding as u8 => Ok(Error::Bonding),
d if d == Error::Unbonding as u8 => Ok(Error::Unbonding),
d if d == Error::ReleaseFounderStake as u8 => Ok(Error::ReleaseFounderStake),
d if d == Error::GetBalance as u8 => Ok(Error::GetBalance),
d if d == Error::InvalidContext as u8 => Ok(Error::InvalidContext),
d if d == Error::ValidatorFundsLocked as u8 => Ok(Error::ValidatorFundsLocked),
d if d == Error::InvalidCaller as u8 => Ok(Error::InvalidCaller),
d if d == Error::InvalidPublicKey as u8 => Ok(Error::InvalidPublicKey),
d if d == Error::BondNotFound as u8 => Ok(Error::BondNotFound),
d if d == Error::CreatePurseFailed as u8 => Ok(Error::CreatePurseFailed),
d if d == Error::UnbondTooLarge as u8 => Ok(Error::UnbondTooLarge),
d if d == Error::BondTooSmall as u8 => Ok(Error::BondTooSmall),
d if d == Error::MissingDelegations as u8 => Ok(Error::MissingDelegations),
d if d == Error::MismatchedEraValidators as u8 => Ok(Error::MismatchedEraValidators),
d if d == Error::MintReward as u8 => Ok(Error::MintReward),
d if d == Error::MintReduceTotalSupply as u8 => Ok(Error::MintReduceTotalSupply),
d if d == Error::InvalidValidatorSlotsValue as u8 => {
Ok(Error::InvalidValidatorSlotsValue)
}
d if d == Error::TransferToDelegatorPurse as u8 => Ok(Error::TransferToDelegatorPurse),
d if d == Error::TransferToDelegatorPurse as u8 => Ok(Error::TransferToDelegatorPurse),
d if d == Error::ValidatorRewardTransfer as u8 => Ok(Error::ValidatorRewardTransfer),
d if d == Error::DelegatorRewardTransfer as u8 => Ok(Error::DelegatorRewardTransfer),
d if d == Error::WithdrawDelegatorReward as u8 => Ok(Error::WithdrawDelegatorReward),
d if d == Error::WithdrawValidatorReward as u8 => Ok(Error::WithdrawValidatorReward),
d if d == Error::TransferToUnbondingPurse as u8 => Ok(Error::TransferToUnbondingPurse),
d if d == Error::RecordEraInfo as u8 => Ok(Error::RecordEraInfo),
d if d == Error::CLValue as u8 => Ok(Error::CLValue),
d if d == Error::MissingSeigniorageRecipients as u8 => {
Ok(Error::MissingSeigniorageRecipients)
}
d if d == Error::Transfer as u8 => Ok(Error::Transfer),
d if d == Error::DelegationRateTooLarge as u8 => Ok(Error::DelegationRateTooLarge),
d if d == Error::DelegatorFundsLocked as u8 => Ok(Error::DelegatorFundsLocked),
d if d == Error::GasLimit as u8 => Ok(Error::GasLimit),
d if d == Error::ArithmeticOverflow as u8 => Ok(Error::ArithmeticOverflow),
_ => Err(TryFromU8ForError(())),
}
}
}
impl ToBytes for Error {
fn to_bytes(&self) -> result::Result<Vec<u8>, bytesrepr::Error> {
let value = *self as u8;
value.to_bytes()
}
fn serialized_length(&self) -> usize {
U8_SERIALIZED_LENGTH
}
}
impl FromBytes for Error {
fn from_bytes(bytes: &[u8]) -> result::Result<(Self, &[u8]), bytesrepr::Error> {
let (value, rem): (u8, _) = FromBytes::from_bytes(bytes)?;
let error: Error = value
.try_into()
.map_err(|_| bytesrepr::Error::Formatting)?;
Ok((error, rem))
}
}
impl From<bytesrepr::Error> for Error {
fn from(_: bytesrepr::Error) -> Self {
Error::Serialization
}
}
#[doc(hidden)]
pub enum PurseLookupError {
KeyNotFound,
KeyUnexpectedType,
}
impl From<PurseLookupError> for Error {
fn from(error: PurseLookupError) -> Self {
match error {
PurseLookupError::KeyNotFound => Error::MissingKey,
PurseLookupError::KeyUnexpectedType => Error::InvalidKeyVariant,
}
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use strum::IntoEnumIterator;
use super::Error;
#[test]
fn error_forward_trips() {
for expected_error_variant in Error::iter() {
assert_eq!(
Error::try_from(expected_error_variant as u8),
Ok(expected_error_variant)
)
}
}
#[test]
fn error_backward_trips() {
for u8 in 0..=u8::max_value() {
match Error::try_from(u8) {
Ok(error_variant) => {
assert_eq!(u8, error_variant as u8, "Error code mismatch")
}
Err(_) => continue,
};
}
}
}