@@ -34,12 +34,8 @@ impl<'a> Blockstore<'a> {
3434 let reader = self . db . reader ( ) ?;
3535 let packed = reader
3636 . 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) ?;
4339 Ok ( block)
4440 }
4541
@@ -49,18 +45,13 @@ impl<'a> Blockstore<'a> {
4945 let block_id = reader
5046 . index_get ( "index_block_num" , block_num. as_bytes ( ) )
5147 . 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}" ) ) )
5550 } ) ?;
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) ?;
6455 Ok ( block)
6556 }
6657
@@ -69,18 +60,13 @@ impl<'a> Blockstore<'a> {
6960 let block_id = reader
7061 . index_get ( "index_batch" , batch_id. as_bytes ( ) )
7162 . 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}" ) ) )
7565 } ) ?;
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) ?;
8470 Ok ( block)
8571 }
8672
@@ -90,30 +76,21 @@ impl<'a> Blockstore<'a> {
9076 . index_get ( "index_transaction" , transaction_id. as_bytes ( ) )
9177 . and_then ( |block_id| {
9278 block_id. ok_or_else ( || {
93- DatabaseError :: NotFoundError ( format ! ( "Transaction not found: {transaction_id}" ) )
79+ DatabaseError :: NotFound ( format ! ( "Transaction not found: {transaction_id}" ) )
9480 } )
9581 } ) ?;
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) ?;
10486 Ok ( block)
10587 }
10688
10789 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 ) ?;
11291 let mut writer = self . db . writer ( ) ?;
11392 // 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 ( ) ?;
11794 writer. put ( block. header_signature . as_bytes ( ) , & packed) ?;
11895
11996 // Add block to block num index
@@ -149,10 +126,8 @@ impl<'a> Blockstore<'a> {
149126 pub fn delete ( & self , block_id : & str ) -> Result < ( ) , DatabaseError > {
150127 let block = self . get ( block_id) ?;
151128 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+
156131 // Delete block from main db
157132 let mut writer = self . db . writer ( ) ?;
158133 writer. delete ( block_id. as_bytes ( ) ) ?;
@@ -181,10 +156,8 @@ impl<'a> Blockstore<'a> {
181156 let mut cursor = reader. index_cursor ( "index_block_num" ) ?;
182157 let ( _, val) = cursor
183158 . 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) ?)
188161 }
189162
190163 // Get the number of blocks
@@ -231,23 +204,19 @@ mod tests {
231204 /// deleting, and looking up blocks), making assertions about the
232205 /// blockstore contents at each step.
233206 #[ test]
234- fn test_blockstore ( ) {
207+ fn test_blockstore ( ) -> Result < ( ) , DatabaseError > {
235208 let path_config = config:: get_path_config ( ) ;
236209
237210 let blockstore_path = & path_config. data_dir . join ( config:: get_blockstore_filename ( ) ) ;
238211
239212 // Set the file size to 10MB, so as to support file systems that do
240213 // 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 ) ) ?;
244215
245216 let database = LmdbDatabase :: new (
246217 & ctx,
247218 & [ "index_batch" , "index_transaction" , "index_block_num" ] ,
248- )
249- . map_err ( |err| DatabaseError :: InitError ( format ! ( "{err}" ) ) )
250- . unwrap ( ) ;
219+ ) ?;
251220
252221 let blockstore = Blockstore :: new ( database) ;
253222
@@ -260,9 +229,9 @@ mod tests {
260229 block. set_header_signature ( format ! ( "block-{i}" ) ) ;
261230 let mut header = BlockHeader :: new ( ) ;
262231 header. set_block_num ( i) ;
263- block. set_header ( header. write_to_bytes ( ) . unwrap ( ) ) ;
232+ block. set_header ( header. write_to_bytes ( ) ? ) ;
264233
265- blockstore. put ( & block) . unwrap ( ) ;
234+ blockstore. put ( & block) ? ;
266235
267236 assert_current_height ( i as usize + 1 , & blockstore) ;
268237 assert_chain_head ( format ! ( "block-{i}" ) , & blockstore) ;
@@ -272,13 +241,13 @@ mod tests {
272241
273242 // Check that the blocks are in the right order.
274243 for i in 0 ..5 {
275- let block = blockstore. get_by_height ( i) . unwrap ( ) ;
244+ let block = blockstore. get_by_height ( i) ? ;
276245
277246 assert_header_signature ( block, format ! ( "block-{i}" ) ) ;
278247 }
279248
280249 // Get a block.
281- let get_block = blockstore. get ( "block-2" ) . unwrap ( ) ;
250+ let get_block = blockstore. get ( "block-2" ) ? ;
282251
283252 assert_header_signature ( get_block, String :: from ( "block-2" ) ) ;
284253
@@ -290,32 +259,34 @@ mod tests {
290259 batch. set_header_signature ( String :: from ( "batch" ) ) ;
291260 batch. set_transactions ( protobuf:: RepeatedField :: from_vec ( vec ! [ transaction] ) ) ;
292261 let batch_header = BatchHeader :: new ( ) ;
293- batch. set_header ( batch_header. write_to_bytes ( ) . unwrap ( ) ) ;
262+ batch. set_header ( batch_header. write_to_bytes ( ) ? ) ;
294263
295264 let mut block = Block :: new ( ) ;
296265 block. set_header_signature ( String :: from ( "block-with-batch" ) ) ;
297266 let mut block_header = BlockHeader :: new ( ) ;
298267 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 ( ) ? ) ;
300269 block. set_batches ( protobuf:: RepeatedField :: from_vec ( vec ! [ batch] ) ) ;
301270
302- blockstore. put ( & block) . unwrap ( ) ;
271+ blockstore. put ( & block) ? ;
303272
304273 assert_current_height ( 6 , & blockstore) ;
305274 assert_chain_head ( String :: from ( "block-with-batch" ) , & blockstore) ;
306275
307- let get_by_batch = blockstore. get_by_batch ( "batch" ) . unwrap ( ) ;
276+ let get_by_batch = blockstore. get_by_batch ( "batch" ) ? ;
308277
309278 assert_header_signature ( get_by_batch, String :: from ( "block-with-batch" ) ) ;
310279
311- let get_by_transaction = blockstore. get_by_transaction ( "transaction" ) . unwrap ( ) ;
280+ let get_by_transaction = blockstore. get_by_transaction ( "transaction" ) ? ;
312281
313282 assert_header_signature ( get_by_transaction, String :: from ( "block-with-batch" ) ) ;
314283
315284 // Delete a block.
316- blockstore. delete ( "block-with-batch" ) . unwrap ( ) ;
285+ blockstore. delete ( "block-with-batch" ) ? ;
317286
318287 assert_current_height ( 5 , & blockstore) ;
319288 assert_chain_head ( String :: from ( "block-4" ) , & blockstore) ;
289+
290+ Ok ( ( ) )
320291 }
321292}
0 commit comments