|
@@ -0,0 +1,823 @@
|
|
1
|
+use serde::{Deserialize, Serialize};
|
|
2
|
+use std::fmt::{Debug, Display};
|
|
3
|
+
|
|
4
|
+use near_primitives_core::{
|
|
5
|
+ hash::CryptoHash,
|
|
6
|
+ serialize::dec_format,
|
|
7
|
+ types::{AccountId, Balance, Gas, Nonce},
|
|
8
|
+};
|
|
9
|
+
|
|
10
|
+use common_api::crypto::prelude::*;
|
|
11
|
+
|
|
12
|
+use borsh::{BorshDeserialize, BorshSerialize};
|
|
13
|
+
|
|
14
|
+/// Error returned in the ExecutionOutcome in case of failure
|
|
15
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
16
|
+pub enum TxExecutionError {
|
|
17
|
+ /// An error happened during Action execution
|
|
18
|
+ ActionError(ActionError),
|
|
19
|
+ /// An error happened during Transaction execution
|
|
20
|
+ InvalidTxError(InvalidTxError),
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+impl std::error::Error for TxExecutionError {}
|
|
24
|
+
|
|
25
|
+impl Display for TxExecutionError {
|
|
26
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
27
|
+ match self {
|
|
28
|
+ TxExecutionError::ActionError(e) => write!(f, "{}", e),
|
|
29
|
+ TxExecutionError::InvalidTxError(e) => write!(f, "{}", e),
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+impl From<ActionError> for TxExecutionError {
|
|
35
|
+ fn from(error: ActionError) -> Self {
|
|
36
|
+ TxExecutionError::ActionError(error)
|
|
37
|
+ }
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+impl From<InvalidTxError> for TxExecutionError {
|
|
41
|
+ fn from(error: InvalidTxError) -> Self {
|
|
42
|
+ TxExecutionError::InvalidTxError(error)
|
|
43
|
+ }
|
|
44
|
+}
|
|
45
|
+
|
|
46
|
+/// Error returned from `Runtime::apply`
|
|
47
|
+#[derive(Debug, Clone, PartialEq, Eq)]
|
|
48
|
+pub enum RuntimeError {
|
|
49
|
+ /// An unexpected integer overflow occurred. The likely issue is an invalid state or the transition.
|
|
50
|
+ UnexpectedIntegerOverflow,
|
|
51
|
+ /// An error happened during TX verification and account charging. It's likely the chunk is invalid.
|
|
52
|
+ /// and should be challenged.
|
|
53
|
+ InvalidTxError(InvalidTxError),
|
|
54
|
+ /// Unexpected error which is typically related to the node storage corruption.
|
|
55
|
+ /// It's possible the input state is invalid or malicious.
|
|
56
|
+ StorageError(StorageError),
|
|
57
|
+ /// An error happens if `check_balance` fails, which is likely an indication of an invalid state.
|
|
58
|
+ BalanceMismatchError(BalanceMismatchError),
|
|
59
|
+ /// The incoming receipt didn't pass the validation, it's likely a malicious behaviour.
|
|
60
|
+ ReceiptValidationError(ReceiptValidationError),
|
|
61
|
+ /// Error when accessing validator information. Happens inside epoch manager.
|
|
62
|
+ ValidatorError(EpochError),
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+impl std::fmt::Display for RuntimeError {
|
|
66
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
67
|
+ f.write_str(&format!("{:?}", self))
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+impl std::error::Error for RuntimeError {}
|
|
72
|
+
|
|
73
|
+/// Internal
|
|
74
|
+#[derive(Debug, Clone, PartialEq, Eq)]
|
|
75
|
+pub enum StorageError {
|
|
76
|
+ /// Key-value db internal failure
|
|
77
|
+ StorageInternalError,
|
|
78
|
+ /// Storage is PartialStorage and requested a missing trie node
|
|
79
|
+ TrieNodeMissing,
|
|
80
|
+ /// Either invalid state or key-value db is corrupted.
|
|
81
|
+ /// For PartialStorage it cannot be corrupted.
|
|
82
|
+ /// Error message is unreliable and for debugging purposes only. It's also probably ok to
|
|
83
|
+ /// panic in every place that produces this error.
|
|
84
|
+ /// We can check if db is corrupted by verifying everything in the state trie.
|
|
85
|
+ StorageInconsistentState(String),
|
|
86
|
+ /// Error from flat storage
|
|
87
|
+ FlatStorageError(String),
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+impl std::fmt::Display for StorageError {
|
|
91
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
92
|
+ f.write_str(&format!("{:?}", self))
|
|
93
|
+ }
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+impl std::error::Error for StorageError {}
|
|
97
|
+
|
|
98
|
+/// An error happened during TX execution
|
|
99
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
100
|
+pub enum InvalidTxError {
|
|
101
|
+ /// Happens if a wrong AccessKey used or AccessKey has not enough permissions
|
|
102
|
+ InvalidAccessKeyError(InvalidAccessKeyError),
|
|
103
|
+ /// TX signer_id is not a valid [`AccountId`]
|
|
104
|
+ InvalidSignerId { signer_id: String },
|
|
105
|
+ /// TX signer_id is not found in a storage
|
|
106
|
+ SignerDoesNotExist { signer_id: AccountId },
|
|
107
|
+ /// Transaction nonce must be `account[access_key].nonce + 1`.
|
|
108
|
+ InvalidNonce { tx_nonce: Nonce, ak_nonce: Nonce },
|
|
109
|
+ /// Transaction nonce is larger than the upper bound given by the block height
|
|
110
|
+ NonceTooLarge { tx_nonce: Nonce, upper_bound: Nonce },
|
|
111
|
+ /// TX receiver_id is not a valid AccountId
|
|
112
|
+ InvalidReceiverId { receiver_id: String },
|
|
113
|
+ /// TX signature is not valid
|
|
114
|
+ InvalidSignature,
|
|
115
|
+ /// Account does not have enough balance to cover TX cost
|
|
116
|
+ NotEnoughBalance {
|
|
117
|
+ signer_id: AccountId,
|
|
118
|
+ #[serde(with = "dec_format")]
|
|
119
|
+ balance: Balance,
|
|
120
|
+ #[serde(with = "dec_format")]
|
|
121
|
+ cost: Balance,
|
|
122
|
+ },
|
|
123
|
+ /// Signer account doesn't have enough balance after transaction.
|
|
124
|
+ LackBalanceForState {
|
|
125
|
+ /// An account which doesn't have enough balance to cover storage.
|
|
126
|
+ signer_id: AccountId,
|
|
127
|
+ /// Required balance to cover the state.
|
|
128
|
+ #[serde(with = "dec_format")]
|
|
129
|
+ amount: Balance,
|
|
130
|
+ },
|
|
131
|
+ /// An integer overflow occurred during transaction cost estimation.
|
|
132
|
+ CostOverflow,
|
|
133
|
+ /// Transaction parent block hash doesn't belong to the current chain
|
|
134
|
+ InvalidChain,
|
|
135
|
+ /// Transaction has expired
|
|
136
|
+ Expired,
|
|
137
|
+ /// An error occurred while validating actions of a Transaction.
|
|
138
|
+ ActionsValidation(ActionsValidationError),
|
|
139
|
+ /// The size of serialized transaction exceeded the limit.
|
|
140
|
+ TransactionSizeExceeded { size: u64, limit: u64 },
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+impl std::error::Error for InvalidTxError {}
|
|
144
|
+
|
|
145
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
146
|
+pub enum InvalidAccessKeyError {
|
|
147
|
+ /// The access key identified by the `public_key` doesn't exist for the account
|
|
148
|
+ AccessKeyNotFound {
|
|
149
|
+ account_id: AccountId,
|
|
150
|
+ public_key: Ed25519PublicKey,
|
|
151
|
+ },
|
|
152
|
+ /// Transaction `receiver_id` doesn't match the access key receiver_id
|
|
153
|
+ ReceiverMismatch {
|
|
154
|
+ tx_receiver: AccountId,
|
|
155
|
+ ak_receiver: String,
|
|
156
|
+ },
|
|
157
|
+ /// Transaction method name isn't allowed by the access key
|
|
158
|
+ MethodNameMismatch { method_name: String },
|
|
159
|
+ /// Transaction requires a full permission access key.
|
|
160
|
+ RequiresFullAccess,
|
|
161
|
+ /// Access Key does not have enough allowance to cover transaction cost
|
|
162
|
+ NotEnoughAllowance {
|
|
163
|
+ account_id: AccountId,
|
|
164
|
+ public_key: Ed25519PublicKey,
|
|
165
|
+ #[serde(with = "dec_format")]
|
|
166
|
+ allowance: Balance,
|
|
167
|
+ #[serde(with = "dec_format")]
|
|
168
|
+ cost: Balance,
|
|
169
|
+ },
|
|
170
|
+ /// Having a deposit with a function call action is not allowed with a function call access key.
|
|
171
|
+ DepositWithFunctionCall,
|
|
172
|
+}
|
|
173
|
+
|
|
174
|
+/// Describes the error for validating a list of actions.
|
|
175
|
+#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
|
176
|
+pub enum ActionsValidationError {
|
|
177
|
+ /// The delete action must be a final aciton in transaction
|
|
178
|
+ DeleteActionMustBeFinal,
|
|
179
|
+ /// The total prepaid gas (for all given actions) exceeded the limit.
|
|
180
|
+ TotalPrepaidGasExceeded { total_prepaid_gas: Gas, limit: Gas },
|
|
181
|
+ /// The number of actions exceeded the given limit.
|
|
182
|
+ TotalNumberOfActionsExceeded {
|
|
183
|
+ total_number_of_actions: u64,
|
|
184
|
+ limit: u64,
|
|
185
|
+ },
|
|
186
|
+ /// The total number of bytes of the method names exceeded the limit in a Add Key action.
|
|
187
|
+ AddKeyMethodNamesNumberOfBytesExceeded {
|
|
188
|
+ total_number_of_bytes: u64,
|
|
189
|
+ limit: u64,
|
|
190
|
+ },
|
|
191
|
+ /// The length of some method name exceeded the limit in a Add Key action.
|
|
192
|
+ AddKeyMethodNameLengthExceeded { length: u64, limit: u64 },
|
|
193
|
+ /// Integer overflow during a compute.
|
|
194
|
+ IntegerOverflow,
|
|
195
|
+ /// Invalid account ID.
|
|
196
|
+ InvalidAccountId { account_id: String },
|
|
197
|
+ /// The size of the contract code exceeded the limit in a DeployContract action.
|
|
198
|
+ ContractSizeExceeded { size: u64, limit: u64 },
|
|
199
|
+ /// The length of the method name exceeded the limit in a Function Call action.
|
|
200
|
+ FunctionCallMethodNameLengthExceeded { length: u64, limit: u64 },
|
|
201
|
+ /// The length of the arguments exceeded the limit in a Function Call action.
|
|
202
|
+ FunctionCallArgumentsLengthExceeded { length: u64, limit: u64 },
|
|
203
|
+ /// An attempt to stake with a public key that is not convertible to ristretto.
|
|
204
|
+ UnsuitableStakingKey { public_key: Ed25519PublicKey },
|
|
205
|
+ /// The attached amount of gas in a FunctionCall action has to be a positive number.
|
|
206
|
+ FunctionCallZeroAttachedGas,
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+/// Describes the error for validating a receipt.
|
|
210
|
+#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
|
211
|
+pub enum ReceiptValidationError {
|
|
212
|
+ /// The `predecessor_id` of a Receipt is not valid.
|
|
213
|
+ InvalidPredecessorId { account_id: String },
|
|
214
|
+ /// The `receiver_id` of a Receipt is not valid.
|
|
215
|
+ InvalidReceiverId { account_id: String },
|
|
216
|
+ /// The `signer_id` of an ActionReceipt is not valid.
|
|
217
|
+ InvalidSignerId { account_id: String },
|
|
218
|
+ /// The `receiver_id` of a DataReceiver within an ActionReceipt is not valid.
|
|
219
|
+ InvalidDataReceiverId { account_id: String },
|
|
220
|
+ /// The length of the returned data exceeded the limit in a DataReceipt.
|
|
221
|
+ ReturnedValueLengthExceeded { length: u64, limit: u64 },
|
|
222
|
+ /// The number of input data dependencies exceeds the limit in an ActionReceipt.
|
|
223
|
+ NumberInputDataDependenciesExceeded {
|
|
224
|
+ number_of_input_data_dependencies: u64,
|
|
225
|
+ limit: u64,
|
|
226
|
+ },
|
|
227
|
+ /// An error occurred while validating actions of an ActionReceipt.
|
|
228
|
+ ActionsValidation(ActionsValidationError),
|
|
229
|
+}
|
|
230
|
+
|
|
231
|
+impl Display for ReceiptValidationError {
|
|
232
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
233
|
+ match self {
|
|
234
|
+ ReceiptValidationError::InvalidPredecessorId { account_id } => {
|
|
235
|
+ write!(f, "The predecessor_id `{}` of a Receipt is not valid.", account_id)
|
|
236
|
+ }
|
|
237
|
+ ReceiptValidationError::InvalidReceiverId { account_id } => {
|
|
238
|
+ write!(f, "The receiver_id `{}` of a Receipt is not valid.", account_id)
|
|
239
|
+ }
|
|
240
|
+ ReceiptValidationError::InvalidSignerId { account_id } => {
|
|
241
|
+ write!(f, "The signer_id `{}` of an ActionReceipt is not valid.", account_id)
|
|
242
|
+ }
|
|
243
|
+ ReceiptValidationError::InvalidDataReceiverId { account_id } => write!(
|
|
244
|
+ f,
|
|
245
|
+ "The receiver_id `{}` of a DataReceiver within an ActionReceipt is not valid.",
|
|
246
|
+ account_id
|
|
247
|
+ ),
|
|
248
|
+ ReceiptValidationError::ReturnedValueLengthExceeded { length, limit } => write!(
|
|
249
|
+ f,
|
|
250
|
+ "The length of the returned data {} exceeded the limit {} in a DataReceipt",
|
|
251
|
+ length, limit
|
|
252
|
+ ),
|
|
253
|
+ ReceiptValidationError::NumberInputDataDependenciesExceeded { number_of_input_data_dependencies, limit } => write!(
|
|
254
|
+ f,
|
|
255
|
+ "The number of input data dependencies {} exceeded the limit {} in an ActionReceipt",
|
|
256
|
+ number_of_input_data_dependencies, limit
|
|
257
|
+ ),
|
|
258
|
+ ReceiptValidationError::ActionsValidation(e) => write!(f, "{}", e),
|
|
259
|
+ }
|
|
260
|
+ }
|
|
261
|
+}
|
|
262
|
+
|
|
263
|
+impl std::error::Error for ReceiptValidationError {}
|
|
264
|
+
|
|
265
|
+impl Display for ActionsValidationError {
|
|
266
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
267
|
+ match self {
|
|
268
|
+ ActionsValidationError::DeleteActionMustBeFinal => {
|
|
269
|
+ write!(f, "The delete action must be the last action in transaction")
|
|
270
|
+ }
|
|
271
|
+ ActionsValidationError::TotalPrepaidGasExceeded { total_prepaid_gas, limit } => {
|
|
272
|
+ write!(f, "The total prepaid gas {} exceeds the limit {}", total_prepaid_gas, limit)
|
|
273
|
+ }
|
|
274
|
+ ActionsValidationError::TotalNumberOfActionsExceeded {total_number_of_actions, limit } => {
|
|
275
|
+ write!(
|
|
276
|
+ f,
|
|
277
|
+ "The total number of actions {} exceeds the limit {}",
|
|
278
|
+ total_number_of_actions, limit
|
|
279
|
+ )
|
|
280
|
+ }
|
|
281
|
+ ActionsValidationError::AddKeyMethodNamesNumberOfBytesExceeded { total_number_of_bytes, limit } => write!(
|
|
282
|
+ f,
|
|
283
|
+ "The total number of bytes in allowed method names {} exceeds the maximum allowed number {} in a AddKey action",
|
|
284
|
+ total_number_of_bytes, limit
|
|
285
|
+ ),
|
|
286
|
+ ActionsValidationError::AddKeyMethodNameLengthExceeded { length, limit } => write!(
|
|
287
|
+ f,
|
|
288
|
+ "The length of some method name {} exceeds the maximum allowed length {} in a AddKey action",
|
|
289
|
+ length, limit
|
|
290
|
+ ),
|
|
291
|
+ ActionsValidationError::IntegerOverflow => write!(
|
|
292
|
+ f,
|
|
293
|
+ "Integer overflow during a compute",
|
|
294
|
+ ),
|
|
295
|
+ ActionsValidationError::InvalidAccountId { account_id } => write!(
|
|
296
|
+ f,
|
|
297
|
+ "Invalid account ID `{}`",
|
|
298
|
+ account_id
|
|
299
|
+ ),
|
|
300
|
+ ActionsValidationError::ContractSizeExceeded { size, limit } => write!(
|
|
301
|
+ f,
|
|
302
|
+ "The length of the contract size {} exceeds the maximum allowed size {} in a DeployContract action",
|
|
303
|
+ size, limit
|
|
304
|
+ ),
|
|
305
|
+ ActionsValidationError::FunctionCallMethodNameLengthExceeded { length, limit } => write!(
|
|
306
|
+ f,
|
|
307
|
+ "The length of the method name {} exceeds the maximum allowed length {} in a FunctionCall action",
|
|
308
|
+ length, limit
|
|
309
|
+ ),
|
|
310
|
+ ActionsValidationError::FunctionCallArgumentsLengthExceeded { length, limit } => write!(
|
|
311
|
+ f,
|
|
312
|
+ "The length of the arguments {} exceeds the maximum allowed length {} in a FunctionCall action",
|
|
313
|
+ length, limit
|
|
314
|
+ ),
|
|
315
|
+ ActionsValidationError::UnsuitableStakingKey { public_key } => write!(
|
|
316
|
+ f,
|
|
317
|
+ "The staking key must be ristretto compatible ED25519 key. {} is provided instead.",
|
|
318
|
+ public_key,
|
|
319
|
+ ),
|
|
320
|
+ ActionsValidationError::FunctionCallZeroAttachedGas => write!(
|
|
321
|
+ f,
|
|
322
|
+ "The attached amount of gas in a FunctionCall action has to be a positive number",
|
|
323
|
+ ),
|
|
324
|
+ }
|
|
325
|
+ }
|
|
326
|
+}
|
|
327
|
+
|
|
328
|
+impl std::error::Error for ActionsValidationError {}
|
|
329
|
+
|
|
330
|
+/// An error happened during Action execution
|
|
331
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
332
|
+pub struct ActionError {
|
|
333
|
+ /// Index of the failed action in the transaction.
|
|
334
|
+ /// Action index is not defined if ActionError.kind is `ActionErrorKind::LackBalanceForState`
|
|
335
|
+ pub index: Option<u64>,
|
|
336
|
+ /// The kind of ActionError happened
|
|
337
|
+ pub kind: ActionErrorKind,
|
|
338
|
+}
|
|
339
|
+
|
|
340
|
+impl std::error::Error for ActionError {}
|
|
341
|
+
|
|
342
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
343
|
+pub enum ActionErrorKind {
|
|
344
|
+ /// Happens when CreateAccount action tries to create an account with account_id which is already exists in the storage
|
|
345
|
+ AccountAlreadyExists { account_id: AccountId },
|
|
346
|
+ /// Happens when TX receiver_id doesn't exist (but action is not Action::CreateAccount)
|
|
347
|
+ AccountDoesNotExist { account_id: AccountId },
|
|
348
|
+ /// A top-level account ID can only be created by registrar.
|
|
349
|
+ CreateAccountOnlyByRegistrar {
|
|
350
|
+ account_id: AccountId,
|
|
351
|
+ registrar_account_id: AccountId,
|
|
352
|
+ predecessor_id: AccountId,
|
|
353
|
+ },
|
|
354
|
+ /// A newly created account must be under a namespace of the creator account
|
|
355
|
+ CreateAccountNotAllowed {
|
|
356
|
+ account_id: AccountId,
|
|
357
|
+ predecessor_id: AccountId,
|
|
358
|
+ },
|
|
359
|
+ /// Administrative actions like `DeployContract`, `Stake`, `AddKey`, `DeleteKey`. can be proceed only if sender=receiver
|
|
360
|
+ /// or the first TX action is a `CreateAccount` action
|
|
361
|
+ ActorNoPermission {
|
|
362
|
+ account_id: AccountId,
|
|
363
|
+ actor_id: AccountId,
|
|
364
|
+ },
|
|
365
|
+ /// Account tries to remove an access key that doesn't exist
|
|
366
|
+ DeleteKeyDoesNotExist {
|
|
367
|
+ account_id: AccountId,
|
|
368
|
+ public_key: Ed25519PublicKey,
|
|
369
|
+ },
|
|
370
|
+ /// The public key is already used for an existing access key
|
|
371
|
+ AddKeyAlreadyExists {
|
|
372
|
+ account_id: AccountId,
|
|
373
|
+ public_key: Ed25519PublicKey,
|
|
374
|
+ },
|
|
375
|
+ /// Account is staking and can not be deleted
|
|
376
|
+ DeleteAccountStaking { account_id: AccountId },
|
|
377
|
+ /// ActionReceipt can't be completed, because the remaining balance will not be enough to cover storage.
|
|
378
|
+ LackBalanceForState {
|
|
379
|
+ /// An account which needs balance
|
|
380
|
+ account_id: AccountId,
|
|
381
|
+ /// Balance required to complete an action.
|
|
382
|
+ #[serde(with = "dec_format")]
|
|
383
|
+ amount: Balance,
|
|
384
|
+ },
|
|
385
|
+ /// Account is not yet staked, but tries to unstake
|
|
386
|
+ TriesToUnstake { account_id: AccountId },
|
|
387
|
+ /// The account doesn't have enough balance to increase the stake.
|
|
388
|
+ TriesToStake {
|
|
389
|
+ account_id: AccountId,
|
|
390
|
+ #[serde(with = "dec_format")]
|
|
391
|
+ stake: Balance,
|
|
392
|
+ #[serde(with = "dec_format")]
|
|
393
|
+ locked: Balance,
|
|
394
|
+ #[serde(with = "dec_format")]
|
|
395
|
+ balance: Balance,
|
|
396
|
+ },
|
|
397
|
+ InsufficientStake {
|
|
398
|
+ account_id: AccountId,
|
|
399
|
+ #[serde(with = "dec_format")]
|
|
400
|
+ stake: Balance,
|
|
401
|
+ #[serde(with = "dec_format")]
|
|
402
|
+ minimum_stake: Balance,
|
|
403
|
+ },
|
|
404
|
+ /// Error occurs when a `CreateAccount` action is called on hex-characters
|
|
405
|
+ /// account of length 64. See implicit account creation NEP:
|
|
406
|
+ /// <https://github.com/nearprotocol/NEPs/pull/71>.
|
|
407
|
+ OnlyImplicitAccountCreationAllowed { account_id: AccountId },
|
|
408
|
+ /// Delete account whose state is large is temporarily banned.
|
|
409
|
+ DeleteAccountWithLargeState { account_id: AccountId },
|
|
410
|
+}
|
|
411
|
+
|
|
412
|
+impl From<ActionErrorKind> for ActionError {
|
|
413
|
+ fn from(e: ActionErrorKind) -> ActionError {
|
|
414
|
+ ActionError {
|
|
415
|
+ index: None,
|
|
416
|
+ kind: e,
|
|
417
|
+ }
|
|
418
|
+ }
|
|
419
|
+}
|
|
420
|
+
|
|
421
|
+impl Display for InvalidTxError {
|
|
422
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
423
|
+ match self {
|
|
424
|
+ InvalidTxError::InvalidSignerId { signer_id } => {
|
|
425
|
+ write!(
|
|
426
|
+ f,
|
|
427
|
+ "Invalid signer account ID {:?} according to requirements",
|
|
428
|
+ signer_id
|
|
429
|
+ )
|
|
430
|
+ }
|
|
431
|
+ InvalidTxError::SignerDoesNotExist { signer_id } => {
|
|
432
|
+ write!(f, "Signer {:?} does not exist", signer_id)
|
|
433
|
+ }
|
|
434
|
+ InvalidTxError::InvalidAccessKeyError(access_key_error) => {
|
|
435
|
+ Display::fmt(&access_key_error, f)
|
|
436
|
+ }
|
|
437
|
+ InvalidTxError::InvalidNonce { tx_nonce, ak_nonce } => write!(
|
|
438
|
+ f,
|
|
439
|
+ "Transaction nonce {} must be larger than nonce of the used access key {}",
|
|
440
|
+ tx_nonce, ak_nonce
|
|
441
|
+ ),
|
|
442
|
+ InvalidTxError::InvalidReceiverId { receiver_id } => {
|
|
443
|
+ write!(
|
|
444
|
+ f,
|
|
445
|
+ "Invalid receiver account ID {:?} according to requirements",
|
|
446
|
+ receiver_id
|
|
447
|
+ )
|
|
448
|
+ }
|
|
449
|
+ InvalidTxError::InvalidSignature => {
|
|
450
|
+ write!(f, "Transaction is not signed with the given public key")
|
|
451
|
+ }
|
|
452
|
+ InvalidTxError::NotEnoughBalance {
|
|
453
|
+ signer_id,
|
|
454
|
+ balance,
|
|
455
|
+ cost,
|
|
456
|
+ } => write!(
|
|
457
|
+ f,
|
|
458
|
+ "Sender {:?} does not have enough balance {} for operation costing {}",
|
|
459
|
+ signer_id, balance, cost
|
|
460
|
+ ),
|
|
461
|
+ InvalidTxError::LackBalanceForState { signer_id, amount } => {
|
|
462
|
+ write!(f, "Failed to execute, because the account {:?} wouldn't have enough balance to cover storage, required to have {} yoctoNEAR more", signer_id, amount)
|
|
463
|
+ }
|
|
464
|
+ InvalidTxError::CostOverflow => {
|
|
465
|
+ write!(f, "Transaction gas or balance cost is too high")
|
|
466
|
+ }
|
|
467
|
+ InvalidTxError::InvalidChain => {
|
|
468
|
+ write!(
|
|
469
|
+ f,
|
|
470
|
+ "Transaction parent block hash doesn't belong to the current chain"
|
|
471
|
+ )
|
|
472
|
+ }
|
|
473
|
+ InvalidTxError::Expired => {
|
|
474
|
+ write!(f, "Transaction has expired")
|
|
475
|
+ }
|
|
476
|
+ InvalidTxError::ActionsValidation(error) => {
|
|
477
|
+ write!(f, "Transaction actions validation error: {}", error)
|
|
478
|
+ }
|
|
479
|
+ InvalidTxError::NonceTooLarge {
|
|
480
|
+ tx_nonce,
|
|
481
|
+ upper_bound,
|
|
482
|
+ } => {
|
|
483
|
+ write!(
|
|
484
|
+ f,
|
|
485
|
+ "Transaction nonce {} must be smaller than the access key nonce upper bound {}",
|
|
486
|
+ tx_nonce, upper_bound
|
|
487
|
+ )
|
|
488
|
+ }
|
|
489
|
+ InvalidTxError::TransactionSizeExceeded { size, limit } => {
|
|
490
|
+ write!(
|
|
491
|
+ f,
|
|
492
|
+ "Size of serialized transaction {} exceeded the limit {}",
|
|
493
|
+ size, limit
|
|
494
|
+ )
|
|
495
|
+ }
|
|
496
|
+ }
|
|
497
|
+ }
|
|
498
|
+}
|
|
499
|
+
|
|
500
|
+impl From<InvalidAccessKeyError> for InvalidTxError {
|
|
501
|
+ fn from(error: InvalidAccessKeyError) -> Self {
|
|
502
|
+ InvalidTxError::InvalidAccessKeyError(error)
|
|
503
|
+ }
|
|
504
|
+}
|
|
505
|
+
|
|
506
|
+impl Display for InvalidAccessKeyError {
|
|
507
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
508
|
+ match self {
|
|
509
|
+ InvalidAccessKeyError::AccessKeyNotFound {
|
|
510
|
+ account_id,
|
|
511
|
+ public_key,
|
|
512
|
+ } => write!(
|
|
513
|
+ f,
|
|
514
|
+ "Signer {:?} doesn't have access key with the given public_key {}",
|
|
515
|
+ account_id, public_key
|
|
516
|
+ ),
|
|
517
|
+ InvalidAccessKeyError::ReceiverMismatch {
|
|
518
|
+ tx_receiver,
|
|
519
|
+ ak_receiver,
|
|
520
|
+ } => write!(
|
|
521
|
+ f,
|
|
522
|
+ "Transaction receiver_id {:?} doesn't match the access key receiver_id {:?}",
|
|
523
|
+ tx_receiver, ak_receiver
|
|
524
|
+ ),
|
|
525
|
+ InvalidAccessKeyError::MethodNameMismatch { method_name } => write!(
|
|
526
|
+ f,
|
|
527
|
+ "Transaction method name {:?} isn't allowed by the access key",
|
|
528
|
+ method_name
|
|
529
|
+ ),
|
|
530
|
+ InvalidAccessKeyError::RequiresFullAccess => {
|
|
531
|
+ write!(f, "Invalid access key type. Full-access keys are required for transactions that have multiple or non-function-call actions")
|
|
532
|
+ }
|
|
533
|
+ InvalidAccessKeyError::NotEnoughAllowance {
|
|
534
|
+ account_id,
|
|
535
|
+ public_key,
|
|
536
|
+ allowance,
|
|
537
|
+ cost,
|
|
538
|
+ } => write!(
|
|
539
|
+ f,
|
|
540
|
+ "Access Key {:?}:{} does not have enough balance {} for transaction costing {}",
|
|
541
|
+ account_id, public_key, allowance, cost
|
|
542
|
+ ),
|
|
543
|
+ InvalidAccessKeyError::DepositWithFunctionCall => {
|
|
544
|
+ write!(f, "Having a deposit with a function call action is not allowed with a function call access key.")
|
|
545
|
+ }
|
|
546
|
+ }
|
|
547
|
+ }
|
|
548
|
+}
|
|
549
|
+
|
|
550
|
+impl std::error::Error for InvalidAccessKeyError {}
|
|
551
|
+
|
|
552
|
+/// Happens when the input balance doesn't match the output balance in Runtime apply.
|
|
553
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
|
554
|
+pub struct BalanceMismatchError {
|
|
555
|
+ // Input balances
|
|
556
|
+ #[serde(with = "dec_format")]
|
|
557
|
+ pub incoming_validator_rewards: Balance,
|
|
558
|
+ #[serde(with = "dec_format")]
|
|
559
|
+ pub initial_accounts_balance: Balance,
|
|
560
|
+ #[serde(with = "dec_format")]
|
|
561
|
+ pub incoming_receipts_balance: Balance,
|
|
562
|
+ #[serde(with = "dec_format")]
|
|
563
|
+ pub processed_delayed_receipts_balance: Balance,
|
|
564
|
+ #[serde(with = "dec_format")]
|
|
565
|
+ pub initial_postponed_receipts_balance: Balance,
|
|
566
|
+ // Output balances
|
|
567
|
+ #[serde(with = "dec_format")]
|
|
568
|
+ pub final_accounts_balance: Balance,
|
|
569
|
+ #[serde(with = "dec_format")]
|
|
570
|
+ pub outgoing_receipts_balance: Balance,
|
|
571
|
+ #[serde(with = "dec_format")]
|
|
572
|
+ pub new_delayed_receipts_balance: Balance,
|
|
573
|
+ #[serde(with = "dec_format")]
|
|
574
|
+ pub final_postponed_receipts_balance: Balance,
|
|
575
|
+ #[serde(with = "dec_format")]
|
|
576
|
+ pub tx_burnt_amount: Balance,
|
|
577
|
+ #[serde(with = "dec_format")]
|
|
578
|
+ pub slashed_burnt_amount: Balance,
|
|
579
|
+ #[serde(with = "dec_format")]
|
|
580
|
+ pub other_burnt_amount: Balance,
|
|
581
|
+}
|
|
582
|
+
|
|
583
|
+impl Display for BalanceMismatchError {
|
|
584
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
585
|
+ // Using saturating add to avoid overflow in display
|
|
586
|
+ let initial_balance = self
|
|
587
|
+ .incoming_validator_rewards
|
|
588
|
+ .saturating_add(self.initial_accounts_balance)
|
|
589
|
+ .saturating_add(self.incoming_receipts_balance)
|
|
590
|
+ .saturating_add(self.processed_delayed_receipts_balance)
|
|
591
|
+ .saturating_add(self.initial_postponed_receipts_balance);
|
|
592
|
+ let final_balance = self
|
|
593
|
+ .final_accounts_balance
|
|
594
|
+ .saturating_add(self.outgoing_receipts_balance)
|
|
595
|
+ .saturating_add(self.new_delayed_receipts_balance)
|
|
596
|
+ .saturating_add(self.final_postponed_receipts_balance)
|
|
597
|
+ .saturating_add(self.tx_burnt_amount)
|
|
598
|
+ .saturating_add(self.slashed_burnt_amount)
|
|
599
|
+ .saturating_add(self.other_burnt_amount);
|
|
600
|
+ write!(
|
|
601
|
+ f,
|
|
602
|
+ "Balance Mismatch Error. The input balance {} doesn't match output balance {}\n\
|
|
603
|
+ Inputs:\n\
|
|
604
|
+ \tIncoming validator rewards sum: {}\n\
|
|
605
|
+ \tInitial accounts balance sum: {}\n\
|
|
606
|
+ \tIncoming receipts balance sum: {}\n\
|
|
607
|
+ \tProcessed delayed receipts balance sum: {}\n\
|
|
608
|
+ \tInitial postponed receipts balance sum: {}\n\
|
|
609
|
+ Outputs:\n\
|
|
610
|
+ \tFinal accounts balance sum: {}\n\
|
|
611
|
+ \tOutgoing receipts balance sum: {}\n\
|
|
612
|
+ \tNew delayed receipts balance sum: {}\n\
|
|
613
|
+ \tFinal postponed receipts balance sum: {}\n\
|
|
614
|
+ \tTx fees burnt amount: {}\n\
|
|
615
|
+ \tSlashed amount: {}\n\
|
|
616
|
+ \tOther burnt amount: {}",
|
|
617
|
+ initial_balance,
|
|
618
|
+ final_balance,
|
|
619
|
+ self.incoming_validator_rewards,
|
|
620
|
+ self.initial_accounts_balance,
|
|
621
|
+ self.incoming_receipts_balance,
|
|
622
|
+ self.processed_delayed_receipts_balance,
|
|
623
|
+ self.initial_postponed_receipts_balance,
|
|
624
|
+ self.final_accounts_balance,
|
|
625
|
+ self.outgoing_receipts_balance,
|
|
626
|
+ self.new_delayed_receipts_balance,
|
|
627
|
+ self.final_postponed_receipts_balance,
|
|
628
|
+ self.tx_burnt_amount,
|
|
629
|
+ self.slashed_burnt_amount,
|
|
630
|
+ self.other_burnt_amount,
|
|
631
|
+ )
|
|
632
|
+ }
|
|
633
|
+}
|
|
634
|
+
|
|
635
|
+impl std::error::Error for BalanceMismatchError {}
|
|
636
|
+
|
|
637
|
+#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq)]
|
|
638
|
+pub struct IntegerOverflowError;
|
|
639
|
+
|
|
640
|
+impl std::fmt::Display for IntegerOverflowError {
|
|
641
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
642
|
+ f.write_str(&format!("{:?}", self))
|
|
643
|
+ }
|
|
644
|
+}
|
|
645
|
+
|
|
646
|
+impl std::error::Error for IntegerOverflowError {}
|
|
647
|
+
|
|
648
|
+impl From<IntegerOverflowError> for InvalidTxError {
|
|
649
|
+ fn from(_: IntegerOverflowError) -> Self {
|
|
650
|
+ InvalidTxError::CostOverflow
|
|
651
|
+ }
|
|
652
|
+}
|
|
653
|
+
|
|
654
|
+impl From<IntegerOverflowError> for RuntimeError {
|
|
655
|
+ fn from(_: IntegerOverflowError) -> Self {
|
|
656
|
+ RuntimeError::UnexpectedIntegerOverflow
|
|
657
|
+ }
|
|
658
|
+}
|
|
659
|
+
|
|
660
|
+impl From<StorageError> for RuntimeError {
|
|
661
|
+ fn from(e: StorageError) -> Self {
|
|
662
|
+ RuntimeError::StorageError(e)
|
|
663
|
+ }
|
|
664
|
+}
|
|
665
|
+
|
|
666
|
+impl From<BalanceMismatchError> for RuntimeError {
|
|
667
|
+ fn from(e: BalanceMismatchError) -> Self {
|
|
668
|
+ RuntimeError::BalanceMismatchError(e)
|
|
669
|
+ }
|
|
670
|
+}
|
|
671
|
+
|
|
672
|
+impl From<InvalidTxError> for RuntimeError {
|
|
673
|
+ fn from(e: InvalidTxError) -> Self {
|
|
674
|
+ RuntimeError::InvalidTxError(e)
|
|
675
|
+ }
|
|
676
|
+}
|
|
677
|
+
|
|
678
|
+impl From<EpochError> for RuntimeError {
|
|
679
|
+ fn from(e: EpochError) -> Self {
|
|
680
|
+ RuntimeError::ValidatorError(e)
|
|
681
|
+ }
|
|
682
|
+}
|
|
683
|
+
|
|
684
|
+impl Display for ActionError {
|
|
685
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
686
|
+ write!(
|
|
687
|
+ f,
|
|
688
|
+ "Action #{}: {}",
|
|
689
|
+ self.index.unwrap_or_default(),
|
|
690
|
+ self.kind
|
|
691
|
+ )
|
|
692
|
+ }
|
|
693
|
+}
|
|
694
|
+
|
|
695
|
+impl Display for ActionErrorKind {
|
|
696
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
|
697
|
+ match self {
|
|
698
|
+ ActionErrorKind::AccountAlreadyExists { account_id } => {
|
|
699
|
+ write!(f, "Can't create a new account {:?}, because it already exists", account_id)
|
|
700
|
+ }
|
|
701
|
+ ActionErrorKind::AccountDoesNotExist { account_id } => write!(
|
|
702
|
+ f,
|
|
703
|
+ "Can't complete the action because account {:?} doesn't exist",
|
|
704
|
+ account_id
|
|
705
|
+ ),
|
|
706
|
+ ActionErrorKind::ActorNoPermission { actor_id, account_id } => write!(
|
|
707
|
+ f,
|
|
708
|
+ "Actor {:?} doesn't have permission to account {:?} to complete the action",
|
|
709
|
+ actor_id, account_id
|
|
710
|
+ ),
|
|
711
|
+ ActionErrorKind::LackBalanceForState { account_id, amount } => write!(
|
|
712
|
+ f,
|
|
713
|
+ "The account {} wouldn't have enough balance to cover storage, required to have {} yoctoNEAR more",
|
|
714
|
+ account_id, amount
|
|
715
|
+ ),
|
|
716
|
+ ActionErrorKind::TriesToUnstake { account_id } => {
|
|
717
|
+ write!(f, "Account {:?} is not yet staked, but tries to unstake", account_id)
|
|
718
|
+ }
|
|
719
|
+ ActionErrorKind::TriesToStake { account_id, stake, locked, balance } => write!(
|
|
720
|
+ f,
|
|
721
|
+ "Account {:?} tries to stake {}, but has staked {} and only has {}",
|
|
722
|
+ account_id, stake, locked, balance
|
|
723
|
+ ),
|
|
724
|
+ ActionErrorKind::CreateAccountOnlyByRegistrar { account_id, registrar_account_id, predecessor_id } => write!(
|
|
725
|
+ f,
|
|
726
|
+ "A top-level account ID {:?} can't be created by {:?}, short top-level account IDs can only be created by {:?}",
|
|
727
|
+ account_id, predecessor_id, registrar_account_id,
|
|
728
|
+ ),
|
|
729
|
+ ActionErrorKind::CreateAccountNotAllowed { account_id, predecessor_id } => write!(
|
|
730
|
+ f,
|
|
731
|
+ "A sub-account ID {:?} can't be created by account {:?}",
|
|
732
|
+ account_id, predecessor_id,
|
|
733
|
+ ),
|
|
734
|
+ ActionErrorKind::DeleteKeyDoesNotExist { account_id, .. } => write!(
|
|
735
|
+ f,
|
|
736
|
+ "Account {:?} tries to remove an access key that doesn't exist",
|
|
737
|
+ account_id
|
|
738
|
+ ),
|
|
739
|
+ ActionErrorKind::AddKeyAlreadyExists { public_key, .. } => write!(
|
|
740
|
+ f,
|
|
741
|
+ "The public key {:?} is already used for an existing access key",
|
|
742
|
+ public_key
|
|
743
|
+ ),
|
|
744
|
+ ActionErrorKind::DeleteAccountStaking { account_id } => {
|
|
745
|
+ write!(f, "Account {:?} is staking and can not be deleted", account_id)
|
|
746
|
+ }
|
|
747
|
+ ActionErrorKind::InsufficientStake { account_id, stake, minimum_stake } => write!(f, "Account {} tries to stake {} but minimum required stake is {}", account_id, stake, minimum_stake),
|
|
748
|
+ ActionErrorKind::OnlyImplicitAccountCreationAllowed { account_id } => write!(f, "CreateAccount action is called on hex-characters account of length 64 {}", account_id),
|
|
749
|
+ ActionErrorKind::DeleteAccountWithLargeState { account_id } => write!(f, "The state of account {} is too large and therefore cannot be deleted", account_id),
|
|
750
|
+ }
|
|
751
|
+ }
|
|
752
|
+}
|
|
753
|
+
|
|
754
|
+#[derive(Eq, PartialEq, Clone)]
|
|
755
|
+pub enum EpochError {
|
|
756
|
+ /// Error calculating threshold from given stakes for given number of seats.
|
|
757
|
+ /// Only should happened if calling code doesn't check for integer value of stake > number of seats.
|
|
758
|
+ ThresholdError { stake_sum: Balance, num_seats: u64 },
|
|
759
|
+ /// Missing block hash in the storage (means there is some structural issue).
|
|
760
|
+ MissingBlock(CryptoHash),
|
|
761
|
+ /// Error due to IO (DB read/write, serialization, etc.).
|
|
762
|
+ IOErr(String),
|
|
763
|
+ /// Error getting information for a shard
|
|
764
|
+ ShardingError(String),
|
|
765
|
+ NotEnoughValidators {
|
|
766
|
+ num_validators: u64,
|
|
767
|
+ num_shards: u64,
|
|
768
|
+ },
|
|
769
|
+}
|
|
770
|
+
|
|
771
|
+impl std::error::Error for EpochError {}
|
|
772
|
+
|
|
773
|
+impl Display for EpochError {
|
|
774
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
775
|
+ match self {
|
|
776
|
+ EpochError::ThresholdError {
|
|
777
|
+ stake_sum,
|
|
778
|
+ num_seats,
|
|
779
|
+ } => write!(
|
|
780
|
+ f,
|
|
781
|
+ "Total stake {} must be higher than the number of seats {}",
|
|
782
|
+ stake_sum, num_seats
|
|
783
|
+ ),
|
|
784
|
+ EpochError::MissingBlock(hash) => write!(f, "Missing block {}", hash),
|
|
785
|
+ EpochError::IOErr(err) => write!(f, "IO: {}", err),
|
|
786
|
+ EpochError::ShardingError(err) => write!(f, "Sharding Error: {}", err),
|
|
787
|
+ EpochError::NotEnoughValidators {
|
|
788
|
+ num_shards,
|
|
789
|
+ num_validators,
|
|
790
|
+ } => {
|
|
791
|
+ write!(f, "There were not enough validator proposals to fill all shards. num_proposals: {}, num_shards: {}", num_validators, num_shards)
|
|
792
|
+ }
|
|
793
|
+ }
|
|
794
|
+ }
|
|
795
|
+}
|
|
796
|
+
|
|
797
|
+impl Debug for EpochError {
|
|
798
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
799
|
+ match self {
|
|
800
|
+ EpochError::ThresholdError {
|
|
801
|
+ stake_sum,
|
|
802
|
+ num_seats,
|
|
803
|
+ } => {
|
|
804
|
+ write!(f, "ThresholdError({}, {})", stake_sum, num_seats)
|
|
805
|
+ }
|
|
806
|
+ EpochError::MissingBlock(hash) => write!(f, "MissingBlock({})", hash),
|
|
807
|
+ EpochError::IOErr(err) => write!(f, "IOErr({})", err),
|
|
808
|
+ EpochError::ShardingError(err) => write!(f, "ShardingError({})", err),
|
|
809
|
+ EpochError::NotEnoughValidators {
|
|
810
|
+ num_shards,
|
|
811
|
+ num_validators,
|
|
812
|
+ } => {
|
|
813
|
+ write!(f, "NotEnoughValidators({}, {})", num_validators, num_shards)
|
|
814
|
+ }
|
|
815
|
+ }
|
|
816
|
+ }
|
|
817
|
+}
|
|
818
|
+
|
|
819
|
+impl From<std::io::Error> for EpochError {
|
|
820
|
+ fn from(error: std::io::Error) -> Self {
|
|
821
|
+ EpochError::IOErr(error.to_string())
|
|
822
|
+ }
|
|
823
|
+}
|