@@ -34,12 +34,8 @@ impl<'a> Blockstore<'a> {
34
34
let reader = self . db . reader ( ) ?;
35
35
let packed = reader
36
36
. get ( block_id. as_bytes ( ) )
37
- . ok_or_else ( || DatabaseError :: NotFoundError ( format ! ( "Block not found: {block_id}" ) ) ) ?;
38
- let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
39
- DatabaseError :: CorruptionError ( format ! (
40
- "Could not interpret stored data as a block: {err}"
41
- ) )
42
- } ) ?;
37
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Block not found: {block_id}" ) ) ) ?;
38
+ let block: Block = Message :: parse_from_bytes ( & packed) ?;
43
39
Ok ( block)
44
40
}
45
41
@@ -49,18 +45,13 @@ impl<'a> Blockstore<'a> {
49
45
let block_id = reader
50
46
. index_get ( "index_block_num" , block_num. as_bytes ( ) )
51
47
. and_then ( |block_id| {
52
- block_id. ok_or_else ( || {
53
- DatabaseError :: NotFoundError ( format ! ( "Block not found: {height}" ) )
54
- } )
48
+ block_id
49
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Block not found: {height}" ) ) )
55
50
} ) ?;
56
- let packed = reader. get ( & block_id) . ok_or_else ( || {
57
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id:?}" ) )
58
- } ) ?;
59
- let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
60
- DatabaseError :: CorruptionError ( format ! (
61
- "Could not interpret stored data as a block: {err}"
62
- ) )
63
- } ) ?;
51
+ let packed = reader
52
+ . get ( & block_id)
53
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Block not found: {block_id:?}" ) ) ) ?;
54
+ let block: Block = Message :: parse_from_bytes ( & packed) ?;
64
55
Ok ( block)
65
56
}
66
57
@@ -69,18 +60,13 @@ impl<'a> Blockstore<'a> {
69
60
let block_id = reader
70
61
. index_get ( "index_batch" , batch_id. as_bytes ( ) )
71
62
. and_then ( |block_id| {
72
- block_id. ok_or_else ( || {
73
- DatabaseError :: NotFoundError ( format ! ( "Batch not found: {batch_id}" ) )
74
- } )
63
+ block_id
64
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Batch not found: {batch_id}" ) ) )
75
65
} ) ?;
76
- let packed = reader. get ( & block_id) . ok_or_else ( || {
77
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id:?}" ) )
78
- } ) ?;
79
- let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
80
- DatabaseError :: CorruptionError ( format ! (
81
- "Could not interpret stored data as a block: {err}"
82
- ) )
83
- } ) ?;
66
+ let packed = reader
67
+ . get ( & block_id)
68
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Block not found: {block_id:?}" ) ) ) ?;
69
+ let block: Block = Message :: parse_from_bytes ( & packed) ?;
84
70
Ok ( block)
85
71
}
86
72
@@ -90,30 +76,21 @@ impl<'a> Blockstore<'a> {
90
76
. index_get ( "index_transaction" , transaction_id. as_bytes ( ) )
91
77
. and_then ( |block_id| {
92
78
block_id. ok_or_else ( || {
93
- DatabaseError :: NotFoundError ( format ! ( "Transaction not found: {transaction_id}" ) )
79
+ DatabaseError :: NotFound ( format ! ( "Transaction not found: {transaction_id}" ) )
94
80
} )
95
81
} ) ?;
96
- let packed = reader. get ( & block_id) . ok_or_else ( || {
97
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id:?}" ) )
98
- } ) ?;
99
- let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
100
- DatabaseError :: CorruptionError ( format ! (
101
- "Could not interpret stored data as a block: {err}"
102
- ) )
103
- } ) ?;
82
+ let packed = reader
83
+ . get ( & block_id)
84
+ . ok_or_else ( || DatabaseError :: NotFound ( format ! ( "Block not found: {block_id:?}" ) ) ) ?;
85
+ let block: Block = Message :: parse_from_bytes ( & packed) ?;
104
86
Ok ( block)
105
87
}
106
88
107
89
pub fn put ( & self , block : & Block ) -> Result < ( ) , DatabaseError > {
108
- let block_header: BlockHeader =
109
- Message :: parse_from_bytes ( & block. header ) . map_err ( |err| {
110
- DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {err}" ) )
111
- } ) ?;
90
+ let block_header: BlockHeader = Message :: parse_from_bytes ( & block. header ) ?;
112
91
let mut writer = self . db . writer ( ) ?;
113
92
// Add block to main db
114
- let packed = block. write_to_bytes ( ) . map_err ( |err| {
115
- DatabaseError :: WriterError ( format ! ( "Failed to serialize block: {err}" ) )
116
- } ) ?;
93
+ let packed = block. write_to_bytes ( ) ?;
117
94
writer. put ( block. header_signature . as_bytes ( ) , & packed) ?;
118
95
119
96
// Add block to block num index
@@ -149,10 +126,8 @@ impl<'a> Blockstore<'a> {
149
126
pub fn delete ( & self , block_id : & str ) -> Result < ( ) , DatabaseError > {
150
127
let block = self . get ( block_id) ?;
151
128
let block_id = & block. header_signature ;
152
- let block_header: BlockHeader =
153
- Message :: parse_from_bytes ( & block. header ) . map_err ( |err| {
154
- DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {err}" ) )
155
- } ) ?;
129
+ let block_header: BlockHeader = Message :: parse_from_bytes ( & block. header ) ?;
130
+
156
131
// Delete block from main db
157
132
let mut writer = self . db . writer ( ) ?;
158
133
writer. delete ( block_id. as_bytes ( ) ) ?;
@@ -181,10 +156,8 @@ impl<'a> Blockstore<'a> {
181
156
let mut cursor = reader. index_cursor ( "index_block_num" ) ?;
182
157
let ( _, val) = cursor
183
158
. last ( )
184
- . ok_or_else ( || DatabaseError :: NotFoundError ( "No chain head" . into ( ) ) ) ?;
185
- String :: from_utf8 ( val) . map_err ( |err| {
186
- DatabaseError :: CorruptionError ( format ! ( "Chain head block id is corrupt: {err}" ) )
187
- } )
159
+ . ok_or_else ( || DatabaseError :: NotFound ( "No chain head" . into ( ) ) ) ?;
160
+ Ok ( String :: from_utf8 ( val) ?)
188
161
}
189
162
190
163
// Get the number of blocks
@@ -231,23 +204,19 @@ mod tests {
231
204
/// deleting, and looking up blocks), making assertions about the
232
205
/// blockstore contents at each step.
233
206
#[ test]
234
- fn test_blockstore ( ) {
207
+ fn test_blockstore ( ) -> Result < ( ) , DatabaseError > {
235
208
let path_config = config:: get_path_config ( ) ;
236
209
237
210
let blockstore_path = & path_config. data_dir . join ( config:: get_blockstore_filename ( ) ) ;
238
211
239
212
// Set the file size to 10MB, so as to support file systems that do
240
213
// not support sparse files.
241
- let ctx = LmdbContext :: new ( blockstore_path, 3 , Some ( 10 * 1024 * 1024 ) )
242
- . map_err ( |err| DatabaseError :: InitError ( format ! ( "{err}" ) ) )
243
- . unwrap ( ) ;
214
+ let ctx = LmdbContext :: new ( blockstore_path, 3 , Some ( 10 * 1024 * 1024 ) ) ?;
244
215
245
216
let database = LmdbDatabase :: new (
246
217
& ctx,
247
218
& [ "index_batch" , "index_transaction" , "index_block_num" ] ,
248
- )
249
- . map_err ( |err| DatabaseError :: InitError ( format ! ( "{err}" ) ) )
250
- . unwrap ( ) ;
219
+ ) ?;
251
220
252
221
let blockstore = Blockstore :: new ( database) ;
253
222
@@ -260,9 +229,9 @@ mod tests {
260
229
block. set_header_signature ( format ! ( "block-{i}" ) ) ;
261
230
let mut header = BlockHeader :: new ( ) ;
262
231
header. set_block_num ( i) ;
263
- block. set_header ( header. write_to_bytes ( ) . unwrap ( ) ) ;
232
+ block. set_header ( header. write_to_bytes ( ) ? ) ;
264
233
265
- blockstore. put ( & block) . unwrap ( ) ;
234
+ blockstore. put ( & block) ? ;
266
235
267
236
assert_current_height ( i as usize + 1 , & blockstore) ;
268
237
assert_chain_head ( format ! ( "block-{i}" ) , & blockstore) ;
@@ -272,13 +241,13 @@ mod tests {
272
241
273
242
// Check that the blocks are in the right order.
274
243
for i in 0 ..5 {
275
- let block = blockstore. get_by_height ( i) . unwrap ( ) ;
244
+ let block = blockstore. get_by_height ( i) ? ;
276
245
277
246
assert_header_signature ( block, format ! ( "block-{i}" ) ) ;
278
247
}
279
248
280
249
// Get a block.
281
- let get_block = blockstore. get ( "block-2" ) . unwrap ( ) ;
250
+ let get_block = blockstore. get ( "block-2" ) ? ;
282
251
283
252
assert_header_signature ( get_block, String :: from ( "block-2" ) ) ;
284
253
@@ -290,32 +259,34 @@ mod tests {
290
259
batch. set_header_signature ( String :: from ( "batch" ) ) ;
291
260
batch. set_transactions ( protobuf:: RepeatedField :: from_vec ( vec ! [ transaction] ) ) ;
292
261
let batch_header = BatchHeader :: new ( ) ;
293
- batch. set_header ( batch_header. write_to_bytes ( ) . unwrap ( ) ) ;
262
+ batch. set_header ( batch_header. write_to_bytes ( ) ? ) ;
294
263
295
264
let mut block = Block :: new ( ) ;
296
265
block. set_header_signature ( String :: from ( "block-with-batch" ) ) ;
297
266
let mut block_header = BlockHeader :: new ( ) ;
298
267
block_header. set_block_num ( 6 ) ;
299
- block. set_header ( block_header. write_to_bytes ( ) . unwrap ( ) ) ;
268
+ block. set_header ( block_header. write_to_bytes ( ) ? ) ;
300
269
block. set_batches ( protobuf:: RepeatedField :: from_vec ( vec ! [ batch] ) ) ;
301
270
302
- blockstore. put ( & block) . unwrap ( ) ;
271
+ blockstore. put ( & block) ? ;
303
272
304
273
assert_current_height ( 6 , & blockstore) ;
305
274
assert_chain_head ( String :: from ( "block-with-batch" ) , & blockstore) ;
306
275
307
- let get_by_batch = blockstore. get_by_batch ( "batch" ) . unwrap ( ) ;
276
+ let get_by_batch = blockstore. get_by_batch ( "batch" ) ? ;
308
277
309
278
assert_header_signature ( get_by_batch, String :: from ( "block-with-batch" ) ) ;
310
279
311
- let get_by_transaction = blockstore. get_by_transaction ( "transaction" ) . unwrap ( ) ;
280
+ let get_by_transaction = blockstore. get_by_transaction ( "transaction" ) ? ;
312
281
313
282
assert_header_signature ( get_by_transaction, String :: from ( "block-with-batch" ) ) ;
314
283
315
284
// Delete a block.
316
- blockstore. delete ( "block-with-batch" ) . unwrap ( ) ;
285
+ blockstore. delete ( "block-with-batch" ) ? ;
317
286
318
287
assert_current_height ( 5 , & blockstore) ;
319
288
assert_chain_head ( String :: from ( "block-4" ) , & blockstore) ;
289
+
290
+ Ok ( ( ) )
320
291
}
321
292
}
0 commit comments