Skip to content

Commit

Permalink
Merge branch 'main' into prover_skipmap
Browse files Browse the repository at this point in the history
  • Loading branch information
anylots authored Dec 23, 2024
2 parents 672d3be + 732f380 commit dac733e
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 279 deletions.
59 changes: 36 additions & 23 deletions .github/workflows/prover.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
name: Prover

# on:
# push:
# branches:
# - main
# paths:
# - "prover/**"
# - ".github/workflows/prover.yaml"
# pull_request:
# paths:
# - "prover/**"
# - ".github/workflows/prover.yaml"
#
# defaults:
# run:
# working-directory: "prover"
#
# jobs:
# check:
# runs-on: ubuntu-22.04
# steps:
# - uses: actions/checkout@v3
# - name: Run build
# run: make build-prover
on:
push:
branches:
- main
paths:
- "prover/**"
- ".github/workflows/prover.yaml"
pull_request:
paths:
- "prover/**"
- ".github/workflows/prover.yaml"

defaults:
run:
working-directory: "prover"

jobs:
fmt:
name: fmt
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- run: cargo fmt --all --check

build:
name: build
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: make build-shadow-prove && make build-shadow-prove
6 changes: 2 additions & 4 deletions prover/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
build-plonk-circuits:
docker build -f Dockerfile.sp1-plonk -t sp1-plonk:latest .


save-plonk-circuits:
rm -rf plonk-circuits && \
mkdir -p plonk-circuits && \
Expand All @@ -17,11 +16,10 @@ start-server:
stop-server:
docker compose -f docker-compose-app.yml down


build-shadow-prove:
cd bin/shadow-prove
cd bin/shadow-prove && \
cargo build --release

build-challenge:
cd bin/challenge
cd bin/challenge && \
cargo build --release
11 changes: 7 additions & 4 deletions prover/bin/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ use once_cell::sync::Lazy;
use prometheus::{IntGauge, Registry};

// environment variables
pub static PROVER_PROOF_DIR: Lazy<String> = Lazy::new(|| read_env_var("PROVER_PROOF_DIR", "./proof".to_string()));
pub static PROVER_L2_RPC: Lazy<String> = Lazy::new(|| read_env_var("PROVER_L2_RPC", "localhost:8545".to_string()));
pub static PROVER_PROOF_DIR: Lazy<String> =
Lazy::new(|| read_env_var("PROVER_PROOF_DIR", "./proof".to_string()));
pub static PROVER_L2_RPC: Lazy<String> =
Lazy::new(|| read_env_var("PROVER_L2_RPC", "localhost:8545".to_string()));

// metrics
pub static REGISTRY: Lazy<Registry> = Lazy::new(Registry::new);
pub static PROVE_RESULT: Lazy<IntGauge> =
Lazy::new(|| IntGauge::new("prove_result", "prove result").expect("prove metric can be created")); // 1 = Ok, 2 = Fail
pub static PROVE_RESULT: Lazy<IntGauge> = Lazy::new(|| {
IntGauge::new("prove_result", "prove result").expect("prove metric can be created")
}); // 1 = Ok, 2 = Fail
pub static PROVE_TIME: Lazy<IntGauge> =
Lazy::new(|| IntGauge::new("prove_time", "prove time").expect("time metric can be created"));

