From 06133bbb0cf725e5a57c336a8fd34dd87f025766 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Mon, 4 Mar 2024 16:11:47 +0100 Subject: [PATCH] chore: Upgrade Rust to version 1.76 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. --- coordinator/src/admin.rs | 2 +- coordinator/src/leaderboard.rs | 24 ++++++++++----------- coordinator/src/orderbook/db/orders.rs | 2 +- coordinator/src/orderbook/trading.rs | 6 +++--- crates/ln-dlc-node/src/node/connection.rs | 20 +++++++++++++++++ crates/ln-dlc-node/src/tests/dlc_channel.rs | 2 +- crates/ln-dlc-node/src/tests/mod.rs | 2 +- mobile/native/src/db/custom_types.rs | 2 +- rust-toolchain.toml | 2 +- 9 files changed, 41 insertions(+), 21 deletions(-) diff --git a/coordinator/src/admin.rs b/coordinator/src/admin.rs index d22d58db1..dc69e2e55 100644 --- a/coordinator/src/admin.rs +++ b/coordinator/src/admin.rs @@ -466,7 +466,7 @@ pub async fn connect_to_peer( target: Json, ) -> 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(()) diff --git a/coordinator/src/leaderboard.rs b/coordinator/src/leaderboard.rs index bb9cd7402..862de2fcf 100644 --- a/coordinator/src/leaderboard.rs +++ b/coordinator/src/leaderboard.rs @@ -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); @@ -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); @@ -229,9 +229,9 @@ 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); @@ -239,9 +239,9 @@ pub mod tests { 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); diff --git a/coordinator/src/orderbook/db/orders.rs b/coordinator/src/orderbook/db/orders.rs index 30a59b8b0..48a09a19d 100644 --- a/coordinator/src/orderbook/db/orders.rs +++ b/coordinator/src/orderbook/db/orders.rs @@ -302,7 +302,7 @@ pub fn get_with_id(conn: &mut PgConnection, uid: Uuid) -> QueryResult(conn)?; - let option = x.get(0).map(|order| OrderbookOrder::from(order.clone())); + let option = x.first().map(|order| OrderbookOrder::from(order.clone())); Ok(option) } diff --git a/coordinator/src/orderbook/trading.rs b/coordinator/src/orderbook/trading.rs index 7be8b3a80..aed137429 100644 --- a/coordinator/src/orderbook/trading.rs +++ b/coordinator/src/orderbook/trading.rs @@ -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); @@ -596,7 +596,7 @@ mod tests { .taker_match .filled_with .matches - .get(0) + .first() .unwrap() .quantity, order.quantity diff --git a/crates/ln-dlc-node/src/node/connection.rs b/crates/ln-dlc-node/src/node/connection.rs index 56a0a58d0..d9917ea23 100644 --- a/crates/ln-dlc-node/src/node/connection.rs +++ b/crates/ln-dlc-node/src/node/connection.rs @@ -84,6 +84,13 @@ impl OnionMessageHandler for TenTenOneOnionMessageHandler { impl Node { + /// 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>>> { #[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 { @@ -128,6 +135,19 @@ impl 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() diff --git a/crates/ln-dlc-node/src/tests/dlc_channel.rs b/crates/ln-dlc-node/src/tests/dlc_channel.rs index 94ccb6872..8d635c529 100644 --- a/crates/ln-dlc-node/src/tests/dlc_channel.rs +++ b/crates/ln-dlc-node/src/tests/dlc_channel.rs @@ -430,7 +430,7 @@ async fn open_channel_and_position( coordinator_dlc_collateral: Amount, fee_rate_sats_per_vbyte: Option, ) -> (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; diff --git a/crates/ln-dlc-node/src/tests/mod.rs b/crates/ln-dlc-node/src/tests/mod.rs index 03af6c35e..3cc351e87 100644 --- a/crates/ln-dlc-node/src/tests/mod.rs +++ b/crates/ln-dlc-node/src/tests/mod.rs @@ -293,7 +293,7 @@ impl Node Result<()> { self.disconnect(peer); tokio::time::sleep(Duration::from_secs(1)).await; - self.connect(peer).await?; + self.connect_once(peer).await?; Ok(()) } } diff --git a/mobile/native/src/db/custom_types.rs b/mobile/native/src/db/custom_types.rs index c1fa45091..115767f5b 100644 --- a/mobile/native/src/db/custom_types.rs +++ b/mobile/native/src/db/custom_types.rs @@ -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); } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 280066c76..c0f4c76c3 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.74" +channel = "1.76" components = ["clippy", "rustfmt"]