Skip to content

Commit

Permalink
Merge pull request paradigmxyz#44 from taikoxyz/gwyneth-cecilia
Browse files Browse the repository at this point in the history
feat(db): SyncDatabase for cross-chain state access
  • Loading branch information
Brechtpd authored Oct 11, 2024
2 parents ee68e26 + be2e95a commit f304454
Show file tree
Hide file tree
Showing 97 changed files with 2,636 additions and 1,569 deletions.
1,359 changes: 545 additions & 814 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ revm-primitives = { version = "9.0.0", features = [

# eth
alloy-chains = "0.1.18"
alloy-dyn-abi = "0.8.0"
alloy-primitives = { version = "0.8.0", default-features = false }
alloy-dyn-abi = "0.8.2"
alloy-primitives = { version = "0.8.2", default-features = false }
alloy-rlp = "0.3.4"
alloy-sol-types = "0.8.0"
alloy-sol-types = "0.8.2"
alloy-trie = { version = "0.5", default-features = false }

alloy-consensus = { version = "0.3.0", default-features = false }
Expand All @@ -432,7 +432,7 @@ alloy-rpc-types = { version = "0.3.0", features = [
alloy-rpc-types-admin = { version = "0.3.0", default-features = false }
alloy-rpc-types-anvil = { version = "0.3.0", default-features = false }
alloy-rpc-types-beacon = { version = "0.3.0", default-features = false }
alloy-rpc-types-engine = { version = "0.3.0", default-features = false, features = ["std"] }
alloy-rpc-types-engine = { version = "0.3.0", default-features = false }
alloy-rpc-types-eth = { version = "0.3.0", default-features = false }
alloy-rpc-types-mev = { version = "0.3.0", default-features = false }
alloy-rpc-types-trace = { version = "0.3.0", default-features = false }
Expand Down Expand Up @@ -565,6 +565,9 @@ similar-asserts = "1.5.0"
tempfile = "3.8"
test-fuzz = "5"

#[patch.crates-io]
#revm = { path = "../revm/crates/revm" }
#revm-primitives = { path = "../revm/crates/primitives" }
[patch.crates-io]
revm = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-interpreter = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-precompile = { git = "https://github.com/taikoxyz/revm.git", branch = "v43-gwyneth" }
revm-inspectors = { git = "https://github.com/taikoxyz/revm-inspectors.git", branch = "main-rbuilder" }
47 changes: 37 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app

LABEL org.opencontainers.image.source=https://github.com/paradigmxyz/reth
LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0"

# Install system dependencies
RUN apt-get update && apt-get -y upgrade && apt-get install -y libclang-dev pkg-config
RUN apt-get update && apt-get -y upgrade && apt-get install -y libclang-dev pkg-config git

# Builds a cargo-chef plan
FROM chef AS planner
Expand All @@ -17,36 +16,64 @@ COPY --from=planner /app/recipe.json recipe.json

# Build profile, release by default
ARG BUILD_PROFILE=release
ENV BUILD_PROFILE=$BUILD_PROFILE
ENV BUILD_PROFILE $BUILD_PROFILE

# Extra Cargo flags
ARG RUSTFLAGS=""
ENV RUSTFLAGS="$RUSTFLAGS"
ENV RUSTFLAGS "$RUSTFLAGS"

# Extra Cargo features
ARG FEATURES=""
ENV FEATURES=$FEATURES
ENV FEATURES $FEATURES

# Builds dependencies
RUN cargo chef cook --profile $BUILD_PROFILE --features "$FEATURES" --recipe-path recipe.json

# Build application
COPY . .
RUN cargo build --profile $BUILD_PROFILE --features "$FEATURES" --locked --bin reth

# ARG is not resolved in COPY so we have to hack around it by copying the
# binary to a temporary location
# Hack: Add a cache busting step (above steps are the more
# time consuming ones but we need to make sure the rbuilder is
# always freshly cloned and not cached !)
# Since the content of this file will change
# with each build, Docker will consider this
# layer (and all subsequent layers) as modified,
# forcing a re-execution of the following steps.
# ADD https://worldtimeapi.org/api/ip /tmp/bustcache

# Clone and build rbuilder (gwyneth branch)
RUN git clone -b gwyneth https://github.com/taikoxyz/rbuilder.git /app/rbuilder
WORKDIR /app/rbuilder
RUN cargo build --release

# Copy binaries to a temporary location
RUN cp /app/target/$BUILD_PROFILE/reth /app/reth
RUN cp /app/rbuilder/target/release/rbuilder /app/rbuilder

# Use Ubuntu as the release image
FROM ubuntu AS runtime
FROM ubuntu:22.04 AS runtime
WORKDIR /app

# Copy reth over from the build stage
# Install necessary runtime dependencies and Rust/Cargo
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*

# Install Rust and Cargo
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

# Copy reth and rbuilder binaries over from the build stage
COPY --from=builder /app/reth /usr/local/bin
COPY --from=builder /app/rbuilder /usr/local/bin

# Copy the entire rbuilder repository
COPY --from=builder /app/rbuilder /app/rbuilder

# Copy licenses
COPY LICENSE-* ./

# Create start script
RUN echo '#!/bin/bash\nrbuilder run /app/rbuilder/config-gwyneth-reth.toml' > /app/start_rbuilder.sh && \
chmod +x /app/start_rbuilder.sh

EXPOSE 30303 30303/udp 9001 8545 8546
ENTRYPOINT ["/usr/local/bin/reth"]
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# Gwyneth

## Install

```sh
cd packages/protocol
pnpm install
make install
```

## Use

Add custom networks to your wallet:

```
chain_id: 167010
name: Gwyneth-1
rpc: http://127.0.0.1:32005
Currency: ETH
Block explorer: http://127.0.0.1:64003
```

```
chain_id: 160010
name: Gwyneth L1
rpc: http://127.0.0.1:32002
Currency: ETH
Block explorer: http://127.0.0.1:64001
```

Add test accounts that have some ETH to play with:
- 0x8943545177806ED17B9F23F0a21ee5948eCaa776 (private key: bcdf20249abf0ed6d944c0288fad489e33f66b3960d9e6229c1cd214ed3bbe31)
- 0xE25583099BA105D9ec0A67f5Ae86D90e50036425 (private key: 39725efee3fb28614de3bacaffe4cc4bd8c436257e2c8bb887c4b5c4be45e76d)
- 0x614561D2d143621E126e87831AEF287678B442b8 (private key: 53321db7c1e331d93a11a41d16f004d7ff63972ec8ec7c25db329728ceeb1710)

Rabby/Brave wallet works, but some issues with nonces so you may have to manually input the correct nonce.

# reth

[![CI status](https://github.com/paradigmxyz/reth/workflows/unit/badge.svg)][gh-ci]
Expand Down
7 changes: 0 additions & 7 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,10 @@ tempfile.workspace = true
backon.workspace = true
similar-asserts.workspace = true
itertools.workspace = true
once_cell = "1"
rusqlite = { version = "0.31.0", features = ["bundled"] }
foundry-blob-explorers = "0.5"

# p2p
discv5.workspace = true

# alloy
alloy-consensus = { version = "0.2", features = ["kzg"] }
alloy-sol-types = { workspace = true, features = ["json"] }

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { version = "0.5.0", optional = true }
libc = "0.2"
Expand Down
13 changes: 10 additions & 3 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use reth_provider::{
ProviderFactory, StageCheckpointReader, StateProviderFactory,
};
use reth_prune::PruneModes;
use reth_revm::{database::StateProviderDatabase, primitives::EnvKzgSettings};
use reth_revm::{
database::{StateProviderDatabase, SyncStateProviderDatabase},
primitives::EnvKzgSettings,
};
use reth_rpc_types::engine::{BlobsBundleV1, PayloadAttributes};
use reth_stages::StageId;
use reth_transaction_pool::{
Expand Down Expand Up @@ -274,13 +277,17 @@ impl Command {

println!("debug_cmd build");

let db = StateProviderDatabase::new(blockchain_db.latest()?);
let chain_id = provider_factory.chain_spec().chain.id();
let db = SyncStateProviderDatabase::new(
Some(chain_id),
StateProviderDatabase::new(blockchain_db.latest()?),
);
let executor = block_executor!(provider_factory.chain_spec()).executor(db);

let block_execution_output =
executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?;
let execution_outcome =
ExecutionOutcome::from((block_execution_output, block.number));
ExecutionOutcome::from((block_execution_output, chain_id, block.number));
debug!(target: "reth::cli", ?execution_outcome, "Executed block");

let hashed_post_state = execution_outcome.hash_state_slow();
Expand Down
17 changes: 11 additions & 6 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use reth_provider::{
HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, ProviderFactory,
StageCheckpointReader, StateWriter, StaticFileProviderFactory, StorageReader,
};
use reth_revm::database::StateProviderDatabase;
use reth_revm::database::{StateProviderDatabase, SyncStateProviderDatabase};
use reth_stages::StageId;
use reth_tasks::TaskExecutor;
use reth_trie::StateRoot;
Expand Down Expand Up @@ -121,17 +121,21 @@ impl Command {

let client = fetch_client.clone();
let chain = provider_factory.chain_spec();
let chain_id = chain.chain().id();
let block = (move || get_single_body(client.clone(), Arc::clone(&chain), header.clone()))
.retry(&backoff)
.notify(
|err, _| warn!(target: "reth::cli", "Error requesting body: {err}. Retrying..."),
)
.await?;

let db = StateProviderDatabase::new(LatestStateProviderRef::new(
provider.tx_ref(),
provider_factory.static_file_provider(),
));
let db = SyncStateProviderDatabase::new(
Some(chain_id),
StateProviderDatabase::new(LatestStateProviderRef::new(
provider.tx_ref(),
provider_factory.static_file_provider(),
)),
);

let executor = block_executor!(provider_factory.chain_spec()).executor(db);

Expand All @@ -148,7 +152,8 @@ impl Command {
)
.into(),
)?;
let execution_outcome = ExecutionOutcome::from((block_execution_output, block.number));
let execution_outcome =
ExecutionOutcome::from((block_execution_output, chain_id, block.number));

// Unpacked `BundleState::state_root_slow` function
let (in_memory_state_root, in_memory_updates) = StateRoot::overlay_root_with_updates(
Expand Down
13 changes: 8 additions & 5 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use reth_primitives::BlockHashOrNumber;
use reth_provider::{
writer::UnifiedStorageWriter, BlockNumReader, BlockWriter, ChainSpecProvider, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter,
StaticFileProviderFactory,
};
use reth_revm::database::StateProviderDatabase;
use reth_revm::database::{StateProviderDatabase, SyncStateProviderDatabase};
use reth_stages::{
stages::{AccountHashingStage, MerkleStage, StorageHashingStage},
ExecInput, Stage, StageCheckpoint,
Expand Down Expand Up @@ -146,12 +147,14 @@ impl Command {
provider_rw.insert_block(sealed_block.clone())?;

td += sealed_block.difficulty;
let mut executor = executor_provider.batch_executor(StateProviderDatabase::new(
LatestStateProviderRef::new(
let db = SyncStateProviderDatabase::new(
Some(provider_factory.chain_spec().chain().id()),
StateProviderDatabase::new(LatestStateProviderRef::new(
provider_rw.tx_ref(),
provider_rw.static_file_provider().clone(),
),
));
)),
);
let mut executor = executor_provider.batch_executor(db);
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
let execution_outcome = executor.finalize();

Expand Down
13 changes: 3 additions & 10 deletions bin/reth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ fn main() -> eyre::Result<()> {
.with_chain(chain_spec.clone())
.with_network(network_config.clone())
.with_unused_ports()
.with_rpc(RpcServerArgs::default().with_unused_ports().with_static_l2_rpc_ip_and_port())
.with_rpc(RpcServerArgs::default().with_unused_ports().with_static_l2_rpc_ip_and_port(chain_spec.chain.id()))
.set_dev(true);

let NodeHandle { node: gwyneth_node, node_exit_future: _ } =
NodeBuilder::new(node_config.clone())
.testing_node(exec.clone())
.gwyneth_node(exec.clone(), chain_spec.chain.id())
.node(GwynethNode::default())
.launch()
.await?;
Expand All @@ -62,19 +62,12 @@ fn main() -> eyre::Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
use clap::{Args, Parser};

/// A helper type to parse Args more easily
#[derive(Parser)]
struct CommandParser<T: Args> {
#[command(flatten)]
args: T,
}

#[test]
fn test_parse_engine_args() {
let default_args = EngineArgs::default();
let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
assert_eq!(args, default_args);
}
}
17 changes: 11 additions & 6 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use reth_evm::execute::BlockExecutorProvider;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, EthereumHardfork, ForkBlock, GotExpected, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
BlockHash, BlockNumHash, BlockNumber, BufMut, EthereumHardfork, ForkBlock, GotExpected,
Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
};
use reth_provider::{
BlockExecutionWriter, BlockNumReader, BlockWriter, CanonStateNotification,
Expand Down Expand Up @@ -390,6 +390,11 @@ where
block: SealedBlockWithSenders,
block_validation_kind: BlockValidationKind,
) -> Result<BlockStatus, InsertBlockErrorKind> {
println!(
"BlockchainTree: try_append_canonical_chain {:?} \n tx {:?}",
block.state_root,
block.transactions().count()
);
let parent = block.parent_num_hash();
let block_num_hash = block.num_hash();
debug!(target: "blockchain_tree", head = ?block_num_hash.hash, ?parent, "Appending block to canonical chain");
Expand Down Expand Up @@ -648,8 +653,8 @@ where
chain_id = ?chain_id,
chain_tip = ?chain.tip().num_hash(),
"Prepend unwound block state to blockchain tree chain");

chain.prepend_state(cloned_execution_outcome.state().clone())
let chain_id = self.externals.provider_factory.chain_spec().chain.id();
chain.prepend_state(cloned_execution_outcome.state(chain_id))
}
}
}
Expand Down Expand Up @@ -1974,12 +1979,12 @@ mod tests {
// chain 0 has two blocks so receipts and reverts len is 2
let chain0 = tree.state.chains.get(&0.into()).unwrap().execution_outcome();
assert_eq!(chain0.receipts().len(), 2);
assert_eq!(chain0.state().reverts.len(), 2);
assert_eq!(chain0.all_states().reverts.len(), 2);
assert_eq!(chain0.first_block(), block1.number);
// chain 1 has one block so receipts and reverts len is 1
let chain1 = tree.state.chains.get(&1.into()).unwrap().execution_outcome();
assert_eq!(chain1.receipts().len(), 1);
assert_eq!(chain1.state().reverts.len(), 1);
assert_eq!(chain1.all_states().reverts.len(), 1);
assert_eq!(chain1.first_block(), block2.number);
}

Expand Down
Loading

0 comments on commit f304454

Please sign in to comment.