Skip to content

Commit

Permalink
feat: impl settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
eigmax authored and captainlee1024 committed Apr 25, 2024
1 parent 47b8e9d commit d8e0d30
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ tokio-stream = { version = "0.1" }
# Misc
eyre = "0.6.8"
thiserror = "1.0.40"
anyhow = "1.0"

c-kzg = "0.4.2"

Expand Down
21 changes: 17 additions & 4 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@
//! They will be launched in Run CMD.
use crate::prover::ProverChannel;
use ethers_providers::{Http, Provider};

use crate::settlement::{NetworkSpec, Settlement};
use ethers_core::types::{Bytes, H160, U256};
use tokio::sync::mpsc::{self, Receiver};
use tokio::time::{interval, Duration};
use std::sync::Arc;

use crate::db::{lfs, Database};
use crate::settlement::ethereum::EthereumSettlement;

pub(crate) struct Operator {
db: Box<dyn Database>,
prover: ProverChannel,
_settler: Provider<Http>,
provider: Arc<Provider<Http>>,
rx_proof: Receiver<Vec<u8>>,
// TODO: use trait object
settler: EthereumSettlement,
}


impl Operator {
pub fn new(_db_path: &str, l1addr: &str, prover_addr: &str) -> Self {
let (sx, rx) = mpsc::channel(10);
let prover = ProverChannel::new(prover_addr, sx);
let db = lfs::open_db(lfs::DBConfig::Memory).unwrap();
// TODO: abstract this in the settlement
let _settler = Provider::<Http>::try_from(l1addr).unwrap();
let provider = Provider::<Http>::try_from(l1addr).unwrap();
let provider = Arc::new(provider);

//let settler = init_settlement(NetworkSpec::Ethereum);
let settler = EthereumSettlement{};
Operator {
prover,
db,
_settler,
provider,
settler,
rx_proof: rx,
}
}
Expand Down Expand Up @@ -72,6 +82,9 @@ impl Operator {
self.prover.execute(block_no).await.unwrap();
let block_no_next = block_no + 1;
self.db.put(batch_key.clone(), block_no_next.to_be_bytes().to_vec());

// TODO
let _ = self.settler.bridge_asset(H160::zero(), self.provider.clone(), 0, H160::zero(), U256::zero(), H160::zero(), true, Bytes::default()).await;
} else {
log::debug!("Wait for the new task coming in");
}
Expand Down
7 changes: 4 additions & 3 deletions src/settlement/ethereum/interfaces/eigen_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ethers_contract::abigen;
use ethers_core::types::{Address, Bytes, U256};
use ethers_providers::{Http, Provider};
use std::sync::Arc;
use anyhow::Result;

// example: https://github.com/gakonst/ethers-rs/blob/master/examples/contracts/examples/abigen.rs#L55
abigen!(
Expand All @@ -14,8 +15,7 @@ abigen!(
]"#,
);

#[allow(dead_code)]
pub async fn bridge_asset(
pub(crate) async fn bridge_asset(
address: Address,
client: Arc<Provider<Http>>,
destination_network: u32,
Expand All @@ -24,7 +24,7 @@ pub async fn bridge_asset(
token: Address,
force_update_global_exit_root: bool,
calldata: Bytes,
) {
) -> Result<()> {
let contract = EigenBridge::new(address, client);

if let Ok(result) = contract
Expand All @@ -41,4 +41,5 @@ pub async fn bridge_asset(
{
log::debug!("bridge asset {result:?}");
}
Ok(())
}
32 changes: 32 additions & 0 deletions src/settlement/ethereum/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
use crate::settlement::Settlement;
pub(crate) mod interfaces;
use anyhow::Result;
use ethers_core::types::{Address, Bytes, U256};
use ethers_providers::{Http, Provider};
use std::sync::Arc;

pub struct EthereumSettlement {}

impl Settlement for EthereumSettlement {
async fn bridge_asset(
&self,
address: Address,
client: Arc<Provider<Http>>,
destination_network: u32,
destination_address: Address,
amount: U256,
token: Address,
force_update_global_exit_root: bool,
calldata: Bytes,
) -> Result<()> {
interfaces::eigen_bridge::bridge_asset(
address,
client,
destination_network,
destination_address,
amount,
token,
force_update_global_exit_root,
calldata,
).await
}
}
35 changes: 35 additions & 0 deletions src/settlement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,39 @@
//! 1. get_state: get the latest state of the settlement layer, including the state root and block number.
//! 2. update_state: update the state of the settlement layer with the given proof and public input.
use ethers_core::types::{Address, Bytes, U256};
use ethers_providers::{Http, Provider};
use std::sync::Arc;
use anyhow::Result;

pub(crate) mod ethereum;

pub trait Settlement {
async fn bridge_asset(
&self,
address: Address,
client: Arc<Provider<Http>>,
destination_network: u32,
destination_address: Address,
amount: U256,
token: Address,
force_update_global_exit_root: bool,
calldata: Bytes,
) -> Result<()>;

// TODO: add more interfaces
}

pub enum NetworkSpec {
Ethereum,
Optimism,
}

//pub fn init_settlement(spec: NetworkSpec) -> Box<dyn Settlement> {
// match spec {
// NetworkSpec::Ethereum => {
// Box::new(ethereum::EthereumSettlement{})
// },
// _ => todo!("Not supported network")
// }
//}

0 comments on commit d8e0d30

Please sign in to comment.