Skip to content

Commit

Permalink
Revert "chore: merge main to dev (#203)"
Browse files Browse the repository at this point in the history
This reverts commit f95f0f2.
  • Loading branch information
MegaRedHand authored Jan 9, 2025
1 parent f95f0f2 commit 3e4cbaf
Show file tree
Hide file tree
Showing 6 changed files with 823 additions and 24 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ This command will generate the bindings files in the folder: `<path-eigensdk-rs>
We are actively looking for contributors. Thank you for your interest. We have strict ci checks in place. In case of any questions and support, feel free to raise an issue.

## Branches

- `main` - Points to the latest **mainnet** release of contracts.
- `testnet` - Points to the latest **testnet** release of contracts.
- `main` - Points to the latest **mainnet** release of contracts.
- `testnet` - Points to the latest **testnet** release of contracts.
- `dev` - Points to the latest **dev** branch of the contracts.

### PR

To test locally :-

You need `foundry` to successfully run it.
You need `foundry` to successfully to run it.

```bash
cargo test
Expand Down
56 changes: 56 additions & 0 deletions crates/chainio/txmanager/src/alloy_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use alloy::{
consensus::TxEnvelope,
eips::BlockNumberOrTag,
primitives::{BlockNumber, TxHash},
providers::{Provider, RootProvider},
rpc::types::{Block, TransactionReceipt, TransactionRequest},
transports::{http::Http, RpcError, TransportErrorKind, TransportResult},
};
use reqwest::Client;

use crate::eth_backend_trait::EthBackend;

pub struct AlloyBackend {
pub provider: RootProvider<Http<Client>>,
}

#[async_trait::async_trait]
impl EthBackend for AlloyBackend {
async fn send_tx_envelope(&self, tx: TxEnvelope) -> TransportResult<TxHash> {
Ok(*self.provider.send_tx_envelope(tx).await?.tx_hash())
}

async fn get_block_number(&self) -> Result<BlockNumber, RpcError<TransportErrorKind>> {
Ok(self.provider.get_block_number().await?)
}

async fn get_max_priority_fee_per_gas(&self) -> Result<u128, RpcError<TransportErrorKind>> {
Ok(self.provider.get_max_priority_fee_per_gas().await?)
}

async fn estimate_gas<'a>(
&self,
tx: &'a TransactionRequest,
) -> Result<u64, RpcError<TransportErrorKind>> {
Ok(self.provider.estimate_gas(tx).await?)
}

async fn get_block_by_number(
&self,
number: BlockNumberOrTag,
hydrate: bool,
) -> Result<Option<Block>, RpcError<TransportErrorKind>> {
Ok(self
.provider
.get_block_by_number(number, hydrate.into())
.await?)
}

async fn get_transaction_receipt(&self, hash: TxHash) -> Option<TransactionReceipt> {
self.provider
.get_transaction_receipt(hash)
.await
.ok()
.flatten()
}
}
38 changes: 38 additions & 0 deletions crates/chainio/txmanager/src/eth_backend_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use alloy::{
consensus::TxEnvelope,
eips::BlockNumberOrTag,
primitives::{BlockNumber, TxHash},
rpc::types::{Block, TransactionReceipt, TransactionRequest},
transports::{RpcError, TransportErrorKind},
};

#[async_trait::async_trait]
pub trait EthBackend {
/// Get the latest block number.
///
/// # Returns
///
/// The latest block number.
async fn get_block_number(&self) -> Result<BlockNumber, RpcError<TransportErrorKind>>;

// when not implemented, will txmanager will fall on gasTipCap
async fn get_max_priority_fee_per_gas(&self) -> Result<u128, RpcError<TransportErrorKind>>;

async fn estimate_gas<'a>(
&self,
tx: &'a TransactionRequest,
) -> Result<u64, RpcError<TransportErrorKind>>;

async fn get_block_by_number(
&self,
number: BlockNumberOrTag,
hydrate: bool,
) -> Result<Option<Block>, RpcError<TransportErrorKind>>;

async fn send_tx_envelope(
&self,
tx: TxEnvelope,
) -> Result<TxHash, RpcError<TransportErrorKind>>;

