Skip to content

Commit

Permalink
Merge branch 'testnet' into fix/explorer-operators-restaked-mobile-view
Browse files Browse the repository at this point in the history
  • Loading branch information
JuArce authored Jan 13, 2025
2 parents 7f6518c + cd0c331 commit 581c283
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 32 deletions.
2 changes: 2 additions & 0 deletions alerts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ EXPRESSION=<GREP_EXPRESSION>
RPC_URL=<YOUR_RPC_URL>
PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
WALLET_NAME=<YOUR_WALLET_NAME> # Example: "Task sender"
WALLET_ADDRESS=<YOUR_WALLET_ADDRESS>
NETWORK=<MAINNET|HOLESKY|STAGE>

# Variables for sender_with_alert.sh
REPETITIONS=<REPETITIONS>
Expand Down
4 changes: 2 additions & 2 deletions alerts/balance_alerts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ balance_alert=false

while :
do
balance_wei=$(cast call --rpc-url $RPC_URL $PAYMENT_CONTRACT_ADDRESS "UserBalances(address)(uint256)" $WALLET_ADDRESS | cut -d' ' -f1)
balance_wei=$(cast call --rpc-url $RPC_URL $PAYMENT_CONTRACT_ADDRESS "user_balances(address)(uint256)" $WALLET_ADDRESS | cut -d' ' -f1)

balance_eth=$(cast from-wei $balance_wei)

if [ 1 -eq "$(echo "$balance_eth < $BALANCE_THRESHOLD" | bc)" ]; then
message="⚠️ WARNING: Wallet $WALLET_ADDRESS balance ($balance_eth ETH) is below $BALANCE_THRESHOLD ETH"
message="⚠️ WARNING: $WALLET_NAME ($NETWORK) Wallet ($WALLET_ADDRESS) balance ($balance_eth ETH) is below $BALANCE_THRESHOLD ETH"
printf "$message\n"
if [ "$balance_alert" = false ]; then
send_slack_message "$message"
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 std::sync::Arc;
use std::time::Duration;

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 @@ -92,6 +92,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 @@ -253,6 +255,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 @@ -1159,6 +1165,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 @@ -1431,13 +1438,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 @@ -1735,7 +1742,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 @@ -1862,4 +1869,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
5 changes: 5 additions & 0 deletions docs/1_introduction/1_try_aligned.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

In this tutorial, you will learn how to send your first SP1 proofs to get verified in Aligned in under 3 minutes.

{% hint style="warning" %}
This tutorial is for sending proofs on Holesky network.
To send proofs on Mainnet, please refer to the [submitting proofs](../3_guides/0_submitting_proofs.md) guide.
{% endhint %}

## Quickstart

We will download a previously generated SP1 proof, send it to Aligned for verification, and retrieve the results from Ethereum Holesky testnet.
Expand Down
7 changes: 7 additions & 0 deletions explorer/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
# General application configuration
import Config

config :explorer,
batcher_submission_gas_cost: 125000,
aggregator_gas_cost: 330000,
additional_submission_gas_cost_per_proof: 2000,
aggregator_fee_percentage_multiplier: 125,
percentage_divider: 100

config :explorer,
generators: [timestamp_type: :utc_datetime],
tracker_api_url: System.get_env("TRACKER_API_URL")
Expand Down
24 changes: 23 additions & 1 deletion explorer/lib/explorer/models/batches.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ defmodule Batches do
query =
from(b in Batches,
where: b.is_verified == true,
select: avg(b.fee_per_proof)
select: sum(b.fee_per_proof * b.amount_of_proofs) / sum(b.amount_of_proofs)
)

case Explorer.Repo.one(query) do
Expand All @@ -164,6 +164,28 @@ defmodule Batches do
end
end

# The following query was built by reading:
# - https://hexdocs.pm/ecto/Ecto.Query.html#module-fragments
# - https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
# - https://stackoverflow.com/questions/43288914/how-to-use-date-trunc-with-timezone-in-ecto-fragment-statement
# - https://glennjon.es/2016/09/24/elixir-ecto-how-to-group-and-count-records-by-week.html
def get_daily_verified_batches_summary() do
submission_constant_cost = Utils.constant_batch_submission_gas_cost()
additional_cost_per_proof = Utils.additional_batch_submission_gas_cost_per_proof()

query = Batches
|> where([b], b.is_verified == true)
|> group_by([b], fragment("DATE_TRUNC('day', ?)", b.response_timestamp))
|> select([b], %{
date: fragment("DATE_TRUNC('day', ?)::date", b.response_timestamp),
amount_of_proofs: sum(b.amount_of_proofs),
gas_cost: sum(fragment("? + ? * ?", ^submission_constant_cost, ^additional_cost_per_proof, b.amount_of_proofs))
})
|> order_by([b], fragment("DATE_TRUNC('day', ?)", b.response_timestamp))

Explorer.Repo.all(query)
end

def insert_or_update(batch_changeset, proofs) do
merkle_root = batch_changeset.changes.merkle_root
stored_proofs = Proofs.get_proofs_from_batch(%{merkle_root: merkle_root})
Expand Down
12 changes: 12 additions & 0 deletions explorer/lib/explorer_web/controllers/data_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule ExplorerWeb.DataController do
use ExplorerWeb, :controller

def verified_batches_summary(conn, _params) do
batches_summary =
Batches.get_daily_verified_batches_summary()

render(conn, :show, %{
batches: batches_summary,
})
end
end
Loading

0 comments on commit 581c283

Please sign in to comment.