Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): deprecate batcher_url parameter #1568

Open
wants to merge 18 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,14 @@ task_sender_send_infinite_proofs_devnet:
cargo run --release -- send-infinite-proofs \
--burst-size $(BURST_SIZE) --burst-time-secs $(BURST_TIME_SECS) \
--eth-rpc-url http://localhost:8545 \
--batcher-url ws://localhost:8080 \
--network devnet \
--proofs-dirpath $(CURDIR)/scripts/test_files/task_sender/proofs \
--private-keys-filepath $(CURDIR)/batcher/aligned-task-sender/wallets/devnet

task_sender_test_connections_devnet:
@cd batcher/aligned-task-sender && \
cargo run --release -- test-connections \
--batcher-url ws://localhost:8080 \
--network devnet
--num-senders $(NUM_SENDERS)

# ===== HOLESKY-STAGE =====
Expand All @@ -514,15 +513,14 @@ task_sender_send_infinite_proofs_holesky_stage:
cargo run --release -- send-infinite-proofs \
--burst-size $(BURST_SIZE) --burst-time-secs $(BURST_TIME_SECS) \
--eth-rpc-url https://ethereum-holesky-rpc.publicnode.com \
--batcher-url wss://stage.batcher.alignedlayer.com \
--network holesky-stage \
--proofs-dirpath $(CURDIR)/scripts/test_files/task_sender/proofs \
--private-keys-filepath $(CURDIR)/batcher/aligned-task-sender/wallets/holesky-stage

task_sender_test_connections_holesky_stage:
@cd batcher/aligned-task-sender && \
cargo run --release -- test-connections \
--batcher-url wss://stage.batcher.alignedlayer.com \
--network holesky-stage
--num-senders $(NUM_SENDERS)

