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