Skip to content

Commit

Permalink
Merge branch 'testnet' into 1573-feat-support-tls-in-batcher-connections
Browse files Browse the repository at this point in the history
  • Loading branch information
JuArce authored Jan 15, 2025
2 parents 4d7e939 + 422f6d4 commit 88bb935
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 80 deletions.
27 changes: 24 additions & 3 deletions alerts/sender_with_alert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ function fetch_tx_cost() {
fi
}

# Function to get the tx cost from the tx hash
# @param tx_hash
function get_number_proofs_in_batch_from_create_task_tx() {
if [[ -z "$1" ]]; then
echo 0
else
# Get the tx receipt from the blockchain
calldata=$(cast tx $1 --rpc-url $RPC_URL input)
decoded_calldata=$(cast --calldata-decode --json "createNewTask(bytes32 batchMerkleRoot, string batchDataPointer, address[] proofSubmitters, uint256 feeForAggregator, uint256 feePerProof, uint256 respondToTaskFeeLimit)" $calldata)
# We count the number of proofSubmitters within the tx which corresponds to the number of proofs sent in the last batch
number_proofs_in_batch=$(echo $decoded_calldata | jq '.[2] | [ match(","; "g")] | length + 1')

echo $number_proofs_in_batch
fi
}

# Function to send PagerDuty alert
# @param message
function send_pagerduty_alert() {
Expand Down Expand Up @@ -94,7 +110,7 @@ do
--rpc_url $RPC_URL \
--batcher_url $BATCHER_URL \
--network $NETWORK \
--max_fee 4000000000000000 \
--max_fee 0.004ether \
2>&1)

echo "$submit"
Expand All @@ -113,6 +129,7 @@ do
fi

total_fee_in_wei=0
total_number_proofs=0
batch_explorer_urls=()
for batch_merkle_root in $batch_merkle_roots
do
Expand All @@ -127,12 +144,16 @@ do
response_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')

# Calculate fees for transactions
number_proofs_in_batch=$(get_number_proofs_in_batch_from_create_task_tx $submission_tx_hash)
submission_fee_in_wei=$(fetch_tx_cost $submission_tx_hash)
response_fee_in_wei=$(fetch_tx_cost $response_tx_hash)
batch_fee_in_wei=$((submission_fee_in_wei + response_fee_in_wei))

# Accumulate the fee
total_fee_in_wei=$(($total_fee_in_wei + $batch_fee_in_wei))

# Accumulate proofs in batch
total_number_proofs=$(($total_number_proofs + $number_proofs_in_batch))
done

# Calculate the spent amount by converting the fee to ETH
Expand Down Expand Up @@ -168,9 +189,9 @@ do
done

if [ $verified -eq 1 ]; then
slack_message="$REPETITIONS Proofs submitted and verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
slack_message="$total_number_proofs proofs submitted and verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
else
slack_message="$REPETITIONS Proofs submitted but not verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
slack_message="$total_number_proofs proofs submitted but not verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
fi