__UTILS__:
Expand Down
51 changes: 33 additions & 18 deletions batcher/aligned-sdk/src/communication/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,49 +175,52 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
Ok(batch_inclusion_data)
}
Ok(SubmitProofResponseMessage::InvalidNonce) => {
error!("Batcher responded with invalid nonce");
error!("Batcher responded with invalid nonce. Funds have not been spent.");
Err(SubmitError::InvalidNonce)
}
Ok(SubmitProofResponseMessage::InvalidSignature) => {
error!("Batcher responded with invalid signature");
error!("Batcher responded with invalid signature. Funds have not been spent.");
Err(SubmitError::InvalidSignature)
}
Ok(SubmitProofResponseMessage::ProofTooLarge) => {
error!("Batcher responded with proof too large");
error!("Batcher responded with proof too large. Funds have not been spent.");
Err(SubmitError::ProofTooLarge)
}
Ok(SubmitProofResponseMessage::InvalidMaxFee) => {
error!("Batcher responded with invalid max fee");
error!("Batcher responded with invalid max fee. Funds have not been spent.");
Err(SubmitError::InvalidMaxFee)
}
Ok(SubmitProofResponseMessage::InsufficientBalance(addr)) => {
error!("Batcher responded with insufficient balance");
error!("Batcher responded with insufficient balance. Funds have not been spent.");
Err(SubmitError::InsufficientBalance(addr))
}
Ok(SubmitProofResponseMessage::InvalidChainId) => {
error!("Batcher responded with invalid chain id");
error!("Batcher responded with invalid chain id. Funds have not been spent.");
Err(SubmitError::InvalidChainId)
}
Ok(SubmitProofResponseMessage::InvalidReplacementMessage) => {
error!("Batcher responded with invalid replacement message");
error!(
"Batcher responded with invalid replacement message. Funds have not been spent."
);
Err(SubmitError::InvalidReplacementMessage)
}
Ok(SubmitProofResponseMessage::AddToBatchError) => {
error!("Batcher responded with add to batch error");
error!("Batcher responded with add to batch error. Funds have not been spent.");
Err(SubmitError::AddToBatchError)
}
Ok(SubmitProofResponseMessage::EthRpcError) => {
error!("Batcher experienced Eth RPC connection error");
error!("Batcher experienced Eth RPC connection error. Funds have not been spent.");
Err(SubmitError::EthereumProviderError(
"Batcher experienced Eth RPC connection error".to_string(),
"Batcher experienced Eth RPC connection error. Funds have not been spent."
.to_string(),
))
}
Ok(SubmitProofResponseMessage::InvalidPaymentServiceAddress(
received_addr,
expected_addr,
)) => {
error!(
"Batcher responded with invalid payment service address: {:?}, expected: {:?}",
"Batcher responded with invalid payment service address: {:?}, expected: {:?}. Funds have not been spent.",
received_addr, expected_addr
);
Err(SubmitError::InvalidPaymentServiceAddress(
Expand All @@ -226,11 +229,17 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
))
}
Ok(SubmitProofResponseMessage::InvalidProof(reason)) => {
error!("Batcher responded with invalid proof: {}", reason);
error!(
"Batcher responded with invalid proof: {}. Funds have not been spent.",
reason
);
Err(SubmitError::InvalidProof(reason))
}
Ok(SubmitProofResponseMessage::CreateNewTaskError(merkle_root, error)) => {
error!("Batcher responded with create new task error: {}", error);
error!(
"Batcher responded with create new task error: {}. Funds have not been spent.",
error
);
Err(SubmitError::BatchSubmissionFailed(
"Could not create task with merkle root ".to_owned()
+ &merkle_root
Expand All @@ -239,22 +248,28 @@ async fn handle_batcher_response(msg: Message) -> Result<BatchInclusionData, Sub
))
}
Ok(SubmitProofResponseMessage::ProtocolVersion(_)) => {
error!("Batcher responded with protocol version instead of batch inclusion data");
error!("Batcher responded with protocol version instead of batch inclusion data. Funds have not been spent.");
Err(SubmitError::UnexpectedBatcherResponse(
"Batcher responded with protocol version instead of batch inclusion data"
"Batcher responded with protocol version instead of batch inclusion data. Funds have not been spent."
.to_string(),
))
}
Ok(SubmitProofResponseMessage::BatchReset) => {
error!("Batcher responded with batch reset");
error!("Batcher responded with batch reset. Funds have not been spent.");
Err(SubmitError::ProofQueueFlushed)
}
Ok(SubmitProofResponseMessage::Error(e)) => {
error!("Batcher responded with error: {}", e);
error!(
"Batcher responded with error: {}. Funds have not been spent.",
e
);
Err(SubmitError::GenericError(e))
}
Err(e) => {
error!("Error while deserializing batch inclusion data: {}", e);
error!(
"Error while deserializing batch inclusion data: {}. Funds have not been spent.",
e
);
Err(SubmitError::SerializationError(e))
}
}
Expand Down
6 changes: 6 additions & 0 deletions batcher/aligned-sdk/src/core/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ pub const BUMP_MIN_RETRY_DELAY: u64 = 500; // milliseconds
pub const BUMP_MAX_RETRIES: usize = 33; // ~ 1 day
pub const BUMP_BACKOFF_FACTOR: f32 = 2.0;
pub const BUMP_MAX_RETRY_DELAY: u64 = 3600; // seconds

/// Batcher URL's
pub const BATCHER_URL_DEVNET: &str = "ws://localhost:8080";
pub const BATCHER_URL_HOLESKY: &str = "wss://holesky.batcher.alignedlayer.com";
pub const BATCHER_URL_HOLESKY_STAGE: &str = "wss://stage.batcher.alignedlayer.com";
pub const BATCHER_URL_MAINNET: &str = "wss://batcher.alignedlayer.com";
15 changes: 15 additions & 0 deletions batcher/aligned-sdk/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use lambdaworks_crypto::merkle_tree::{
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};

use super::constants::{
BATCHER_URL_DEVNET, BATCHER_URL_HOLESKY, BATCHER_URL_HOLESKY_STAGE, BATCHER_URL_MAINNET,
};

use super::errors::VerifySignatureError;

// VerificationData is a bytes32 instead of a VerificationData struct because in the BatcherPaymentService contract
Expand Down Expand Up @@ -421,6 +425,17 @@ impl FromStr for Network {
}
}

impl Network {
pub fn get_batcher_url(&self) -> &str {
match self {
Self::Devnet => BATCHER_URL_DEVNET,
Self::Holesky => BATCHER_URL_HOLESKY,
Self::HoleskyStage => BATCHER_URL_HOLESKY_STAGE,
Self::Mainnet => BATCHER_URL_MAINNET,
}
}
}

