Skip to content

Commit

Permalink
Merge pull request #4572 from ASuciuX/chore/fix-clippy-nursery-warnings
Browse files Browse the repository at this point in the history
Chore/fix clippy nursery warning
  • Loading branch information
jbencin authored Mar 23, 2024
2 parents 441b7ed + 9a6f698 commit f9e9791
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 42 deletions.
19 changes: 9 additions & 10 deletions stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ pub struct StackingSignatureMethod(Pox4SignatureTopic);

impl StackingSignatureMethod {
/// Get the inner `Pox4SignatureTopic`
pub fn topic(&self) -> &Pox4SignatureTopic {
pub const fn topic(&self) -> &Pox4SignatureTopic {
&self.0
}
}

impl From<Pox4SignatureTopic> for StackingSignatureMethod {
fn from(topic: Pox4SignatureTopic) -> Self {
StackingSignatureMethod(topic)
Self(topic)
}
}

Expand All @@ -210,9 +210,9 @@ impl ValueEnum for StackingSignatureMethod {

fn value_variants<'a>() -> &'a [Self] {
&[
StackingSignatureMethod(Pox4SignatureTopic::StackStx),
StackingSignatureMethod(Pox4SignatureTopic::StackExtend),
StackingSignatureMethod(Pox4SignatureTopic::AggregationCommit),
Self(Pox4SignatureTopic::StackStx),
Self(Pox4SignatureTopic::StackExtend),
Self(Pox4SignatureTopic::AggregationCommit),
]
}

Expand Down Expand Up @@ -262,11 +262,10 @@ fn parse_contract(contract: &str) -> Result<QualifiedContractIdentifier, String>

/// Parse a BTC address argument and return a `PoxAddress`
pub fn parse_pox_addr(pox_address_literal: &str) -> Result<PoxAddress, String> {
if let Some(pox_address) = PoxAddress::from_b58(pox_address_literal) {
Ok(pox_address)
} else {
Err(format!("Invalid pox address: {}", pox_address_literal))
}
PoxAddress::from_b58(pox_address_literal).map_or_else(
|| Err(format!("Invalid pox address: {pox_address_literal}")),
|pox_address| Ok(pox_address),
)
}

/// Parse the hexadecimal Stacks private key
Expand Down
2 changes: 1 addition & 1 deletion stacks-signer/src/client/stackerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct StackerDB {

impl From<&SignerConfig> for StackerDB {
fn from(config: &SignerConfig) -> Self {
StackerDB::new(
Self::new(
&config.node_host,
config.stacks_private_key,
config.mainnet,
Expand Down
19 changes: 9 additions & 10 deletions stacks-signer/src/client/stacks_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl StacksClient {
}

/// Get our signer address
pub fn get_signer_address(&self) -> &StacksAddress {
pub const fn get_signer_address(&self) -> &StacksAddress {
&self.stacks_address
}

Expand Down Expand Up @@ -145,7 +145,7 @@ impl StacksClient {
value: ClarityValue,
) -> Result<Vec<(StacksAddress, u128)>, ClientError> {
debug!("Parsing signer slots...");
let value = value.clone().expect_result_ok()?;
let value = value.expect_result_ok()?;
let values = value.expect_list()?;
let mut signer_slots = Vec::with_capacity(values.len());
for value in values {
Expand Down Expand Up @@ -267,11 +267,10 @@ impl StacksClient {
function_args,
)?;
let inner_data = value.expect_optional()?;
if let Some(key_value) = inner_data {
self.parse_aggregate_public_key(key_value)
} else {
Ok(None)
}
inner_data.map_or_else(
|| Ok(None),
|key_value| self.parse_aggregate_public_key(key_value),
)
}

/// Retrieve the current account nonce for the provided address
Expand Down Expand Up @@ -512,9 +511,9 @@ impl StacksClient {
let path = self.read_only_path(contract_addr, contract_name, function_name);
let response = self
.stacks_node_client
.post(path.clone())
.post(path)
.header("Content-Type", "application/json")
.body(body.clone())
.body(body)
.send()?;
if !response.status().is_success() {
return Err(ClientError::RequestFailure(response.status()));
Expand All @@ -525,7 +524,7 @@ impl StacksClient {
"{function_name}: {}",
call_read_only_response
.cause
.unwrap_or("unknown".to_string())
.unwrap_or_else(|| "unknown".to_string())
)));
}
let hex = call_read_only_response.result.unwrap_or_default();
Expand Down
21 changes: 10 additions & 11 deletions stacks-signer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum ConfigError {
UnsupportedAddressVersion,
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq)]
#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
/// The Stacks network to use.
pub enum Network {
Expand All @@ -78,31 +78,31 @@ impl std::fmt::Display for Network {

impl Network {
/// Converts a Network enum variant to a corresponding chain id
pub fn to_chain_id(&self) -> u32 {
pub const fn to_chain_id(&self) -> u32 {
match self {
Self::Mainnet => CHAIN_ID_MAINNET,
Self::Testnet | Self::Mocknet => CHAIN_ID_TESTNET,
}
}

/// Convert a Network enum variant to a corresponding address version
pub fn to_address_version(&self) -> u8 {
pub const fn to_address_version(&self) -> u8 {
match self {
Self::Mainnet => C32_ADDRESS_VERSION_MAINNET_SINGLESIG,
Self::Testnet | Self::Mocknet => C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
}
}

/// Convert a Network enum variant to a Transaction Version
pub fn to_transaction_version(&self) -> TransactionVersion {
pub const fn to_transaction_version(&self) -> TransactionVersion {
match self {
Self::Mainnet => TransactionVersion::Mainnet,
Self::Testnet | Self::Mocknet => TransactionVersion::Testnet,
}
}

/// Check if the network is Mainnet or not
pub fn is_mainnet(&self) -> bool {
pub const fn is_mainnet(&self) -> bool {
match self {
Self::Mainnet => true,
Self::Testnet | Self::Mocknet => false,
Expand Down Expand Up @@ -219,7 +219,7 @@ struct RawConfigFile {
impl RawConfigFile {
/// load the config from a string
pub fn load_from_str(data: &str) -> Result<Self, ConfigError> {
let config: RawConfigFile =
let config: Self =
toml::from_str(data).map_err(|e| ConfigError::ParseError(format!("{e:?}")))?;
Ok(config)
}
Expand All @@ -234,7 +234,7 @@ impl TryFrom<&PathBuf> for RawConfigFile {
type Error = ConfigError;

fn try_from(path: &PathBuf) -> Result<Self, Self::Error> {
RawConfigFile::load_from_str(&fs::read_to_string(path).map_err(|e| {
Self::load_from_str(&fs::read_to_string(path).map_err(|e| {
ConfigError::InvalidConfig(format!("failed to read config file: {e:?}"))
})?)
}
Expand All @@ -255,10 +255,9 @@ impl TryFrom<RawConfigFile> for GlobalConfig {
.to_socket_addrs()
.map_err(|_| ConfigError::BadField("endpoint".to_string(), raw_data.endpoint.clone()))?
.next()
.ok_or(ConfigError::BadField(
"endpoint".to_string(),
raw_data.endpoint.clone(),
))?;
.ok_or_else(|| {
ConfigError::BadField("endpoint".to_string(), raw_data.endpoint.clone())
})?;

let stacks_private_key =
StacksPrivateKey::from_hex(&raw_data.stacks_private_key).map_err(|_| {
Expand Down
2 changes: 1 addition & 1 deletion stacks-signer/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl From<PublicKeys> for CoordinatorSelector {
/// Create a new Coordinator selector from the given list of public keys
fn from(public_keys: PublicKeys) -> Self {
let coordinator_ids =
CoordinatorSelector::calculate_coordinator_ids(&public_keys, &ConsensusHash::empty());
Self::calculate_coordinator_ids(&public_keys, &ConsensusHash::empty());
let coordinator_id = *coordinator_ids
.first()
.expect("FATAL: No registered signers");
Expand Down
8 changes: 4 additions & 4 deletions stacks-signer/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct RunLoopCommand {
}

/// The runloop state
#[derive(PartialEq, Debug, Clone, Copy)]
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum State {
/// The runloop is uninitialized
Uninitialized,
Expand All @@ -52,7 +52,7 @@ pub enum State {
}

/// The current reward cycle info
#[derive(PartialEq, Debug, Clone, Copy)]
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct RewardCycleInfo {
/// The current reward cycle
pub reward_cycle: u64,
Expand All @@ -68,7 +68,7 @@ pub struct RewardCycleInfo {

impl RewardCycleInfo {
/// Check if the provided burnchain block height is part of the reward cycle
pub fn is_in_reward_cycle(&self, burnchain_block_height: u64) -> bool {
pub const fn is_in_reward_cycle(&self, burnchain_block_height: u64) -> bool {
let blocks_mined = burnchain_block_height.saturating_sub(self.first_burnchain_block_height);
let reward_cycle_length = self
.reward_phase_block_length
Expand Down Expand Up @@ -109,7 +109,7 @@ impl From<GlobalConfig> for RunLoop {
/// Creates new runloop from a config
fn from(config: GlobalConfig) -> Self {
let stacks_client = StacksClient::from(&config);
RunLoop {
Self {
config,
stacks_client,
stacks_signers: HashMap::with_capacity(2),
Expand Down
8 changes: 4 additions & 4 deletions stacks-signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct BlockInfo {

impl BlockInfo {
/// Create a new BlockInfo
pub fn new(block: NakamotoBlock) -> Self {
pub const fn new(block: NakamotoBlock) -> Self {
Self {
block,
vote: None,
Expand All @@ -91,7 +91,7 @@ impl BlockInfo {
}

/// Create a new BlockInfo with an associated nonce request packet
pub fn new_with_request(block: NakamotoBlock, nonce_request: NonceRequest) -> Self {
pub const fn new_with_request(block: NakamotoBlock, nonce_request: NonceRequest) -> Self {
Self {
block,
vote: None,
Expand Down Expand Up @@ -124,7 +124,7 @@ pub enum Command {
}

/// The Signer state
#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum State {
/// The signer is idle, waiting for messages and commands
Idle,
Expand Down Expand Up @@ -284,7 +284,7 @@ impl From<SignerConfig> for Signer {
coordinator_selector,
approved_aggregate_public_key: None,
miner_key: None,
db_path: signer_config.db_path.clone(),
db_path: signer_config.db_path,
signer_db,
}
}
Expand Down
2 changes: 1 addition & 1 deletion stacks-signer/src/signerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl SignerDb {
/// Create a new `SignerState` instance.
/// This will create a new SQLite database at the given path
/// or an in-memory database if the path is ":memory:"
pub fn new(db_path: impl AsRef<Path>) -> Result<SignerDb, DBError> {
pub fn new(db_path: impl AsRef<Path>) -> Result<Self, DBError> {
let connection = Self::connect(db_path)?;

let signer_db = Self { db: connection };
Expand Down

0 comments on commit f9e9791

Please sign in to comment.