Skip to content

Commit

Permalink
Merge 9438f62 into e06df5c
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosNicolau authored Oct 18, 2024
2 parents e06df5c + 9438f62 commit 7a20d9c
Show file tree
Hide file tree
Showing 38 changed files with 1,429 additions and 1,604 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jobs:
run: go build operator/cmd/main.go
- name: Build aggregator
run: go build aggregator/cmd/main.go

2 changes: 1 addition & 1 deletion .github/workflows/build-and-test-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
cargo build --all
test:
runs-on: ubuntu-latest
runs-on: aligned-runner
needs: build
steps:
- name: Checkout code
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ anvil_upgrade_add_aggregator:
@echo "Adding Aggregator to Aligned Contracts..."
. contracts/scripts/anvil/upgrade_add_aggregator_to_service_manager.sh

anvil_upgrade_initialize_disable_verifiers:
@echo "Initializing disabled verifiers..."
. contracts/scripts/anvil/upgrade_disabled_verifiers_in_service_manager.sh

lint_contracts:
@cd contracts && npm run lint:sol

Expand Down Expand Up @@ -230,6 +234,23 @@ operator_register_with_aligned_layer:
operator_deposit_and_register: operator_deposit_into_strategy operator_register_with_aligned_layer


# The verifier ID to enable or disable corresponds to the index of the verifier in the `ProvingSystemID` enum.
verifier_enable_devnet:
@echo "Enabling verifier with id: $(VERIFIER_ID)"
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 RPC_URL=http://localhost:8545 OUTPUT_PATH=./script/output/devnet/alignedlayer_deployment_output.json ./contracts/scripts/enable_verifier.sh $(VERIFIER_ID)

verifier_disable_devnet:
@echo "Disabling verifier with id: $(VERIFIER_ID)"
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 RPC_URL=http://localhost:8545 OUTPUT_PATH=./script/output/devnet/alignedlayer_deployment_output.json ./contracts/scripts/disable_verifier.sh $(VERIFIER_ID)

verifier_enable:
@echo "Enabling verifier with ID: $(VERIFIER_ID)"
@. contracts/scripts/.env && . contracts/scripts/enable_verifier.sh $(VERIFIER_ID)

verifier_disable:
@echo "Disabling verifier with ID: $(VERIFIER_ID)"
@. contracts/scripts/.env && . contracts/scripts/disable_verifier.sh $(VERIFIER_ID)

__BATCHER__:

BURST_SIZE=5
Expand Down Expand Up @@ -455,6 +476,10 @@ upgrade_add_aggregator: ## Add Aggregator to Aligned Contracts
@echo "Adding Aggregator to Aligned Contracts..."
@. contracts/scripts/.env && . contracts/scripts/upgrade_add_aggregator_to_service_manager.sh

upgrade_initialize_disabled_verifiers:
@echo "Adding disabled verifiers to Aligned Service Manager..."
@. contracts/scripts/.env && . contracts/scripts/upgrade_disabled_verifiers_in_service_manager.sh