#[cfg(test)]
mod tests {
use ethers::signers::LocalWallet;
Expand Down
45 changes: 12 additions & 33 deletions batcher/aligned-sdk/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use std::path::PathBuf;

/// Submits multiple proofs to the batcher to be verified in Aligned and waits for the verification on-chain.
/// # Arguments
/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
/// * `eth_rpc_url` - The URL of the Ethereum RPC node.
/// * `chain` - The chain on which the verification will be done.
/// * `verification_data` - An array of verification data of each proof.
Expand Down Expand Up @@ -79,23 +78,15 @@ use std::path::PathBuf;
/// * `GenericError` if the error doesn't match any of the previous ones.
#[allow(clippy::too_many_arguments)] // TODO: Refactor this function, use NoncedVerificationData
pub async fn submit_multiple_and_wait_verification(
batcher_url: &str,
eth_rpc_url: &str,
network: Network,
verification_data: &[VerificationData],
max_fee: U256,
wallet: Wallet<SigningKey>,
nonce: U256,
) -> Vec<Result<AlignedVerificationData, errors::SubmitError>> {
let mut aligned_verification_data = submit_multiple(
batcher_url,
network,
verification_data,
max_fee,
wallet,
nonce,
)
.await;
let mut aligned_verification_data =
submit_multiple(network, verification_data, max_fee, wallet, nonce).await;

// TODO: open issue: use a join to .await all at the same time, avoiding the loop
// And await only once per batch, no need to await multiple proofs if they are in the same batch.
Expand Down Expand Up @@ -218,7 +209,6 @@ async fn fetch_gas_price(

/// Submits multiple proofs to the batcher to be verified in Aligned.
/// # Arguments
/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
/// * `network` - The netork on which the verification will be done.
/// * `verification_data` - An array of verification data of each proof.
/// * `max_fees` - An array of the maximum fee that the submitter is willing to pay for each proof verification.
Expand All @@ -242,14 +232,13 @@ async fn fetch_gas_price(
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
/// * `GenericError` if the error doesn't match any of the previous ones.
pub async fn submit_multiple(
batcher_url: &str,
network: Network,
verification_data: &[VerificationData],
max_fee: U256,
wallet: Wallet<SigningKey>,
nonce: U256,
) -> Vec<Result<AlignedVerificationData, errors::SubmitError>> {
let (ws_stream, _) = match connect_async(batcher_url).await {
let (ws_stream, _) = match connect_async(network.get_batcher_url()).await {
Ok((ws_stream, response)) => (ws_stream, response),
Err(e) => return vec![Err(errors::SubmitError::WebSocketConnectionError(e))],
};
Expand Down Expand Up @@ -354,7 +343,6 @@ async fn _submit_multiple(

/// Submits a proof to the batcher to be verified in Aligned and waits for the verification on-chain.
/// # Arguments
/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
/// * `eth_rpc_url` - The URL of the Ethereum RPC node.
/// * `chain` - The chain on which the verification will be done.
/// * `verification_data` - The verification data of the proof.
Expand Down Expand Up @@ -384,7 +372,6 @@ async fn _submit_multiple(
/// * `GenericError` if the error doesn't match any of the previous ones.
#[allow(clippy::too_many_arguments)] // TODO: Refactor this function, use NoncedVerificationData
pub async fn submit_and_wait_verification(
batcher_url: &str,
eth_rpc_url: &str,
network: Network,
verification_data: &VerificationData,
Expand All @@ -395,7 +382,6 @@ pub async fn submit_and_wait_verification(
let verification_data = vec![verification_data.clone()];

let aligned_verification_data = submit_multiple_and_wait_verification(
batcher_url,
eth_rpc_url,
network,
&verification_data,
Expand All @@ -416,7 +402,6 @@ pub async fn submit_and_wait_verification(

/// Submits a proof to the batcher to be verified in Aligned.
/// # Arguments
/// * `batcher_url` - The url of the batcher to which the proof will be submitted.
/// * `chain` - The chain on which the verification will be done.
/// * `verification_data` - The verification data of the proof.
/// * `max_fee` - The maximum fee that the submitter is willing to pay for the verification.
Expand All @@ -440,7 +425,6 @@ pub async fn submit_and_wait_verification(
/// * `ProofQueueFlushed` if there is an error in the batcher and the proof queue is flushed.
/// * `GenericError` if the error doesn't match any of the previous ones.
pub async fn submit(
batcher_url: &str,
network: Network,
verification_data: &VerificationData,
max_fee: U256,
Expand All @@ -449,15 +433,8 @@ pub async fn submit(
) -> Result<AlignedVerificationData, errors::SubmitError> {
let verification_data = vec![verification_data.clone()];

let aligned_verification_data = submit_multiple(
batcher_url,
network,
&verification_data,
max_fee,
wallet,
nonce,
)
.await;
let aligned_verification_data =
submit_multiple(network, &verification_data, max_fee, wallet, nonce).await;

match aligned_verification_data.first() {
Some(Ok(aligned_verification_data)) => Ok(aligned_verification_data.clone()),
Expand Down Expand Up @@ -558,19 +535,21 @@ pub fn get_vk_commitment(
/// as the batcher proofs might not yet be on ethereum,
/// producing an out-of-sync nonce with the payment service contract on ethereum
/// # Arguments
/// * `batcher_url` - The batcher websocket url.
/// * `address` - The user address for which the nonce will be retrieved.
/// * `network` - The network from which the nonce will be retrieved.
/// # Returns
/// * The next nonce of the proof submitter account.
/// # Errors
/// * `EthRpcError` if the batcher has an error in the Ethereum call when retrieving the nonce if not already cached.
pub async fn get_nonce_from_batcher(
batcher_ws_url: &str,
address: Address,
network: Network,
) -> Result<U256, GetNonceError> {
let (ws_stream, _) = connect_async(batcher_ws_url).await.map_err(|_| {
GetNonceError::ConnectionFailed("Ws connection to batcher failed".to_string())
})?;
let (ws_stream, _) = connect_async(network.get_batcher_url())
.await
.map_err(|_| {
GetNonceError::ConnectionFailed("Ws connection to batcher failed".to_string())
})?;

debug!("WebSocket handshake has been successfully completed");
let (mut ws_write, mut ws_read) = ws_stream.split();
Expand Down
2 changes: 0 additions & 2 deletions batcher/aligned-task-sender/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ To run it, you can:
cargo run --release -- send-infinite-proofs \
--burst-size <BURST_SIZE> --burst-time-secs <BURST_TIME_SECS> \
--eth-rpc-url <RPC_URL> \
--batcher-url <BATCHER_URL> \
--network holesky-stage \
--proofs-dirpath $(PWD)/scripts/test_files/task_sender/proofs \
--private-keys-filepath <PATH_TO_PRIVATE_KEYS_FILE>
Expand All @@ -82,7 +81,6 @@ This command enables and hangs N connections with the Batcher.
To run it, you can:
```
cargo run --release -- test-connections \
--batcher-url <BATCHER_URL> \
--num-senders <NUM_SENDERS>
```

Expand Down
15 changes: 5 additions & 10 deletions batcher/aligned-task-sender/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,13 @@ pub async fn generate_and_fund_wallets(args: GenerateAndFundWalletsArgs) {

/// infinitely hangs connections
pub async fn test_connection(args: TestConnectionsArgs) {
if args.batcher_url == "wss://batcher.alignedlayer.com" {
error!("Network not supported by the connection tester");
return;
}
info!("Going to only open a connection");
let mut handlers = vec![];

for i in 0..args.num_senders {
let ws_url = args.batcher_url.clone();
let network: Network = args.network.into();
let handle = tokio::spawn(async move {
let ws_url = network.get_batcher_url();
let conn = connect_async(ws_url).await;
if let Ok((mut ws_stream, _)) = conn {
info!("Opened connection for {}", i);
Expand Down Expand Up @@ -317,15 +314,15 @@ pub async fn send_infinite_proofs(args: SendInfiniteProofsArgs) {
info!("Starting senders!");
for (i, sender) in senders.iter().enumerate() {
// this is necessary because of the move
let batcher_url = args.batcher_url.clone();
let network = args.network.into();
let wallet = sender.wallet.clone();
let verification_data = verification_data.clone();

// a thread to send tasks from each loaded wallet:
let handle = tokio::spawn(async move {
loop {
let mut result = Vec::with_capacity(args.burst_size);
let nonce = get_nonce_from_batcher(&batcher_url, wallet.address())
let nonce = get_nonce_from_batcher(wallet.address(), network)
.await
.inspect_err(|e| {
error!(
Expand All @@ -346,11 +343,9 @@ pub async fn send_infinite_proofs(args: SendInfiniteProofsArgs) {
"Sending {:?} Proofs to Aligned Batcher on {:?} from sender {}, nonce: {}, address: {:?}",
args.burst_size, args.network, i, nonce, wallet.address(),
);
let batcher_url = batcher_url.clone();

let aligned_verification_data = submit_multiple(
&batcher_url.clone(),
args.network.into(),
network,
&verification_data_to_send.clone(),
max_fee,
wallet.clone(),
Expand Down
Loading
Loading