## Send Update to Slack
Expand Down
12 changes: 12 additions & 0 deletions batcher/aligned-batcher/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl NonPayingConfig {

#[derive(Debug, Deserialize)]
pub struct BatcherConfigFromYaml {
#[serde(default = "default_aggregator_fee_percentage_multiplier")]
pub aggregator_fee_percentage_multiplier: u128,
#[serde(default = "default_aggregator_gas_cost")]
pub aggregator_gas_cost: u128,
pub block_interval: u64,
pub transaction_wait_timeout: u64,
pub max_proof_size: usize,
Expand Down Expand Up @@ -86,3 +90,11 @@ impl ContractDeploymentOutput {
serde_json::from_str(&deployment_output).expect("Failed to parse deployment output file")
}
}

fn default_aggregator_fee_percentage_multiplier() -> u128 {
aligned_sdk::core::constants::DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER
}

fn default_aggregator_gas_cost() -> u128 {
aligned_sdk::core::constants::DEFAULT_AGGREGATOR_GAS_COST
}
30 changes: 21 additions & 9 deletions batcher/aligned-batcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use types::batch_state::BatchState;
use types::user_state::UserState;

use aligned_sdk::core::constants::{
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, AGGREGATOR_GAS_COST, BUMP_BACKOFF_FACTOR,
BUMP_MAX_RETRIES, BUMP_MAX_RETRY_DELAY, BUMP_MIN_RETRY_DELAY, CONNECTION_TIMEOUT,
CONSTANT_GAS_COST, DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER, DEFAULT_MAX_FEE_PER_PROOF,
ETHEREUM_CALL_BACKOFF_FACTOR, ETHEREUM_CALL_MAX_RETRIES, ETHEREUM_CALL_MAX_RETRY_DELAY,
ETHEREUM_CALL_MIN_RETRY_DELAY, GAS_PRICE_PERCENTAGE_MULTIPLIER, PERCENTAGE_DIVIDER,
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, BATCHER_SUBMISSION_BASE_GAS_COST,
BUMP_BACKOFF_FACTOR, BUMP_MAX_RETRIES, BUMP_MAX_RETRY_DELAY, BUMP_MIN_RETRY_DELAY,
CONNECTION_TIMEOUT, DEFAULT_MAX_FEE_PER_PROOF, ETHEREUM_CALL_BACKOFF_FACTOR,
ETHEREUM_CALL_MAX_RETRIES, ETHEREUM_CALL_MAX_RETRY_DELAY, ETHEREUM_CALL_MIN_RETRY_DELAY,
GAS_PRICE_PERCENTAGE_MULTIPLIER, PERCENTAGE_DIVIDER,
RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER,
};
use aligned_sdk::core::types::{
Expand Down Expand Up @@ -95,6 +95,8 @@ pub struct Batcher {
non_paying_config: Option<NonPayingConfig>,
posting_batch: Mutex<bool>,
disabled_verifiers: Mutex<U256>,
aggregator_fee_percentage_multiplier: u128,
aggregator_gas_cost: u128,
pub metrics: metrics::BatcherMetrics,
pub telemetry: TelemetrySender,
}
Expand Down Expand Up @@ -256,6 +258,10 @@ impl Batcher {
last_uploaded_batch_block: Mutex::new(last_uploaded_batch_block),
pre_verification_is_enabled: config.batcher.pre_verification_is_enabled,
non_paying_config,
aggregator_fee_percentage_multiplier: config
.batcher
.aggregator_fee_percentage_multiplier,
aggregator_gas_cost: config.batcher.aggregator_gas_cost,
posting_batch: Mutex::new(false),
batch_state: Mutex::new(batch_state),
disabled_verifiers: Mutex::new(disabled_verifiers),
Expand Down Expand Up @@ -1184,6 +1190,7 @@ impl Batcher {
gas_price,
self.max_batch_byte_size,
self.max_batch_proof_qty,
self.constant_gas_cost(),
)
.inspect_err(|e| {
*batch_posting = false;
Expand Down Expand Up @@ -1456,13 +1463,13 @@ impl Batcher {
let batch_data_pointer: String = "".to_owned() + &self.download_endpoint + "/" + &file_name;

let num_proofs_in_batch = leaves.len();
let gas_per_proof = (CONSTANT_GAS_COST
let gas_per_proof = (self.constant_gas_cost()
+ ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF * num_proofs_in_batch as u128)
/ num_proofs_in_batch as u128;
let fee_per_proof = U256::from(gas_per_proof) * gas_price;
let fee_for_aggregator = (U256::from(AGGREGATOR_GAS_COST)
let fee_for_aggregator = (U256::from(self.aggregator_gas_cost)
* gas_price
* U256::from(DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER))
* U256::from(self.aggregator_fee_percentage_multiplier))
/ U256::from(PERCENTAGE_DIVIDER);
let respond_to_task_fee_limit = (fee_for_aggregator
* U256::from(RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER))
Expand Down Expand Up @@ -1760,7 +1767,7 @@ impl Batcher {
let nonced_verification_data = NoncedVerificationData::new(
client_msg.verification_data.verification_data.clone(),
client_msg.verification_data.nonce,
DEFAULT_MAX_FEE_PER_PROOF.into(), // 13_000 gas per proof * 100 gwei gas price (upper bound)
DEFAULT_MAX_FEE_PER_PROOF.into(), // 2_000 gas per proof * 100 gwei gas price (upper bound)
self.chain_id,
self.payment_service.address(),
);
Expand Down Expand Up @@ -1887,4 +1894,9 @@ impl Batcher {
})?;
Ok(())
}

fn constant_gas_cost(&self) -> u128 {
(self.aggregator_fee_percentage_multiplier * self.aggregator_gas_cost) / PERCENTAGE_DIVIDER
+ BATCHER_SUBMISSION_BASE_GAS_COST
}
}
63 changes: 53 additions & 10 deletions batcher/aligned-batcher/src/types/batch_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ pub(crate) fn try_build_batch(
gas_price: U256,
max_batch_byte_size: usize,
max_batch_proof_qty: usize,
constant_gas_cost: u128,
) -> Result<Vec<BatchQueueEntry>, BatcherError> {
let mut finalized_batch = batch_queue;
let mut batch_size = calculate_batch_size(&finalized_batch)?;

while let Some((entry, _)) = finalized_batch.peek() {
let batch_len = finalized_batch.len();
let fee_per_proof = calculate_fee_per_proof(batch_len, gas_price);
let fee_per_proof = calculate_fee_per_proof(batch_len, gas_price, constant_gas_cost);

// if batch is not acceptable:
if batch_size > max_batch_byte_size
Expand Down Expand Up @@ -197,8 +198,8 @@ pub(crate) fn try_build_batch(
Ok(finalized_batch.clone().into_sorted_vec())
}

fn calculate_fee_per_proof(batch_len: usize, gas_price: U256) -> U256 {
let gas_per_proof = (crate::CONSTANT_GAS_COST
fn calculate_fee_per_proof(batch_len: usize, gas_price: U256, constant_gas_cost: u128) -> U256 {
let gas_per_proof = (constant_gas_cost
+ crate::ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF * batch_len as u128)
/ batch_len as u128;

Expand All @@ -207,6 +208,7 @@ fn calculate_fee_per_proof(batch_len: usize, gas_price: U256) -> U256 {

#[cfg(test)]
mod test {
use aligned_sdk::core::constants::DEFAULT_CONSTANT_GAS_COST;
use aligned_sdk::core::types::ProvingSystemId;
use aligned_sdk::core::types::VerificationData;
use ethers::types::Address;
Expand Down Expand Up @@ -303,7 +305,14 @@ mod test {
batch_queue.push(entry_3, batch_priority_3);

let gas_price = U256::from(1);
let finalized_batch = try_build_batch(batch_queue, gas_price, 5000000, 50).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
50,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

assert_eq!(
finalized_batch[0].nonced_verification_data.max_fee,
Expand Down Expand Up @@ -408,7 +417,14 @@ mod test {
batch_queue.push(entry_3, batch_priority_3);

let gas_price = U256::from(1);
let finalized_batch = try_build_batch(batch_queue.clone(), gas_price, 5000000, 50).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
50,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

// All entries from the batch queue should be in
// the finalized batch.
Expand Down Expand Up @@ -511,7 +527,14 @@ mod test {
batch_queue.push(entry_3.clone(), batch_priority_3.clone());

let gas_price = U256::from(1);
let finalized_batch = try_build_batch(batch_queue.clone(), gas_price, 5000000, 2).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
2,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

// One Entry from the batch_queue should not be in the finalized batch
// Particularly, nonce_3 is not in the finalized batch
Expand Down Expand Up @@ -614,7 +637,14 @@ mod test {
batch_queue.push(entry_3, batch_priority_3);

let gas_price = U256::from(1);
let finalized_batch = try_build_batch(batch_queue.clone(), gas_price, 5000000, 50).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
50,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

// All entries from the batch queue should be in
// the finalized batch.
Expand Down Expand Up @@ -723,7 +753,14 @@ mod test {
batch_queue.push(entry_3, batch_priority_3);

let gas_price = U256::from(1);
let finalized_batch = try_build_batch(batch_queue.clone(), gas_price, 5000000, 50).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
50,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

// All but one entries from the batch queue should be in the finalized batch.
assert_eq!(batch_queue.len(), 3);
Expand Down Expand Up @@ -832,8 +869,14 @@ mod test {
// The max batch len is 2, so the algorithm should stop at the second entry.
let max_batch_proof_qty = 2;

let finalized_batch =
try_build_batch(batch_queue.clone(), gas_price, 5000000, max_batch_proof_qty).unwrap();
let finalized_batch = try_build_batch(
batch_queue.clone(),
gas_price,
5000000,
max_batch_proof_qty,
DEFAULT_CONSTANT_GAS_COST,
)
.unwrap();

assert_eq!(batch_queue.len(), 3);
assert_eq!(finalized_batch.len(), 2);
Expand Down
13 changes: 7 additions & 6 deletions batcher/aligned-sdk/src/core/constants.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/// Batcher ///
pub const GAS_PRICE_INCREMENT_PERCENTAGE_PER_ITERATION: usize = 5;
pub const AGGREGATOR_GAS_COST: u128 = 400_000;
pub const DEFAULT_AGGREGATOR_GAS_COST: u128 = 330_000;
pub const BATCHER_SUBMISSION_BASE_GAS_COST: u128 = 125_000;
pub const ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF: u128 = 13_000;
pub const CONSTANT_GAS_COST: u128 =
((AGGREGATOR_GAS_COST * DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER) / PERCENTAGE_DIVIDER)
+ BATCHER_SUBMISSION_BASE_GAS_COST;
pub const ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF: u128 = 2_000;
pub const DEFAULT_CONSTANT_GAS_COST: u128 = ((DEFAULT_AGGREGATOR_GAS_COST
* DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER)
/ PERCENTAGE_DIVIDER)
+ BATCHER_SUBMISSION_BASE_GAS_COST;
pub const DEFAULT_MAX_FEE_PER_PROOF: u128 =
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF * 100_000_000_000; // gas_price = 100 Gwei = 0.0000001 ether (high gas price)
pub const CONNECTION_TIMEOUT: u64 = 30; // 30 secs

// % modifiers: (100% is x1, 10% is x0.1, 1000% is x10)
pub const RESPOND_TO_TASK_FEE_LIMIT_PERCENTAGE_MULTIPLIER: u128 = 250; // fee_for_aggregator -> respondToTaskFeeLimit modifier
pub const DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER: u128 = 150; // feeForAggregator modifier
pub const DEFAULT_AGGREGATOR_FEE_PERCENTAGE_MULTIPLIER: u128 = 125; // feeForAggregator modifier
pub const GAS_PRICE_PERCENTAGE_MULTIPLIER: u128 = 110; // gasPrice modifier
pub const OVERRIDE_GAS_PRICE_PERCENTAGE_MULTIPLIER: u128 = 120; // gasPrice modifier to override previous transactions
pub const PERCENTAGE_DIVIDER: u128 = 100;
Expand Down
4 changes: 2 additions & 2 deletions batcher/aligned-sdk/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
core::{
constants::{
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, CONSTANT_GAS_COST,
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, DEFAULT_CONSTANT_GAS_COST,
MAX_FEE_BATCH_PROOF_NUMBER, MAX_FEE_DEFAULT_PROOF_NUMBER,
},
errors::{self, GetNonceError},
Expand Down Expand Up @@ -191,7 +191,7 @@ pub async fn fee_per_proof(
let gas_price = fetch_gas_price(&eth_rpc_provider).await?;

// Cost for estimate `num_proofs_per_batch` proofs
let estimated_gas_per_proof = (CONSTANT_GAS_COST
let estimated_gas_per_proof = (DEFAULT_CONSTANT_GAS_COST
+ ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF * num_proofs_per_batch as u128)
/ num_proofs_per_batch as u128;

Expand Down
2 changes: 2 additions & 0 deletions config-files/config-batcher-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ecdsa:

## Batcher configurations
batcher:
aggregator_fee_percentage_multiplier: 125
aggregator_gas_cost: 330000
block_interval: 3
batch_size_interval: 10
transaction_wait_timeout: 96000 # 8 blocks
Expand Down
2 changes: 2 additions & 0 deletions config-files/config-batcher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ecdsa:

## Batcher configurations
batcher:
aggregator_fee_percentage_multiplier: 125
aggregator_gas_cost: 330000
block_interval: 3
batch_size_interval: 10
transaction_wait_timeout: 96000 # 8 blocks
Expand Down
3 changes: 3 additions & 0 deletions explorer/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ MAX_BATCH_SIZE=268435456 # 256 MiB

# Time we wait for a batch to be verified before marking it stale
BATCH_TTL_MINUTES=5

# Latest aligned release that operators should be running
LATEST_RELEASE=v0.13.0
3 changes: 3 additions & 0 deletions explorer/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ TRACKER_API_URL=<tracker_api_url>

# Time we wait for a batch to be verified before marking it stale
BATCH_TTL_MINUTES=5

# Latest aligned release that operators should be running
LATEST_RELEASE=v0.13.0
5 changes: 1 addition & 4 deletions explorer/assets/vendor/dark_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const isDark = () => {
.split("; ")
.find((row) => row.startsWith(`${themeCookieKey}=`))
?.split("=")[1];
return (
theme == "dark" ||
window.matchMedia("(prefers-color-scheme: dark)").matches
);
return theme == "dark";
};

const setThemeCookie = (theme) => {
Expand Down
Loading

0 comments on commit 88bb935

Please sign in to comment.