Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 714486e

Browse files
authored
Merge pull request #2440 from suchapalaver/fix-clippy-warnings-1-3
Fix errors and warnings
2 parents 50352a5 + 580b8ab commit 714486e

File tree

99 files changed

+1566
-1379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1566
-1379
lines changed

adm/build.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ extern crate glob;
2020
extern crate protoc_rust;
2121

2222
use std::env;
23-
use std::error::Error;
2423
use std::fs;
2524
use std::io::Write;
2625
use std::path::Path;
2726
use std::time::{Duration, UNIX_EPOCH};
2827

29-
use protoc_rust::Customize;
30-
3128
const PROTO_FILES_DIR: &str = "../protos";
3229
const SETTINGS_PROTO_FILES_DIR: &str = "../families/settings/protos";
3330
const PROTO_DIR_NAME: &str = "proto";
@@ -49,49 +46,45 @@ struct ProtoFile {
4946

5047
fn main() {
5148
// Generate protobuf files
52-
let mut proto_src_files = glob_simple(&format!("{}/*.proto", PROTO_FILES_DIR));
49+
let mut proto_src_files = glob_simple(&format!("{PROTO_FILES_DIR}/*.proto"));
5350
proto_src_files.append(&mut glob_simple(&format!(
54-
"{}/*.proto",
55-
SETTINGS_PROTO_FILES_DIR
51+
"{SETTINGS_PROTO_FILES_DIR}/*.proto"
5652
)));
5753
let last_build_time = read_last_build_time();
5854

59-
let latest_change =
60-
proto_src_files
61-
.iter()
62-
.fold(Duration::from_secs(0), |max, ref proto_file| {
63-
if proto_file.last_modified > max {
64-
proto_file.last_modified
65-
} else {
66-
max
67-
}
68-
});
55+
let latest_change = proto_src_files
56+
.iter()
57+
.fold(Duration::from_secs(0), |max, proto_file| {
58+
if proto_file.last_modified > max {
59+
proto_file.last_modified
60+
} else {
61+
max
62+
}
63+
});
6964

7065
let out_dir = env::var("OUT_DIR").expect("No OUT_DIR env variable");
7166
let dest_path = Path::new(&out_dir).join(PROTO_DIR_NAME);
7267

7368
if latest_change > last_build_time {
74-
println!("{:?}", proto_src_files);
69+
println!("{proto_src_files:?}");
7570
fs::create_dir_all(&dest_path).unwrap();
76-
protoc_rust::run(protoc_rust::Args {
77-
out_dir: &dest_path.to_str().expect("Invalid proto destination path"),
78-
input: &proto_src_files
79-
.iter()
80-
.map(|proto_file| proto_file.file_path.as_ref())
81-
.collect::<Vec<&str>>(),
82-
includes: &["src", PROTO_FILES_DIR, SETTINGS_PROTO_FILES_DIR],
83-
customize: Customize::default(),
84-
})
85-
.expect("unable to run protoc");
71+
protoc_rust::Codegen::new()
72+
.out_dir(dest_path.to_str().expect("Invalid proto destination path"))
73+
.inputs(
74+
&proto_src_files
75+
.iter()
76+
.map(|proto_file| proto_file.file_path.as_ref())
77+
.collect::<Vec<&str>>(),
78+
)
79+
.include(PROTO_FILES_DIR)
80+
.include(SETTINGS_PROTO_FILES_DIR)
81+
.run()
82+
.expect("unable to run protoc");
8683

8784
let mod_file_name = format!("{}/mod.rs", &dest_path.to_str().unwrap());
8885
let mod_file_path = Path::new(&mod_file_name);
89-
let mut file = match fs::File::create(&mod_file_path) {
90-
Err(err) => panic!(
91-
"Unable to create file {}: {}",
92-
mod_file_name,
93-
err.description()
94-
),
86+
let mut file = match fs::File::create(mod_file_path) {
87+
Err(err) => panic!("Unable to create file {}: {}", mod_file_name, err),
9588
Ok(file) => file,
9689
};
9790

@@ -105,12 +98,8 @@ fn main() {
10598
.join("\n")
10699
);
107100
match file.write_all(content.as_bytes()) {
108-
Err(err) => panic!(
109-
"Unable to write to {}: {}",
110-
mod_file_name,
111-
err.description()
112-
),
113-
Ok(_) => println!("generated {}", mod_file_name),
101+
Err(err) => panic!("Unable to write to {}: {}", mod_file_name, err),
102+
Ok(_) => println!("generated {mod_file_name}"),
114103
}
115104
} else {
116105
println!(
@@ -153,11 +142,7 @@ fn read_last_build_time() -> Duration {
153142
let dest_path = Path::new(&out_dir).join(PROTO_DIR_NAME).join("mod.rs");
154143
match fs::File::open(Path::new(&dest_path.to_str().unwrap())) {
155144
Err(err) => {
156-
println!(
157-
"unable to open {:?}: {}; defaulting to 0",
158-
dest_path,
159-
err.description()
160-
);
145+
println!("unable to open {dest_path:?}: {err}; defaulting to 0");
161146
Duration::new(0, 0)
162147
}
163148
Ok(file) => get_modified_time(file),

adm/src/blockstore.rs

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
use proto::block::{Block, BlockHeader};
19-
use protobuf;
2019
use protobuf::Message;
2120

2221
use database::error::DatabaseError;
@@ -33,35 +32,33 @@ impl<'a> Blockstore<'a> {
3332

3433
pub fn get(&self, block_id: &str) -> Result<Block, DatabaseError> {
3534
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| {
4039
DatabaseError::CorruptionError(format!(
41-
"Could not interpret stored data as a block: {}",
42-
err
40+
"Could not interpret stored data as a block: {err}"
4341
))
4442
})?;
4543
Ok(block)
4644
}
4745

4846
pub fn get_by_height(&self, height: u64) -> Result<Block, DatabaseError> {
4947
let reader = self.db.reader()?;
50-
let block_num = format!("0x{:0>16x}", height);
48+
let block_num = format!("0x{height:0>16x}");
5149
let block_id = reader
52-
.index_get("index_block_num", &block_num.as_bytes())
50+
.index_get("index_block_num", block_num.as_bytes())
5351
.and_then(|block_id| {
5452
block_id.ok_or_else(|| {
55-
DatabaseError::NotFoundError(format!("Block not found: {}", height))
53+
DatabaseError::NotFoundError(format!("Block not found: {height}"))
5654
})
5755
})?;
5856
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:?}"))
6058
})?;
61-
let block: Block = protobuf::parse_from_bytes(&packed).map_err(|err| {
59+
let block: Block = Message::parse_from_bytes(&packed).map_err(|err| {
6260
DatabaseError::CorruptionError(format!(
63-
"Could not interpret stored data as a block: {}",
64-
err
61+
"Could not interpret stored data as a block: {err}"
6562
))
6663
})?;
6764
Ok(block)
@@ -70,19 +67,18 @@ impl<'a> Blockstore<'a> {
7067
pub fn get_by_batch(&self, batch_id: &str) -> Result<Block, DatabaseError> {
7168
let reader = self.db.reader()?;
7269
let block_id = reader
73-
.index_get("index_batch", &batch_id.as_bytes())
70+
.index_get("index_batch", batch_id.as_bytes())
7471
.and_then(|block_id| {
7572
block_id.ok_or_else(|| {
76-
DatabaseError::NotFoundError(format!("Batch not found: {}", batch_id))
73+
DatabaseError::NotFoundError(format!("Batch not found: {batch_id}"))
7774
})
7875
})?;
7976
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:?}"))
8178
})?;
82-
let block: Block = protobuf::parse_from_bytes(&packed).map_err(|err| {
79+
let block: Block = Message::parse_from_bytes(&packed).map_err(|err| {
8380
DatabaseError::CorruptionError(format!(
84-
"Could not interpret stored data as a block: {}",
85-
err
81+
"Could not interpret stored data as a block: {err}"
8682
))
8783
})?;
8884
Ok(block)
@@ -91,53 +87,49 @@ impl<'a> Blockstore<'a> {
9187
pub fn get_by_transaction(&self, transaction_id: &str) -> Result<Block, DatabaseError> {
9288
let reader = self.db.reader()?;
9389
let block_id = reader
94-
.index_get("index_transaction", &transaction_id.as_bytes())
90+
.index_get("index_transaction", transaction_id.as_bytes())
9591
.and_then(|block_id| {
9692
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}"))
10194
})
10295
})?;
10396
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:?}"))
10598
})?;
106-
let block: Block = protobuf::parse_from_bytes(&packed).map_err(|err| {
99+
let block: Block = Message::parse_from_bytes(&packed).map_err(|err| {
107100
DatabaseError::CorruptionError(format!(
108-
"Could not interpret stored data as a block: {}",
109-
err
101+
"Could not interpret stored data as a block: {err}"
110102
))
111103
})?;
112104
Ok(block)
113105
}
114106

115107
pub fn put(&self, block: &Block) -> Result<(), DatabaseError> {
116108
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}"))
119111
})?;
120112
let mut writer = self.db.writer()?;
121113
// Add block to main db
122114
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}"))
124116
})?;
125-
writer.put(&block.header_signature.as_bytes(), &packed)?;
117+
writer.put(block.header_signature.as_bytes(), &packed)?;
126118

