16
16
*/
17
17
18
18
use proto:: block:: { Block , BlockHeader } ;
19
- use protobuf;
20
19
use protobuf:: Message ;
21
20
22
21
use database:: error:: DatabaseError ;
@@ -33,35 +32,33 @@ impl<'a> Blockstore<'a> {
33
32
34
33
pub fn get ( & self , block_id : & str ) -> Result < Block , DatabaseError > {
35
34
let reader = self . db . reader ( ) ?;
36
- let packed = reader. get ( & block_id . as_bytes ( ) ) . ok_or_else ( || {
37
- DatabaseError :: NotFoundError ( format ! ( "Block not found: {}" , block_id) )
38
- } ) ?;
39
- let block: Block = protobuf :: parse_from_bytes ( & packed) . map_err ( |err| {
35
+ let packed = reader
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| {
40
39
DatabaseError :: CorruptionError ( format ! (
41
- "Could not interpret stored data as a block: {}" ,
42
- err
40
+ "Could not interpret stored data as a block: {err}"
43
41
) )
44
42
} ) ?;
45
43
Ok ( block)
46
44
}
47
45
48
46
pub fn get_by_height ( & self , height : u64 ) -> Result < Block , DatabaseError > {
49
47
let reader = self . db . reader ( ) ?;
50
- let block_num = format ! ( "0x{:0>16x}" , height ) ;
48
+ let block_num = format ! ( "0x{height :0>16x}" ) ;
51
49
let block_id = reader
52
- . index_get ( "index_block_num" , & block_num. as_bytes ( ) )
50
+ . index_get ( "index_block_num" , block_num. as_bytes ( ) )
53
51
. and_then ( |block_id| {
54
52
block_id. ok_or_else ( || {
55
- DatabaseError :: NotFoundError ( format ! ( "Block not found: {}" , height ) )
53
+ DatabaseError :: NotFoundError ( format ! ( "Block not found: {height}" ) )
56
54
} )
57
55
} ) ?;
58
56
let packed = reader. get ( & block_id) . ok_or_else ( || {
59
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {:?}" , block_id ) )
57
+ DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id :?}" ) )
60
58
} ) ?;
61
- let block: Block = protobuf :: parse_from_bytes ( & packed) . map_err ( |err| {
59
+ let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
62
60
DatabaseError :: CorruptionError ( format ! (
63
- "Could not interpret stored data as a block: {}" ,
64
- err
61
+ "Could not interpret stored data as a block: {err}"
65
62
) )
66
63
} ) ?;
67
64
Ok ( block)
@@ -70,19 +67,18 @@ impl<'a> Blockstore<'a> {
70
67
pub fn get_by_batch ( & self , batch_id : & str ) -> Result < Block , DatabaseError > {
71
68
let reader = self . db . reader ( ) ?;
72
69
let block_id = reader
73
- . index_get ( "index_batch" , & batch_id. as_bytes ( ) )
70
+ . index_get ( "index_batch" , batch_id. as_bytes ( ) )
74
71
. and_then ( |block_id| {
75
72
block_id. ok_or_else ( || {
76
- DatabaseError :: NotFoundError ( format ! ( "Batch not found: {}" , batch_id ) )
73
+ DatabaseError :: NotFoundError ( format ! ( "Batch not found: {batch_id}" ) )
77
74
} )
78
75
} ) ?;
79
76
let packed = reader. get ( & block_id) . ok_or_else ( || {
80
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {:?}" , block_id ) )
77
+ DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id :?}" ) )
81
78
} ) ?;
82
- let block: Block = protobuf :: parse_from_bytes ( & packed) . map_err ( |err| {
79
+ let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
83
80
DatabaseError :: CorruptionError ( format ! (
84
- "Could not interpret stored data as a block: {}" ,
85
- err
81
+ "Could not interpret stored data as a block: {err}"
86
82
) )
87
83
} ) ?;
88
84
Ok ( block)
@@ -91,53 +87,49 @@ impl<'a> Blockstore<'a> {
91
87
pub fn get_by_transaction ( & self , transaction_id : & str ) -> Result < Block , DatabaseError > {
92
88
let reader = self . db . reader ( ) ?;
93
89
let block_id = reader
94
- . index_get ( "index_transaction" , & transaction_id. as_bytes ( ) )
90
+ . index_get ( "index_transaction" , transaction_id. as_bytes ( ) )
95
91
. and_then ( |block_id| {
96
92
block_id. ok_or_else ( || {
97
- DatabaseError :: NotFoundError ( format ! (
98
- "Transaction not found: {}" ,
99
- transaction_id
100
- ) )
93
+ DatabaseError :: NotFoundError ( format ! ( "Transaction not found: {transaction_id}" ) )
101
94
} )
102
95
} ) ?;
103
96
let packed = reader. get ( & block_id) . ok_or_else ( || {
104
- DatabaseError :: CorruptionError ( format ! ( "Block not found: {:?}" , block_id ) )
97
+ DatabaseError :: CorruptionError ( format ! ( "Block not found: {block_id :?}" ) )
105
98
} ) ?;
106
- let block: Block = protobuf :: parse_from_bytes ( & packed) . map_err ( |err| {
99
+ let block: Block = Message :: parse_from_bytes ( & packed) . map_err ( |err| {
107
100
DatabaseError :: CorruptionError ( format ! (
108
- "Could not interpret stored data as a block: {}" ,
109
- err
101
+ "Could not interpret stored data as a block: {err}"
110
102
) )
111
103
} ) ?;
112
104
Ok ( block)
113
105
}
114
106
115
107
pub fn put ( & self , block : & Block ) -> Result < ( ) , DatabaseError > {
116
108
let block_header: BlockHeader =
117
- protobuf :: parse_from_bytes ( & block. header ) . map_err ( |err| {
118
- DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {}" , err ) )
109
+ Message :: parse_from_bytes ( & block. header ) . map_err ( |err| {
110
+ DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {err}" ) )
119
111
} ) ?;
120
112
let mut writer = self . db . writer ( ) ?;
121
113
// Add block to main db
122
114
let packed = block. write_to_bytes ( ) . map_err ( |err| {
123
- DatabaseError :: WriterError ( format ! ( "Failed to serialize block: {}" , err ) )
115
+ DatabaseError :: WriterError ( format ! ( "Failed to serialize block: {err}" ) )
124
116
} ) ?;
125
- writer. put ( & block. header_signature . as_bytes ( ) , & packed) ?;
117
+ writer. put ( block. header_signature . as_bytes ( ) , & packed) ?;
126
118
127
119
// Add block to block num index
128
120
let block_num_index = format ! ( "0x{:0>16x}" , block_header. block_num) ;
129
121
writer. index_put (
130
122
"index_block_num" ,
131
- & block_num_index. as_bytes ( ) ,
132
- & block. header_signature . as_bytes ( ) ,
123
+ block_num_index. as_bytes ( ) ,
124
+ block. header_signature . as_bytes ( ) ,
133
125
) ?;
134
126
135
127
for batch in block. batches . iter ( ) {
136
128
for txn in batch. transactions . iter ( ) {
137
129
writer. index_put (
138
130
"index_transaction" ,
139
- & txn. header_signature . as_bytes ( ) ,
140
- & block. header_signature . as_bytes ( ) ,
131
+ txn. header_signature . as_bytes ( ) ,
132
+ block. header_signature . as_bytes ( ) ,
141
133
) ?;
142
134
}
143
135
}
@@ -146,8 +138,8 @@ impl<'a> Blockstore<'a> {
146
138
for batch in block. batches . iter ( ) {
147
139
writer. index_put (
148
140
"index_batch" ,
149
- & batch. header_signature . as_bytes ( ) ,
150
- & block. header_signature . as_bytes ( ) ,
141
+ batch. header_signature . as_bytes ( ) ,
142
+ block. header_signature . as_bytes ( ) ,
151
143
) ?;
152
144
}
153
145
@@ -158,27 +150,27 @@ impl<'a> Blockstore<'a> {
158
150
let block = self . get ( block_id) ?;
159
151
let block_id = & block. header_signature ;
160
152
let block_header: BlockHeader =
161
- protobuf :: parse_from_bytes ( & block. header ) . map_err ( |err| {
162
- DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {}" , err ) )
153
+ Message :: parse_from_bytes ( & block. header ) . map_err ( |err| {
154
+ DatabaseError :: CorruptionError ( format ! ( "Invalid block header: {err}" ) )
163
155
} ) ?;
164
156
// Delete block from main db
165
157
let mut writer = self . db . writer ( ) ?;
166
- writer. delete ( & block_id. as_bytes ( ) ) ?;
158
+ writer. delete ( block_id. as_bytes ( ) ) ?;
167
159
168
160
// Delete block from block_num index
169
161
let block_num_index = format ! ( "0x{:0>16x}" , block_header. block_num) ;
170
- writer. index_delete ( "index_block_num" , & block_num_index. as_bytes ( ) ) ?;
162
+ writer. index_delete ( "index_block_num" , block_num_index. as_bytes ( ) ) ?;
171
163
172
164
// Delete block from transaction index
173
165
for batch in block. batches . iter ( ) {
174
166
for txn in batch. transactions . iter ( ) {
175
- writer. index_delete ( "index_transaction" , & txn. header_signature . as_bytes ( ) ) ?;
167
+ writer. index_delete ( "index_transaction" , txn. header_signature . as_bytes ( ) ) ?;
176
168
}
177
169
}
178
170
179
171
// Delete block from batch index
180
172
for batch in block. batches . iter ( ) {
181
- writer. index_delete ( "index_batch" , & batch. header_signature . as_bytes ( ) ) ?;
173
+ writer. index_delete ( "index_batch" , batch. header_signature . as_bytes ( ) ) ?;
182
174
}
183
175
writer. commit ( )
184
176
}
@@ -191,7 +183,7 @@ impl<'a> Blockstore<'a> {
191
183
. last ( )
192
184
. ok_or_else ( || DatabaseError :: NotFoundError ( "No chain head" . into ( ) ) ) ?;
193
185
String :: from_utf8 ( val) . map_err ( |err| {
194
- DatabaseError :: CorruptionError ( format ! ( "Chain head block id is corrupt: {}" , err ) )
186
+ DatabaseError :: CorruptionError ( format ! ( "Chain head block id is corrupt: {err}" ) )
195
187
} )
196
188
}
197
189
@@ -247,14 +239,14 @@ mod tests {
247
239
// Set the file size to 10MB, so as to support file systems that do
248
240
// not support sparse files.
249
241
let ctx = LmdbContext :: new ( blockstore_path, 3 , Some ( 10 * 1024 * 1024 ) )
250
- . map_err ( |err| DatabaseError :: InitError ( format ! ( "{}" , err ) ) )
242
+ . map_err ( |err| DatabaseError :: InitError ( format ! ( "{err}" ) ) )
251
243
. unwrap ( ) ;
252
244
253
245
let database = LmdbDatabase :: new (
254
246
& ctx,
255
247
& [ "index_batch" , "index_transaction" , "index_block_num" ] ,
256
248
)
257
- . map_err ( |err| DatabaseError :: InitError ( format ! ( "{}" , err ) ) )
249
+ . map_err ( |err| DatabaseError :: InitError ( format ! ( "{err}" ) ) )
258
250
. unwrap ( ) ;
259
251
260
252
let blockstore = Blockstore :: new ( database) ;
@@ -265,15 +257,15 @@ mod tests {
265
257
// Add 5 blocks.
266
258
for i in 0 ..5 {
267
259
let mut block = Block :: new ( ) ;
268
- block. set_header_signature ( format ! ( "block-{}" , i ) ) ;
260
+ block. set_header_signature ( format ! ( "block-{i}" ) ) ;
269
261
let mut header = BlockHeader :: new ( ) ;
270
262
header. set_block_num ( i) ;
271
263
block. set_header ( header. write_to_bytes ( ) . unwrap ( ) ) ;
272
264
273
265
blockstore. put ( & block) . unwrap ( ) ;
274
266
275
267
assert_current_height ( i as usize + 1 , & blockstore) ;
276
- assert_chain_head ( format ! ( "block-{}" , i ) , & blockstore) ;
268
+ assert_chain_head ( format ! ( "block-{i}" ) , & blockstore) ;
277
269
}
278
270
279
271
assert_current_height ( 5 , & blockstore) ;
@@ -282,7 +274,7 @@ mod tests {
282
274
for i in 0 ..5 {
283
275
let block = blockstore. get_by_height ( i) . unwrap ( ) ;
284
276
285
- assert_header_signature ( block, format ! ( "block-{}" , i ) ) ;
277
+ assert_header_signature ( block, format ! ( "block-{i}" ) ) ;
286
278
}
287
279
288
280
// Get a block.
0 commit comments