async fn get_transaction_receipt(&self, hash: TxHash) -> Option<TransactionReceipt>;
}
132 changes: 132 additions & 0 deletions crates/chainio/txmanager/src/fake_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use alloy::{
consensus::{ReceiptEnvelope, Transaction, TxEnvelope},
eips::BlockNumberOrTag,
primitives::{map::HashMap, Address, BlockNumber, TxHash},
rpc::types::{Block, Header, TransactionReceipt, TransactionRequest},
transports::{RpcError, TransportErrorKind, TransportResult},
};
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::eth_backend_trait::EthBackend;

pub struct FakeEthBackend {
pub base_fee_per_gas: u64,
pub mining_params: Arc<Mutex<MiningParams>>,
}

pub struct MiningParams {
pub block_number: u64,
pub congested_blocks: u64,
// we use a single nonce for now because the txmgr only supports a single sender
pub nonce: u64,
pub mempool: HashMap<u64, TxEnvelope>,
pub mined_txs: HashMap<TxHash, u64>,
pub max_priority_fee: u128,
}

impl FakeEthBackend {
#[allow(dead_code)] // only used for testing
pub async fn start_mining(&self) {
let params = self.mining_params.clone();
tokio::spawn(async move {
loop {
let mut params = params.lock().await;
if let Some(tx) = params.mempool.get(&params.nonce).cloned() {
if tx.max_priority_fee_per_gas().unwrap() >= params.max_priority_fee {
let block_number = params.block_number;
params.mined_txs.insert(*tx.tx_hash(), block_number);
params.nonce += 1;
params.block_number += 1;
}
}
params.congested_blocks = params.congested_blocks.saturating_sub(1);
drop(params);
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
});
}
}

#[async_trait::async_trait]
impl EthBackend for FakeEthBackend {
async fn get_block_number(&self) -> Result<BlockNumber, RpcError<TransportErrorKind>> {
Ok(self.mining_params.lock().await.block_number)
}

async fn get_max_priority_fee_per_gas(&self) -> Result<u128, RpcError<TransportErrorKind>> {
let prev_max_priority_fee = {
let mut params = self.mining_params.lock().await;
if params.congested_blocks > 0 {
params.max_priority_fee += 1;
}
params.max_priority_fee
};
Ok(prev_max_priority_fee)
}

async fn estimate_gas<'a>(
&self,
_tx: &'a TransactionRequest,
) -> Result<u64, RpcError<TransportErrorKind>> {
Ok(0)
}

async fn get_block_by_number(
&self,
_number: BlockNumberOrTag,
_hydrate: bool,
) -> Result<Option<Block>, RpcError<TransportErrorKind>> {
let header = Header {
inner: alloy::consensus::Header {
base_fee_per_gas: Some(self.base_fee_per_gas),
..Default::default()
},
..Default::default()
};
Ok(Some(Block {
header,
..Default::default()
}))
}

async fn send_tx_envelope(&self, tx: TxEnvelope) -> TransportResult<TxHash> {
if tx.max_fee_per_gas() < self.base_fee_per_gas as u128 {
return Err(TransportErrorKind::custom_str(
"tx.max_fee_per_gas < base_fee_per_gas",
));
}
let tx_hash = *tx.tx_hash();
let nonce = tx.nonce();
{
let mut params = self.mining_params.lock().await;
if nonce < params.nonce {
return Err(TransportErrorKind::custom_str("tx.nonce < current nonce"));
}
params.mempool.insert(nonce, tx);
}
Ok(tx_hash)
}

async fn get_transaction_receipt(&self, hash: TxHash) -> Option<TransactionReceipt> {
let params = self.mining_params.lock().await;
params
.mined_txs
.get(&hash)
.map(|block_number| TransactionReceipt {
block_number: Some(*block_number),
transaction_hash: hash,
gas_used: 0,
inner: ReceiptEnvelope::Legacy(Default::default()),
transaction_index: None,
block_hash: None,
effective_gas_price: 0,
blob_gas_used: None,
blob_gas_price: None,
from: Address::default(),
to: None,
contract_address: None,
authorization_list: None,
})
}
}
Loading

0 comments on commit 3e4cbaf

Please sign in to comment.