Skip to content

Commit 36d49b0

Browse files
authored
Merge pull request #5741 from stacks-network/chore/clippy-unnecesary-to-owned-unwrap-or-default
Apply `clippy::unnecesary_to_owned` and `clippy::unwrap_or_default` fixes throughout stacks core
2 parents 5e11f2d + b2c34d9 commit 36d49b0

Some content is hidden

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

83 files changed

+615
-654
lines changed

libsigner/src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn test_simple_signer() {
128128
reward_cycle: 1,
129129
};
130130
for i in 0..max_events {
131-
let privk = Secp256k1PrivateKey::new();
131+
let privk = Secp256k1PrivateKey::random();
132132
let message = SignerMessage::BlockProposal(block_proposal.clone());
133133
let message_bytes = message.serialize_to_vec();
134134
let mut chunk = StackerDBChunkData::new(i as u32, 1, message_bytes);

libsigner/src/v0/messages.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ mod test {
11921192
let rejection = BlockRejection::new(
11931193
Sha512Trunc256Sum([0u8; 32]),
11941194
RejectCode::ValidationFailed(ValidateRejectCode::InvalidBlock),
1195-
&StacksPrivateKey::new(),
1195+
&StacksPrivateKey::random(),
11961196
thread_rng().gen_bool(0.5),
11971197
thread_rng().next_u64(),
11981198
);
@@ -1204,7 +1204,7 @@ mod test {
12041204
let rejection = BlockRejection::new(
12051205
Sha512Trunc256Sum([1u8; 32]),
12061206
RejectCode::ConnectivityIssues,
1207-
&StacksPrivateKey::new(),
1207+
&StacksPrivateKey::random(),
12081208
thread_rng().gen_bool(0.5),
12091209
thread_rng().next_u64(),
12101210
);
@@ -1231,7 +1231,7 @@ mod test {
12311231
let response = BlockResponse::Rejected(BlockRejection::new(
12321232
Sha512Trunc256Sum([1u8; 32]),
12331233
RejectCode::ValidationFailed(ValidateRejectCode::InvalidBlock),
1234-
&StacksPrivateKey::new(),
1234+
&StacksPrivateKey::random(),
12351235
thread_rng().gen_bool(0.5),
12361236
thread_rng().next_u64(),
12371237
));
@@ -1318,10 +1318,10 @@ mod test {
13181318

13191319
#[test]
13201320
fn verify_sign_mock_proposal() {
1321-
let private_key = StacksPrivateKey::new();
1321+
let private_key = StacksPrivateKey::random();
13221322
let public_key = StacksPublicKey::from_private(&private_key);
13231323

1324-
let bad_private_key = StacksPrivateKey::new();
1324+
let bad_private_key = StacksPrivateKey::random();
13251325
let bad_public_key = StacksPublicKey::from_private(&bad_private_key);
13261326

13271327
let mut mock_proposal = random_mock_proposal();
@@ -1353,7 +1353,7 @@ mod test {
13531353
#[test]
13541354
fn serde_mock_proposal() {
13551355
let mut mock_signature = random_mock_proposal();
1356-
mock_signature.sign(&StacksPrivateKey::new()).unwrap();
1356+
mock_signature.sign(&StacksPrivateKey::random()).unwrap();
13571357
let serialized_signature = mock_signature.serialize_to_vec();
13581358
let deserialized_signature = read_next::<MockProposal, _>(&mut &serialized_signature[..])
13591359
.expect("Failed to deserialize MockSignature");
@@ -1368,7 +1368,7 @@ mod test {
13681368
metadata: SignerMessageMetadata::default(),
13691369
};
13701370
mock_signature
1371-
.sign(&StacksPrivateKey::new())
1371+
.sign(&StacksPrivateKey::random())
13721372
.expect("Failed to sign MockSignature");
13731373
let serialized_signature = mock_signature.serialize_to_vec();
13741374
let deserialized_signature = read_next::<MockSignature, _>(&mut &serialized_signature[..])
@@ -1379,8 +1379,10 @@ mod test {
13791379
#[test]
13801380
fn serde_mock_block() {
13811381
let mock_proposal = random_mock_proposal();
1382-
let mock_signature_1 = MockSignature::new(mock_proposal.clone(), &StacksPrivateKey::new());
1383-
let mock_signature_2 = MockSignature::new(mock_proposal.clone(), &StacksPrivateKey::new());
1382+
let mock_signature_1 =
1383+
MockSignature::new(mock_proposal.clone(), &StacksPrivateKey::random());
1384+
let mock_signature_2 =
1385+
MockSignature::new(mock_proposal.clone(), &StacksPrivateKey::random());
13841386
let mock_block = MockBlock {
13851387
mock_proposal,
13861388
mock_signatures: vec![mock_signature_1, mock_signature_2],

libstackerdb/src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::*;
2424

2525
#[test]
2626
fn test_stackerdb_slot_metadata_sign_verify() {
27-
let pk = StacksPrivateKey::new();
27+
let pk = StacksPrivateKey::random();
2828
let addr = StacksAddress::from_public_keys(
2929
C32_ADDRESS_VERSION_MAINNET_SINGLESIG,
3030
&AddressHashMode::SerializeP2PKH,

stacks-common/src/util/secp256k1.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Default for Secp256k1PublicKey {
123123
impl Secp256k1PublicKey {
124124
#[cfg(any(test, feature = "testing"))]
125125
pub fn new() -> Secp256k1PublicKey {
126-
Secp256k1PublicKey::from_private(&Secp256k1PrivateKey::new())
126+
Secp256k1PublicKey::from_private(&Secp256k1PrivateKey::random())
127127
}
128128

129129
pub fn from_hex(hex_string: &str) -> Result<Secp256k1PublicKey, &'static str> {
@@ -249,14 +249,8 @@ impl PublicKey for Secp256k1PublicKey {
249249
}
250250
}
251251

252-
impl Default for Secp256k1PrivateKey {
253-
fn default() -> Self {
254-
Self::new()
255-
}
256-
}
257-
258252
impl Secp256k1PrivateKey {
259-
pub fn new() -> Secp256k1PrivateKey {
253+
pub fn random() -> Secp256k1PrivateKey {
260254
let mut rng = rand::thread_rng();
261255
loop {
262256
// keep trying to generate valid bytes
@@ -460,7 +454,7 @@ mod tests {
460454

461455
#[test]
462456
fn test_parse_serialize_compressed() {
463-
let mut t1 = Secp256k1PrivateKey::new();
457+
let mut t1 = Secp256k1PrivateKey::random();
464458
t1.set_compress_public(true);
465459
let h_comp = t1.to_hex();
466460
t1.set_compress_public(false);
@@ -654,7 +648,7 @@ mod tests {
654648
let mut rng = rand::thread_rng();
655649

656650
for i in 0..100 {
657-
let privk = Secp256k1PrivateKey::new();
651+
let privk = Secp256k1PrivateKey::random();
658652
let pubk = Secp256k1PublicKey::from_private(&privk);
659653

660654
let mut msg = [0u8; 32];

stacks-signer/src/client/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub(crate) mod tests {
302302
pox_consensus_hash: Option<ConsensusHash>,
303303
) -> (String, RPCPeerInfoData) {
304304
// Generate some random info
305-
let private_key = StacksPrivateKey::new();
305+
let private_key = StacksPrivateKey::random();
306306
let public_key = StacksPublicKey::from_private(&private_key);
307307
let public_key_buf = StacksPublicKeyBuffer::from_public_key(&public_key);
308308
let public_key_hash = Hash160::from_node_public_key(&public_key);
@@ -376,7 +376,7 @@ pub(crate) mod tests {
376376
let private_key = if signer_id == 0 {
377377
config.stacks_private_key
378378
} else {
379-
StacksPrivateKey::new()
379+
StacksPrivateKey::random()
380380
};
381381
let public_key = StacksPublicKey::from_private(&private_key);
382382

stacks-signer/src/client/stackerdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ mod tests {
248248
#[test]
249249
fn send_signer_message_should_succeed() {
250250
let signer_config = build_signer_config_tomls(
251-
&[StacksPrivateKey::new()],
251+
&[StacksPrivateKey::random()],
252252
"localhost:20443",
253253
Some(Duration::from_millis(128)), // Timeout defaults to 5 seconds. Let's override it to 128 milliseconds.
254254
&Network::Testnet,

stacks-signer/src/client/stacks_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ mod tests {
11971197
#[test]
11981198
fn get_reward_set_should_succeed() {
11991199
let mock = MockServerClient::new();
1200-
let private_key = StacksPrivateKey::new();
1200+
let private_key = StacksPrivateKey::random();
12011201
let public_key = StacksPublicKey::from_private(&private_key);
12021202
let mut bytes = [0u8; 33];
12031203
bytes.copy_from_slice(&public_key.to_bytes_compressed());

stacks-signer/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ pub mod tests {
409409
#[test]
410410
fn test_verify_vote() {
411411
let mut rand = rand::thread_rng();
412-
let private_key = Secp256k1PrivateKey::new();
412+
let private_key = Secp256k1PrivateKey::random();
413413
let public_key = StacksPublicKey::from_private(&private_key);
414414

415-
let invalid_private_key = Secp256k1PrivateKey::new();
415+
let invalid_private_key = Secp256k1PrivateKey::random();
416416
let invalid_public_key = StacksPublicKey::from_private(&invalid_private_key);
417417

418418
let sip = rand.next_u32();

stacks-signer/src/monitor_signers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl SignerMonitor {
5555
pub fn new(args: MonitorSignersArgs) -> Self {
5656
url::Url::parse(&format!("http://{}", args.host)).expect("Failed to parse node host");
5757
let stacks_client = StacksClient::try_from_host(
58-
StacksPrivateKey::new(), // We don't need a private key to read
58+
StacksPrivateKey::random(), // We don't need a private key to read
5959
args.host.clone(),
6060
"FOO".to_string(), // We don't care about authorized paths. Just accessing public info
6161
)

stacks-signer/src/runloop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,8 @@ mod tests {
544544
let weight = 10;
545545
let mut signer_entries = Vec::with_capacity(nmb_signers);
546546
for _ in 0..nmb_signers {
547-
let key = StacksPublicKey::from_private(&StacksPrivateKey::new()).to_bytes_compressed();
547+
let key =
548+
StacksPublicKey::from_private(&StacksPrivateKey::random()).to_bytes_compressed();
548549
let mut signing_key = [0u8; 33];
549550
signing_key.copy_from_slice(&key);
550551
signer_entries.push(NakamotoSignerEntry {

0 commit comments

Comments
 (0)