Skip to content

Commit 267ea7e

Browse files
committed
Extract pre check functions
1 parent 37be9e4 commit 267ea7e

File tree

1 file changed

+146
-53
lines changed
  • batcher/aligned-batcher/src

1 file changed

+146
-53
lines changed

batcher/aligned-batcher/src/lib.rs

Lines changed: 146 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -563,60 +563,24 @@ impl Batcher {
563563
// * Perform validations over the message *
564564
// * ---------------------------------------------------*
565565

566-
// This check does not save against "Holesky" and "HoleskyStage", since both are chain_id 17000
567-
let msg_chain_id = client_msg.verification_data.chain_id;
568-
if msg_chain_id != self.chain_id {
569-
warn!("Received message with incorrect chain id: {msg_chain_id}");
570-
send_message(
571-
ws_conn_sink.clone(),
572-
SubmitProofResponseMessage::InvalidChainId,
573-
)
574-
.await;
575-
self.metrics.user_error(&["invalid_chain_id", ""]);
566+
// All check functions sends the error to the metrics server and logs it
567+
// if they return false
568+
569+
if !self.msg_chain_id_is_valid(&client_msg, &ws_conn_sink).await {
576570
return Ok(());
577571
}
578572

579-
// This checks saves against "Holesky" and "HoleskyStage", since each one has a different payment service address
580-
let msg_payment_service_addr = client_msg.verification_data.payment_service_addr;
581-
if msg_payment_service_addr != self.payment_service.address() {
582-
warn!("Received message with incorrect payment service address: {msg_payment_service_addr}");
583-
send_message(
584-
ws_conn_sink.clone(),
585-
SubmitProofResponseMessage::InvalidPaymentServiceAddress(
586-
msg_payment_service_addr,
587-
self.payment_service.address(),
588-
),
589-
)
590-
.await;
591-
self.metrics
592-
.user_error(&["invalid_payment_service_address", ""]);
573+
if !self.msg_batcher_payment_addr_is_valid(&client_msg, &ws_conn_sink).await {
593574
return Ok(());
594575
}
595576

596-
info!("Verifying message signature...");
597-
let Ok(addr) = client_msg.verify_signature() else {
598-
error!("Signature verification error");
599-
send_message(
600-
ws_conn_sink.clone(),
601-
SubmitProofResponseMessage::InvalidSignature,
602-
)
603-
.await;
604-
self.metrics.user_error(&["invalid_signature", ""]);
577+
if !self.msg_proof_size_is_valid(&client_msg, &ws_conn_sink).await {
605578
return Ok(());
606-
};
607-
info!("Message signature verified");
579+
}
608580

609-
let proof_size = client_msg.verification_data.verification_data.proof.len();
610-
if proof_size > self.max_proof_size {
611-
error!("Proof size exceeds the maximum allowed size.");
612-
send_message(
613-
ws_conn_sink.clone(),
614-
SubmitProofResponseMessage::ProofTooLarge,
615-
)
616-
.await;
617-
self.metrics.user_error(&["proof_too_large", ""]);
581+
let Some(addr) = self.msg_signature_is_valid(&client_msg, &ws_conn_sink).await else {
618582
return Ok(());
619-
}
583+
};
620584

621585
let nonced_verification_data = client_msg.verification_data.clone();
622586

@@ -661,6 +625,7 @@ impl Batcher {
661625
}
662626

663627
if self.is_nonpaying(&addr) {
628+
// TODO: Non paying msg and paying should share some logic
664629
return self
665630
.handle_nonpaying_msg(ws_conn_sink.clone(), &client_msg)
666631
.await;
@@ -671,17 +636,12 @@ impl Batcher {
671636
// We don't need a batch state lock here, since if the user locks its funds
672637
// after the check, some blocks should pass until he can withdraw.
673638
// It is safe to do just do this here.
674-
if self.user_balance_is_unlocked(&addr).await {
675-
send_message(
676-
ws_conn_sink.clone(),
677-
SubmitProofResponseMessage::InsufficientBalance(addr),
678-
)
679-
.await;
680-
self.metrics.user_error(&["insufficient_balance", ""]);
639+
if !self.msg_user_balance_is_locked(&addr, &ws_conn_sink).await {
681640
return Ok(());
682641
}
683642

684-
// We aquire the lock first only to query if the user is already present and the lock is dropped.
643+
644+
// We acquire the lock first only to query if the user is already present and the lock is dropped.
685645
// If it was not present, then the user nonce is queried to the Aligned contract.
686646
// Lastly, we get a lock of the batch state again and insert the user state if it was still missing.
687647

@@ -1894,4 +1854,137 @@ impl Batcher {
18941854
(self.aggregator_fee_percentage_multiplier * self.aggregator_gas_cost) / PERCENTAGE_DIVIDER
18951855
+ BATCHER_SUBMISSION_BASE_GAS_COST
18961856
}
1857+
1858+
/// Checks if the message signature is valid
1859+
/// and returns the address if its.
1860+
/// If not, returns false, logs the error,
1861+
/// and sends it to the metrics server
1862+
async fn msg_signature_is_valid(
1863+
&self,
1864+
client_msg: &SubmitProofMessage,
1865+
ws_conn_sink: &WsMessageSink,
1866+
) -> Option<Address> {
1867+
let Ok(addr) = client_msg.verify_signature() else {
1868+
error!("Signature verification error");
1869+
send_message(
1870+
ws_conn_sink.clone(),
1871+
SubmitProofResponseMessage::InvalidSignature,
1872+
)
1873+
.await;
1874+
self.metrics.user_error(&["invalid_signature", ""]);
1875+
return None;
1876+
};
1877+
1878+
Some(addr)
1879+
}
1880+
1881+
/// Checks if the proof size + pub inputs is valid (not exceeding max_proof_size)
1882+
/// Returns false, logs the error,
1883+
/// and sends it to the metrics server if the size is too large
1884+
async fn msg_proof_size_is_valid(
1885+
&self,
1886+
client_msg: &SubmitProofMessage,
1887+
ws_conn_sink: &WsMessageSink,
1888+
) -> bool {
1889+
1890+
let verification_data = match cbor_serialize(&client_msg.verification_data) {
1891+
Ok(data) => data,
1892+
// This should never happened, the user sent all his data serialized
1893+
Err(_) => {
1894+
error!("Proof serialization error");
1895+
send_message(
1896+
ws_conn_sink.clone(),
1897+
SubmitProofResponseMessage::Error("Proof serialization error".to_string()),
1898+
)
1899+
.await;
1900+
self.metrics.user_error(&["proof_serialization_error", ""]);
1901+
return false;
1902+
}
1903+
};
1904+
1905+
if verification_data.len() > self.max_proof_size {
1906+
error!("Proof size exceeds the maximum allowed size.");
1907+
send_message(
1908+
ws_conn_sink.clone(),
1909+
SubmitProofResponseMessage::ProofTooLarge,
1910+
)
1911+
.await;
1912+
self.metrics.user_error(&["proof_too_large", ""]);
1913+
return false;
1914+
}
1915+
1916+
true
1917+
}
1918+
1919+
1920+
/// Checks if the chain id matches the one in the config
1921+
/// Returns false, logs the error,
1922+
/// and sends it to the metrics server if it doesn't matches
1923+
async fn msg_chain_id_is_valid(
1924+
&self,
1925+
client_msg: &SubmitProofMessage,
1926+
ws_conn_sink: &WsMessageSink,
1927+
) -> bool {
1928+
let msg_chain_id = client_msg.verification_data.chain_id;
1929+
if msg_chain_id != self.chain_id {
1930+
warn!("Received message with incorrect chain id: {msg_chain_id}");
1931+
send_message(
1932+
ws_conn_sink.clone(),
1933+
SubmitProofResponseMessage::InvalidChainId,
1934+
)
1935+
.await;
1936+
self.metrics.user_error(&["invalid_chain_id", ""]);
1937+
return false;
1938+
}
1939+
1940+
true
1941+
}
1942+
1943+
/// Checks if the message has a valid payment service address
1944+
/// Returns false, logs the error,
1945+
/// and sends it to the metrics server if it doesn't match
1946+
async fn msg_batcher_payment_addr_is_valid(
1947+
&self,
1948+
client_msg: &SubmitProofMessage,
1949+
ws_conn_sink: &WsMessageSink,
1950+
) -> bool {
1951+
let msg_payment_service_addr = client_msg.verification_data.payment_service_addr;
1952+
if msg_payment_service_addr != self.payment_service.address() {
1953+
warn!("Received message with incorrect payment service address: {msg_payment_service_addr}");
1954+
send_message(
1955+
ws_conn_sink.clone(),
1956+
SubmitProofResponseMessage::InvalidPaymentServiceAddress(
1957+
msg_payment_service_addr,
1958+
self.payment_service.address(),
1959+
),
1960+
)
1961+
.await;
1962+
self.metrics
1963+
.user_error(&["invalid_payment_service_address", ""]);
1964+
return false;
1965+
}
1966+
1967+
true
1968+
}
1969+
1970+
/// Checks if the user's balance is unlocked
1971+
/// Returns false if balance is unlocked, logs the error,
1972+
/// and sends it to the metrics server
1973+
async fn msg_user_balance_is_locked(
1974+
&self,
1975+
addr: &Address,
1976+
ws_conn_sink: &WsMessageSink,
1977+
) -> bool {
1978+
if self.user_balance_is_unlocked(addr).await {
1979+
send_message(
1980+
ws_conn_sink.clone(),
1981+
SubmitProofResponseMessage::InsufficientBalance(*addr),
1982+
)
1983+
.await;
1984+
self.metrics.user_error(&["insufficient_balance", ""]);
1985+
return false;
1986+
}
1987+
1988+
true
1989+
}
18971990
}

0 commit comments

Comments
 (0)