Expand Down
9 changes: 2 additions & 7 deletions prover/crates/core/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ impl BatchInfo {
/// Construct by block traces
pub fn from_block_traces<T: Block>(traces: &[T]) -> (Self, Rc<ZkMemoryDb>) {
let chain_id = traces.first().unwrap().chain_id();
let prev_state_root = traces
.first()
.expect("at least 1 block needed")
.root_before();
let prev_state_root = traces.first().expect("at least 1 block needed").root_before();
let post_state_root = traces.last().expect("at least 1 block needed").root_after();

let mut data_hasher = Keccak::v256();
Expand Down Expand Up @@ -143,9 +140,7 @@ mod tests {
pub struct BlockTraceJsonRpcResult {
pub result: BlockTrace,
}
serde_json::from_str::<BlockTraceJsonRpcResult>(s)
.unwrap()
.result
serde_json::from_str::<BlockTraceJsonRpcResult>(s).unwrap().result
});

let fork_config = HardforkConfig::default_from_chain_id(traces[0].chain_id());
Expand Down
107 changes: 46 additions & 61 deletions prover/crates/core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use sbv_primitives::{
zk_trie::{SharedMemoryDb, ZkMemoryDb, ZkTrie},
Block,
};
use std::rc::Rc;
use std::{cell::RefCell, collections::HashMap, convert::Infallible, fmt};
use std::{cell::RefCell, collections::HashMap, convert::Infallible, fmt, rc::Rc};

type Result<T, E = ZkTrieError> = std::result::Result<T, E>;

Expand Down Expand Up @@ -92,19 +91,13 @@ impl ReadOnlyDB {
address: Address,
storage_root: B256,
) -> Option<B256> {
self.prev_storage_roots
.borrow_mut()
.insert(address, storage_root)
self.prev_storage_roots.borrow_mut().insert(address, storage_root)
}

/// Get the previous storage root of an account.
#[inline]
pub(crate) fn prev_storage_root(&self, address: &Address) -> B256 {
self.prev_storage_roots
.borrow()
.get(address)
.copied()
.unwrap_or_default()
self.prev_storage_roots.borrow().get(address).copied().unwrap_or_default()
}

/// Get the zkTrie root.
Expand Down Expand Up @@ -162,41 +155,35 @@ impl DatabaseRef for ReadOnlyDB {

/// Get basic account information.
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(self
.zktrie_db_ref
.get_account(address.as_slice())
.map(|account_data| {
let code_size =
u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize;
let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap());
let balance = U256::from_be_bytes(account_data[1]);
let code_hash = B256::from(account_data[3]);
let poseidon_code_hash = B256::from(account_data[4]);

let storage_root = B256::from(account_data[2]);
self.prev_storage_roots
.borrow_mut()
.entry(address)
.or_insert(storage_root.0.into());

let zktrie_db = self.zktrie_db.clone();
self.storage_trie_refs.borrow_mut().insert(
address,
Lazy::new(Box::new(move || {
zktrie_db
.new_ref_trie(&storage_root.0)
.expect("storage trie associated with account not found")
})),
);
AccountInfo {
balance,
nonce,
code_size,
code_hash,
poseidon_code_hash,
code: self.code_db.get(&code_hash).cloned(),
}
}))
Ok(self.zktrie_db_ref.get_account(address.as_slice()).map(|account_data| {
let code_size =
u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize;
let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap());
let balance = U256::from_be_bytes(account_data[1]);
let code_hash = B256::from(account_data[3]);
let poseidon_code_hash = B256::from(account_data[4]);

let storage_root = B256::from(account_data[2]);
self.prev_storage_roots.borrow_mut().entry(address).or_insert(storage_root.0.into());

let zktrie_db = self.zktrie_db.clone();
self.storage_trie_refs.borrow_mut().insert(
address,
Lazy::new(Box::new(move || {
zktrie_db
.new_ref_trie(&storage_root.0)
.expect("storage trie associated with account not found")
})),
);
AccountInfo {
balance,
nonce,
code_size,
code_hash,
poseidon_code_hash,
code: self.code_db.get(&code_hash).cloned(),
}
}))
}

