Skip to content

Commit

Permalink
chore: Upgrade Rust to version 1.76
Browse files Browse the repository at this point in the history
This upgrade highlighted the fact that we were using `Node::connect`
in two different ways:

- Caring about the returned value, a future that will be ready once
the connection is dropped.
- Not caring about the returned value.

Clippy was now complaining about us not always using the returned
future. To fix this, I decided to split the API into two, so that the
caller can be more explicit about what they want to do.
  • Loading branch information
luckysori committed Mar 4, 2024
1 parent 3786bb4 commit 06133bb
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion coordinator/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ pub async fn connect_to_peer(
target: Json<NodeInfo>,
) -> Result<(), AppError> {
let target = target.0;
state.node.inner.connect(target).await.map_err(|err| {
state.node.inner.connect_once(target).await.map_err(|err| {
AppError::InternalServerError(format!("Could not connect to {target}. Error: {err}"))
})?;
Ok(())
Expand Down
24 changes: 12 additions & 12 deletions coordinator/src/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ pub mod tests {
.into();

let leader_board = sort_leader_board(3, LeaderBoardCategory::Pnl, false, positions.clone());
assert_eq!(leader_board.get(0).unwrap().pnl, dec!(200));
assert_eq!(leader_board.get(0).unwrap().rank, 1);
assert_eq!(leader_board.get(0).unwrap().trader, trader_0);
assert_eq!(leader_board.first().unwrap().pnl, dec!(200));
assert_eq!(leader_board.first().unwrap().rank, 1);
assert_eq!(leader_board.first().unwrap().trader, trader_0);

assert_eq!(leader_board.get(1).unwrap().pnl, dec!(0));
assert_eq!(leader_board.get(1).unwrap().rank, 2);
Expand All @@ -196,9 +196,9 @@ pub mod tests {
assert_eq!(leader_board.get(2).unwrap().trader, trader_2);

let leader_board = sort_leader_board(3, LeaderBoardCategory::Pnl, true, positions);
assert_eq!(leader_board.get(0).unwrap().pnl, dec!(-100));
assert_eq!(leader_board.get(0).unwrap().rank, 1);
assert_eq!(leader_board.get(0).unwrap().trader, trader_2);
assert_eq!(leader_board.first().unwrap().pnl, dec!(-100));
assert_eq!(leader_board.first().unwrap().rank, 1);
assert_eq!(leader_board.first().unwrap().trader, trader_2);

assert_eq!(leader_board.get(1).unwrap().pnl, dec!(0));
assert_eq!(leader_board.get(1).unwrap().rank, 2);
Expand Down Expand Up @@ -229,19 +229,19 @@ pub mod tests {
let leader_board =
sort_leader_board(2, LeaderBoardCategory::Volume, false, positions.clone());
assert_eq!(leader_board.len(), 2);
assert_eq!(leader_board.get(0).unwrap().volume, dec!(300));
assert_eq!(leader_board.get(0).unwrap().rank, 1);
assert_eq!(leader_board.get(0).unwrap().trader, trader_2);
assert_eq!(leader_board.first().unwrap().volume, dec!(300));
assert_eq!(leader_board.first().unwrap().rank, 1);
assert_eq!(leader_board.first().unwrap().trader, trader_2);

assert_eq!(leader_board.get(1).unwrap().volume, dec!(200));
assert_eq!(leader_board.get(1).unwrap().rank, 2);
assert_eq!(leader_board.get(1).unwrap().trader, trader_0);

let leader_board = sort_leader_board(2, LeaderBoardCategory::Volume, true, positions);
assert_eq!(leader_board.len(), 2);
assert_eq!(leader_board.get(0).unwrap().volume, dec!(100));
assert_eq!(leader_board.get(0).unwrap().rank, 1);
assert_eq!(leader_board.get(0).unwrap().trader, trader_1);
assert_eq!(leader_board.first().unwrap().volume, dec!(100));
assert_eq!(leader_board.first().unwrap().rank, 1);
assert_eq!(leader_board.first().unwrap().trader, trader_1);

assert_eq!(leader_board.get(1).unwrap().volume, dec!(200));
assert_eq!(leader_board.get(1).unwrap().rank, 2);
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/orderbook/db/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub fn get_with_id(conn: &mut PgConnection, uid: Uuid) -> QueryResult<Option<Ord
.filter(orders::trader_order_id.eq(uid))
.load::<Order>(conn)?;

let option = x.get(0).map(|order| OrderbookOrder::from(order.clone()));
let option = x.first().map(|order| OrderbookOrder::from(order.clone()));
Ok(option)
}

Expand Down
6 changes: 3 additions & 3 deletions coordinator/src/orderbook/trading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,13 +581,13 @@ mod tests {
assert_eq!(matched_orders.makers_matches.len(), 1);
let maker_matches = matched_orders
.makers_matches
.get(0)
.first()
.unwrap()
.filled_with
.matches
.clone();
assert_eq!(maker_matches.len(), 1);
assert_eq!(maker_matches.get(0).unwrap().quantity, dec!(100));
assert_eq!(maker_matches.first().unwrap().quantity, dec!(100));

assert_eq!(matched_orders.taker_match.filled_with.order_id, order.id);
assert_eq!(matched_orders.taker_match.filled_with.matches.len(), 1);
Expand All @@ -596,7 +596,7 @@ mod tests {
.taker_match
.filled_with
.matches
.get(0)
.first()
.unwrap()
.quantity,
order.quantity
Expand Down
20 changes: 20 additions & 0 deletions crates/ln-dlc-node/src/node/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ impl OnionMessageHandler for TenTenOneOnionMessageHandler {
impl<D: BdkStorage, S: TenTenOneStorage + 'static, N: Storage + Sync + Send + 'static>
Node<D, S, N>
{
/// Establish a connection with a peer.
///
/// # Returns
///
/// If successful, a [`Future`] is returned which will be ready once the connection has been
/// _lost_. This is meant to be used by the caller to know when to initiate a reconnect if they
/// want to keep the connection alive.
pub async fn connect(&self, peer: NodeInfo) -> Result<Pin<Box<impl Future<Output = ()>>>> {
#[allow(clippy::async_yields_async)] // We want to poll this future in a loop elsewhere
let connection_closed_future = tokio::time::timeout(Duration::from_secs(15), async {
Expand Down Expand Up @@ -128,6 +135,19 @@ impl<D: BdkStorage, S: TenTenOneStorage + 'static, N: Storage + Sync + Send + 's
Ok(connection_closed_future)
}

/// Establish a one-time connection with a peer.
///
/// The caller is not interested in knowing if the connection is ever lost. If the caller does
/// care about that, they should use `connect` instead.
pub async fn connect_once(&self, peer: NodeInfo) -> Result<()> {
let fut = self.connect(peer).await?;

// The caller does not care if the connection is dropped eventually.
drop(fut);

Ok(())
}

pub fn is_connected(&self, pubkey: PublicKey) -> bool {
self.peer_manager
.get_peer_node_ids()
Expand Down
2 changes: 1 addition & 1 deletion crates/ln-dlc-node/src/tests/dlc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ async fn open_channel_and_position(
coordinator_dlc_collateral: Amount,
fee_rate_sats_per_vbyte: Option<u64>,
) -> (SignedChannel, SignedChannel) {
app.connect(coordinator.info).await.unwrap();
app.connect_once(coordinator.info).await.unwrap();

let app_balance_before_sat = app.get_on_chain_balance().confirmed;
let coordinator_balance_before_sat = coordinator.get_on_chain_balance().confirmed;
Expand Down
2 changes: 1 addition & 1 deletion crates/ln-dlc-node/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl Node<on_chain_wallet::InMemoryStorage, TenTenOneInMemoryStorage, InMemorySt
pub async fn reconnect(&self, peer: NodeInfo) -> Result<()> {
self.disconnect(peer);
tokio::time::sleep(Duration::from_secs(1)).await;
self.connect(peer).await?;
self.connect_once(peer).await?;
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion mobile/native/src/db/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ mod tests {

assert_eq!(vec.len(), 1);

let loaded_struct = vec.get(0).unwrap();
let loaded_struct = vec.first().unwrap();
assert_eq!(loaded_struct, &sample_struct);
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.74"
channel = "1.76"
components = ["clippy", "rustfmt"]

0 comments on commit 06133bb

Please sign in to comment.