Skip to content

Commit

Permalink
gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron committed Sep 17, 2024
1 parent bccdccf commit bbbf378
Show file tree
Hide file tree
Showing 49 changed files with 2,256 additions and 87 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
workflow_dispatch:
pull_request:
branches: [main]
types: [opened, synchronize, reopened, ready_for_review]

concurrency:
group: pr-checks-${{ github.workflow }}-${{ github.head_ref || github.run_id }}
Expand All @@ -16,24 +17,29 @@ permissions:
jobs:
changelog:
name: Enforce CHANGELOG
if: github.event.pull_request.draft == false
uses: ./.github/workflows/changelog.yml

linters:
name: Run linters
if: github.event.pull_request.draft == false
uses: ./.github/workflows/linters.yml
needs: changelog

rust_check:
name: Run check
if: github.event.pull_request.draft == false
uses: ./.github/workflows/rust-check.yml
needs: changelog

linters_cargo:
name: Run Cargo linters
if: github.event.pull_request.draft == false
uses: ./.github/workflows/linters-cargo.yml
needs: rust_check

coverage:
name: Run Coverage
if: github.event.pull_request.draft == false
uses: ./.github/workflows/coverage.yml
needs: changelog
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- feat: new crate gateway client & server
- refactor: change default chain id and add custom flag to override
- fix: generate a fixed set of public and private keys for devnet
- fix: defaulted l1 gas price in devnet mode
Expand Down
81 changes: 77 additions & 4 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/client/sync",
"crates/client/eth",
"crates/client/rpc",
"crates/client/gateway",
"crates/client/telemetry",
"crates/client/metrics",
"crates/client/devnet",
Expand All @@ -15,6 +16,7 @@ members = [
"crates/primitives/convert",
"crates/primitives/transactions",
"crates/primitives/class",
"crates/primitives/gateway",
"crates/primitives/receipt",
"crates/primitives/state_update",
"crates/primitives/chain_config",
Expand All @@ -28,6 +30,7 @@ default-members = [
"crates/client/exec",
"crates/client/sync",
"crates/client/eth",
"crates/client/gateway",
"crates/client/rpc",
"crates/client/telemetry",
"crates/client/metrics",
Expand All @@ -39,6 +42,7 @@ default-members = [
"crates/primitives/convert",
"crates/primitives/transactions",
"crates/primitives/class",
"crates/primitives/gateway",
"crates/primitives/receipt",
"crates/primitives/state_update",
"crates/primitives/chain_config",
Expand Down Expand Up @@ -88,6 +92,7 @@ mp-block = { path = "crates/primitives/block", default-features = false }
mp-convert = { path = "crates/primitives/convert", default-features = false }
mp-transactions = { path = "crates/primitives/transactions", default-features = false }
mp-class = { path = "crates/primitives/class", default-features = false }
mp-gateway = { path = "crates/primitives/gateway", default-features = false }
mp-receipt = { path = "crates/primitives/receipt", default-features = false }
mp-state-update = { path = "crates/primitives/state_update", default-features = false }
mp-utils = { path = "crates/primitives/utils", default-features = false }
Expand All @@ -98,6 +103,7 @@ mc-telemetry = { path = "crates/client/telemetry" }
mc-db = { path = "crates/client/db" }
mc-exec = { path = "crates/client/exec" }
mc-rpc = { path = "crates/client/rpc" }
mc-gateway = { path = "crates/client/gateway" }
mc-sync = { path = "crates/client/sync" }
mc-eth = { path = "crates/client/eth" }
mc-metrics = { path = "crates/client/metrics" }
Expand Down Expand Up @@ -158,6 +164,7 @@ rand = "0.8"
reqwest = { version = "0.12", features = ["json"] }
rstest = "0.18"
serde = { version = "1.0", default-features = false, features = ["std"] }
serde_with = "3.9"
serde_json = { version = "1.0", default-features = false, features = ["std"] }
thiserror = "1.0"
tokio = { version = "1.34", features = ["signal"] }
Expand Down
6 changes: 3 additions & 3 deletions crates/client/block_import/src/verify_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ fn block_hash(
transaction_commitment,
event_count,
event_commitment,
state_diff_length,
state_diff_commitment,
receipt_commitment,
state_diff_length: Some(state_diff_length),
state_diff_commitment: Some(state_diff_commitment),
receipt_commitment: Some(receipt_commitment),
protocol_version,
l1_gas_price,
l1_da_mode,
Expand Down
25 changes: 16 additions & 9 deletions crates/client/db/src/class_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ impl MadaraBackend {
Ok(Some(info.class_info))
}

pub fn contains_class(&self, id: &impl DbBlockIdResolvable, class_hash: &Felt) -> Result<bool, MadaraStorageError> {
// TODO(perf): make fast path, this only needs one db contains() call and no deserialization in most cases (block id pending/latest)
Ok(self.get_class_info(id, class_hash)?.is_some())
pub fn contains_class(&self, class_hash: &Felt) -> Result<bool, MadaraStorageError> {
let col = self.db.get_column(Column::ClassInfo);
let key_encoded = bincode::serialize(class_hash)?;
Ok(self.db.get_pinned_cf(&col, &key_encoded)?.is_some())
}

pub fn get_sierra_compiled(
Expand Down Expand Up @@ -123,12 +124,18 @@ impl MadaraBackend {
for converted_class in chunk {
let class_hash = converted_class.class_hash();
let key_bin = bincode::serialize(&class_hash)?;
// TODO: find a way to avoid this allocation
batch.put_cf(
col,
&key_bin,
bincode::serialize(&ClassInfoWithBlockNumber { class_info: converted_class.info(), block_id })?,
);
// this is a patch because some legacy classes are declared multiple times
if !self.contains_class(&class_hash)? {
// TODO: find a way to avoid this allocation
batch.put_cf(
col,
&key_bin,
bincode::serialize(&ClassInfoWithBlockNumber {
class_info: converted_class.info(),
block_id,
})?,
);
}
}
self.db.write_opt(batch, &writeopts)?;
Ok::<_, MadaraStorageError>(())
Expand Down
40 changes: 40 additions & 0 deletions crates/client/gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
description = "Madara client rpc service"
name = "mc-gateway"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true
homepage.workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]

# Madara
mc-db = { workspace = true }
mc-rpc = { workspace = true }
mp-block = { workspace = true }
mp-class = { workspace = true }
mp-gateway = { workspace = true }
mp-utils = { workspace = true }

# Starknet
starknet-core = { workspace = true }
starknet-types-core = { workspace = true }

# Other
anyhow = { workspace = true }
hyper = { workspace = true }
log = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
url = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }
45 changes: 45 additions & 0 deletions crates/client/gateway/src/client/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::collections::HashMap;

use reqwest::Client;
use url::Url;

#[derive(Debug, Clone)]
pub struct FeederClient {
pub(crate) client: Client,
pub(crate) gateway_url: Url,
pub(crate) feeder_gateway_url: Url,
pub(crate) headers: HashMap<String, String>,
}

impl FeederClient {
pub fn new(gateway_url: Url, feeder_gateway_url: Url) -> Self {
Self { client: Client::new(), gateway_url, feeder_gateway_url, headers: HashMap::new() }
}

pub fn new_with_headers(gateway_url: Url, feeder_gateway_url: Url, headers: &[(String, String)]) -> Self {
let headers = headers.iter().cloned().collect();
Self { client: Client::new(), gateway_url, feeder_gateway_url, headers }
}

pub fn add_header(&mut self, key: &str, value: &str) {
self.headers.insert(key.to_string(), value.to_string());
}

pub fn remove_header(&mut self, key: &str) -> Option<String> {
self.headers.remove(key)
}

pub fn starknet_alpha_mainnet() -> Self {
Self::new(
Url::parse("https://alpha-mainnet.starknet.io/gateway/").unwrap(),
Url::parse("https://alpha-mainnet.starknet.io/feeder_gateway/").unwrap(),
)
}

pub fn starknet_alpha_sepolia() -> Self {
Self::new(
Url::parse("https://alpha-sepolia.starknet.io/gateway/").unwrap(),
Url::parse("https://alpha-sepolia.starknet.io/feeder_gateway/").unwrap(),
)
}
}
Loading

0 comments on commit bbbf378

Please sign in to comment.