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
464
465
466
467
468
469
use std::io;

use chrono::DateTime;
use chrono::Utc;

use unc_primitives::block::BlockValidityError;
use unc_primitives::challenge::{ChunkProofs, ChunkState};
use unc_primitives::errors::{BlockError, EpochError, StorageError};
use unc_primitives::hash::CryptoHash;
use unc_primitives::shard_layout::ShardLayoutError;
use unc_primitives::sharding::{ChunkHash, ShardChunkHeader};
use unc_primitives::types::{BlockHeight, EpochId, ShardId};

#[derive(thiserror::Error, Debug)]
pub enum QueryError {
    #[error("Account ID {requested_account_id} is invalid")]
    InvalidAccount {
        requested_account_id: unc_primitives::types::AccountId,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error("Account {requested_account_id} does not exist while viewing")]
    UnknownAccount {
        requested_account_id: unc_primitives::types::AccountId,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error(
        "Contract code for contract ID {contract_account_id} has never been observed on the node"
    )]
    NoContractCode {
        contract_account_id: unc_primitives::types::AccountId,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error("Access key for public key {public_key} does not exist while viewing")]
    UnknownAccessKey {
        public_key: unc_crypto::PublicKey,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error("Internal error occurred: {error_message}")]
    InternalError {
        error_message: String,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error("Function call returned an error: {error_message}")]
    ContractExecutionError {
        error_message: String,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
    #[error("The state of account {requested_account_id} is too large")]
    TooLargeContractState {
        requested_account_id: unc_primitives::types::AccountId,
        block_height: unc_primitives::types::BlockHeight,
        block_hash: unc_primitives::hash::CryptoHash,
    },
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// The block is already known
    #[error("Block is known: {0}")]
    BlockKnown(#[from] BlockKnownError),
    #[error("Too many blocks being processed")]
    TooManyProcessingBlocks,
    /// Orphan block.
    #[error("Orphan")]
    Orphan,
    /// Chunk is missing.
    #[error("Chunk Missing (unavailable on the node): {0:?}")]
    ChunkMissing(ChunkHash),
    /// Chunks missing with header info.
    #[error("Chunks Missing: {0:?}")]
    ChunksMissing(Vec<ShardChunkHeader>),
    /// Block time is before parent block time.
    #[error("Invalid Block Time: block time {1} before previous {0}")]
    InvalidBlockPastTime(DateTime<Utc>, DateTime<Utc>),
    /// Block time is from too much in the future.
    #[error("Invalid Block Time: Too far in the future: {0}")]
    InvalidBlockFutureTime(DateTime<Utc>),
    /// Block height is invalid (not previous + 1).
    #[error("Invalid Block Height {0}")]
    InvalidBlockHeight(BlockHeight),
    /// Invalid block proposed signature.
    #[error("Invalid Block Proposer Signature")]
    InvalidBlockProposer,
    /// Invalid state root hash.
    #[error("Invalid State Root Hash")]
    InvalidStateRoot,
    /// Invalid block tx root hash.
    #[error("Invalid Block Tx Root Hash")]
    InvalidTxRoot,
    /// Invalid chunk receipts root hash.
    #[error("Invalid Chunk Receipts Root Hash")]
    InvalidChunkReceiptsRoot,
    /// Invalid chunk headers root hash.
    #[error("Invalid Chunk Headers Root Hash")]
    InvalidChunkHeadersRoot,
    /// Invalid chunk tx root hash.
    #[error("Invalid Chunk Tx Root Hash")]
    InvalidChunkTxRoot,
    /// Invalid receipts proof.
    #[error("Invalid Receipts Proof")]
    InvalidReceiptsProof,
    /// Invalid outcomes proof.
    #[error("Invalid Outcomes Proof")]
    InvalidOutcomesProof,
    /// Invalid state payload on state sync.
    #[error("Invalid State Payload")]
    InvalidStatePayload,
    /// Invalid transactions in the block.
    #[error("Invalid Transactions")]
    InvalidTransactions,
    /// Invalid Challenge Root (doesn't match actual challenge)
    #[error("Invalid Challenge Root")]
    InvalidChallengeRoot,
    /// Invalid challenge (wrong signature or format).
    #[error("Invalid Challenge")]
    InvalidChallenge,
    /// Incorrect (malicious) challenge (slash the sender).
    #[error("Malicious Challenge")]
    MaliciousChallenge,
    /// Incorrect number of chunk headers
    #[error("Incorrect Number of Chunk Headers")]
    IncorrectNumberOfChunkHeaders,
    /// Invalid chunk.
    #[error("Invalid Chunk")]
    InvalidChunk,
    /// One of the chunks has invalid proofs
    #[error("Invalid Chunk Proofs")]
    InvalidChunkProofs(Box<ChunkProofs>),
    /// Invalid chunk state.
    #[error("Invalid Chunk State")]
    InvalidChunkState(Box<ChunkState>),
    #[error("Invalid Chunk State Witness")]
    InvalidChunkStateWitness(String),
    /// Invalid chunk mask
    #[error("Invalid Chunk Mask")]
    InvalidChunkMask,
    /// The chunk height is outside of the horizon
    #[error("Invalid Chunk Height")]
    InvalidChunkHeight,
    /// Invalid epoch hash
    #[error("Invalid Epoch Hash")]
    InvalidEpochHash,
    /// `next_bps_hash` doens't correspond to the actual next block producers set
    #[error("Invalid Next BP Hash")]
    InvalidNextBPHash,
    /// The block has a protocol version that's outdated
    #[error("Invalid protocol version")]
    InvalidProtocolVersion,
    /// The block doesn't have approvals from 50% of the block producers
    #[error("Not enough approvals")]
    NotEnoughApprovals,
    /// The information about the last final block is incorrect
    #[error("Invalid finality info")]
    InvalidFinalityInfo,
    /// Invalid validator proposals in the block.
    #[error("Invalid Validator Proposals")]
    InvalidValidatorProposals,
    /// Invalid Signature
    #[error("Invalid Signature")]
    InvalidSignature,
    /// Invalid Approvals
    #[error("Invalid Approvals")]
    InvalidApprovals,
    /// Invalid Gas Limit
    #[error("Invalid Gas Limit")]
    InvalidGasLimit,
    /// Invalid Gas Price
    #[error("Invalid Gas Price")]
    InvalidGasPrice,
    /// Invalid Gas Used
    #[error("Invalid Gas Used")]
    InvalidGasUsed,
    /// Invalid Balance Burnt
    #[error("Invalid Balance Burnt")]
    InvalidBalanceBurnt,
    /// Invalid shard id
    #[error("Shard id {0} does not exist")]
    InvalidShardId(ShardId),
    /// Invalid shard id
    #[error("Invalid state request: {0}")]
    InvalidStateRequest(String),
    /// Invalid VRF proof, or incorrect random_output in the header
    #[error("Invalid Randomness Beacon Output")]
    InvalidRandomnessBeaconOutput,
    /// Invalid block merkle root.
    #[error("Invalid Block Merkle Root")]
    InvalidBlockMerkleRoot,
    /// Invalid split shard ids.
    #[error("Invalid Split Shard Ids when resharding. shard_id: {0}, parent_shard_id: {1}")]
    InvalidSplitShardsIds(u64, u64),
    /// Someone is not a validator. Usually happens in signature verification
    #[error("Not A Validator")]
    NotAValidator,
    /// Someone is not a chunk validator. Happens if we're asked to validate a chunk we're not
    /// supposed to validate, or to verify a chunk approval signed by a validator that isn't
    /// supposed to validate the chunk.
    #[error("Not A Chunk Validator")]
    NotAChunkValidator,
    /// Validator error.
    #[error("Validator Error: {0}")]
    ValidatorError(String),
    /// Block out of bounds. Usually if received block is too far in the future or alternative fork.
    #[error("Epoch Out Of Bounds: {:?}", _0)]
    BlockOutOfBounds(CryptoHash),
    /// Epoch out of bounds. Usually if received block is too far in the future or alternative fork.
    #[error("Epoch Out Of Bounds: {:?}", _0)]
    EpochOutOfBounds(EpochId),
    /// A challenged block is on the chain that was attempted to become the head
    #[error("Challenged block on chain")]
    ChallengedBlockOnChain,
    /// Block cannot be finalized.
    #[error("Block cannot be finalized")]
    CannotBeFinalized,
    /// IO Error.
    #[error("IO Error: {0}")]
    IOErr(#[from] io::Error),
    /// Not found record in the DB.
    #[error("DB Not Found Error: {0}")]
    DBNotFoundErr(String),
    /// Storage error. Used for internal passing the error.
    #[error("Storage Error: {0}")]
    StorageError(#[from] StorageError),
    /// GC error.
    #[error("GC Error: {0}")]
    GCError(String),
    /// Anything else
    #[error("Other Error: {0}")]
    Other(String),
}

/// For now StorageError can happen at any time from ViewClient because of
/// the used isolation level + running ViewClient in a separate thread.
pub trait LogTransientStorageError {
    fn log_storage_error(self, message: &str) -> Self;
}

impl<T> LogTransientStorageError for Result<T, Error> {
    fn log_storage_error(self, message: &str) -> Self {
        if let Err(err) = &self {
            tracing::error!(target: "client", "Transient storage error: {message}, {err}");
        }
        self
    }
}

impl Error {
    pub fn is_bad_data(&self) -> bool {
        match self {
            Error::BlockKnown(_)
            | Error::TooManyProcessingBlocks
            | Error::Orphan
            | Error::ChunkMissing(_)
            | Error::ChunksMissing(_)
            | Error::InvalidChunkHeight
            | Error::IOErr(_)
            | Error::Other(_)
            | Error::ValidatorError(_)
            | Error::EpochOutOfBounds(_)
            | Error::ChallengedBlockOnChain
            | Error::CannotBeFinalized
            | Error::StorageError(_)
            | Error::GCError(_)
            | Error::DBNotFoundErr(_) => false,
            Error::InvalidBlockPastTime(_, _)
            | Error::InvalidBlockFutureTime(_)
            | Error::InvalidBlockHeight(_)
            | Error::InvalidBlockProposer
            | Error::InvalidChunk
            | Error::InvalidChunkProofs(_)
            | Error::InvalidChunkState(_)
            | Error::InvalidChunkStateWitness(_)
            | Error::InvalidChunkMask
            | Error::InvalidStateRoot
            | Error::InvalidTxRoot
            | Error::InvalidChunkReceiptsRoot
            | Error::InvalidOutcomesProof
            | Error::InvalidChunkHeadersRoot
            | Error::InvalidChunkTxRoot
            | Error::InvalidReceiptsProof
            | Error::InvalidStatePayload
            | Error::InvalidTransactions
            | Error::InvalidChallenge
            | Error::InvalidSplitShardsIds(_, _)
            | Error::MaliciousChallenge
            | Error::IncorrectNumberOfChunkHeaders
            | Error::InvalidEpochHash
            | Error::InvalidNextBPHash
            | Error::NotEnoughApprovals
            | Error::InvalidFinalityInfo
            | Error::InvalidValidatorProposals
            | Error::InvalidSignature
            | Error::InvalidApprovals
            | Error::InvalidGasLimit
            | Error::InvalidGasPrice
            | Error::InvalidGasUsed
            | Error::InvalidBalanceBurnt
            | Error::InvalidShardId(_)
            | Error::InvalidStateRequest(_)
            | Error::InvalidRandomnessBeaconOutput
            | Error::InvalidBlockMerkleRoot
            | Error::InvalidProtocolVersion
            | Error::NotAValidator
            | Error::NotAChunkValidator
	        | Error::BlockOutOfBounds(_)
            | Error::InvalidChallengeRoot => true,
        }
    }

    pub fn is_error(&self) -> bool {
        match self {
            Error::IOErr(_) | Error::Other(_) | Error::DBNotFoundErr(_) => true,
            _ => false,
        }
    }

    /// Some blockchain errors are reported in the prometheus metrics. In such cases a report might
    /// contain a label that specifies the type of error that has occured. For example when the node
    /// receives a block with an invalid signature this would be reported as:
    ///  `unc_num_invalid_blocks{error="invalid_signature"}`.
    /// This function returns the value of the error label for a specific instance of Error.
    pub fn prometheus_label_value(&self) -> &'static str {
        match self {
            Error::BlockKnown(_) => "block_known",
            Error::TooManyProcessingBlocks => "too_many_processing_blocks",
            Error::Orphan => "orphan",
            Error::ChunkMissing(_) => "chunk_missing",
            Error::ChunksMissing(_) => "chunks_missing",
            Error::InvalidChunkHeight => "invalid_chunk_height",
            Error::IOErr(_) => "io_err",
            Error::Other(_) => "other",
            Error::ValidatorError(_) => "validator_error",
            Error::EpochOutOfBounds(_) => "epoch_out_of_bounds",
            Error::ChallengedBlockOnChain => "challenged_block_on_chain",
            Error::CannotBeFinalized => "cannot_be_finalized",
            Error::StorageError(_) => "storage_error",
            Error::GCError(_) => "gc_error",
            Error::DBNotFoundErr(_) => "db_not_found_err",
            Error::InvalidBlockPastTime(_, _) => "invalid_block_past_time",
            Error::InvalidBlockFutureTime(_) => "invalid_block_future_time",
            Error::InvalidBlockHeight(_) => "invalid_block_height",
            Error::InvalidBlockProposer => "invalid_block_proposer",
            Error::InvalidChunk => "invalid_chunk",
            Error::InvalidChunkProofs(_) => "invalid_chunk_proofs",
            Error::InvalidChunkState(_) => "invalid_chunk_state",
            Error::InvalidChunkStateWitness(_) => "invalid_chunk_state_witness",
            Error::InvalidChunkMask => "invalid_chunk_mask",
            Error::InvalidStateRoot => "invalid_state_root",
            Error::InvalidTxRoot => "invalid_tx_root",
            Error::InvalidChunkReceiptsRoot => "invalid_chunk_receipts_root",
            Error::InvalidOutcomesProof => "invalid_outcomes_proof",
            Error::InvalidChunkHeadersRoot => "invalid_chunk_headers_root",
            Error::InvalidChunkTxRoot => "invalid_chunk_tx_root",
            Error::InvalidReceiptsProof => "invalid_receipts_proof",
            Error::InvalidStatePayload => "invalid_state_payload",
            Error::InvalidTransactions => "invalid_transactions",
            Error::InvalidChallenge => "invalid_challenge",
            Error::InvalidSplitShardsIds(_, _) => "invalid_split_shard_ids",
            Error::MaliciousChallenge => "malicious_challenge",
            Error::IncorrectNumberOfChunkHeaders => "incorrect_number_of_chunk_headers",
            Error::InvalidEpochHash => "invalid_epoch_hash",
            Error::InvalidNextBPHash => "invalid_next_bp_hash",
            Error::NotEnoughApprovals => "not_enough_approvals",
            Error::InvalidFinalityInfo => "invalid_finality_info",
            Error::InvalidValidatorProposals => "invalid_validator_proposals",
            Error::InvalidSignature => "invalid_signature",
            Error::InvalidApprovals => "invalid_approvals",
            Error::InvalidGasLimit => "invalid_gas_limit",
            Error::InvalidGasPrice => "invalid_gas_price",
            Error::InvalidGasUsed => "invalid_gas_used",
            Error::InvalidBalanceBurnt => "invalid_balance_burnt",
            Error::InvalidShardId(_) => "invalid_shard_id",
            Error::InvalidStateRequest(_) => "invalid_state_request",
            Error::InvalidRandomnessBeaconOutput => "invalid_randomness_beacon_output",
            Error::InvalidBlockMerkleRoot => "invalid_block_merkele_root",
            Error::InvalidProtocolVersion => "invalid_protocol_version",
            Error::NotAValidator => "not_a_validator",
            Error::NotAChunkValidator => "not_a_chunk_validator",
            Error::InvalidChallengeRoot => "invalid_challenge_root",
            Error::BlockOutOfBounds(_)=> "block_out_of_bounds"
        }
    }
}

impl From<EpochError> for Error {
    fn from(error: EpochError) -> Self {
        match error {
            EpochError::EpochOutOfBounds(epoch_id) => Error::EpochOutOfBounds(epoch_id),
            EpochError::MissingBlock(h) => Error::DBNotFoundErr(format!("epoch block: {h}")),
            EpochError::NotAValidator(_account_id, _epoch_id) => Error::NotAValidator,
            err => Error::Other(err.to_string()),
        }
    }
}

impl From<BlockError> for Error {
    fn from(error: BlockError) -> Self {
        match error {
            err => Error::ValidatorError(err.to_string()),
        }
    }
}

pub trait EpochErrorResultToChainError<T> {
    fn into_chain_error(self) -> Result<T, Error>;
}

impl<T> EpochErrorResultToChainError<T> for Result<T, EpochError> {
    fn into_chain_error(self: Result<T, EpochError>) -> Result<T, Error> {
        self.map_err(|err| err.into())
    }
}

impl From<ShardLayoutError> for Error {
    fn from(error: ShardLayoutError) -> Self {
        match error {
            ShardLayoutError::InvalidShardIdError { shard_id } => Error::InvalidShardId(shard_id),
        }
    }
}

impl From<BlockValidityError> for Error {
    fn from(error: BlockValidityError) -> Self {
        match error {
            BlockValidityError::InvalidStateRoot => Error::InvalidStateRoot,
            BlockValidityError::InvalidReceiptRoot => Error::InvalidChunkReceiptsRoot,
            BlockValidityError::InvalidTransactionRoot => Error::InvalidTxRoot,
            BlockValidityError::InvalidChunkHeaderRoot => Error::InvalidChunkHeadersRoot,
            BlockValidityError::InvalidChunkMask => Error::InvalidChunkMask,
            BlockValidityError::InvalidChallengeRoot => Error::InvalidChallengeRoot,
        }
    }
}

#[derive(Clone, Eq, PartialEq, Debug, thiserror::Error)]
pub enum BlockKnownError {
    #[error("already known in header")]
    KnownInHeader,
    #[error("already known in head")]
    KnownInHead,
    #[error("already known in orphan")]
    KnownInOrphan,
    #[error("already known in missing chunks")]
    KnownInMissingChunks,
    #[error("already known in store")]
    KnownInStore,
    #[error("already known in blocks in processing")]
    KnownInProcessing,
    #[error("already known in invalid blocks")]
    KnownAsInvalid,
}

#[cfg(feature = "new_epoch_sync")]
pub mod epoch_sync {
    #[derive(thiserror::Error, std::fmt::Debug)]
    pub enum EpochSyncInfoError {
        #[error(transparent)]
        EpochSyncInfoErr(#[from] unc_primitives::errors::epoch_sync::EpochSyncInfoError),
        #[error(transparent)]
        IOErr(#[from] std::io::Error),
        #[error(transparent)]
        ChainErr(#[from] crate::Error),
    }
}