127119
// Add block to block num index
128120
let block_num_index = format!("0x{:0>16x}", block_header.block_num);
129121
writer.index_put(
130122
"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(),
133125
)?;
134126

135127
for batch in block.batches.iter() {
136128
for txn in batch.transactions.iter() {
137129
writer.index_put(
138130
"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(),
141133
)?;
142134
}
143135
}
@@ -146,8 +138,8 @@ impl<'a> Blockstore<'a> {
146138
for batch in block.batches.iter() {
147139
writer.index_put(
148140
"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(),
151143
)?;
152144
}
153145

@@ -158,27 +150,27 @@ impl<'a> Blockstore<'a> {
158150
let block = self.get(block_id)?;
159151
let block_id = &block.header_signature;
160152
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}"))
163155
})?;
164156
// Delete block from main db
165157
let mut writer = self.db.writer()?;
166-
writer.delete(&block_id.as_bytes())?;
158+
writer.delete(block_id.as_bytes())?;
167159

168160
// Delete block from block_num index
169161
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())?;
171163

172164
// Delete block from transaction index
173165
for batch in block.batches.iter() {
174166
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())?;
176168
}
177169
}
178170

179171
// Delete block from batch index
180172
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())?;
182174
}
183175
writer.commit()
184176
}
@@ -191,7 +183,7 @@ impl<'a> Blockstore<'a> {
191183
.last()
192184
.ok_or_else(|| DatabaseError::NotFoundError("No chain head".into()))?;
193185
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}"))
195187
})
196188
}
197189

