Skip to content

Commit

Permalink
chore: use standard lint settings and fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Apr 29, 2022
1 parent dacb3cd commit 3c285fd
Show file tree
Hide file tree
Showing 29 changed files with 207 additions and 192 deletions.
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn main_inner() -> Result<(), ExitError> {
let password = cli
.password
.as_ref()
.or(config.wallet.password.as_ref())
.or_else(|| config.wallet.password.as_ref())
.map(|s| s.to_owned());

if password.is_none() {
Expand Down
28 changes: 16 additions & 12 deletions base_layer/common_types/src/tx_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ impl TxId {
pub fn as_u64(self) -> u64 {
self.0
}

/// Returns a cast to i64. This number may be negative.
/// Although this is usually a bad idea, in this case TxId is never used in calculations and
/// the data within TxId is not lost when converting to i64.
///
/// Use this function to say explicitly that this is acceptable.
///
/// ```rust
/// let a = u64::MAX;
/// let b = a as i64; // -1
/// assert_eq!(a, b as u64);
/// ```
#[allow(clippy::cast_possible_wrap)]
pub fn as_i64_wrapped(self) -> i64 {
self.0 as i64
}
}

impl Hash for TxId {
Expand Down Expand Up @@ -80,24 +96,12 @@ impl From<usize> for TxId {
}
}

impl From<i32> for TxId {
fn from(s: i32) -> Self {
Self(s as u64)
}
}

impl From<TxId> for u64 {
fn from(s: TxId) -> Self {
s.0
}
}

impl From<TxId> for i64 {
fn from(s: TxId) -> Self {
s.0 as i64
}
}

impl fmt::Display for TxId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/transactions/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod test {
#[test]
pub fn test_derive_clone() {
let f0 = Fee::new(TransactionWeight::latest());
let f1 = f0.clone();
let f1 = f0;
assert_eq!(
f0.weighting().params().kernel_weight,
f1.weighting().params().kernel_weight
Expand Down
38 changes: 13 additions & 25 deletions base_layer/core/src/transactions/transaction_protocol/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,54 +807,42 @@ mod test {

#[test]
fn test_errors() {
let stp = SenderTransactionProtocol {
let mut stp = SenderTransactionProtocol {
state: SenderState::Failed(TransactionProtocolError::InvalidStateError),
};
assert_eq!(
stp.clone().get_transaction(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(stp.get_transaction(), Err(TransactionProtocolError::InvalidStateError));
assert_eq!(
stp.clone().take_transaction(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(stp.clone().check_tx_id(0.into()), false);
assert_eq!(
stp.clone().get_tx_id(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_total_amount(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_amount_to_self(),
Err(TransactionProtocolError::InvalidStateError)
);
assert!(!stp.check_tx_id(0.into()));
assert_eq!(stp.get_tx_id(), Err(TransactionProtocolError::InvalidStateError));
assert_eq!(stp.get_total_amount(), Err(TransactionProtocolError::InvalidStateError));
assert_eq!(
stp.clone().get_change_amount(),
stp.get_amount_to_self(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_change_unblinded_output(),
stp.get_change_amount(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_change_output_metadata_signature(),
stp.get_change_unblinded_output(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_change_sender_offset_public_key(),
stp.get_change_output_metadata_signature(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_recipient_sender_offset_private_key(0),
stp.get_change_sender_offset_public_key(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(
stp.clone().get_fee_amount(),
stp.get_recipient_sender_offset_private_key(0),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(stp.get_fee_amount(), Err(TransactionProtocolError::InvalidStateError));
assert_eq!(
stp.clone().build_single_round_message(),
Err(TransactionProtocolError::InvalidStateError)
Expand All @@ -867,7 +855,7 @@ mod test {
stp.clone().get_single_round_message(),
Err(TransactionProtocolError::InvalidStateError)
);
assert_eq!(stp.clone().sign(), Err(TransactionProtocolError::InvalidStateError));
assert_eq!(stp.sign(), Err(TransactionProtocolError::InvalidStateError));
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion base_layer/mmr/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub fn family_branch(pos: usize, last_pos: usize) -> Vec<(usize, usize)> {
}

/// The height of a node in a full binary tree from its index.
#[inline(always)]
pub fn bintree_height(num: usize) -> usize {
if num == 0 {
return 0;
Expand Down
1 change: 0 additions & 1 deletion base_layer/mmr/src/merkle_mountain_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ where
}

/// Return the number of nodes in the full Merkle Mountain range, excluding bagged hashes
#[inline(always)]
pub fn len(&self) -> Result<usize, MerkleMountainRangeError> {
self.hashes
.len()
Expand Down
1 change: 0 additions & 1 deletion base_layer/mmr/src/mutable_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ where
/// nodes in the MMR, while this function returns the number of leaf nodes minus the number of nodes marked for
/// deletion.
#[allow(clippy::len_without_is_empty)]
#[inline(always)]
pub fn len(&self) -> u32 {
self.size - u32::try_from(self.deleted.cardinality()).unwrap()
}
Expand Down
1 change: 0 additions & 1 deletion base_layer/mmr/src/pruned_hashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl ArrayLike for PrunedHashSet {
type Error = MerkleMountainRangeError;
type Value = Hash;

#[inline(always)]
fn len(&self) -> Result<usize, Self::Error> {
Ok(self.base_offset + self.hashes.len())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase {
}

for output in &outputs {
if output.received_in_tx_id == Some(i64::from(tx_id)) {
if output.received_in_tx_id == Some(tx_id.as_i64_wrapped()) {
info!(
target: LOG_TARGET,
"Cancelling pending inbound output with Commitment: {} - MMR Position: {:?} from TxId: {}",
Expand All @@ -879,7 +879,7 @@ impl OutputManagerBackend for OutputManagerSqliteDatabase {
},
&conn,
)?;
} else if output.spent_in_tx_id == Some(i64::from(tx_id)) {
} else if output.spent_in_tx_id == Some(tx_id.as_i64_wrapped()) {
info!(
target: LOG_TARGET,
"Cancelling pending outbound output with Commitment: {} - MMR Position: {:?} from TxId: {}",
Expand Down Expand Up @@ -1272,8 +1272,8 @@ impl From<UpdateOutput> for UpdateOutputSql {
script_private_key: u.script_private_key,
metadata_signature_nonce: u.metadata_signature_nonce,
metadata_signature_u_key: u.metadata_signature_u_key,
received_in_tx_id: u.received_in_tx_id.map(|o| o.map(i64::from)),
spent_in_tx_id: u.spent_in_tx_id.map(|o| o.map(i64::from)),
received_in_tx_id: u.received_in_tx_id.map(|o| o.map(TxId::as_i64_wrapped)),
spent_in_tx_id: u.spent_in_tx_id.map(|o| o.map(TxId::as_i64_wrapped)),
mined_in_block: u.mined_in_block,
}
}
Expand Down Expand Up @@ -1578,7 +1578,7 @@ mod test {
.update(
UpdateOutput {
status: Some(OutputStatus::Unspent),
received_in_tx_id: Some(Some(44.into())),
received_in_tx_id: Some(Some(44u64.into())),
..Default::default()
},
&conn,
Expand All @@ -1590,14 +1590,14 @@ mod test {
.update(
UpdateOutput {
status: Some(OutputStatus::EncumberedToBeReceived),
received_in_tx_id: Some(Some(44.into())),
received_in_tx_id: Some(Some(44u64.into())),
..Default::default()
},
&conn,
)
.unwrap();

let result = OutputSql::find_by_tx_id_and_encumbered(44.into(), &conn).unwrap();
let result = OutputSql::find_by_tx_id_and_encumbered(44u64.into(), &conn).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].spending_key, outputs[1].spending_key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ impl OutputSql {
Ok(outputs::table
.filter(
outputs::received_in_tx_id
.eq(i64::from(tx_id))
.or(outputs::spent_in_tx_id.eq(i64::from(tx_id))),
.eq(tx_id.as_i64_wrapped())
.or(outputs::spent_in_tx_id.eq(tx_id.as_i64_wrapped())),
)
.load(conn)?)
}
Expand Down
22 changes: 16 additions & 6 deletions base_layer/wallet/src/transaction_service/storage/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,23 @@ impl fmt::Debug for DbKey {
// Add in i64 representatives for easy debugging in sqlite. This should probably be removed at some point
match self {
PendingOutboundTransaction(tx_id) => {
write!(f, "PendingOutboundTransaction ({}u64, {}i64)", tx_id, i64::from(*tx_id))
write!(
f,
"PendingOutboundTransaction ({}u64, {}i64)",
tx_id,
tx_id.as_i64_wrapped()
)
},
PendingInboundTransaction(tx_id) => {
write!(f, "PendingInboundTransaction ({}u64, {}i64)", tx_id, i64::from(*tx_id))
write!(
f,
"PendingInboundTransaction ({}u64, {}i64)",
tx_id,
tx_id.as_i64_wrapped()
)
},
CompletedTransaction(tx_id) => {
write!(f, "CompletedTransaction ({}u64, {}i64)", tx_id, i64::from(*tx_id))
write!(f, "CompletedTransaction ({}u64, {}i64)", tx_id, tx_id.as_i64_wrapped())
},
PendingOutboundTransactions => {
write!(f, "PendingOutboundTransactions ")
Expand All @@ -215,19 +225,19 @@ impl fmt::Debug for DbKey {
f,
"CancelledPendingOutboundTransaction ({}u64, {}i64)",
tx_id,
i64::from(*tx_id)
tx_id.as_i64_wrapped()
)
},
CancelledPendingInboundTransaction(tx_id) => {
write!(
f,
"CancelledPendingInboundTransaction ({}u64, {}i64)",
tx_id,
i64::from(*tx_id)
tx_id.as_i64_wrapped()
)
},
AnyTransaction(tx_id) => {
write!(f, "AnyTransaction ({}u64, {}i64)", tx_id, i64::from(*tx_id))
write!(f, "AnyTransaction ({}u64, {}i64)", tx_id, tx_id.as_i64_wrapped())
},
}
}
Expand Down
22 changes: 12 additions & 10 deletions base_layer/wallet/src/transaction_service/storage/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,9 +2263,10 @@ mod test {
let outbound_txs = OutboundTransactionSql::index_by_cancelled(&conn, false).unwrap();
assert_eq!(outbound_txs.len(), 2);

let returned_outbound_tx =
OutboundTransaction::try_from(OutboundTransactionSql::find_by_cancelled(1.into(), false, &conn).unwrap())
.unwrap();
let returned_outbound_tx = OutboundTransaction::try_from(
OutboundTransactionSql::find_by_cancelled(1u64.into(), false, &conn).unwrap(),
)
.unwrap();
assert_eq!(
OutboundTransactionSql::try_from(returned_outbound_tx).unwrap(),
OutboundTransactionSql::try_from(outbound_tx1.clone()).unwrap()
Expand Down Expand Up @@ -2318,7 +2319,7 @@ mod test {
assert_eq!(inbound_txs.len(), 2);

let returned_inbound_tx =
InboundTransaction::try_from(InboundTransactionSql::find_by_cancelled(2.into(), false, &conn).unwrap())
InboundTransaction::try_from(InboundTransactionSql::find_by_cancelled(2u64.into(), false, &conn).unwrap())
.unwrap();
assert_eq!(
InboundTransactionSql::try_from(returned_inbound_tx).unwrap(),
Expand Down Expand Up @@ -2391,9 +2392,10 @@ mod test {
let completed_txs = CompletedTransactionSql::index_by_cancelled(&conn, false).unwrap();
assert_eq!(completed_txs.len(), 2);

let returned_completed_tx =
CompletedTransaction::try_from(CompletedTransactionSql::find_by_cancelled(2.into(), false, &conn).unwrap())
.unwrap();
let returned_completed_tx = CompletedTransaction::try_from(
CompletedTransactionSql::find_by_cancelled(2u64.into(), false, &conn).unwrap(),
)
.unwrap();
assert_eq!(
CompletedTransactionSql::try_from(returned_completed_tx).unwrap(),
CompletedTransactionSql::try_from(completed_tx1.clone()).unwrap()
Expand Down Expand Up @@ -2600,7 +2602,7 @@ mod test {
inbound_tx_sql.commit(&conn).unwrap();
inbound_tx_sql.encrypt(&cipher).unwrap();
inbound_tx_sql.update_encryption(&conn).unwrap();
let mut db_inbound_tx = InboundTransactionSql::find_by_cancelled(1.into(), false, &conn).unwrap();
let mut db_inbound_tx = InboundTransactionSql::find_by_cancelled(1u64.into(), false, &conn).unwrap();
db_inbound_tx.decrypt(&cipher).unwrap();
let decrypted_inbound_tx = InboundTransaction::try_from(db_inbound_tx).unwrap();
assert_eq!(inbound_tx, decrypted_inbound_tx);
Expand All @@ -2624,7 +2626,7 @@ mod test {
outbound_tx_sql.commit(&conn).unwrap();
outbound_tx_sql.encrypt(&cipher).unwrap();
outbound_tx_sql.update_encryption(&conn).unwrap();
let mut db_outbound_tx = OutboundTransactionSql::find_by_cancelled(2.into(), false, &conn).unwrap();
let mut db_outbound_tx = OutboundTransactionSql::find_by_cancelled(2u64.into(), false, &conn).unwrap();
db_outbound_tx.decrypt(&cipher).unwrap();
let decrypted_outbound_tx = OutboundTransaction::try_from(db_outbound_tx).unwrap();
assert_eq!(outbound_tx, decrypted_outbound_tx);
Expand Down Expand Up @@ -2660,7 +2662,7 @@ mod test {
completed_tx_sql.commit(&conn).unwrap();
completed_tx_sql.encrypt(&cipher).unwrap();
completed_tx_sql.update_encryption(&conn).unwrap();
let mut db_completed_tx = CompletedTransactionSql::find_by_cancelled(3.into(), false, &conn).unwrap();
let mut db_completed_tx = CompletedTransactionSql::find_by_cancelled(3u64.into(), false, &conn).unwrap();
db_completed_tx.decrypt(&cipher).unwrap();
let decrypted_completed_tx = CompletedTransaction::try_from(db_completed_tx).unwrap();
assert_eq!(completed_tx, decrypted_completed_tx);
Expand Down
Loading

0 comments on commit 3c285fd

Please sign in to comment.