Skip to content

Commit db25392

Browse files
committed
fix: non paying proofs are ignored when queue is full
1 parent b39a038 commit db25392

7 files changed

Lines changed: 50 additions & 64 deletions

File tree

batcher/aligned-batcher/src/lib.rs

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use types::errors::{BatcherError, TransactionSendError};
5353

5454
use crate::config::{ConfigFromYaml, ContractDeploymentOutput};
5555
use crate::telemetry::sender::TelemetrySender;
56+
use crate::types::non_paying::NonPayingData;
5657

5758
mod config;
5859
mod connection;
@@ -585,14 +586,15 @@ impl Batcher {
585586
return Ok(());
586587
}
587588

588-
let Some(addr) = self
589+
let Some(mut addr) = self
589590
.msg_signature_is_valid(&client_msg, &ws_conn_sink)
590591
.await
591592
else {
592593
return Ok(());
593594
};
594595

595-
let nonced_verification_data = client_msg.verification_data.clone();
596+
let mut nonced_verification_data = client_msg.verification_data.clone();
597+
let mut signature = client_msg.signature.clone();
596598

597599
// When pre-verification is enabled, batcher will verify proofs for faster feedback with clients
598600
if self.pre_verification_is_enabled {
@@ -635,13 +637,22 @@ impl Batcher {
635637
}
636638

637639
if self.is_nonpaying(&addr) {
638-
// TODO: Non paying msg and paying should share some logic
639-
return self
640-
.handle_nonpaying_msg(ws_conn_sink.clone(), &client_msg)
640+
info!("Generating non-paying data");
641+
let Ok(non_paying_data) = self.generate_non_paying_data(&client_msg).await else {
642+
error!("Failed to generate non paying data");
643+
send_message(
644+
ws_conn_sink.clone(),
645+
SubmitProofResponseMessage::NonPayingAddressError,
646+
)
641647
.await;
648+
return Ok(());
649+
};
650+
addr = non_paying_data.address;
651+
nonced_verification_data = non_paying_data.nonced_verification_data;
652+
signature = non_paying_data.signature;
642653
}
643654

644-
info!("Handling paying message");
655+
info!("Handling message");
645656

646657
// We don't need a batch state lock here, since if the user locks its funds
647658
// after the check, some blocks should pass until he can withdraw.
@@ -867,7 +878,7 @@ impl Batcher {
867878
batch_state_lock,
868879
nonced_verification_data,
869880
ws_conn_sink.clone(),
870-
client_msg.signature,
881+
signature,
871882
addr,
872883
)
873884
.await
@@ -1756,83 +1767,36 @@ impl Batcher {
17561767
}
17571768

17581769
/// Only relevant for testing and for users to easily use Aligned in testnet.
1759-
async fn handle_nonpaying_msg(
1770+
async fn generate_non_paying_data(
17601771
&self,
1761-
ws_sink: WsMessageSink,
17621772
client_msg: &SubmitProofMessage,
1763-
) -> Result<(), Error> {
1773+
) -> Result<NonPayingData, TransactionSendError> {
17641774
info!("Handling nonpaying message");
17651775
let Some(non_paying_config) = self.non_paying_config.as_ref() else {
17661776
warn!("There isn't a non-paying configuration loaded. This message will be ignored");
1767-
send_message(ws_sink.clone(), SubmitProofResponseMessage::InvalidNonce).await;
1768-
return Ok(());
1769-
};
1770-
1771-
let replacement_addr = non_paying_config.replacement.address();
1772-
let Some(replacement_user_balance) = self.get_user_balance(&replacement_addr).await else {
1773-
error!("Could not get balance for non-paying address {replacement_addr:?}");
1774-
send_message(
1775-
ws_sink.clone(),
1776-
SubmitProofResponseMessage::InsufficientBalance(replacement_addr),
1777-
)
1778-
.await;
1779-
return Ok(());
1777+
return Err(TransactionSendError::NonPayingAddressNotAllowed);
17801778
};
17811779

1782-
if replacement_user_balance == U256::from(0) {
1783-
error!("Insufficient funds for non-paying address {replacement_addr:?}");
1784-
send_message(
1785-
ws_sink.clone(),
1786-
SubmitProofResponseMessage::InsufficientBalance(replacement_addr),
1787-
)
1788-
.await;
1789-
return Ok(());
1790-
}
1791-
1792-
let batch_state_lock = self.batch_state.lock().await;
1793-
1794-
if batch_state_lock.is_queue_full() {
1795-
error!("Can't add new entry, the batcher queue is full");
1796-
send_message(
1797-
ws_sink.clone(),
1798-
SubmitProofResponseMessage::UnderpricedProof,
1799-
)
1800-
.await;
1801-
return Ok(());
1802-
}
1803-
18041780
let nonced_verification_data = NoncedVerificationData::new(
18051781
client_msg.verification_data.verification_data.clone(),
18061782
client_msg.verification_data.nonce,
1807-
DEFAULT_MAX_FEE_PER_PROOF.into(), // 2_000 gas per proof * 100 gwei gas price (upper bound)
1783+
(DEFAULT_MAX_FEE_PER_PROOF * 100).into(), // 2_000 gas per proof * 100 gwei gas price (upper bound) * 100 to make sure it is enough
18081784
self.chain_id,
18091785
self.payment_service.address(),
18101786
);
18111787

18121788
let client_msg = SubmitProofMessage::new(
1813-
nonced_verification_data.clone(),
1789+
client_msg.verification_data.clone(),
18141790
non_paying_config.replacement.clone(),
18151791
)
18161792
.await;
18171793

1818-
let signature = client_msg.signature;
1819-
if let Err(e) = self
1820-
.add_to_batch(
1821-
batch_state_lock,
1822-
nonced_verification_data,
1823-
ws_sink.clone(),
1824-
signature,
1825-
replacement_addr,
1826-
)
1827-
.await
1828-
{
1829-
info!("Error while adding nonpaying address entry to batch: {e:?}");
1830-
send_message(ws_sink, SubmitProofResponseMessage::AddToBatchError).await;
1831-
return Ok(());
1832-
};
1833-
18341794
info!("Non-paying verification data message handled");
1835-
Ok(())
1795+
Ok(NonPayingData {
1796+
address: non_paying_config.replacement.address(),
1797+
nonced_verification_data,
1798+
signature: client_msg.signature,
1799+
})
18361800
}
18371801

18381802
/// Gets the balance of user with address `addr` from Ethereum.

batcher/aligned-batcher/src/types/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum TransactionSendError {
1111
BatchAlreadySubmitted,
1212
InsufficientFunds,
1313
OnlyBatcherAllowed,
14+
NonPayingAddressNotAllowed,
1415
Generic(String),
1516
}
1617

@@ -170,6 +171,9 @@ impl fmt::Display for TransactionSendError {
170171
TransactionSendError::Generic(e) => {
171172
write!(f, "Generic error: {}", e)
172173
}
174+
TransactionSendError::NonPayingAddressNotAllowed => {
175+
write!(f, "Non-paying address not allowed")
176+
}
173177
}
174178
}
175179
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub(crate) mod batch_queue;
22
pub(crate) mod batch_state;
33
pub mod errors;
4+
pub(crate) mod non_paying;
45
pub(crate) mod user_state;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use ethers::types::{Address, Signature};
2+
use serde::{Deserialize, Serialize};
3+
use aligned_sdk::common::types::NoncedVerificationData;
4+
5+
#[derive(Serialize, Deserialize, Clone, Debug)]
6+
pub struct NonPayingData {
7+
pub address: Address,
8+
pub nonced_verification_data: NoncedVerificationData,
9+
pub signature: Signature,
10+
}

batcher/aligned-sdk/src/common/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub enum SubmitError {
9797
InvalidProofInclusionData,
9898
GetNonceError(String),
9999
BatchQueueLimitExceededError,
100+
NonPayingAddressError,
100101
GenericError(String),
101102
}
102103

@@ -216,6 +217,7 @@ impl fmt::Display for SubmitError {
216217
}
217218

218219
SubmitError::GetNonceError(e) => write!(f, "Error while getting nonce {}", e),
220+
SubmitError::NonPayingAddressError => write!(f, "Non-paying address error"),
219221
}
220222
}
221223
}

batcher/aligned-sdk/src/common/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ pub enum SubmitProofResponseMessage {
399399
EthRpcError,
400400
InvalidPaymentServiceAddress(Address, Address),
401401
UnderpricedProof,
402+
NonPayingAddressError,
402403
}
403404

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

batcher/aligned-sdk/src/communication/messaging.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
276276
);
277277
Err(SubmitError::SerializationError(e))
278278
}
279+
Ok(SubmitProofResponseMessage::NonPayingAddressError) => {
280+
error!("Batcher responded with non-paying address error. Funds have not been spent.");
281+
Err(SubmitError::NonPayingAddressError)
282+
}
279283
}
280284
}
281285

0 commit comments

Comments
 (0)