Skip to content

Commit

Permalink
fix: update the chain id (#1435)
Browse files Browse the repository at this point in the history
* update the chain id + bump to 0.9.1 + move constants

* fix dockers

* fix again
  • Loading branch information
greged93 authored Oct 9, 2024
1 parent 428bb0c commit a6dff03
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 224 deletions.
32 changes: 8 additions & 24 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,14 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
# Install Python
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: 3.10.14
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v4
with:
path: ~/.local
key: poetry-${{ runner.os }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
# Install UV
- uses: astral-sh/setup-uv@v2
with:
enable-cache: true
cache-dependency-glob: lib/kakarot/uv.lock
- uses: actions/setup-python@v5
with:
python-version-file: lib/kakarot/.python-version
- name: Install asdf & tools
uses: asdf-vm/actions/setup@v3
- name: install scarb
Expand Down
32 changes: 9 additions & 23 deletions .github/workflows/rust_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Setup rust env
uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -24,30 +26,14 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
# Install Python
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: 3.10.14
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v4
with:
path: ~/.local
key: poetry-${{ runner.os }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
# Install UV
- uses: astral-sh/setup-uv@v2
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
enable-cache: true
cache-dependency-glob: lib/kakarot/uv.lock
- uses: actions/setup-python@v5
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
python-version-file: lib/kakarot/.python-version
- name: Install asdf & tools
uses: asdf-vm/actions/setup@v3
- name: install scarb
Expand Down Expand Up @@ -85,7 +71,7 @@ jobs:
platforms: linux/amd64
build-args: |
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
APIBARA_STARKNET_BIN_DIR=ns4qwsl6fgbv7mxhxpnaqhd66wnic8i6
APIBARA_STARKNET_BIN_DIR=3l93dydg7m71r66x5wllf1j9czvj7zdk
APIBARA_SINK_BIN_DIR=81f00xxzyxs6ih6376cw0qbznf6cl6bn
outputs: type=docker,dest=./artifacts/kakarot_image.tar
- name: Checkout hive tests
Expand Down
28 changes: 6 additions & 22 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,14 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
# Install Python
- name: Set up Python 3.10
uses: actions/setup-python@v5
# Install UV
- uses: astral-sh/setup-uv@v2
with:
python-version: 3.10.14
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v4
enable-cache: true
cache-dependency-glob: lib/kakarot/uv.lock
- uses: actions/setup-python@v5
with:
path: ~/.local
key: poetry-${{ runner.os }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
python-version-file: lib/kakarot/.python-version
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: 0.7.0
Expand Down
4 changes: 2 additions & 2 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- trivy@0.55.2
- [email protected].6
- trivy@0.56.1
- [email protected].7
- [email protected]
- [email protected]
ignore:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ TL;DR:
and running the various services and components in a consistent environment.
- [Python](https://www.python.org/): Used primarily for interacting with and
building our Kakarot programs.
- [Poetry](https://python-poetry.org/docs/): A Python dependency management tool
used for managing the dependencies of our Kakarot programs.
- [UV](https://github.com/astral-sh/uv): An extremely fast Python package and
project manager, written in Rust. Used for managing the dependencies of our
Kakarot programs.
- [Deno](https://docs.deno.com/runtime/manual/): A JavaScript runtime used for
our indexing service, based on the [Apibara](https://www.apibara.com/docs)
third-party service.
Expand Down
29 changes: 13 additions & 16 deletions docker/hive/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM ghcr.io/dojoengine/dojo:v1.0.0-alpha.14 as katana

# Indexer service
### Apibara DNA indexer and indexer
FROM quay.io/apibara/starknet:1.5.0 as apibara
FROM quay.io/apibara/starknet:1.6.0 as apibara
FROM quay.io/apibara/sink-mongo:0.7.1 as indexer
FROM debian:bookworm as apibara-build
RUN apt-get update && apt-get install --no-install-recommends -y patchelf && rm -rf /var/lib/apt/lists/*
Expand All @@ -15,17 +15,17 @@ RUN apt-get update && apt-get install --no-install-recommends -y patchelf && rm
# Depending on the platform, the path to the binaries will be different
# These values need to be updated if the version of ApiBara Starknet or Sink change.
# platform: linux/amd64:
# - APIBARA_STARKNET_BIN_DIR: ns4qwsl6fgbv7mxhxpnaqhd66wnic8i6
# - APIBARA_STARKNET_BIN_DIR: 3l93dydg7m71r66x5wllf1j9czvj7zdk
# - APIBARA_SINK_BIN_DIR: 81f00xxzyxs6ih6376cw0qbznf6cl6bn
# platform: linux/arm64
# - APIBARA_STARKNET_BIN_DIR: 3fz40zh8pscx7wp54nb78wm6p5y2v3g0
# - APIBARA_STARKNET_BIN_DIR: ksmcmb1ybrij98hjy4q54v31ag7d2l6l
# - APIBARA_SINK_BIN_DIR: ww2ghdaw1xpx42cf93zk5n3h222438ia
ARG APIBARA_STARKNET_BIN_DIR
ARG APIBARA_SINK_BIN_DIR
# Run `docker image inspect apibara/starknet:1.5.0-x86_64` to get the exact path
# Run `docker image inspect apibara/starknet:1.5.0-aarch64` to get the exact path
# Run `docker image inspect apibara/starknet:1.6.0-x86_64` to get the exact path
# Run `docker image inspect apibara/starknet:1.6.0-aarch64` to get the exact path
# ⚠️ This path is subject to change, so it's important to check it before building the image ⚠️
COPY --from=apibara /nix/store/${APIBARA_STARKNET_BIN_DIR}-apibara-starknet-1.5.0/bin/apibara-starknet /usr/local/bin/starknet
COPY --from=apibara /nix/store/${APIBARA_STARKNET_BIN_DIR}-apibara-starknet-1.6.0/bin/apibara-starknet /usr/local/bin/starknet
COPY --from=indexer /nix/store/${APIBARA_SINK_BIN_DIR}-apibara-sink-mongo-0.7.1/bin/apibara-sink-mongo /usr/local/bin/sink-mongo
# Change the interpreter path.
ARG BUILDPLATFORM
Expand All @@ -50,17 +50,12 @@ FROM mongo:6.0.8 as mongo
# Ethereum RPC Server
### Build the Cairo programs artifacts
FROM python:3.10.13 as compiler
# install poetry
ENV POETRY_VERSION=1.7.1
ENV PATH="$PATH:/root/.local/bin:/root/.foundry/bin"
ENV PATH="$PATH:/root/.local/bin:/root/.foundry/bin:/root/.cargo/bin"

RUN curl -sSL https://install.python-poetry.org | python3 -
RUN poetry config virtualenvs.create false

WORKDIR /usr/src/compiler

# Install dependencies
RUN curl -L https://foundry.paradigm.xyz -o foundry.sh \
RUN --mount=type=cache,target=/root/.cache curl -L https://foundry.paradigm.xyz -o foundry.sh \
&& curl -LsSf https://astral.sh/uv/install.sh | sh \
&& chmod +x foundry.sh \
&& ./foundry.sh \
&& foundryup \
Expand All @@ -72,6 +67,8 @@ RUN curl -L https://foundry.paradigm.xyz -o foundry.sh \
unzip \
zip

WORKDIR /usr/src/compiler

COPY . .

# Similar to `make setup` but we remove the `make build-sol` step
Expand Down Expand Up @@ -122,8 +119,7 @@ COPY . .

COPY --from=compiler /usr/src/compiler/.kakarot/artifacts /usr/src/rpc/.kakarot/artifacts


RUN cargo build \
RUN --mount=type=cache,target=/root/.cache/cargo cargo build \
--features hive --release --target-dir /usr/src/rpc/target && \
cargo build \
--bin hive_genesis --release --features "testing,binaries" --target-dir /usr/src/rpc/target && \
Expand Down Expand Up @@ -158,6 +154,7 @@ ENV MAX_FELTS_IN_CALLDATA=30000
ENV MAX_LOGS=10000
ENV DEFAULT_BLOCK_GAS_LIMIT=7000000
ENV RELAYER_PRIVATE_KEY=0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a
ENV RELAYERS_ADDRESSES=0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca
ENV RUST_LOG=info

HEALTHCHECK --interval=10s --timeout=10s --start-period=15s --retries=5 \
Expand Down
4 changes: 2 additions & 2 deletions docker/rpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ COPY --from=planner /app/recipe.json recipe.json
COPY . .
# Install system dependencies
RUN apt-get update && \
apt-get -y upgrade && \
apt-get -y upgrade && \
apt-get install --no-install-recommends -y libclang-dev
RUN cargo build --release --bin kakarot-rpc
RUN --mount=type=cache,target=/root/.cache/cargo cargo build --release --bin kakarot-rpc

FROM debian:bookworm-slim AS runtime
WORKDIR /usr/src/app
Expand Down
14 changes: 3 additions & 11 deletions src/bin/hive_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
use alloy_rlp::Decodable;
use clap::Parser;
use kakarot_rpc::{
constants::STARKNET_CHAIN_ID,
into_via_try_wrapper,
providers::{
eth_provider::{constant::CHAIN_ID, starknet::relayer::LockedRelayer},
sn_provider::StarknetProvider,
},
providers::{eth_provider::starknet::relayer::LockedRelayer, sn_provider::StarknetProvider},
};
use reth_primitives::{bytes::Buf, Block, BlockBody, BytesMut};
use starknet::{
Expand Down Expand Up @@ -69,12 +67,6 @@ async fn main() -> eyre::Result<()> {
std::env::set_var("MAX_FELTS_IN_CALLDATA", MAX_FELTS_IN_CALLDATA);
std::env::set_var("STARKNET_NETWORK", STARKNET_RPC_URL);

// Set the chain id
let chain_id = starknet_provider.chain_id().await?;
let modulo = (1u64 << 53) - 1;
let chain_id_mod: u64 = (Felt::from(modulo).to_bigint() & chain_id.to_bigint()).try_into()?;
let _ = CHAIN_ID.get_or_init(|| Felt::from(chain_id_mod));

// Prepare the relayer
let relayer_balance = starknet_provider.balance_at(args.relayer_address, BlockId::Tag(BlockTag::Latest)).await?;
let relayer_balance = into_via_try_wrapper!(relayer_balance)?;
Expand All @@ -85,7 +77,7 @@ async fn main() -> eyre::Result<()> {
args.relayer_address,
relayer_balance,
JsonRpcClient::new(HttpTransport::new(Url::from_str(STARKNET_RPC_URL)?)),
chain_id,
*STARKNET_CHAIN_ID,
);

// Read the rlp file
Expand Down
25 changes: 11 additions & 14 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
constants::ETH_CHAIN_ID,
pool::{
mempool::{KakarotPool, TransactionOrdering},
validate::KakarotTransactionValidatorBuilder,
},
providers::{
eth_provider::{
database::Database,
error::{EthApiError, EthereumDataFormatError, KakarotError, SignatureError},
error::{EthApiError, EthereumDataFormatError, SignatureError},
provider::{EthApiResult, EthDataProvider},
TransactionProvider, TxPoolProvider,
},
Expand All @@ -15,7 +16,6 @@ use crate::{
};
use alloy_rlp::Decodable;
use async_trait::async_trait;
use num_traits::ToPrimitive;
use reth_chainspec::ChainSpec;
use reth_primitives::{Address, Bytes, TransactionSigned, TransactionSignedEcRecovered, B256};
use reth_rpc_eth_types::TransactionSource;
Expand All @@ -24,7 +24,7 @@ use reth_transaction_pool::{
blobstore::NoopBlobStore, AllPoolTransactions, EthPooledTransaction, PoolConfig, PoolTransaction,
TransactionOrigin, TransactionPool,
};
use starknet::{core::types::Felt, providers::Provider};
use starknet::providers::Provider;
use std::{collections::BTreeMap, sync::Arc};

#[async_trait]
Expand Down Expand Up @@ -57,18 +57,15 @@ where
}

/// Tries to start a [`EthClient`] by fetching the current chain id, initializing a [`EthDataProvider`] and a [`Pool`].
pub async fn try_new(starknet_provider: SP, pool_config: PoolConfig, database: Database) -> eyre::Result<Self> {
// We take the chain id modulo 2**53 to keep compatibility with the tooling.
let modulo = (1u64 << 53) - 1;
let starknet_chain_id = starknet_provider.chain_id().await.map_err(KakarotError::from)?;
let chain = (starknet_chain_id.to_bigint() & Felt::from(modulo).to_bigint()).to_u64().unwrap();

pub fn new(starknet_provider: SP, pool_config: PoolConfig, database: Database) -> Self {
// Create a new EthDataProvider instance with the initialized database and Starknet provider.
let eth_provider = EthDataProvider::try_new(database, StarknetProvider::new(starknet_provider)).await?;
let eth_provider = EthDataProvider::new(database, StarknetProvider::new(starknet_provider));

let validator =
KakarotTransactionValidatorBuilder::new(&Arc::new(ChainSpec { chain: chain.into(), ..Default::default() }))
.build::<_, EthPooledTransaction>(eth_provider.clone());
let validator = KakarotTransactionValidatorBuilder::new(&Arc::new(ChainSpec {
chain: (*ETH_CHAIN_ID).into(),
..Default::default()
}))
.build::<_, EthPooledTransaction>(eth_provider.clone());

let pool = Arc::new(KakarotPool::new(
validator,
Expand All @@ -77,7 +74,7 @@ where
pool_config,
));

Ok(Self { eth_provider, pool })
Self { eth_provider, pool }
}

/// Returns a clone of the [`EthDataProvider`]
Expand Down
31 changes: 31 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{config::KakarotRpcConfig, eth_rpc::config::RPCConfig};
use num_traits::ToPrimitive;
use starknet::{
core::types::{Felt, NonZeroFelt},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider},
};
use std::sync::LazyLock;

/// The max chain id allowed by [Metamask](https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553)
pub static MAX_CHAIN_ID: u64 = (2u64.pow(53) - 39) / 2;
/// The chain id of the underlying Starknet chain.
pub static STARKNET_CHAIN_ID: LazyLock<Felt> = LazyLock::new(|| {
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let provider = JsonRpcClient::new(HttpTransport::new(KAKAROT_RPC_CONFIG.network_url.clone()));
provider.chain_id().await.expect("failed to get chain for chain")
})
})
});
/// The chain id for the Ethereum chain running on the Starknet chain.
pub static ETH_CHAIN_ID: LazyLock<u64> = LazyLock::new(|| {
STARKNET_CHAIN_ID.div_rem(&NonZeroFelt::from_felt_unchecked(Felt::from(MAX_CHAIN_ID))).1.to_u64().expect("modulo")
});

/// The Kakarot RPC configuration.
pub static KAKAROT_RPC_CONFIG: LazyLock<KakarotRpcConfig> =
LazyLock::new(|| KakarotRpcConfig::from_env().expect("failed to load Kakarot RPC config"));

/// The RPC configuration.
pub static RPC_CONFIG: LazyLock<RPCConfig> =
LazyLock::new(|| RPCConfig::from_env().expect("failed to load RPC config"));
Loading

0 comments on commit a6dff03

Please sign in to comment.