Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on forking mismatch #409

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,10 @@ jobs:
key: << parameters.cargo_cache_key >>
- run:
name: Unit tests
# if jobs not limited - fails
# problem: https://app.circleci.com/pipelines/github/0xSpaceShard/starknet-devnet-rs/339/workflows/97e98c29-1563-4aa0-b716-4bfd023c563e/jobs/335/steps
# solution: https://stackoverflow.com/questions/71962406/how-to-use-less-memory-when-compiling-to-avoid-killing-the-build
command: cargo test --jobs 7 --lib --bins --no-fail-fast
- run:
name: Integration tests
# if jobs not limited - fails
# problem: https://app.circleci.com/pipelines/github/0xSpaceShard/starknet-devnet-rs/339/workflows/97e98c29-1563-4aa0-b716-4bfd023c563e/jobs/335/steps
# solution: https://stackoverflow.com/questions/71962406/how-to-use-less-memory-when-compiling-to-avoid-killing-the-build
# fails if jobs not limited; read more in https://github.com/0xSpaceShard/starknet-devnet-rs/issues/378
command: cargo test --jobs 3 --test '*' --no-fail-fast

image-build-amd:
Expand Down
9 changes: 4 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

## Checklist:

- [ ] Checked the relevant parts of the [Development section of the docs](https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#development)
- [ ] Checked the relevant parts of [development docs](https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#development)
- [ ] Applied formatting - `./scripts/format.sh`
- [ ] No linter errors - `./scripts/clippy_check.sh`
- [ ] No unused dependencies - `./scripts/check_unused_deps.sh`
- [ ] Performed code self-review
- [ ] Rebased to the last commit of the target branch (or merged it into my branch)
- [ ] Documented the changes
- [ ] Checked if the [TODO section of the docs](https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#todo-to-reach-feature-parity-with-the-pythonic-devnet) can be edited
- [ ] Rebased to the latest commit of the target branch (or merged it into my branch)
- [ ] Updated the docs if needed, including the [TODO section](https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#todo-to-reach-feature-parity-with-the-pythonic-devnet)
- [ ] Linked the issues which this PR resolves
- [ ] Updated the tests if needed; all passing ([execution info](<(https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#test-execution)>))
- [ ] Updated the tests if needed; all passing ([execution info](https://github.com/0xSpaceShard/starknet-devnet-rs/?tab=readme-ov-file#test-execution))
51 changes: 44 additions & 7 deletions crates/starknet-devnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::net::SocketAddr;
use anyhow::Ok;
use clap::Parser;
use cli::Args;
use server::api::json_rpc::RPC_SPEC_VERSION;
use server::api::Api;
use server::server::serve_http_api_json_rpc;
use server::ServerConfig;
Expand All @@ -19,7 +20,7 @@ use starknet_rs_providers::{JsonRpcClient, Provider};
use starknet_types::chain_id::ChainId;
use starknet_types::rpc::state::Balance;
use starknet_types::traits::ToHexString;
use tracing::info;
use tracing::{info, warn};
use tracing_subscriber::EnvFilter;

mod cli;
Expand Down Expand Up @@ -63,7 +64,7 @@ fn log_predeployed_accounts(
}
}

fn print_predeployed_contracts() {
fn log_predeployed_contracts() {
println!("Predeployed FeeToken");
println!("ETH Address: {ETH_ERC20_CONTRACT_ADDRESS}");
println!("STRK Address: {STRK_ERC20_CONTRACT_ADDRESS}");
Expand All @@ -75,11 +76,44 @@ fn print_predeployed_contracts() {
println!();
}

fn print_chain_id(chain_id: ChainId) {
fn log_chain_id(chain_id: ChainId) {
println!("Chain ID: {} ({})", chain_id, chain_id.to_felt().to_prefixed_hex_str());
}

pub async fn set_and_log_fork_block(fork_config: &mut ForkConfig) -> Result<(), anyhow::Error> {
async fn check_forking_spec_version(
client: &JsonRpcClient<HttpTransport>,
) -> Result<(), anyhow::Error> {
let origin_spec_version = client.spec_version().await?;
if origin_spec_version != RPC_SPEC_VERSION {
warn!(
"JSON-RPC API version of origin ({}) does not match this Devnet's version ({}).",
origin_spec_version, RPC_SPEC_VERSION
);
}
Ok(())
}

async fn check_forking_chain_id(
client: &JsonRpcClient<HttpTransport>,
devnet_chain_id: ChainId,
) -> Result<(), anyhow::Error> {
let origin_chain_id = client.chain_id().await?;
let devnet_chain_id_felt = devnet_chain_id.into();
if origin_chain_id != devnet_chain_id_felt {
warn!(
"Origin chain ID ({:#x}) does not match this Devnet's chain ID ({:#x}).",
origin_chain_id, devnet_chain_id_felt
);
}
Ok(())
}

/// Logs forking info if forking specified. If block_number not specified, it is set to the latest
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
/// block number.
pub async fn set_and_log_fork_config(
fork_config: &mut ForkConfig,
chain_id: ChainId,
) -> Result<(), anyhow::Error> {
if let Some(url) = &fork_config.url {
let json_rpc_client = JsonRpcClient::new(HttpTransport::new(url.clone()));
let block_id =
Expand All @@ -101,6 +135,9 @@ pub async fn set_and_log_fork_block(fork_config: &mut ForkConfig) -> Result<(),
}
_ => panic!("Unreachable"),
};

check_forking_spec_version(&json_rpc_client).await?;
check_forking_chain_id(&json_rpc_client, chain_id).await?;
}

Ok(())
Expand All @@ -114,7 +151,7 @@ async fn main() -> Result<(), anyhow::Error> {
let args = Args::parse();
let mut starknet_config = args.to_starknet_config()?;

set_and_log_fork_block(&mut starknet_config.fork_config).await?;
set_and_log_fork_config(&mut starknet_config.fork_config, starknet_config.chain_id).await?;

let mut addr: SocketAddr = SocketAddr::new(starknet_config.host, starknet_config.port);

Expand All @@ -127,8 +164,8 @@ async fn main() -> Result<(), anyhow::Error> {
);
};

print_predeployed_contracts();
print_chain_id(starknet_config.chain_id);
log_predeployed_contracts();
log_chain_id(starknet_config.chain_id);

let predeployed_accounts = api.starknet.read().await.get_predeployed_accounts();
log_predeployed_accounts(
Expand Down
2 changes: 2 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ RUN cargo build --bin starknet-devnet --release
FROM debian:buster-slim

# Use tini to avoid hanging process on Ctrl+C
# Use ca-certificates to allow forking from URLs using https scheme
RUN apt-get -y update && \
apt-get install tini && \
apt-get install libssl-dev -y && \
apt-get install ca-certificates -y && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
Expand Down