@@ -247,14 +239,14 @@ mod tests {
247239
// Set the file size to 10MB, so as to support file systems that do
248240
// not support sparse files.
249241
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}")))
251243
.unwrap();
252244

253245
let database = LmdbDatabase::new(
254246
&ctx,
255247
&["index_batch", "index_transaction", "index_block_num"],
256248
)
257-
.map_err(|err| DatabaseError::InitError(format!("{}", err)))
249+
.map_err(|err| DatabaseError::InitError(format!("{err}")))
258250
.unwrap();
259251

260252
let blockstore = Blockstore::new(database);
@@ -265,15 +257,15 @@ mod tests {
265257
// Add 5 blocks.
266258
for i in 0..5 {
267259
let mut block = Block::new();
268-
block.set_header_signature(format!("block-{}", i));
260+
block.set_header_signature(format!("block-{i}"));
269261
let mut header = BlockHeader::new();
270262
header.set_block_num(i);
271263
block.set_header(header.write_to_bytes().unwrap());
272264

273265
blockstore.put(&block).unwrap();
274266

275267
assert_current_height(i as usize + 1, &blockstore);
276-
assert_chain_head(format!("block-{}", i), &blockstore);
268+
assert_chain_head(format!("block-{i}"), &blockstore);
277269
}
278270

279271
assert_current_height(5, &blockstore);
@@ -282,7 +274,7 @@ mod tests {
282274
for i in 0..5 {
283275
let block = blockstore.get_by_height(i).unwrap();
284276

285-
assert_header_signature(block, format!("block-{}", i));
277+
assert_header_signature(block, format!("block-{i}"));
286278
}
287279

288280
// Get a block.

0 commit comments

Comments
 (0)