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
mod utils;
mod settlement;
mod backend;

pub use self::backend::RocksBackend;

use std::{fmt, error as stderror};
use std::sync::Arc;
use parity_codec::{Encode, Decode};
use rocksdb::DB;
use blockchain::backend::OperationError;

#[derive(Debug)]
/// RocksDB backend errors
pub enum Error {
	/// Invalid Operation
	InvalidOperation,
	/// Trying to import a block that is genesis
	IsGenesis,
	/// Query does not exist
	NotExist,
	/// Corrupted database,
	Corrupted,
	/// RocksDB errors
	Rocks(rocksdb::Error),
}

impl From<rocksdb::Error> for Error {
	fn from(error: rocksdb::Error) -> Error {
		Error::Rocks(error)
	}
}

impl OperationError for Error {
	fn invalid_operation() -> Self {
		Error::InvalidOperation
	}

	fn block_is_genesis() -> Self {
		Error::IsGenesis
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{:?}", self)
	}
}

impl stderror::Error for Error { }

impl From<Error> for blockchain::import::Error {
	fn from(error: Error) -> Self {
		match error {
			Error::IsGenesis => blockchain::import::Error::IsGenesis,
			error => blockchain::import::Error::Backend(Box::new(error)),
		}
	}
}

pub trait RocksState {
	type Raw: Encode + Decode;

	fn from_raw(raw: Self::Raw, db: Arc<DB>) -> Self;
	fn into_raw(self) -> Self::Raw;
}