deploy_verify_batch_inclusion_caller:
@echo "Deploying VerifyBatchInclusionCaller contract..."
@. examples/verify/.env && . examples/verify/scripts/deploy_verify_batch_inclusion_caller.sh
Expand Down
2 changes: 2 additions & 0 deletions batcher/aligned-batcher/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ impl ConfigFromYaml {
pub struct Addresses {
#[serde(rename = "batcherPaymentService")]
pub batcher_payment_service: String,
#[serde(rename = "alignedLayerServiceManager")]
pub service_manager: String,
}

#[derive(Debug, Deserialize)]
Expand Down
123 changes: 4 additions & 119 deletions batcher/aligned-batcher/src/eth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,120 +1,5 @@
use std::str::FromStr;
use std::sync::Arc;
pub(crate) mod payment_service;
pub(crate) mod service_manager;
pub mod utils;

use aligned_sdk::eth::batcher_payment_service::BatcherPaymentServiceContract;
use ethers::prelude::k256::ecdsa::SigningKey;
use ethers::prelude::*;
use gas_escalator::{Frequency, GeometricGasPrice};
use log::info;

use crate::{config::ECDSAConfig, types::errors::BatcherSendError};

#[derive(Debug, Clone, EthEvent)]
pub struct BatchVerified {
pub batch_merkle_root: [u8; 32],
}

pub type SignerMiddlewareT =
SignerMiddleware<GasEscalatorMiddleware<Provider<RetryClient<Http>>>, Wallet<SigningKey>>;

pub type BatcherPaymentService = BatcherPaymentServiceContract<SignerMiddlewareT>;

const MAX_RETRIES: u32 = 15; // Max retries for the retry client. Will only retry on network errors
const INITIAL_BACKOFF: u64 = 1000; // Initial backoff for the retry client in milliseconds, will increase every retry
const GAS_MULTIPLIER: f64 = 1.125; // Multiplier for the gas price for gas escalator
const GAS_ESCALATOR_INTERVAL: u64 = 12; // Time in seconds between gas escalations

#[derive(Debug, Clone)]
pub struct CreateNewTaskFeeParams {
pub fee_for_aggregator: U256,
pub fee_per_proof: U256,
pub gas_price: U256,
pub respond_to_task_fee_limit: U256,
}

impl CreateNewTaskFeeParams {
pub fn new(
fee_for_aggregator: U256,
fee_per_proof: U256,
gas_price: U256,
respond_to_task_fee_limit: U256,
) -> Self {
CreateNewTaskFeeParams {
fee_for_aggregator,
fee_per_proof,
gas_price,
respond_to_task_fee_limit,
}
}
}

pub fn get_provider(eth_rpc_url: String) -> Result<Provider<RetryClient<Http>>, anyhow::Error> {
let provider = Http::from_str(eth_rpc_url.as_str())
.map_err(|e| anyhow::Error::msg(format!("Failed to create provider: {}", e)))?;

let client = RetryClient::new(
provider,
Box::<ethers::providers::HttpRateLimitRetryPolicy>::default(),
MAX_RETRIES,
INITIAL_BACKOFF,
);

Ok(Provider::<RetryClient<Http>>::new(client))
}

pub async fn get_batcher_payment_service(
provider: Provider<RetryClient<Http>>,
ecdsa_config: ECDSAConfig,
contract_address: String,
) -> Result<BatcherPaymentService, anyhow::Error> {
let chain_id = provider.get_chainid().await?;

let escalator = GeometricGasPrice::new(GAS_MULTIPLIER, GAS_ESCALATOR_INTERVAL, None::<u64>);

let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::PerBlock);

// get private key from keystore
let wallet = Wallet::decrypt_keystore(
&ecdsa_config.private_key_store_path,
&ecdsa_config.private_key_store_password,
)?
.with_chain_id(chain_id.as_u64());

let signer = Arc::new(SignerMiddleware::new(provider, wallet));

let service_manager =
BatcherPaymentService::new(H160::from_str(contract_address.as_str())?, signer);

Ok(service_manager)
}

pub async fn try_create_new_task(
batch_merkle_root: [u8; 32],
batch_data_pointer: String,
proofs_submitters: Vec<Address>,
fee_params: CreateNewTaskFeeParams,
payment_service: &BatcherPaymentService,
) -> Result<TransactionReceipt, BatcherSendError> {
let call = payment_service
.create_new_task(
batch_merkle_root,
batch_data_pointer,
proofs_submitters,
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);

info!("Creating task for: {}", hex::encode(batch_merkle_root));

let pending_tx = call.send().await.map_err(|err| match err {
ContractError::Revert(err) => BatcherSendError::TransactionReverted(err.to_string()),
_ => BatcherSendError::UnknownError(err.to_string()),
})?;

pending_tx
.await
.map_err(|err| BatcherSendError::UnknownError(err.to_string()))?
.ok_or(BatcherSendError::ReceiptNotFound)
}
pub use utils::get_provider;
103 changes: 103 additions & 0 deletions batcher/aligned-batcher/src/eth/payment_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use std::str::FromStr;
use std::sync::Arc;

use aligned_sdk::eth::batcher_payment_service::BatcherPaymentServiceContract;
use ethers::prelude::k256::ecdsa::SigningKey;
use ethers::prelude::*;
use gas_escalator::{Frequency, GeometricGasPrice};
use log::info;

use crate::{config::ECDSAConfig, types::errors::BatcherSendError};

use super::utils::{GAS_ESCALATOR_INTERVAL, GAS_MULTIPLIER};

#[derive(Debug, Clone, EthEvent)]
pub struct BatchVerified {
pub batch_merkle_root: [u8; 32],
}

pub type SignerMiddlewareT =
SignerMiddleware<GasEscalatorMiddleware<Provider<RetryClient<Http>>>, Wallet<SigningKey>>;

pub type BatcherPaymentService = BatcherPaymentServiceContract<SignerMiddlewareT>;

#[derive(Debug, Clone)]
pub struct CreateNewTaskFeeParams {
pub fee_for_aggregator: U256,
pub fee_per_proof: U256,
pub gas_price: U256,
pub respond_to_task_fee_limit: U256,
}

impl CreateNewTaskFeeParams {
pub fn new(
fee_for_aggregator: U256,
fee_per_proof: U256,
gas_price: U256,
respond_to_task_fee_limit: U256,
) -> Self {
CreateNewTaskFeeParams {
fee_for_aggregator,
fee_per_proof,
gas_price,
respond_to_task_fee_limit,
}
}
}

