Skip to content

Commit

Permalink
add add_transaction endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron committed Sep 19, 2024
1 parent 36831f0 commit 4eb62dc
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions crates/client/gateway/src/client/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ mod tests {
async fn test_get_state_update() {
let client = FeederClient::starknet_alpha_mainnet();

let block = client.get_state_update(BlockId::Number(0)).await.unwrap();
let block = client
let _block = client.get_state_update(BlockId::Number(0)).await.unwrap();
let _block = client
.get_state_update(BlockId::Hash(Felt::from_hex_unchecked(
"0x47c3637b57c2b079b93c61539950c17e868a28f46cdef28f88521067f21e943",
)))
.await
.unwrap();
let block = client.get_state_update(BlockId::Tag(BlockTag::Latest)).await.unwrap();
let block = client.get_state_update(BlockId::Tag(BlockTag::Pending)).await.unwrap();
let _block = client.get_state_update(BlockId::Tag(BlockTag::Latest)).await.unwrap();
let _block = client.get_state_update(BlockId::Tag(BlockTag::Pending)).await.unwrap();
}
}
75 changes: 60 additions & 15 deletions crates/client/gateway/src/server/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use hyper::{Body, Request, Response};
use hyper::{body, Body, Request, Response};
use mc_db::MadaraBackend;
use mc_rpc::providers::AddTransactionProvider;
use mp_block::{BlockId, BlockTag, MadaraBlock, MadaraPendingBlock};
Expand All @@ -10,7 +10,10 @@ use mp_gateway::{
state_update::{PendingStateUpdateProvider, StateUpdateProvider},
};
use serde_json::json;
use starknet_core::types::BroadcastedTransaction;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
BroadcastedTransaction,
};
use starknet_types_core::felt::Felt;

use crate::error::{StarknetError, StarknetErrorCode};
Expand All @@ -19,7 +22,7 @@ use super::{
error::{GatewayError, ResultExt},
helpers::{
block_id_from_params, create_json_response, create_response_with_json_body, get_params_from_request,
include_block_params, not_implemented_response,
include_block_params,
},
};

