Skip to content

Commit 73661ab

Browse files
feat: notify user after replacement message (#2131)
Co-authored-by: Julian Arce <[email protected]>
1 parent 24695db commit 73661ab

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

crates/batcher/src/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,13 +1496,26 @@ impl Batcher {
14961496
// Close old sink in old entry and replace it with the new one
14971497
{
14981498
if let Some(messaging_sink) = replacement_entry.messaging_sink {
1499-
let mut old_sink = messaging_sink.write().await;
1500-
if let Err(e) = old_sink.close().await {
1501-
// we dont want to exit here, just log the error
1502-
warn!("Error closing sink: {e:?}");
1503-
} else {
1504-
info!("Old websocket sink closed");
1505-
}
1499+
tokio::spawn(async move {
1500+
// Before closing the old sink, send a message to the client notifying that their proof
1501+
// has been replaced
1502+
send_message(
1503+
messaging_sink.clone(),
1504+
SubmitProofResponseMessage::ProofReplaced,
1505+
)
1506+
.await;
1507+
1508+
// Note: This shuts down the sink, but does not wait for it to close, so the other side
1509+
// might not receive the message. However, we don't want to wait here since it would
1510+
// block the batcher.
1511+
let mut old_sink = messaging_sink.write().await;
1512+
if let Err(e) = old_sink.close().await {
1513+
// we dont want to exit here, just log the error
1514+
warn!("Error closing sink: {e:?}");
1515+
} else {
1516+
info!("Old websocket sink closed");
1517+
}
1518+
});
15061519
} else {
15071520
warn!(
15081521
"Old websocket sink was empty. This should only happen in testing environments"

crates/sdk/src/common/errors.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub enum SubmitError {
9999
BatchQueueLimitExceededError,
100100
GenericError(String),
101101
UserFundsUnlocked,
102+
ProofReplaced,
102103
}
103104

104105
impl From<tokio_tungstenite::tungstenite::Error> for SubmitError {
@@ -213,9 +214,15 @@ impl fmt::Display for SubmitError {
213214
write!(f, "Batcher responded with invalid batch inclusion data. Can't verify your proof was correctly included in the batch.")
214215
}
215216
SubmitError::BatchQueueLimitExceededError => {
216-
write!(f, "Error while adding entry to batch, queue limit exeeded.")
217+
write!(
218+
f,
219+
"Error while adding entry to batch, queue limit exceeded."
220+
)
217221
}
218-
222+
SubmitError::ProofReplaced => write!(
223+
f,
224+
"Proof has been replaced by a higher fee for the same nonce"
225+
),
219226
SubmitError::GetNonceError(e) => write!(f, "Error while getting nonce {}", e),
220227
SubmitError::UserFundsUnlocked => write!(
221228
f,

crates/sdk/src/common/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ pub enum SubmitProofResponseMessage {
454454
UnderpricedProof,
455455
ServerBusy,
456456
UserFundsUnlocked,
457+
ProofReplaced,
457458
}
458459

459460
#[derive(Debug, Clone, Serialize, Deserialize)]

crates/sdk/src/communication/messaging.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
279279
error!("User funds have been unlocked and proofs removed from queue. Funds have not been spent.");
280280
Err(SubmitError::UserFundsUnlocked)
281281
}
282+
Ok(SubmitProofResponseMessage::ProofReplaced) => {
283+
error!("Proof has been replaced by a higher fee for the same nonce. Funds have not been spent.");
284+
Err(SubmitError::ProofReplaced)
285+
}
282286
Err(e) => {
283287
error!(
284288
"Error while deserializing batch inclusion data: {}. Funds have not been spent.",

crates/sdk/src/verification_layer/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use std::path::PathBuf;
7878
/// * `ProofTooLarge` if the proof is too large.
7979
/// * `InsufficientBalance` if the sender balance is insufficient or unlocked
8080
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
81+
/// * `ProofReplaced` if the proof has been replaced.
8182
/// * `GenericError` if the error doesn't match any of the previous ones.
8283
#[allow(clippy::too_many_arguments)] // TODO: Refactor this function, use NoncedVerificationData
8384
pub async fn submit_multiple_and_wait_verification(
@@ -245,6 +246,7 @@ async fn fetch_gas_price(
245246
/// * `ProofTooLarge` if the proof is too large.
246247
/// * `InsufficientBalance` if the sender balance is insufficient or unlocked.
247248
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
249+
/// * `ProofReplaced` if the proof has been replaced.
248250
/// * `GenericError` if the error doesn't match any of the previous ones.
249251
pub async fn submit_multiple(
250252
network: Network,
@@ -364,6 +366,7 @@ async fn _submit_multiple(
364366
/// * `ProofTooLarge` if the proof is too large.
365367
/// * `InsufficientBalance` if the sender balance is insufficient or unlocked
366368
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
369+
/// * `ProofReplaced` if the proof has been replaced.
367370
/// * `GenericError` if the error doesn't match any of the previous ones.
368371
#[allow(clippy::too_many_arguments)] // TODO: Refactor this function, use NoncedVerificationData
369372
pub async fn submit_and_wait_verification(
@@ -421,6 +424,7 @@ pub async fn submit_and_wait_verification(
421424
/// * `ProofTooLarge` if the proof is too large.
422425
/// * `InsufficientBalance` if the sender balance is insufficient or unlocked
423426
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
427+
/// * `ProofReplaced` if the proof has been replaced.
424428
/// * `GenericError` if the error doesn't match any of the previous ones.
425429
pub async fn submit(
426430
network: Network,

docs/3_guides/1.2_SDK_api_reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub async fn submit(
4444
- `InsufficientBalance` if the sender balance is not enough or unlocked
4545
- `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
4646
- `NotAContract(address)` if you are trying to send to an address that is not a contract. This generally occurs if you have misconfigured the `environment` parameter.
47+
- `ProofReplaced` if the proof has been replaced.
4748
- `GenericError` if the error doesn't match any of the previous ones.
4849

4950
### `submit_multiple`
@@ -87,6 +88,7 @@ pub async fn submit_multiple(
8788
- `ProofTooLarge` if the proof is too large.
8889
- `InsufficientBalance` if the sender balance is not enough or unlocked
8990
- `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
91+
- `ProofReplaced` if the proof has been replaced.
9092
- `GenericError` if the error doesn't match any of the previous ones.
9193

9294
### `submit_and_wait_verification`
@@ -137,6 +139,7 @@ pub async fn submit_and_wait_verification(
137139
- `InsufficientBalance` if the sender balance is not enough or unlocked
138140
- `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
139141
- `NotAContract(address)` if you are trying to send to an address that is not a contract. This generally occurs if you have misconfigured the `environment` parameter.
142+
- `ProofReplaced` if the proof has been replaced.
140143
- `GenericError` if the error doesn't match any of the previous ones.
141144

142145
### `submit_multiple_and_wait_verification`
@@ -186,6 +189,7 @@ pub async fn submit_multiple_and_wait_verification(
186189
- `InsufficientBalance` if the sender balance is not enough or unlocked
187190
- `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
188191
- `NotAContract(address)` if you are trying to send to an address that is not a contract. This generally occurs if you have misconfigured the `environment` parameter.
192+
- `ProofReplaced` if the proof has been replaced.
189193
- `GenericError` if the error doesn't match any of the previous ones.
190194

191195
### `is_proof_verified`

0 commit comments

Comments
 (0)