Skip to content

Commit ef21e1e

Browse files
committed
prefer non-blocking async api
1 parent 253c290 commit ef21e1e

File tree

7 files changed

+60
-62
lines changed

7 files changed

+60
-62
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wallets/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ axum.workspace = true
3131
foundry-common.workspace = true
3232
serde_json.workspace = true
3333
tokio = { workspace = true, features = ["macros"] }
34-
parking_lot.workspace = true
3534
uuid.workspace = true
3635
webbrowser = "1.0.6"
3736

crates/wallets/src/wallet_browser/handlers.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) async fn serve_logo_png() -> impl axum::response::IntoResponse {
8484
pub(crate) async fn get_next_transaction_request(
8585
State(state): State<Arc<BrowserWalletState>>,
8686
) -> Json<BrowserApiResponse<BrowserTransactionRequest>> {
87-
match state.read_next_transaction_request() {
87+
match state.read_next_transaction_request().await {
8888
Some(tx) => Json(BrowserApiResponse::with_data(tx)),
8989
None => Json(BrowserApiResponse::error("No pending transaction request")),
9090
}
@@ -97,7 +97,7 @@ pub(crate) async fn post_transaction_response(
9797
Json(body): Json<BrowserTransactionResponse>,
9898
) -> Json<BrowserApiResponse> {
9999
// Ensure that the transaction request exists.
100-
if !state.has_transaction_request(&body.id) {
100+
if !state.has_transaction_request(&body.id).await {
101101
return Json(BrowserApiResponse::error("Unknown transaction id"));
102102
}
103103

@@ -127,7 +127,7 @@ pub(crate) async fn post_transaction_response(
127127
}
128128
}
129129

130-
state.add_transaction_response(body);
130+
state.add_transaction_response(body).await;
131131

132132
Json(BrowserApiResponse::ok())
133133
}
@@ -137,7 +137,7 @@ pub(crate) async fn post_transaction_response(
137137
pub(crate) async fn get_next_signing_request(
138138
State(state): State<Arc<BrowserWalletState>>,
139139
) -> Json<BrowserApiResponse<BrowserSignRequest>> {
140-
match state.read_next_signing_request() {
140+
match state.read_next_signing_request().await {
141141
Some(req) => Json(BrowserApiResponse::with_data(req)),
142142
None => Json(BrowserApiResponse::error("No pending signing request")),
143143
}
@@ -150,7 +150,7 @@ pub(crate) async fn post_signing_response(
150150
Json(body): Json<BrowserSignResponse>,
151151
) -> Json<BrowserApiResponse> {
152152
// Ensure that the signing request exists.
153-
if !state.has_signing_request(&body.id) {
153+
if !state.has_signing_request(&body.id).await {
154154
return Json(BrowserApiResponse::error("Unknown signing request id"));
155155
}
156156

@@ -167,7 +167,7 @@ pub(crate) async fn post_signing_response(
167167
_ => {}
168168
}
169169

170-
state.add_signing_response(body);
170+
state.add_signing_response(body).await;
171171

172172
Json(BrowserApiResponse::ok())
173173
}
@@ -177,7 +177,7 @@ pub(crate) async fn post_signing_response(
177177
pub(crate) async fn get_connection_info(
178178
State(state): State<Arc<BrowserWalletState>>,
179179
) -> Json<BrowserApiResponse<Option<Connection>>> {
180-
let connection = state.get_connection();
180+
let connection = state.get_connection().await;
181181

182182
Json(BrowserApiResponse::with_data(connection))
183183
}
@@ -188,7 +188,7 @@ pub(crate) async fn post_connection_update(
188188
State(state): State<Arc<BrowserWalletState>>,
189189
Json(body): Json<Option<Connection>>,
190190
) -> Json<BrowserApiResponse> {
191-
state.set_connection(body);
191+
state.set_connection(body).await;
192192

193193
Json(BrowserApiResponse::ok())
194194
}

crates/wallets/src/wallet_browser/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mod tests {
4040
let client = client_with_token(&server);
4141

4242
// Check initial state
43-
assert!(!server.is_connected());
43+
assert!(!server.is_connected().await);
4444
assert!(!server.open_browser());
4545
assert!(server.timeout() == DEFAULT_TIMEOUT);
4646

@@ -68,22 +68,22 @@ mod tests {
6868

6969
// Check connection state
7070
let Connection { address, chain_id } =
71-
server.get_connection().expect("expected an active wallet connection");
71+
server.get_connection().await.expect("expected an active wallet connection");
7272
assert_eq!(address, ALICE);
7373
assert_eq!(chain_id, 1);
7474

7575
// Disconnect wallet
7676
disconnect_wallet(&client, &server).await;
7777

7878
// Check disconnected state
79-
assert!(!server.is_connected());
79+
assert!(!server.is_connected().await);
8080

8181
// Connect Bob's wallet
8282
connect_wallet(&client, &server, Connection::new(BOB, 42)).await;
8383

8484
// Check connection state
8585
let Connection { address, chain_id } =
86-
server.get_connection().expect("expected an active wallet connection");
86+
server.get_connection().await.expect("expected an active wallet connection");
8787
assert_eq!(address, BOB);
8888
assert_eq!(chain_id, 42);
8989

@@ -100,14 +100,14 @@ mod tests {
100100
// Connect Alice, assert connected
101101
connect_wallet(&client, &server, Connection::new(ALICE, 1)).await;
102102
let Connection { address, chain_id } =
103-
server.get_connection().expect("expected an active wallet connection");
103+
server.get_connection().await.expect("expected an active wallet connection");
104104
assert_eq!(address, ALICE);
105105
assert_eq!(chain_id, 1);
106106

107107
// Connect Bob, assert switched
108108
connect_wallet(&client, &server, Connection::new(BOB, 42)).await;
109109
let Connection { address, chain_id } =
110-
server.get_connection().expect("expected an active wallet connection");
110+
server.get_connection().await.expect("expected an active wallet connection");
111111
assert_eq!(address, BOB);
112112
assert_eq!(chain_id, 42);
113113

crates/wallets/src/wallet_browser/server.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,32 @@ impl BrowserWalletServer {
106106
}
107107

108108
/// Check if a wallet is connected.
109-
pub fn is_connected(&self) -> bool {
110-
self.state.is_connected()
109+
pub async fn is_connected(&self) -> bool {
110+
self.state.is_connected().await
111111
}
112112

113113
/// Get current wallet connection.
114-
pub fn get_connection(&self) -> Option<Connection> {
115-
self.state.get_connection()
114+
pub async fn get_connection(&self) -> Option<Connection> {
115+
self.state.get_connection().await
116116
}
117117

118118
/// Request a transaction to be signed and sent via the browser wallet.
119119
pub async fn request_transaction(
120120
&self,
121121
request: BrowserTransactionRequest,
122122
) -> Result<TxHash, BrowserWalletError> {
123-
if !self.is_connected() {
123+
if !self.is_connected().await {
124124
return Err(BrowserWalletError::NotConnected);
125125
}
126126

127127
let tx_id = request.id;
128128

129-
self.state.add_transaction_request(request);
129+
self.state.add_transaction_request(request).await;
130130

131131
let start = Instant::now();
132132

133133
loop {
134-
if let Some(response) = self.state.get_transaction_response(&tx_id) {
134+
if let Some(response) = self.state.get_transaction_response(&tx_id).await {
135135
if let Some(hash) = response.hash {
136136
return Ok(hash);
137137
} else if let Some(error) = response.error {
@@ -147,7 +147,7 @@ impl BrowserWalletServer {
147147
}
148148

149149
if start.elapsed() > self.timeout {
150-
self.state.remove_transaction_request(&tx_id);
150+
self.state.remove_transaction_request(&tx_id).await;
151151
return Err(BrowserWalletError::Timeout { operation: "Transaction" });
152152
}
153153

@@ -160,18 +160,18 @@ impl BrowserWalletServer {
160160
&self,
161161
request: BrowserSignRequest,
162162
) -> Result<Bytes, BrowserWalletError> {
163-
if !self.is_connected() {
163+
if !self.is_connected().await {
164164
return Err(BrowserWalletError::NotConnected);
165165
}
166166

167167
let tx_id = request.id;
168168

169-
self.state.add_signing_request(request);
169+
self.state.add_signing_request(request).await;
170170

171171
let start = Instant::now();
172172

173173
loop {
174-
if let Some(response) = self.state.get_signing_response(&tx_id) {
174+
if let Some(response) = self.state.get_signing_response(&tx_id).await {
175175
if let Some(signature) = response.signature {
176176
return Ok(signature);
177177
} else if let Some(error) = response.error {
@@ -187,7 +187,7 @@ impl BrowserWalletServer {
187187
}
188188

189189
if start.elapsed() > self.timeout {
190-
self.state.remove_signing_request(&tx_id);
190+
self.state.remove_signing_request(&tx_id).await;
191191
return Err(BrowserWalletError::Timeout { operation: "Signing" });
192192
}
193193

crates/wallets/src/wallet_browser/signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl BrowserSigner {
4444
let start = Instant::now();
4545

4646
loop {
47-
if let Some(Connection { address, chain_id }) = server.get_connection() {
47+
if let Some(Connection { address, chain_id }) = server.get_connection().await {
4848
let _ = sh_println!("Wallet connected: {}", address);
4949
let _ = sh_println!("Chain ID: {}", chain_id);
5050

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use parking_lot::Mutex;
3+
use tokio::sync::{Mutex, RwLock};
44
use uuid::Uuid;
55

66
use crate::wallet_browser::{
@@ -14,7 +14,7 @@ use crate::wallet_browser::{
1414
#[derive(Debug, Clone)]
1515
pub(crate) struct BrowserWalletState {
1616
/// Current information about the wallet connection.
17-
connection: Arc<Mutex<Option<Connection>>>,
17+
connection: Arc<RwLock<Option<Connection>>>,
1818
/// Request/response queue for transactions.
1919
transactions: Arc<Mutex<RequestQueue<BrowserTransactionRequest, BrowserTransactionResponse>>>,
2020
/// Request/response queue for signings.
@@ -33,7 +33,7 @@ impl BrowserWalletState {
3333
/// Create a new browser wallet state.
3434
pub fn new(session_token: String, development: bool) -> Self {
3535
Self {
36-
connection: Arc::new(Mutex::new(None)),
36+
connection: Arc::new(RwLock::new(None)),
3737
transactions: Arc::new(Mutex::new(RequestQueue::new())),
3838
signings: Arc::new(Mutex::new(RequestQueue::new())),
3939
session_token,
@@ -55,83 +55,83 @@ impl BrowserWalletState {
5555
}
5656

5757
/// Check if wallet is connected.
58-
pub fn is_connected(&self) -> bool {
59-
self.connection.lock().is_some()
58+
pub async fn is_connected(&self) -> bool {
59+
self.connection.read().await.is_some()
6060
}
6161

6262
/// Get current connection information.
63-
pub fn get_connection(&self) -> Option<Connection> {
64-
*self.connection.lock()
63+
pub async fn get_connection(&self) -> Option<Connection> {
64+
*self.connection.read().await
6565
}
6666

6767
/// Set connection information.
68-
pub fn set_connection(&self, connection: Option<Connection>) {
69-
*self.connection.lock() = connection;
68+
pub async fn set_connection(&self, connection: Option<Connection>) {
69+
*self.connection.write().await = connection;
7070
}
7171

7272
/// Add a transaction request.
73-
pub fn add_transaction_request(&self, request: BrowserTransactionRequest) {
74-
self.transactions.lock().add_request(request);
73+
pub async fn add_transaction_request(&self, request: BrowserTransactionRequest) {
74+
self.transactions.lock().await.add_request(request);
7575
}
7676

7777
/// Check if a transaction request exists.
78-
pub fn has_transaction_request(&self, id: &Uuid) -> bool {
79-
self.transactions.lock().has_request(id)
78+
pub async fn has_transaction_request(&self, id: &Uuid) -> bool {
79+
self.transactions.lock().await.has_request(id)
8080
}
8181

8282
/// Read the next transaction request.
83-
pub fn read_next_transaction_request(&self) -> Option<BrowserTransactionRequest> {
84-
self.transactions.lock().read_request().cloned()
83+
pub async fn read_next_transaction_request(&self) -> Option<BrowserTransactionRequest> {
84+
self.transactions.lock().await.read_request().cloned()
8585
}
8686

8787
// Remove a transaction request.
88-
pub fn remove_transaction_request(&self, id: &Uuid) {
89-
self.transactions.lock().remove_request(id);
88+
pub async fn remove_transaction_request(&self, id: &Uuid) {
89+
self.transactions.lock().await.remove_request(id);
9090
}
9191

9292
/// Add transaction response.
93-
pub fn add_transaction_response(&self, response: BrowserTransactionResponse) {
93+
pub async fn add_transaction_response(&self, response: BrowserTransactionResponse) {
9494
let id = response.id;
95-
let mut transactions = self.transactions.lock();
95+
let mut transactions = self.transactions.lock().await;
9696
transactions.add_response(id, response);
9797
transactions.remove_request(&id);
9898
}
9999

100100
/// Get transaction response, removing it from the queue.
101-
pub fn get_transaction_response(&self, id: &Uuid) -> Option<BrowserTransactionResponse> {
102-
self.transactions.lock().get_response(id)
101+
pub async fn get_transaction_response(&self, id: &Uuid) -> Option<BrowserTransactionResponse> {
102+
self.transactions.lock().await.get_response(id)
103103
}
104104

105105
/// Add a signing request.
106-
pub fn add_signing_request(&self, request: BrowserSignRequest) {
107-
self.signings.lock().add_request(request);
106+
pub async fn add_signing_request(&self, request: BrowserSignRequest) {
107+
self.signings.lock().await.add_request(request);
108108
}
109109

110110
/// Check if a signing request exists.
111-
pub fn has_signing_request(&self, id: &Uuid) -> bool {
112-
self.signings.lock().has_request(id)
111+
pub async fn has_signing_request(&self, id: &Uuid) -> bool {
112+
self.signings.lock().await.has_request(id)
113113
}
114114

115115
/// Read the next signing request.
116-
pub fn read_next_signing_request(&self) -> Option<BrowserSignRequest> {
117-
self.signings.lock().read_request().cloned()
116+
pub async fn read_next_signing_request(&self) -> Option<BrowserSignRequest> {
117+
self.signings.lock().await.read_request().cloned()
118118
}
119119

120120
/// Remove a signing request.
121-
pub fn remove_signing_request(&self, id: &Uuid) {
122-
self.signings.lock().remove_request(id);
121+
pub async fn remove_signing_request(&self, id: &Uuid) {
122+
self.signings.lock().await.remove_request(id);
123123
}
124124

125125
/// Add signing response.
126-
pub fn add_signing_response(&self, response: BrowserSignResponse) {
126+
pub async fn add_signing_response(&self, response: BrowserSignResponse) {
127127
let id = response.id;
128-
let mut signings = self.signings.lock();
128+
let mut signings = self.signings.lock().await;
129129
signings.add_response(id, response);
130130
signings.remove_request(&id);
131131
}
132132

133133
/// Get signing response, removing it from the queue.
134-
pub fn get_signing_response(&self, id: &Uuid) -> Option<BrowserSignResponse> {
135-
self.signings.lock().get_response(id)
134+
pub async fn get_signing_response(&self, id: &Uuid) -> Option<BrowserSignResponse> {
135+
self.signings.lock().await.get_response(id)
136136
}
137137
}

0 commit comments

Comments
 (0)