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
pub mod address;
pub mod base58;
pub mod bip32;
pub mod contracthash;
pub mod decimal;
pub mod hash;
pub mod iter;
pub mod misc;
pub mod patricia_tree;
pub mod uint;
use byteorder;
use std::{error, fmt, io};
pub trait BitArray {
fn bit(&self, idx: usize) -> bool;
fn bit_slice(&self, start: usize, end: usize) -> Self;
fn mask(&self, n: usize) -> Self;
fn trailing_zeros(&self) -> usize;
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
ByteOrder(byteorder::Error),
BadNetworkMagic(u32, u32),
BadNetworkMessage(String),
DuplicateHash,
BlockNotFound,
ParseFailed,
PrevHashNotFound,
SpvBadTarget,
SpvBadProofOfWork,
Detail(String, Box<Error>)
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref e) => fmt::Display::fmt(e, f),
Error::ByteOrder(ref e) => fmt::Display::fmt(e, f),
Error::BadNetworkMagic(exp, got) => write!(f, "expected network magic 0x{:x}, got 0x{:x}", exp, got),
Error::BadNetworkMessage(ref got) => write!(f, "incorrect network message {}", got),
Error::Detail(ref s, ref e) => write!(f, "{}: {}", s, e),
ref x => f.write_str(error::Error::description(x))
}
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::ByteOrder(ref e) => Some(e),
Error::Detail(_, ref e) => Some(e),
_ => None
}
}
fn description(&self) -> &str {
match *self {
Error::Io(ref e) => e.description(),
Error::ByteOrder(ref e) => e.description(),
Error::BadNetworkMagic(_, _) => "incorrect network magic",
Error::BadNetworkMessage(_) => "incorrect/unexpected network message",
Error::DuplicateHash => "duplicate hash",
Error::BlockNotFound => "no such block",
Error::ParseFailed => "parsing error",
Error::PrevHashNotFound => "prevhash not found",
Error::SpvBadTarget => "target incorrect",
Error::SpvBadProofOfWork => "target correct but not attained",
Error::Detail(_, ref e) => e.description()
}
}
}
pub fn propagate_err<T>(s: String, res: Result<T, Error>) -> Result<T, Error> {
res.map_err(|err| Error::Detail(s, Box::new(err)))
}