Skip to content

Commit

Permalink
feat: Aptos state update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tranduy1dol committed Sep 4, 2024
1 parent 558b4ee commit 4d0f0e1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ members = [
"crates/primitives/receipt",
"crates/primitives/state_update",
"crates/primitives/utils",
"crates/tests",
"crates/tests", "crates/client/aptos",
]
resolver = "2"
# All previous except for `starknet-rpc-test` and `starknet-e2e-test`
Expand Down Expand Up @@ -147,7 +147,7 @@ tokio = { version = "1.34", features = ["signal"] }
url = "2.4"
rayon = "1.10"
bincode = "1.3"
prometheus = "0.13.4"
prometheus = { version = "0.13.3", default-features = false }
fdlimit = "0.3.0"
proptest = "1.5.0"
proptest-derive = "0.5.0"
Expand All @@ -156,6 +156,8 @@ httpmock = "0.7.0"
tempfile = "3.10.1"
env_logger = "0.11.3"

aptos-sdk = { git = "https://github.com/aptos-labs/aptos-core", rev = "dd781812dee44192c3d9389c584e98945dabff6d" }

[patch.crates-io]
starknet-core = { git = "https://github.com/kasarlabs/starknet-rs.git", branch = "fork" }
starknet-providers = { git = "https://github.com/kasarlabs/starknet-rs.git", branch = "fork" }
Expand All @@ -164,3 +166,6 @@ starknet-providers = { git = "https://github.com/kasarlabs/starknet-rs.git", bra
starknet-types-core = { git = "https://github.com/kasarlabs/types-rs.git", branch = "feat-deserialize-v0.1.5" }

blockifier = { git = "https://github.com/starkware-libs/blockifier", tag = "v0.8.0-rc.0" }

merlin = { git = "https://github.com/aptos-labs/merlin" }
x25519-dalek = { git = "https://github.com/Tranduy1dol/x25519-dalek", branch = "zeroize_v1.7" }
2 changes: 2 additions & 0 deletions crates/client/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ url = { workspace = true }
starknet-types-core = { workspace = true }
serde = { workspace = true, features = ["derive"] }
dc-db = { workspace = true }
dp-transactions = { workspace = true }
log = { workspace = true }

95 changes: 95 additions & 0 deletions crates/client/aptos/src/state_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::client::{AptosClient, L1BlockMetrics};
use crate::utils::trim_hash;
use anyhow::Context;
use dc_db::DeoxysBackend;
use dp_transactions::MAIN_CHAIN_ID;
use serde::Deserialize;
use starknet_types_core::felt::Felt;

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct L1StateUpdate {
pub block_number: u64,
pub global_root: Felt,
pub block_hash: Felt,
}

pub async fn get_initial_state(aptos_client: &AptosClient) -> anyhow::Result<L1StateUpdate> {
let block_number = aptos_client.get_last_verified_block_number().await?;
let block_hash = aptos_client.get_last_verified_block_hash().await?;
let global_root = aptos_client.get_last_state_root().await?;

Ok(L1StateUpdate { global_root, block_number, block_hash })
}

pub async fn listen_and_update_state(
aptos_client: &AptosClient,
backend: &DeoxysBackend,
block_metrics: &L1BlockMetrics,
chain_id: Felt,
) -> anyhow::Result<()> {
let event_filter = aptos_client
.provider
.get_account_events(aptos_client.l1_core_contract.address(), "LogStateUpdate", "", None, None)
.await?
.into_inner();

for event in event_filter {
log::info!("Formatting event into an L1StateUpdate");

// TODO: Remove unwrap()
let data = event.data;
let block_number = data.get("block_number").unwrap().as_u64().unwrap();
let global_root = Felt::from(data.get("global_root").unwrap().as_u64().unwrap());
let block_hash = Felt::from(data.get("block_hash").unwrap().as_u64().unwrap());

let format_event = L1StateUpdate { block_number, global_root, block_hash };

update_l1(backend, format_event, block_metrics, chain_id)?
}

Ok(())
}

pub fn update_l1(
backend: &DeoxysBackend,
state_update: L1StateUpdate,
block_metrics: &L1BlockMetrics,
chain_id: Felt,
) -> anyhow::Result<()> {
if state_update.block_number > 500000u64 || chain_id == MAIN_CHAIN_ID {
log::info!(
"🔄 Updated L1 head #{} ({}) with state root ({})",
state_update.block_number,
trim_hash(&state_update.block_hash),
trim_hash(&state_update.global_root)
);

block_metrics.l1_block_number.set(state_update.block_number as f64);

backend
.write_last_confirmed_block(state_update.block_number)
.context("Setting l1 last confirmed block number")?;
log::debug!("update_l1: wrote last confirmed block number");
}

Ok(())
}

pub async fn state_update_worker(
backend: &DeoxysBackend,
aptos_client: &AptosClient,
chain_id: Felt,
) -> anyhow::Result<()> {
backend.clear_last_confirmed_block().context("Clearing l1 last confirmed block number")?;
log::debug!("update_l1: cleared confirmed block number");

log::info!("🚀 Subscribed to L1 state verification");
let initial_state = get_initial_state(aptos_client).await.context("Getting initial ethereum state")?;
update_l1(backend, initial_state, &aptos_client.l1_block_metrics, chain_id)?;

listen_and_update_state(aptos_client, backend, &aptos_client.l1_block_metrics, chain_id)
.await
.context("Subscribing to the LogStateUpdate event")?;

Ok(())
}

0 comments on commit 4d0f0e1

Please sign in to comment.