Skip to content

Commit

Permalink
Dev1.1.4 (#30)
Browse files Browse the repository at this point in the history
* update version

* delete holesky

* fix duplicated blocknumber fetch

* prover: optional to onceLock

* add print status to prover initialization
  • Loading branch information
kbizikav authored Nov 1, 2024
1 parent 958d95c commit 895aaa2
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Common variables
NETWORK="holesky"
RPC_URL=https://eth-holesky.alchemyapi.io/v2/your-api-key
NETWORK="base-sepolia"
RPC_URL=https://base-sepolia.alchemyapi.io/v2/your-api-key
MAX_GAS_PRICE="20" # in GWei.
ENCRYPT="false" # true or false

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mining-cli"
version = "1.1.3"
version = "1.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
33 changes: 0 additions & 33 deletions config/config.holesky.toml

This file was deleted.

8 changes: 1 addition & 7 deletions src/cli/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ use crate::{
};

pub fn select_network() -> anyhow::Result<Network> {
let items = vec![
"base",
"base-sepolia (testnet)",
"mainnet (legacy)",
"holesky (legacy-testnet)",
];
let items = vec!["base", "base-sepolia (testnet)", "mainnet (legacy)"];
let selection = Select::new()
.with_prompt("Choose network")
.items(&items)
Expand All @@ -33,7 +28,6 @@ pub fn select_network() -> anyhow::Result<Network> {
0 => "base",
1 => "base-sepolia",
2 => "mainnet",
3 => "holesky",
_ => unreachable!(),
};
Network::from_str(network).map_err(|_| anyhow::anyhow!("Invalid network"))
Expand Down
21 changes: 3 additions & 18 deletions src/services/claim/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
contracts::events::Deposited,
intmax::gnark::{fetch_gnark_proof, gnark_start_prove},
},
services::utils::initialize_prover,
state::{key::Key, state::State},
utils::config::Settings,
};
Expand All @@ -23,7 +22,6 @@ pub async fn single_claim_task(
is_short_term: bool,
events: &[Deposited],
) -> anyhow::Result<()> {
initialize_prover(state).await?;
from_step1(state, key, is_short_term, events).await?;
Ok(())
}
Expand Down Expand Up @@ -71,22 +69,14 @@ async fn from_step2(state: &State, key: &Key) -> anyhow::Result<()> {
print_status("Claim: proving with plonky2");
let mut status = temp::ClaimStatus::new()?;
ensure!(status.next_step == temp::ClaimStep::Plonky2Prove);
ensure!(state.prover.is_some(), "Prover is not initialized");
let mut cyclic_proof = None;
for w in &status.witness {
let proof = state
.prover
.as_ref()
.unwrap()
.claim_processor
.prove(w, &cyclic_proof)?;
let proof = state.prover.claim_processor().prove(w, &cyclic_proof)?;
cyclic_proof = Some(proof);
}
let plonky2_proof = state
.prover
.as_ref()
.unwrap()
.claim_wrapper_processor
.claim_wrapper_processor()
.prove(&cyclic_proof.unwrap())?;
status.plonlky2_proof = Some(plonky2_proof.clone());
status.next_step = temp::ClaimStep::GnarkStart;
Expand Down Expand Up @@ -167,10 +157,7 @@ async fn from_step5(_state: &State, key: &Key) -> anyhow::Result<()> {

#[cfg(test)]
mod tests {
use crate::{
state::prover::Prover,
test::{get_dummy_keys, get_dummy_state},
};
use crate::test::{get_dummy_keys, get_dummy_state};

use super::*;

Expand All @@ -182,8 +169,6 @@ mod tests {
let mut state = get_dummy_state().await;
let assets_status = state.sync_and_fetch_assets(&dummy_key).await.unwrap();

let prover = Prover::new();
state.prover = Some(prover);
let is_short_term = true;
let not_claimed_events = assets_status.get_not_claimed_events(is_short_term);
assert!(not_claimed_events.len() > 0);
Expand Down
18 changes: 3 additions & 15 deletions src/services/mining/withdrawal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
withdrawal::submit_withdrawal,
},
},
services::utils::{await_until_low_gas_price, initialize_prover},
services::utils::await_until_low_gas_price,
state::{key::Key, state::State},
utils::config::Settings,
};
Expand All @@ -21,7 +21,6 @@ pub mod temp;
pub mod witness_generation;

pub async fn withdrawal_task(state: &mut State, key: &Key, event: Deposited) -> anyhow::Result<()> {
initialize_prover(state).await?;
from_step1(state, key, event).await?;
Ok(())
}
Expand Down Expand Up @@ -63,12 +62,9 @@ async fn from_step2(state: &State, key: &Key) -> anyhow::Result<()> {
print_status("Withdrawal: proving with plonky2");
let mut status = temp::WithdrawalStatus::new()?;
ensure!(status.next_step == temp::WithdrawalStep::Plonky2Prove);
ensure!(state.prover.is_some(), "Prover is not initialized");
let plonky2_proof = state
.prover
.as_ref()
.unwrap()
.withdrawal_wrapper_processor
.withdrawal_wrapper_processor()
.prove(&status.witness)?;
status.plonlky2_proof = Some(plonky2_proof.clone());
status.next_step = temp::WithdrawalStep::GnarkStart;
Expand Down Expand Up @@ -148,10 +144,7 @@ async fn from_step5(_state: &State, _key: &Key) -> anyhow::Result<()> {

#[cfg(test)]
mod tests {
use crate::{
state::prover::Prover,
test::{get_dummy_keys, get_dummy_state},
};
use crate::test::{get_dummy_keys, get_dummy_state};

#[tokio::test]
#[ignore]
Expand All @@ -163,9 +156,6 @@ mod tests {
let events = assets_status.get_not_withdrawn_events();
assert!(events.len() > 0);

let prover = Prover::new();
state.prover = Some(prover);

super::withdrawal_task(&mut state, &dummy_key, events[0].clone())
.await
.unwrap();
Expand All @@ -177,8 +167,6 @@ mod tests {
let mut state = get_dummy_state().await;
state.sync_trees().await.unwrap();
let dummy_key = get_dummy_keys();
let prover = Prover::new();
state.prover = Some(prover);
super::resume_withdrawal_task(&state, &dummy_key)
.await
.unwrap();
Expand Down
8 changes: 5 additions & 3 deletions src/services/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ pub async fn sync_trees(
// retry if TreeRootSyncError occurs
let update = || async {
if let Some(bin_deposit_tree) = bin_deposit_tree {
*deposit_hash_tree =
let (new_deposit_hash_tree, new_block_number) =
parse_and_validate_bin_deposit_tree(bin_deposit_tree).await?;
*deposit_hash_tree = new_deposit_hash_tree;
*last_deposit_block_number = new_block_number;
}
if let Some(bin_short_term_eligible_tree) = bin_short_term_eligible_tree {
*short_term_eligible_tree =
Expand Down Expand Up @@ -109,7 +111,7 @@ pub async fn sync_trees(

async fn parse_and_validate_bin_deposit_tree(
bin_deposit_tree: BinDepositTree,
) -> Result<DepositHashTree, Error> {
) -> Result<(DepositHashTree, u64), Error> {
let deposit_tree_info: DepositTreeInfo = bin_deposit_tree
.try_into()
.map_err(|e: anyhow::Error| Error::TreeDeserializationError(e.to_string()))?;
Expand All @@ -123,7 +125,7 @@ async fn parse_and_validate_bin_deposit_tree(
deposit_tree_info.root
)));
}
Ok(deposit_tree_info.tree)
Ok((deposit_tree_info.tree, deposit_tree_info.block_number))
}

async fn parse_and_validate_bin_eligible_tree(
Expand Down
10 changes: 0 additions & 10 deletions src/services/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
contracts::utils::{get_account_nonce, get_balance, get_client, get_gas_price},
intmax::gas_estimation::get_gas_estimation,
},
state::{prover::Prover, state::State},
utils::{config::Settings, env_config::EnvConfig, network::is_legacy, time::sleep_for},
};

Expand Down Expand Up @@ -141,15 +140,6 @@ pub async fn await_until_low_gas_price() -> anyhow::Result<()> {
Ok(())
}

pub async fn initialize_prover(state: &mut State) -> anyhow::Result<()> {
if state.prover.is_none() {
print_status("Waiting for prover to be ready");
let prover = Prover::new();
state.prover = Some(prover);
}
Ok(())
}

pub async fn is_address_used(deposit_address: Address) -> bool {
get_account_nonce(deposit_address).await.unwrap() > 0
|| get_balance(deposit_address).await.unwrap() > 0.into()
Expand Down
60 changes: 31 additions & 29 deletions src/state/prover.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
use log::info;
use std::sync::OnceLock;

use mining_circuit_v1::{
claim::{claim_processor::ClaimProcessor, claim_wrapper_processor::ClaimWrapperProcessor},
withdrawal::simple_withdrawal_wrapper_processor::SimpleWithdrawalWrapperProcessor,
};
use plonky2::{field::goldilocks_field::GoldilocksField, plonk::config::PoseidonGoldilocksConfig};

use crate::cli::console::print_status;

type F = GoldilocksField;
const D: usize = 2;
type C = PoseidonGoldilocksConfig;

pub struct Prover {
pub withdrawal_wrapper_processor: SimpleWithdrawalWrapperProcessor,
pub claim_processor: ClaimProcessor<F, C, D>,
pub claim_wrapper_processor: ClaimWrapperProcessor,
withdrawal_wrapper_processor: OnceLock<SimpleWithdrawalWrapperProcessor>,
claim_processor: OnceLock<ClaimProcessor<F, C, D>>,
claim_wrapper_processor: OnceLock<ClaimWrapperProcessor>,
}

impl Prover {
pub fn new() -> Self {
let withdrawal_wrapper_processor = SimpleWithdrawalWrapperProcessor::new();
let claim_processor = ClaimProcessor::new();
let claim_wrapper_processor = ClaimWrapperProcessor::new(&claim_processor.claim_circuit);

let withdrawal_digest = serde_json::to_string(
&withdrawal_wrapper_processor
.wrapper_circuit1
.data
.verifier_only
.circuit_digest,
)
.unwrap();
let claim_digest = serde_json::to_string(
&claim_wrapper_processor
.wrapper_circuit1
.data
.verifier_only
.circuit_digest,
)
.unwrap();
info!("Withdrawal digest: {}", withdrawal_digest);
info!("Claim digest: {}", claim_digest);
Self {
withdrawal_wrapper_processor,
claim_processor,
claim_wrapper_processor,
withdrawal_wrapper_processor: OnceLock::new(),
claim_processor: OnceLock::new(),
claim_wrapper_processor: OnceLock::new(),
}
}

pub fn withdrawal_wrapper_processor(&self) -> &SimpleWithdrawalWrapperProcessor {
self.withdrawal_wrapper_processor.get_or_init(|| {
print_status("Waiting for withdrawal prover to be ready");
SimpleWithdrawalWrapperProcessor::new()
})
}

pub fn claim_processor(&self) -> &ClaimProcessor<F, C, D> {
self.claim_processor.get_or_init(|| {
print_status("Waiting for claim prover to be ready");
ClaimProcessor::new()
})
}

pub fn claim_wrapper_processor(&self) -> &ClaimWrapperProcessor {
self.claim_wrapper_processor.get_or_init(|| {
print_status("Waiting for claim wrapper prover to be ready");
ClaimWrapperProcessor::new(&self.claim_processor().claim_circuit)
})
}
}
9 changes: 2 additions & 7 deletions src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct State {
pub long_term_eligible_tree: EligibleTreeWithMap,
pub last_tree_feched_at: NaiveDateTime,
pub last_deposit_synced_block: u64,
pub prover: Option<Prover>,
pub prover: Prover,
}

impl State {
Expand All @@ -26,15 +26,10 @@ impl State {
long_term_eligible_tree: EligibleTreeWithMap::new(),
last_tree_feched_at: NaiveDateTime::default(),
last_deposit_synced_block: 0,
prover: None,
prover: Prover::new(),
}
}

pub fn build_circuit(&mut self) -> anyhow::Result<()> {
self.prover = Some(Prover::new());
Ok(())
}

pub async fn sync_trees(&mut self) -> anyhow::Result<()> {
sync_trees(
&mut self.last_deposit_synced_block,
Expand Down
4 changes: 2 additions & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use num_bigint::BigUint;

use crate::{
external_api::contracts::utils::get_address,
state::{key::Key, state::State},
state::{key::Key, prover::Prover, state::State},
utils::{deposit_hash_tree::DepositHashTree, eligible_tree_with_map::EligibleTreeWithMap},
};

Expand Down Expand Up @@ -40,7 +40,7 @@ pub async fn get_dummy_state() -> State {
long_term_eligible_tree: eligible_tree.clone(),
last_tree_feched_at: NaiveDateTime::default(),
last_deposit_synced_block: 0,
prover: None,
prover: Prover::new(),
};
state
}
2 changes: 0 additions & 2 deletions src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ impl Settings {

const BASE_SEPOLIA_CONFIG: &'static [u8] = include_bytes!("../../config/config.base-sepolia.toml");
const BASE_CONFIG: &'static [u8] = include_bytes!("../../config/config.base.toml");
const HOLESKY_CONFIG: &'static [u8] = include_bytes!("../../config/config.holesky.toml");
const MAINNET_CONFIG: &'static [u8] = include_bytes!("../../config/config.mainnet.toml");

pub fn create_config_files() -> anyhow::Result<()> {
create_file_with_content(&config_path(Network::BaseSepolia), BASE_SEPOLIA_CONFIG)?;
create_file_with_content(&config_path(Network::Base), BASE_CONFIG)?;
create_file_with_content(&config_path(Network::Holesky), HOLESKY_CONFIG)?;
create_file_with_content(&config_path(Network::Mainnet), MAINNET_CONFIG)?;
Ok(())
}
Expand Down

0 comments on commit 895aaa2

Please sign in to comment.