Skip to content

Commit 335ae1e

Browse files
taturosatientropidelicuri-99JuArceMauroToscano
authored
feat (batcher): allow whitelist of non paying specific address (#523)
Co-authored-by: Mariano Nicolini <mariano.nicolini.91@gmail.com> Co-authored-by: Urix <43704209+uri-99@users.noreply.github.com> Co-authored-by: JuArce <52429267+JuArce@users.noreply.github.com> Co-authored-by: MauroFab <maurotoscano2@gmail.com> Co-authored-by: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com>
1 parent faa299b commit 335ae1e

4 files changed

Lines changed: 58 additions & 28 deletions

File tree

batcher/aligned-batcher/src/config/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ethers::types::Address;
12
use serde::Deserialize;
23

34
#[derive(Clone, Debug, Deserialize)]
@@ -6,6 +7,12 @@ pub struct ECDSAConfig {
67
pub private_key_store_password: String,
78
}
89

10+
#[derive(Debug, Deserialize)]
11+
pub struct NonPayingConfig {
12+
pub address: Address,
13+
pub replacement: Address,
14+
}
15+
916
#[derive(Debug, Deserialize)]
1017
pub struct BatcherConfigFromYaml {
1118
pub block_interval: u64,
@@ -14,6 +21,7 @@ pub struct BatcherConfigFromYaml {
1421
pub max_batch_size: usize,
1522
pub eth_ws_reconnects: usize,
1623
pub pre_verification_is_enabled: bool,
24+
pub non_paying: Option<NonPayingConfig>,
1725
}
1826

1927
#[derive(Debug, Deserialize)]

batcher/aligned-batcher/src/lib.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,33 @@ use std::net::SocketAddr;
55
use std::sync::Arc;
66
use std::time::Duration;
77

8-
use crate::eth::BatchVerifiedEventStream;
9-
use aligned_batcher_lib::types::{
10-
BatchInclusionData, ClientMessage, VerificationCommitmentBatch, VerificationData,
11-
VerificationDataCommitment,
12-
};
138
use aws_sdk_s3::client::Client as S3Client;
14-
use eth::{BatchVerifiedFilter, BatcherPaymentService};
159
use ethers::prelude::{Middleware, Provider};
1610
use ethers::providers::Ws;
1711
use ethers::types::{Address, U256};
1812
use futures_util::stream::{self, SplitSink};
1913
use futures_util::{future, SinkExt, StreamExt, TryStreamExt};
2014
use lambdaworks_crypto::merkle_tree::merkle::MerkleTree;
21-
use log::{debug, error, info};
15+
use log::{debug, error, info, warn};
2216
use tokio::net::{TcpListener, TcpStream};
2317
use tokio::sync::{Mutex, RwLock};
2418
use tokio::time::timeout;
2519
use tokio_tungstenite::tungstenite::error::ProtocolError;
2620
use tokio_tungstenite::tungstenite::protocol::{frame::coding::CloseCode, CloseFrame};
2721
use tokio_tungstenite::tungstenite::{Error, Message};
2822
use tokio_tungstenite::WebSocketStream;
23+
24+
use aligned_batcher_lib::types::{
25+
BatchInclusionData, ClientMessage, VerificationCommitmentBatch, VerificationData,
26+
VerificationDataCommitment,
27+
};
28+
use eth::{BatchVerifiedFilter, BatcherPaymentService};
2929
use types::batch_queue::BatchQueue;
3030
use types::errors::BatcherError;
3131

32-
use crate::config::{ConfigFromYaml, ContractDeploymentOutput};
32+
use crate::config::{ConfigFromYaml, ContractDeploymentOutput, NonPayingConfig};
3333
use crate::eth::AlignedLayerServiceManager;
34+
use crate::eth::BatchVerifiedEventStream;
3435

3536
mod config;
3637
mod eth;
@@ -59,6 +60,7 @@ pub struct Batcher {
5960
last_uploaded_batch_block: Mutex<u64>,
6061
pre_verification_is_enabled: bool,
6162
protocol_version: u16,
63+
non_paying_config: Option<NonPayingConfig>,
6264
}
6365

6466
impl Batcher {
@@ -102,6 +104,11 @@ impl Batcher {
102104
.await
103105
.expect("Failed to get Batcher Payment Service contract");
104106

107+
if let Some(non_paying_config) = &config.batcher.non_paying {
108+
warn!("Non-paying address configuration detected. Will replace non-paying address {} with configured address {}.",
109+
non_paying_config.address, non_paying_config.replacement);
110+
}
111+
105112
Self {
106113
s3_client,
107114
eth_ws_provider,
@@ -115,6 +122,7 @@ impl Batcher {
115122
last_uploaded_batch_block: Mutex::new(last_uploaded_batch_block),
116123
pre_verification_is_enabled: config.batcher.pre_verification_is_enabled,
117124
protocol_version: PROTOCOL_VERSION,
125+
non_paying_config: config.batcher.non_paying,
118126
}
119127
}
120128

@@ -197,11 +205,18 @@ impl Batcher {
197205
serde_json::from_str(message.to_text().expect("Message is not text"))
198206
.expect("Failed to deserialize task");
199207

200-
// FIXME: We are not doing anything for the moment with the address from the
201-
// sender, this logic should be added for the payment system.
202208
info!("Verifying message signature...");
203209
let submitter_addr = if let Ok(addr) = client_msg.verify_signature() {
204210
info!("Message signature verified");
211+
212+
let mut addr = addr;
213+
if let Some(non_paying_config) = &self.non_paying_config {
214+
if addr == non_paying_config.address {
215+
info!("Non-paying address detected. Replacing with configured address");
216+
addr = non_paying_config.replacement;
217+
}
218+
}
219+
205220
let user_balance = self
206221
.payment_service
207222
.user_balances(addr)
@@ -215,6 +230,7 @@ impl Batcher {
215230
ProtocolError::HandshakeIncomplete,
216231
));
217232
}
233+
218234
addr
219235
} else {
220236
error!("Signature verification error");
@@ -356,10 +372,9 @@ impl Batcher {
356372
let batch_merkle_tree: MerkleTree<VerificationCommitmentBatch> =
357373
MerkleTree::build(&batch_data_comm);
358374

359-
let submitter_addresses: Vec<Address> = finalized_batch
360-
.clone()
361-
.into_iter()
362-
.map(|(_, _, _, addr)| addr)
375+
let submitter_addresses = finalized_batch
376+
.iter()
377+
.map(|(_, _, _, addr)| *addr)
363378
.collect();
364379

365380
let events = self.service_manager.event::<BatchVerifiedFilter>();
@@ -386,7 +401,10 @@ impl Batcher {
386401
// connected clients
387402
let await_batch_verified_fut =
388403
await_batch_verified_event(&mut stream, &batch_merkle_tree.root);
389-
if (timeout(Duration::from_secs(60), await_batch_verified_fut).await).is_err() {
404+
if timeout(Duration::from_secs(60), await_batch_verified_fut)
405+
.await
406+
.is_err()
407+
{
390408
send_timeout_close(finalized_batch).await?;
391409
} else {
392410
send_batch_inclusion_data_responses(finalized_batch, &batch_merkle_tree).await;

batcher/aligned/src/main.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1-
mod errors;
2-
mod eth;
3-
mod types;
4-
51
use std::fs::File;
62
use std::io::BufReader;
73
use std::io::Write;
84
use std::str::FromStr;
95
use std::{path::PathBuf, sync::Arc};
106

11-
use aligned_batcher_lib::types::ClientMessage;
12-
use aligned_batcher_lib::types::VerificationCommitmentBatch;
13-
use aligned_batcher_lib::types::VerificationDataCommitment;
7+
use clap::Subcommand;
8+
use clap::{Parser, ValueEnum};
149
use env_logger::Env;
1510
use ethers::prelude::*;
11+
use ethers::utils::hex;
1612
use futures_util::{
1713
future,
1814
stream::{SplitSink, SplitStream},
1915
SinkExt, StreamExt, TryStreamExt,
2016
};
2117
use log::warn;
2218
use log::{error, info};
19+
use sha3::{Digest, Keccak256};
2320
use tokio::{net::TcpStream, sync::Mutex};
2421
use tokio_tungstenite::connect_async;
2522
use tokio_tungstenite::tungstenite::Message;
2623
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
2724

25+
use aligned_batcher_lib::types::ClientMessage;
26+
use aligned_batcher_lib::types::VerificationCommitmentBatch;
27+
use aligned_batcher_lib::types::VerificationDataCommitment;
2828
use aligned_batcher_lib::types::{BatchInclusionData, ProvingSystemId, VerificationData};
29-
use clap::Subcommand;
30-
use ethers::core::rand::thread_rng;
31-
use ethers::utils::hex;
32-
use sha3::{Digest, Keccak256};
3329

3430
use crate::errors::BatcherClientError;
3531
use crate::types::AlignedVerificationData;
3632
use crate::AlignedCommands::GetVerificationKeyCommitment;
3733
use crate::AlignedCommands::Submit;
3834
use crate::AlignedCommands::VerifyProofOnchain;
3935

40-
use clap::{Parser, ValueEnum};
36+
mod errors;
37+
mod eth;
38+
mod types;
4139

4240
#[derive(Parser, Debug)]
4341
#[command(version, about, long_about = None)]
@@ -156,6 +154,8 @@ pub enum ProvingSystemArg {
156154
Risc0,
157155
}
158156

157+
const ANVIL_PRIVATE_KEY: &str = "2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6"; // Anvil address 9
158+
159159
impl From<ProvingSystemArg> for ProvingSystemId {
160160
fn from(proving_system: ProvingSystemArg) -> Self {
161161
match proving_system {
@@ -235,7 +235,7 @@ async fn main() -> Result<(), errors::BatcherClientError> {
235235
private_key.parse::<LocalWallet>()?
236236
} else {
237237
warn!("Missing keystore used for payment. This proof will not be included if sent to Eth Mainnet");
238-
LocalWallet::new(&mut thread_rng())
238+
LocalWallet::from_str(ANVIL_PRIVATE_KEY).expect("Failed to create wallet")
239239
};
240240

241241
let msg = ClientMessage::new(verification_data, wallet).await;

config-files/config-batcher.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ batcher:
2020
max_batch_size: 268435456 # 256 MiB
2121
eth_ws_reconnects: 99999999999999
2222
pre_verification_is_enabled: true
23+
non_paying:
24+
address: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 # Anvil address 9
25+
replacement: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 # Anvil address 1
26+

0 commit comments

Comments
 (0)