/// Get account code by its code hash.
Expand All @@ -217,22 +204,20 @@ impl DatabaseRef for ReadOnlyDB {
/// Get storage value of address at index.
fn storage_ref(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
let mut storage_trie_refs = self.storage_trie_refs.borrow_mut();
let trie = storage_trie_refs
.entry(address)
.or_insert_with_key(|address| {
let storage_root = self
.zktrie_db_ref
.get_account(address.as_slice())
.map(|account_data| B256::from(account_data[2]))
.unwrap_or_default();
let zktrie_db = self.zktrie_db.clone();
Lazy::new(Box::new(move || {
zktrie_db
.clone()
.new_ref_trie(&storage_root.0)
.expect("storage trie associated with account not found")
}))
});
let trie = storage_trie_refs.entry(address).or_insert_with_key(|address| {
let storage_root = self
.zktrie_db_ref
.get_account(address.as_slice())
.map(|account_data| B256::from(account_data[2]))
.unwrap_or_default();
let zktrie_db = self.zktrie_db.clone();
Lazy::new(Box::new(move || {
zktrie_db
.clone()
.new_ref_trie(&storage_root.0)
.expect("storage trie associated with account not found")
}))
});

Ok(trie
.get_store(&index.to_be_bytes::<32>())
Expand Down
3 changes: 1 addition & 2 deletions prover/crates/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use revm::primitives::alloy_primitives::SignatureError;
use revm::primitives::{EVMError, B256};
use revm::primitives::{alloy_primitives::SignatureError, EVMError, B256};
use std::convert::Infallible;

/// Error variants encountered during manipulation of a zkTrie.
Expand Down
11 changes: 4 additions & 7 deletions prover/crates/core/src/executor/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::ZkTrieError;
use crate::{executor::hooks::ExecuteHooks, EvmExecutor, HardforkConfig, ReadOnlyDB};
use crate::{
error::ZkTrieError, executor::hooks::ExecuteHooks, EvmExecutor, HardforkConfig, ReadOnlyDB,
};
use core::fmt;
use revm::db::CacheDB;
use sbv_primitives::{zk_trie::ZkMemoryDb, Block};
Expand All @@ -25,11 +26,7 @@ impl fmt::Debug for EvmExecutorBuilder<'_, ()> {
impl<'e> EvmExecutorBuilder<'e, ()> {
/// Create a new builder.
pub fn new(zktrie_db: Rc<ZkMemoryDb>) -> Self {
Self {
hardfork_config: (),
execute_hooks: ExecuteHooks::default(),
zktrie_db,
}
Self { hardfork_config: (), execute_hooks: ExecuteHooks::default(), zktrie_db }
}
}

Expand Down
5 changes: 1 addition & 4 deletions prover/crates/core/src/executor/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ impl Debug for ExecuteHooks<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ExecuteHooks")
.field("tx_rlp_handlers", &self.tx_rlp_handlers.len())
.field(
"post_tx_execution_handlers",
&self.post_tx_execution_handlers.len(),
)
.field("post_tx_execution_handlers", &self.post_tx_execution_handlers.len())
.finish()
}
}
30 changes: 6 additions & 24 deletions prover/crates/core/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,9 @@ pub const MORPH_MAINNET_CHAIN_ID: u64 = 2818;
/// Hardfork heights for Scroll networks, grouped by chain id.
static HARDFORK_HEIGHTS: Lazy<HashMap<u64, HashMap<SpecId, u64>>> = Lazy::new(|| {
let mut map = HashMap::new();
map.insert(
MORPH_DEVNET_CHAIN_ID,
HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]),
);
map.insert(
MORPH_TESTNET_CHAIN_ID,
HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]),
);
map.insert(
MORPH_MAINNET_CHAIN_ID,
HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]),
);
map.insert(MORPH_DEVNET_CHAIN_ID, HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]));
map.insert(MORPH_TESTNET_CHAIN_ID, HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]));
map.insert(MORPH_MAINNET_CHAIN_ID, HashMap::from([(SpecId::BERNOULLI, 0), (SpecId::CURIE, 0)]));

map
});
Expand Down Expand Up @@ -103,14 +94,8 @@ impl HardforkConfig {
let l1_gas_price_oracle_acc = Account {
info: l1_gas_price_oracle_info,
storage: HashMap::from([
(
l1_gas_price_oracle::IS_CURIE_SLOT,
EvmStorageSlot::new(U256::from(1)),
),
(
l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT,
EvmStorageSlot::new(U256::from(1)),
),
(l1_gas_price_oracle::IS_CURIE_SLOT, EvmStorageSlot::new(U256::from(1))),
(l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT, EvmStorageSlot::new(U256::from(1))),
(
l1_gas_price_oracle::COMMIT_SCALAR_SLOT,
EvmStorageSlot::new(l1_gas_price_oracle::INITIAL_COMMIT_SCALAR),
Expand All @@ -123,10 +108,7 @@ impl HardforkConfig {
status: AccountStatus::Touched,
};

db.commit(HashMap::from([(
l1_gas_price_oracle_addr,
l1_gas_price_oracle_acc,
)]));
db.commit(HashMap::from([(l1_gas_price_oracle_addr, l1_gas_price_oracle_acc)]));

Ok(())
}
Expand Down
8 changes: 2 additions & 6 deletions prover/crates/morph-executor/host/src/zstd_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ pub(crate) fn init_zstd_encoder(target_block_size: u32) -> Encoder<'static, Vec<
.set_parameter(CParameter::LiteralCompressionMode(ParamSwitch::Disable))
.expect("infallible");
// with a hack in zstd we can set window log <= 17 with single segment kept
encoder
.set_parameter(CParameter::WindowLog(17))
.expect("infallible");
encoder.set_parameter(CParameter::WindowLog(17)).expect("infallible");
// set target block size to fit within a single block.
encoder
.set_parameter(CParameter::TargetCBlockSize(target_block_size))
.expect("infallible");
encoder.set_parameter(CParameter::TargetCBlockSize(target_block_size)).expect("infallible");
// do not include the checksum at the end of the encoded data.
encoder.include_checksum(false).expect("infallible");
// do not include magic bytes at the start of the frame since we will have a single
Expand Down
Loading

0 comments on commit dac733e

Please sign in to comment.