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
use std::cmp;
use std::ops::Range;
use serde_json::Value;
use storage::ListProof;
use crypto::Hash;
use blockchain::{Schema, Blockchain, Block, TxLocation, TransactionResult, TransactionErrorType};
use messages::Precommit;
use api::ApiError;
use helpers::Height;
#[derive(Debug)]
pub struct BlockchainExplorer<'a> {
blockchain: &'a Blockchain,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockInfo {
pub block: Block,
pub precommits: Vec<Precommit>,
pub txs: Vec<Hash>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TxInfo {
pub content: Value,
pub location: TxLocation,
pub location_proof: ListProof<Hash>,
pub status: TxStatus,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum TxStatus {
Success,
Panic {
description: String,
},
Error {
code: u8,
description: String,
},
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlocksRange {
pub range: Range<u64>,
pub blocks: Vec<Block>,
}
impl<'a> BlockchainExplorer<'a> {
pub fn new(blockchain: &'a Blockchain) -> Self {
BlockchainExplorer { blockchain }
}
pub fn tx_info(&self, tx_hash: &Hash) -> Result<Option<TxInfo>, ApiError> {
let schema = Schema::new(self.blockchain.snapshot());
let raw_tx = match schema.transactions().get(tx_hash) {
Some(val) => val,
None => {
return Ok(None);
}
};
let box_transaction = self.blockchain.tx_from_raw(raw_tx.clone()).ok_or_else(|| {
ApiError::InternalError(format!("Service not found for tx: {:?}", raw_tx).into())
})?;
let content = box_transaction.serialize_field().map_err(
ApiError::InternalError,
)?;
let location = schema.tx_location_by_tx_hash().get(tx_hash).expect(
&format!(
"Not found tx_hash location: {:?}",
tx_hash
),
);
let location_proof = schema.block_txs(location.block_height()).get_proof(
location.position_in_block(),
);
let status = match schema.transaction_results().get(tx_hash).unwrap() {
Ok(()) => TxStatus::Success,
Err(e) => {
let description = e.description().unwrap_or_default().to_owned();
match e.error_type() {
TransactionErrorType::Panic => TxStatus::Panic { description },
TransactionErrorType::Code(code) => TxStatus::Error { code, description },
}
}
};
Ok(Some(TxInfo {
content,
location,
location_proof,
status,
}))
}
pub fn block_info(&self, height: Height) -> Option<BlockInfo> {
let schema = Schema::new(self.blockchain.snapshot());
let txs_table = schema.block_txs(height);
let block_proof = schema.block_and_precommits(height);
match block_proof {
None => None,
Some(proof) => {
let bl = BlockInfo {
block: proof.block,
precommits: proof.precommits,
txs: txs_table.iter().collect(),
};
Some(bl)
}
}
}
pub fn blocks_range(
&self,
count: u64,
upper: Option<u64>,
skip_empty_blocks: bool,
) -> BlocksRange {
let schema = Schema::new(self.blockchain.snapshot());
let hashes = schema.block_hashes_by_height();
let blocks = schema.blocks();
let max_height = hashes.len() - 1;
let upper = upper.map(|x| cmp::min(x, max_height)).unwrap_or(max_height);
let mut height = upper + 1;
let mut genesis = false;
let mut v = Vec::new();
let mut collected: u64 = 0;
loop {
if genesis || (collected == count) {
break;
}
height -= 1;
genesis = height == 0;
let block_txs = schema.block_txs(Height(height));
if skip_empty_blocks && block_txs.is_empty() {
continue;
}
let block_hash = hashes.get(height).expect(&format!(
"Block not found, height:{:?}",
height
));
let block = blocks.get(&block_hash).expect(&format!(
"Block not found, hash:{:?}",
block_hash
));
v.push(block);
collected += 1;
}
BlocksRange {
range: height..upper + 1,
blocks: v,
}
}
pub fn transaction_result(&self, hash: &Hash) -> Option<TransactionResult> {
let schema = Schema::new(self.blockchain.snapshot());
schema.transaction_results().get(hash)
}
}