pub async fn get_batcher_payment_service(
provider: Provider<RetryClient<Http>>,
ecdsa_config: ECDSAConfig,
contract_address: String,
) -> Result<BatcherPaymentService, anyhow::Error> {
let chain_id = provider.get_chainid().await?;

let escalator = GeometricGasPrice::new(GAS_MULTIPLIER, GAS_ESCALATOR_INTERVAL, None::<u64>);

let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::PerBlock);

// get private key from keystore
let wallet = Wallet::decrypt_keystore(
&ecdsa_config.private_key_store_path,
&ecdsa_config.private_key_store_password,
)?
.with_chain_id(chain_id.as_u64());

let signer = Arc::new(SignerMiddleware::new(provider, wallet));

let payment_service =
BatcherPaymentService::new(H160::from_str(contract_address.as_str())?, signer);

Ok(payment_service)
}

pub async fn try_create_new_task(
batch_merkle_root: [u8; 32],
batch_data_pointer: String,
proofs_submitters: Vec<Address>,
fee_params: CreateNewTaskFeeParams,
payment_service: &BatcherPaymentService,
) -> Result<TransactionReceipt, BatcherSendError> {
let call = payment_service
.create_new_task(
batch_merkle_root,
batch_data_pointer,
proofs_submitters,
fee_params.fee_for_aggregator,
fee_params.fee_per_proof,
fee_params.respond_to_task_fee_limit,
)
.gas_price(fee_params.gas_price);

info!("Creating task for: {}", hex::encode(batch_merkle_root));

let pending_tx = call.send().await.map_err(|err| match err {
ContractError::Revert(err) => BatcherSendError::TransactionReverted(err.to_string()),
_ => BatcherSendError::UnknownError(err.to_string()),
})?;

pending_tx
.await
.map_err(|err| BatcherSendError::UnknownError(err.to_string()))?
.ok_or(BatcherSendError::ReceiptNotFound)
}
47 changes: 47 additions & 0 deletions batcher/aligned-batcher/src/eth/service_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{str::FromStr, sync::Arc};

use aligned_sdk::eth::aligned_service_manager::AlignedLayerServiceManagerContract;
use ethers::{
core::k256::ecdsa::SigningKey,
middleware::{
gas_escalator::{Frequency, GeometricGasPrice},
GasEscalatorMiddleware, SignerMiddleware,
},
providers::{Http, Middleware, Provider, RetryClient},
signers::{Signer, Wallet},
types::H160,
};

use crate::config::ECDSAConfig;

use super::utils::{GAS_ESCALATOR_INTERVAL, GAS_MULTIPLIER};

pub type SignerMiddlewareT =
SignerMiddleware<GasEscalatorMiddleware<Provider<RetryClient<Http>>>, Wallet<SigningKey>>;

pub type ServiceManager = AlignedLayerServiceManagerContract<SignerMiddlewareT>;

pub async fn get_service_manager(
provider: Provider<RetryClient<Http>>,
ecdsa_config: ECDSAConfig,
contract_address: String,
) -> Result<ServiceManager, anyhow::Error> {
let chain_id = provider.get_chainid().await?;

let escalator = GeometricGasPrice::new(GAS_MULTIPLIER, GAS_ESCALATOR_INTERVAL, None::<u64>);

let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::PerBlock);

// get private key from keystore
let wallet = Wallet::decrypt_keystore(
&ecdsa_config.private_key_store_path,
&ecdsa_config.private_key_store_password,
)?
.with_chain_id(chain_id.as_u64());

let signer = Arc::new(SignerMiddleware::new(provider, wallet));

let service_manager = ServiceManager::new(H160::from_str(contract_address.as_str())?, signer);

Ok(service_manager)
}
22 changes: 22 additions & 0 deletions batcher/aligned-batcher/src/eth/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::str::FromStr;

use ethers::providers::{Http, Provider, RetryClient};

const MAX_RETRIES: u32 = 15; // Max retries for the retry client. Will only retry on network errors
const INITIAL_BACKOFF: u64 = 1000; // Initial backoff for the retry client in milliseconds, will increase every retry
pub(crate) const GAS_MULTIPLIER: f64 = 1.125; // Multiplier for the gas price for gas escalator
pub(crate) const GAS_ESCALATOR_INTERVAL: u64 = 12; // Time in seconds between gas escalations

pub fn get_provider(eth_rpc_url: String) -> Result<Provider<RetryClient<Http>>, anyhow::Error> {
let provider = Http::from_str(eth_rpc_url.as_str())
.map_err(|e| anyhow::Error::msg(format!("Failed to create provider: {}", e)))?;

let client = RetryClient::new(
provider,
Box::<ethers::providers::HttpRateLimitRetryPolicy>::default(),
MAX_RETRIES,
INITIAL_BACKOFF,
);

Ok(Provider::<RetryClient<Http>>::new(client))
}
Loading

0 comments on commit 7a20d9c

Please sign in to comment.