Expand Down Expand Up @@ -245,18 +248,60 @@ pub async fn handle_get_class_by_hash(req: Request<Body>, backend: Arc<MadaraBac

pub async fn handle_add_transaction(
req: Request<Body>,
backend: Arc<MadaraBackend>,
add_transaction_provider: Arc<dyn AddTransactionProvider>,
) -> Response<Body> {
// let transaction = match serde_json::from_slice::<BroadcastedTransaction>(req.body())) {
// Ok(transaction) => transaction,
// Err(e) => {
// return GatewayError::StarknetError(StarknetError {
// code: StarknetErrorCode::MalformedRequest,
// message: format!("Failed to parse transaction: {}", e),
// })
// .into()
// }
// };
not_implemented_response()
let whole_body = match body::to_bytes(req.into_body()).await {
Ok(body) => body,
Err(e) => {
log::error!("Failed to read request body: {}", e);
return GatewayError::InternalServerError.into();
}
};

let transaction = match serde_json::from_slice::<BroadcastedTransaction>(whole_body.as_ref()) {
Ok(transaction) => transaction,
Err(e) => {
return GatewayError::StarknetError(StarknetError {
code: StarknetErrorCode::MalformedRequest,
message: format!("Failed to parse transaction: {}", e),
})
.into()
}
};

match transaction {
BroadcastedTransaction::Declare(tx) => declare_transaction(tx, add_transaction_provider).await,
BroadcastedTransaction::DeployAccount(tx) => deploy_account_transaction(tx, add_transaction_provider).await,
BroadcastedTransaction::Invoke(tx) => invoke_transaction(tx, add_transaction_provider).await,
}
}

async fn declare_transaction(
tx: BroadcastedDeclareTransaction,
add_transaction_provider: Arc<dyn AddTransactionProvider>,
) -> Response<Body> {
match add_transaction_provider.add_declare_transaction(tx).await {
Ok(result) => create_json_response(hyper::StatusCode::OK, &result),
Err(e) => create_json_response(hyper::StatusCode::OK, &e),
}
}

async fn deploy_account_transaction(
tx: BroadcastedDeployAccountTransaction,
add_transaction_provider: Arc<dyn AddTransactionProvider>,
) -> Response<Body> {
match add_transaction_provider.add_deploy_account_transaction(tx).await {
Ok(result) => create_json_response(hyper::StatusCode::OK, &result),
Err(e) => create_json_response(hyper::StatusCode::OK, &e),
}
}

async fn invoke_transaction(
tx: BroadcastedInvokeTransaction,
add_transaction_provider: Arc<dyn AddTransactionProvider>,
) -> Response<Body> {
match add_transaction_provider.add_invoke_transaction(tx).await {
Ok(result) => create_json_response(hyper::StatusCode::OK, &result),
Err(e) => create_json_response(hyper::StatusCode::OK, &e),
}
}
7 changes: 2 additions & 5 deletions crates/client/gateway/src/server/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) async fn main_router(
match (req.uri().path(), feeder_gateway_enable, gateway_enable) {
("/health", _, _) => Ok(Response::new(Body::from("OK"))),
(path, true, _) if path.starts_with("/feeder_gateway/") => feeder_gateway_router(req, backend).await,
(path, _, true) if path.starts_with("/feeder/") => gateway_router(req, backend, add_transaction_provider).await,
(path, _, true) if path.starts_with("/feeder/") => gateway_router(req, add_transaction_provider).await,
(path, false, _) if path.starts_with("/feeder_gateway/") => Ok(service_unavailable_response("Feeder Gateway")),
(path, _, false) if path.starts_with("/feeder/") => Ok(service_unavailable_response("Feeder")),
_ => Ok(not_found_response()),
Expand All @@ -38,13 +38,10 @@ async fn feeder_gateway_router(req: Request<Body>, backend: Arc<MadaraBackend>)
// Router for requests related to feeder
async fn gateway_router(
req: Request<Body>,
backend: Arc<MadaraBackend>,
add_transaction_provider: Arc<dyn AddTransactionProvider>,
) -> Result<Response<Body>, Infallible> {
match (req.method(), req.uri().path()) {
(&Method::POST, "/feeder/add_transaction") => {
Ok(handle_add_transaction(req, backend, add_transaction_provider).await)
}
(&Method::POST, "/feeder/add_transaction") => Ok(handle_add_transaction(req, add_transaction_provider).await),
_ => Ok(not_found_response()),
}
}
8 changes: 2 additions & 6 deletions crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ use std::sync::Arc;

use anyhow::Context;
use clap::Parser;
use cli::{NetworkType, RunCmd};
use mc_block_import::BlockImporter;

use mc_db::DatabaseService;
use mc_mempool::{GasPriceProvider, L1DataProvider, Mempool};
use mc_metrics::MetricsService;
use mc_rpc::providers::{AddTransactionProvider, ForwardToProvider, MempoolAddTxProvider};
use mc_telemetry::{SysInfo, TelemetryService};
use mp_convert::ToFelt;
use mp_utils::service::{Service, ServiceGroup};
use service::{BlockProductionService, GatewayService, RpcService, SyncService};
use service::{BlockProductionService, GatewayService, L1SyncService, RpcService, SyncService};
use starknet_providers::SequencerGatewayProvider;

use cli::{NetworkType, RunCmd};
use service::L1SyncService;
use service::{BlockProductionService, RpcService, SyncService};

const GREET_IMPL_NAME: &str = "Madara";
const GREET_SUPPORT_URL: &str = "https://github.com/madara-alliance/madara/issues";

Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ targets = ["x86_64-unknown-linux-gnu"]
# Deoxys
mp-block = { workspace = true }
mp-chain-config = { workspace = true }
mp-convert = { workspace = true }
mp-class = { workspace = true }
mp-convert = { workspace = true }
mp-receipt = { workspace = true }
mp-state-update = { workspace = true }
mp-transactions = { workspace = true }

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

# Other
Expand Down
Loading

0 comments on commit 4eb62dc

Please sign in to comment.