1use std::{
6 any::Any,
7 convert::Infallible,
8 net::{SocketAddr, TcpStream},
9 sync::Arc,
10 time::{Duration, Instant},
11};
12
13use async_graphql::{
14 Context, Data, Schema, SchemaBuilder,
15 extensions::{ApolloTracing, ExtensionFactory, Tracing},
16 http::ALL_WEBSOCKET_PROTOCOLS,
17};
18use async_graphql_axum::{GraphQLProtocol, GraphQLRequest, GraphQLResponse, GraphQLWebSocket};
19use axum::{
20 Extension, Router,
21 body::Body,
22 extract::{
23 ConnectInfo, FromRef, FromRequestParts, Query as AxumQuery, State, ws::WebSocketUpgrade,
24 },
25 http::{HeaderMap, StatusCode},
26 middleware::{self},
27 response::{IntoResponse, Response as AxumResponse},
28 routing::{MethodRouter, Route, get, post},
29};
30use axum_extra::{TypedHeader, headers::ContentLength};
31use chrono::Utc;
32use http::{HeaderValue, Method, Request};
33use iota_graphql_rpc_headers::LIMITS_HEADER;
34use iota_grpc_client::GrpcClient;
35use iota_indexer::{
36 apis::{OptimisticWriteApi, ReadApi, WriteApi},
37 db::{
38 ConnectionPoolConfig, get_pool_connection, new_connection_pool,
39 setup_postgres::check_db_migration_consistency,
40 },
41 metrics::IndexerMetrics,
42 optimistic_indexing::OptimisticTransactionExecutor,
43 read::IndexerReader,
44 store::PgIndexerStore,
45};
46use iota_metrics::spawn_monitored_task;
47use iota_network_stack::callback::{CallbackLayer, MakeCallbackHandler, ResponseHandler};
48use iota_package_resolver::{PackageStoreWithLruCache, Resolver};
49use tokio::{join, net::TcpListener, sync::OnceCell};
50use tokio_util::sync::CancellationToken;
51use tower::{Layer, Service};
52use tower_http::cors::{AllowOrigin, CorsLayer};
53use tracing::info;
54use uuid::Uuid;
55
56use crate::{
57 config::{ConnectionConfig, ServerConfig, ServiceConfig, Version},
58 context_data::db_data_provider::PgManager,
59 data::{
60 DataLoader, Db,
61 package_resolver::{DbPackageStore, PackageResolver},
62 },
63 error::Error,
64 extensions::{
65 directive_checker::DirectiveChecker,
66 feature_gate::FeatureGate,
67 logger::Logger,
68 query_limits_checker::{PayloadSize, QueryLimitsChecker, ShowUsage},
69 timeout::Timeout,
70 },
71 metrics::Metrics,
72 mutation::Mutation,
73 server::{
74 exchange_rates_task::TriggerExchangeRatesTask,
75 system_package_task::SystemPackageTask,
76 version::{check_version_middleware, set_version_middleware},
77 watermark_task::{Watermark, WatermarkLock, WatermarkTask},
78 },
79 types::{
80 chain_identifier::ChainIdentifierCache,
81 datatype::IMoveDatatype,
82 move_object::IMoveObject,
83 object::IObject,
84 owner::IOwner,
85 query::{IotaGraphQLSchema, Query},
86 subscription::{GraphQLStream, Subscription},
87 },
88};
89
90const DEFAULT_MAX_CHECKPOINT_LAG: Duration = Duration::from_secs(300);
93const MAX_COMPLEXITY: usize = 6000;
95const SUBSCRIPTION_POOL_DIVISOR: u32 = 2;
98
99pub(crate) struct Server {
100 router: Router,
101 address: SocketAddr,
102 watermark_task: WatermarkTask,
103 system_package_task: SystemPackageTask,
104 trigger_exchange_rates_task: TriggerExchangeRatesTask,
105 state: AppState,
106}
107
108impl Server {
109 pub async fn run(mut self) -> Result<(), Error> {
113 get_or_init_server_start_time().await;
114
115 let watermark_task = {
119 info!("Starting watermark update task");
120 spawn_monitored_task!(async move {
121 self.watermark_task.run().await;
122 })
123 };
124
125 let system_package_task = {
128 info!("Starting system package task");
129 spawn_monitored_task!(async move {
130 self.system_package_task.run().await;
131 })
132 };
133
134 let trigger_exchange_rates_task = {
135 info!("Starting trigger exchange rates task");
136 spawn_monitored_task!(async move {
137 self.trigger_exchange_rates_task.run().await;
138 })
139 };
140
141 let server_task = {
142 info!("Starting graphql service");
143 let cancellation_token = self.state.cancellation_token.clone();
144 let address = self.address;
145 let router = self.router;
146 spawn_monitored_task!(async move {
147 axum::serve(
148 TcpListener::bind(address)
149 .await
150 .map_err(|e| Error::Internal(format!("listener bind failed: {e}")))?,
151 router.into_make_service_with_connect_info::<SocketAddr>(),
152 )
153 .with_graceful_shutdown(async move {
154 cancellation_token.cancelled().await;
155 info!("Shutdown signal received, terminating graphql service");
156 })
157 .await
158 .map_err(|e| Error::Internal(format!("Server run failed: {e}")))
159 })
160 };
161
162 let _ = join!(
166 watermark_task,
167 system_package_task,
168 trigger_exchange_rates_task,
169 server_task
170 );
171
172 Ok(())
173 }
174}
175
176pub(crate) struct ServerBuilder {
177 state: AppState,
178 schema: SchemaBuilder<Query, Mutation, Subscription>,
179 router: Option<Router>,
180 db_reader: Option<Db>,
181 resolver: Option<PackageResolver>,
182}
183
184#[derive(Clone)]
185pub(crate) struct AppState {
186 connection: ConnectionConfig,
187 service: ServiceConfig,
188 metrics: Metrics,
189 cancellation_token: CancellationToken,
190 pub version: Version,
191}
192
193impl AppState {
194 pub(crate) fn new(
195 connection: ConnectionConfig,
196 service: ServiceConfig,
197 metrics: Metrics,
198 cancellation_token: CancellationToken,
199 version: Version,
200 ) -> Self {
201 Self {
202 connection,
203 service,
204 metrics,
205 cancellation_token,
206 version,
207 }
208 }
209}
210
211impl FromRef<AppState> for ConnectionConfig {
212 fn from_ref(app_state: &AppState) -> ConnectionConfig {
213 app_state.connection.clone()
214 }
215}
216
217impl FromRef<AppState> for Metrics {
218 fn from_ref(app_state: &AppState) -> Metrics {
219 app_state.metrics.clone()
220 }
221}
222
223impl ServerBuilder {
224 pub fn new(state: AppState) -> Self {
225 Self {
226 state,
227 schema: schema_builder(),
228 router: None,
229 db_reader: None,
230 resolver: None,
231 }
232 }
233
234 pub fn address(&self) -> String {
235 format!(
236 "{}:{}",
237 self.state.connection.host, self.state.connection.port
238 )
239 }
240
241 pub fn context_data(mut self, context_data: impl Any + Send + Sync) -> Self {
242 self.schema = self.schema.data(context_data);
243 self
244 }
245
246 pub fn extension(mut self, extension: impl ExtensionFactory) -> Self {
247 self.schema = self.schema.extension(extension);
248 self
249 }
250
251 fn build_schema(self) -> Schema<Query, Mutation, Subscription> {
252 self.schema.finish()
253 }
254
255 fn build_components(
258 self,
259 ) -> (
260 String,
261 Schema<Query, Mutation, Subscription>,
262 Db,
263 PackageResolver,
264 Router,
265 ) {
266 let address = self.address();
267 let ServerBuilder {
268 state: _,
269 schema,
270 db_reader,
271 resolver,
272 router,
273 } = self;
274 (
275 address,
276 schema.finish(),
277 db_reader.expect("db reader not initialized"),
278 resolver.expect("package resolver not initialized"),
279 router.expect("router not initialized"),
280 )
281 }
282
283 fn init_router(&mut self) {
284 if self.router.is_none() {
285 let router: Router = Router::new()
286 .route("/", post(graphql_handler))
287 .route("/subscriptions", get(subscription_handler))
288 .route("/{version}", post(graphql_handler))
289 .route("/{version}/subscriptions", get(subscription_handler))
290 .route("/graphql", post(graphql_handler))
291 .route("/graphql/subscriptions", get(subscription_handler))
292 .route("/graphql/{version}", post(graphql_handler))
293 .route(
294 "/graphql/{version}/subscriptions",
295 get(subscription_handler),
296 )
297 .route("/health", get(health_check))
298 .route("/graphql/health", get(health_check))
299 .route("/graphql/{version}/health", get(health_check))
300 .with_state(self.state.clone())
301 .route_layer(CallbackLayer::new(MetricsMakeCallbackHandler {
302 metrics: self.state.metrics.clone(),
303 }));
304 self.router = Some(router);
305 }
306 }
307
308 pub fn route(mut self, path: &str, method_handler: MethodRouter) -> Self {
309 self.init_router();
310 self.router = self.router.map(|router| router.route(path, method_handler));
311 self
312 }
313
314 pub fn layer<L>(mut self, layer: L) -> Self
315 where
316 L: Layer<Route> + Clone + Send + Sync + 'static,
317 L::Service: Service<Request<Body>> + Clone + Send + Sync + 'static,
318 <L::Service as Service<Request<Body>>>::Response: IntoResponse + 'static,
319 <L::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static,
320 <L::Service as Service<Request<Body>>>::Future: Send + 'static,
321 {
322 self.init_router();
323 self.router = self.router.map(|router| router.layer(layer));
324 self
325 }
326
327 fn cors() -> Result<CorsLayer, Error> {
328 let acl = match std::env::var("ACCESS_CONTROL_ALLOW_ORIGIN") {
329 Ok(value) => {
330 let allow_hosts = value
331 .split(',')
332 .map(HeaderValue::from_str)
333 .collect::<Result<Vec<_>, _>>()
334 .map_err(|_| {
335 Error::Internal(
336 "Cannot resolve access control origin env variable".to_string(),
337 )
338 })?;
339 AllowOrigin::list(allow_hosts)
340 }
341 _ => AllowOrigin::any(),
342 };
343 info!("Access control allow origin set to: {acl:?}");
344
345 let cors = CorsLayer::new()
346 .allow_methods([Method::POST, Method::GET])
348 .allow_origin(acl)
350 .allow_headers([hyper::header::CONTENT_TYPE, LIMITS_HEADER.clone()]);
351 Ok(cors)
352 }
353
354 pub fn build(self) -> Result<Server, Error> {
356 let state = self.state.clone();
357 let (address, schema, db_reader, resolver, router) = self.build_components();
358
359 let watermark_task = WatermarkTask::new(
361 db_reader.clone(),
362 state.metrics.clone(),
363 std::time::Duration::from_millis(state.service.background_tasks.watermark_update_ms),
364 state.cancellation_token.clone(),
365 );
366
367 let system_package_task = SystemPackageTask::new(
368 resolver,
369 watermark_task.epoch_receiver(),
370 state.cancellation_token.clone(),
371 );
372
373 let trigger_exchange_rates_task = TriggerExchangeRatesTask::new(
374 db_reader,
375 watermark_task.epoch_receiver(),
376 state.cancellation_token.clone(),
377 );
378
379 let router = router
380 .route_layer(middleware::from_fn_with_state(
381 state.version,
382 set_version_middleware,
383 ))
384 .route_layer(middleware::from_fn_with_state(
385 state.version,
386 check_version_middleware,
387 ))
388 .layer(axum::extract::Extension(schema))
389 .layer(axum::extract::Extension(watermark_task.lock()))
390 .layer(Self::cors()?);
391
392 Ok(Server {
393 router,
394 address: address
395 .parse()
396 .map_err(|_| Error::Internal(format!("Failed to parse address {address}")))?,
397 watermark_task,
398 system_package_task,
399 trigger_exchange_rates_task,
400 state,
401 })
402 }
403
404 pub async fn from_config(
407 config: &ServerConfig,
408 version: &Version,
409 cancellation_token: CancellationToken,
410 ) -> Result<Self, Error> {
411 let prom_addr: SocketAddr = format!(
413 "{}:{}",
414 config.connection.prom_host, config.connection.prom_port
415 )
416 .parse()
417 .map_err(|_| {
418 Error::Internal(format!(
419 "Failed to parse url {}, port {} into socket address",
420 config.connection.prom_host, config.connection.prom_port
421 ))
422 })?;
423
424 let registry_service = iota_metrics::start_prometheus_server(prom_addr);
425 info!("Starting Prometheus HTTP endpoint at {}", prom_addr);
426 let registry = registry_service.default_registry();
427 registry
428 .register(iota_metrics::uptime_metric(
429 "graphql",
430 version.full,
431 "unknown",
432 ))
433 .unwrap();
434
435 let metrics = Metrics::new(®istry);
437 let indexer_metrics = IndexerMetrics::new(®istry);
438 let state = AppState::new(
439 config.connection.clone(),
440 config.service.clone(),
441 metrics.clone(),
442 cancellation_token.clone(),
443 *version,
444 );
445 let mut builder = ServerBuilder::new(state);
446
447 let iota_names_config = config.service.iota_names.clone();
448 let reader = PgManager::reader_with_config(
449 &config.connection.db_url,
450 config.connection.db_pool_size,
451 config.service.limits.request_timeout_ms.into(),
455 indexer_metrics.clone(),
456 cancellation_token,
457 &config.historic_fallback,
458 ®istry,
459 )
460 .map_err(|e| Error::ServerInit(format!("Failed to initialize indexer reader: {e}")))?;
461
462 if !config.connection.skip_migration_consistency_check {
463 info!("Starting compatibility check");
465 let mut connection = get_pool_connection(&reader.get_pool())?;
466 check_db_migration_consistency(&mut connection)?;
467 info!("Compatibility check passed");
468 }
469
470 let db = Db::new(
472 reader.clone(),
473 config.service.limits.clone(),
474 metrics.clone(),
475 config.connection.max_available_range,
476 );
477 let loader = DataLoader::new(db.clone());
478 let pg_conn_pool = PgManager::new(reader.clone());
479 let package_store = DbPackageStore::new(loader.clone());
480 let resolver = Arc::new(Resolver::new_with_limits(
481 PackageStoreWithLruCache::new(package_store),
482 config.service.limits.package_resolver_limits(),
483 ));
484
485 builder.db_reader = Some(db.clone());
486 builder.resolver = Some(resolver.clone());
487
488 let Some(fullnode_url) = config.tx_exec_full_node.node_rpc_url.as_ref() else {
489 return Err(Error::ServerInit(
490 "No fullnode url found in config".to_string(),
491 ));
492 };
493
494 let mut subscription_pool_config = ConnectionPoolConfig::default();
496 let pool_size = (config.connection.db_pool_size / SUBSCRIPTION_POOL_DIVISOR).max(2);
498 subscription_pool_config.set_pool_size(pool_size);
499 subscription_pool_config.set_statement_timeout(Duration::from_millis(
500 config.service.limits.request_timeout_ms.into(),
501 ));
502 let subscription_pool =
503 new_connection_pool(&config.connection.db_url, &subscription_pool_config).map_err(
504 |e| {
505 Error::ServerInit(format!(
506 "failed to create subscription connection pool: {e}"
507 ))
508 },
509 )?;
510 let subscription_reader = IndexerReader::new_without_watermark_cache(subscription_pool);
511 let graphql_streams =
512 GraphQLStream::new(subscription_reader, ®istry, &config.service.limits).await?;
513
514 let fullnode_grpc_client =
515 GrpcClient::new(fullnode_url).map_err(|e| Error::ServerInit(e.to_string()))?;
516
517 let read_api = ReadApi::new(reader.clone(), fullnode_grpc_client.clone());
518 let write_api = build_write_api(fullnode_grpc_client, reader, indexer_metrics).await?;
519
520 builder = builder
521 .context_data(config.service.clone())
522 .context_data(loader)
523 .context_data(db)
524 .context_data(pg_conn_pool)
525 .context_data(resolver)
526 .context_data(write_api)
527 .context_data(read_api)
528 .context_data(iota_names_config)
529 .context_data(metrics.clone())
530 .context_data(config.clone())
531 .context_data(graphql_streams)
532 .context_data(ChainIdentifierCache::default());
533
534 if config.internal_features.feature_gate {
535 builder = builder.extension(FeatureGate);
536 }
537
538 if config.internal_features.logger {
539 builder = builder.extension(Logger::default());
540 }
541
542 if config.internal_features.query_limits_checker {
543 builder = builder.extension(QueryLimitsChecker);
544 }
545
546 if config.internal_features.directive_checker {
547 builder = builder.extension(DirectiveChecker);
548 }
549
550 if config.internal_features.query_timeout {
551 builder = builder.extension(Timeout);
552 }
553
554 if config.internal_features.tracing {
555 builder = builder.extension(Tracing);
556 }
557
558 if config.internal_features.apollo_tracing {
559 builder = builder.extension(ApolloTracing);
560 }
561
562 Ok(builder)
566 }
567}
568
569fn schema_builder() -> SchemaBuilder<Query, Mutation, Subscription> {
570 async_graphql::Schema::build(Query, Mutation, Subscription)
571 .limit_complexity(MAX_COMPLEXITY)
572 .register_output_type::<IMoveObject>()
573 .register_output_type::<IObject>()
574 .register_output_type::<IOwner>()
575 .register_output_type::<IMoveDatatype>()
576}
577
578pub fn export_schema() -> String {
580 schema_builder().finish().sdl()
581}
582
583async fn graphql_handler(
588 ConnectInfo(addr): ConnectInfo<SocketAddr>,
589 TypedHeader(ContentLength(content_length)): TypedHeader<ContentLength>,
590 schema: Extension<IotaGraphQLSchema>,
591 Extension(watermark_lock): Extension<WatermarkLock>,
592 headers: HeaderMap,
593 req: GraphQLRequest,
594) -> (axum::http::Extensions, GraphQLResponse) {
595 let mut req = req.into_inner();
596 req.data.insert(PayloadSize(content_length));
597 req.data.insert(Uuid::new_v4());
598 if headers.contains_key(ShowUsage::name()) {
599 req.data.insert(ShowUsage)
600 }
601
602 req.data.insert(addr);
606 req.data.insert(Watermark::new(watermark_lock).await);
607
608 let result = schema.execute(req).await;
609
610 let mut extensions = axum::http::Extensions::new();
613 if result.is_err() {
614 extensions.insert(GraphqlErrors(std::sync::Arc::new(result.errors.clone())));
615 };
616 (extensions, result.into())
617}
618
619pub(crate) fn get_write_api<'ctx>(
620 ctx: &'ctx Context<'_>,
621) -> Result<&'ctx OptimisticWriteApi, Error> {
622 ctx.data_opt()
623 .ok_or_else(|| Error::Internal("Unable to get node execution interface".to_string()))
624}
625
626async fn build_write_api(
627 fullnode_grpc_client: GrpcClient,
628 reader: IndexerReader,
629 metrics: IndexerMetrics,
630) -> Result<OptimisticWriteApi, Error> {
631 let indexer_store = PgIndexerStore::new(reader.get_pool(), metrics.clone());
632 let optimistic_tx_executor = OptimisticTransactionExecutor::new(
633 fullnode_grpc_client.clone(),
634 reader.clone(),
635 indexer_store,
636 metrics,
637 )
638 .await?;
639 Ok(OptimisticWriteApi::new(
640 WriteApi::new(fullnode_grpc_client, reader),
641 optimistic_tx_executor,
642 ))
643}
644
645async fn subscription_handler(
651 ConnectInfo(addr): ConnectInfo<SocketAddr>,
652 Extension(schema): Extension<IotaGraphQLSchema>,
653 req: http::Request<Body>,
654) -> AxumResponse {
655 let headers_contains_show_usage = req.headers().contains_key(ShowUsage::name());
656 let (mut parts, _body) = req.into_parts();
657
658 let protocol = match GraphQLProtocol::from_request_parts(&mut parts, &()).await {
660 Ok(protocol) => protocol,
661 Err(err) => return err.into_response(),
662 };
663
664 let upgrade = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await {
666 Ok(upgrade) => upgrade,
667 Err(err) => return err.into_response(),
668 };
669
670 let resp = upgrade
671 .protocols(ALL_WEBSOCKET_PROTOCOLS)
672 .on_upgrade(move |stream| async move {
673 let mut connection_data = Data::default();
675 connection_data.insert(PayloadSize(0));
677 connection_data.insert(Uuid::new_v4());
678 if headers_contains_show_usage {
679 connection_data.insert(ShowUsage)
680 }
681 connection_data.insert(addr);
682
683 let connection =
684 GraphQLWebSocket::new(stream, schema, protocol).with_data(connection_data);
685 connection.serve().await;
686 });
687 resp
688}
689
690#[derive(Clone)]
691struct MetricsMakeCallbackHandler {
692 metrics: Metrics,
693}
694
695impl MakeCallbackHandler for MetricsMakeCallbackHandler {
696 type Handler = MetricsCallbackHandler;
697
698 fn make_handler(&self, _request: &http::request::Parts) -> Self::Handler {
699 let start = Instant::now();
700 let metrics = self.metrics.clone();
701
702 metrics.request_metrics.inflight_requests.inc();
703 metrics.inc_num_queries();
704
705 MetricsCallbackHandler { metrics, start }
706 }
707}
708
709struct MetricsCallbackHandler {
710 metrics: Metrics,
711 start: Instant,
712}
713
714impl ResponseHandler for MetricsCallbackHandler {
715 fn on_response(&mut self, response: &http::response::Parts) {
716 if let Some(errors) = response.extensions.get::<GraphqlErrors>() {
717 self.metrics.inc_errors(&errors.0);
718 }
719 }
720
721 fn on_error<E>(&mut self, _error: &E) {
722 }
727}
728
729impl Drop for MetricsCallbackHandler {
730 fn drop(&mut self) {
731 self.metrics.query_latency(self.start.elapsed());
732 self.metrics.request_metrics.inflight_requests.dec();
733 }
734}
735
736#[derive(Debug, Clone)]
737struct GraphqlErrors(std::sync::Arc<Vec<async_graphql::ServerError>>);
738
739async fn db_health_check(State(connection): State<ConnectionConfig>) -> StatusCode {
741 let Ok(url) = reqwest::Url::parse(connection.db_url.as_str()) else {
742 return StatusCode::INTERNAL_SERVER_ERROR;
743 };
744
745 let Some(host) = url.host_str() else {
746 return StatusCode::INTERNAL_SERVER_ERROR;
747 };
748
749 let tcp_url = if let Some(port) = url.port() {
750 format!("{host}:{port}")
751 } else {
752 host.to_string()
753 };
754
755 if TcpStream::connect(tcp_url).is_err() {
756 StatusCode::INTERNAL_SERVER_ERROR
757 } else {
758 StatusCode::OK
759 }
760}
761
762#[derive(serde::Deserialize)]
763struct HealthParam {
764 max_checkpoint_lag_ms: Option<u64>,
765}
766
767async fn health_check(
773 State(connection): State<ConnectionConfig>,
774 Extension(watermark_lock): Extension<WatermarkLock>,
775 AxumQuery(query_params): AxumQuery<HealthParam>,
776) -> StatusCode {
777 let db_health_check = db_health_check(axum::extract::State(connection)).await;
778 if db_health_check != StatusCode::OK {
779 return db_health_check;
780 }
781
782 let max_checkpoint_lag_ms = query_params
783 .max_checkpoint_lag_ms
784 .map(Duration::from_millis)
785 .unwrap_or_else(|| DEFAULT_MAX_CHECKPOINT_LAG);
786
787 let checkpoint_timestamp =
788 Duration::from_millis(watermark_lock.read().await.checkpoint_timestamp_ms);
789
790 let now_millis = Utc::now().timestamp_millis();
791
792 let now: Duration = match u64::try_from(now_millis) {
794 Ok(val) => Duration::from_millis(val),
795 Err(_) => return StatusCode::INTERNAL_SERVER_ERROR,
796 };
797
798 if (now - checkpoint_timestamp) > max_checkpoint_lag_ms {
799 return StatusCode::GATEWAY_TIMEOUT;
800 }
801
802 db_health_check
803}
804
805async fn get_or_init_server_start_time() -> &'static Instant {
807 static ONCE: OnceCell<Instant> = OnceCell::const_new();
808 ONCE.get_or_init(|| async move { Instant::now() }).await
809}
810
811pub mod tests {
812 use std::{sync::Arc, time::Duration};
813
814 use async_graphql::{
815 Request, Response, Variables,
816 extensions::{Extension, ExtensionContext, NextExecute},
817 };
818 use iota_sdk_types::Transaction;
819 use iota_types::transaction::TransactionAPI;
820 use serde_json::json;
821 use uuid::Uuid;
822
823 use super::*;
824 use crate::{
825 config::{ConnectionConfig, HistoricFallbackOptions, Limits, ServiceConfig, Version},
826 context_data::db_data_provider::PgManager,
827 extensions::{query_limits_checker::QueryLimitsChecker, timeout::Timeout},
828 test_infra::cluster::Cluster,
829 };
830
831 fn prep_schema(
835 connection_config: Option<ConnectionConfig>,
836 service_config: Option<ServiceConfig>,
837 ) -> ServerBuilder {
838 let connection_config = connection_config.unwrap_or_default();
839 let service_config = service_config.unwrap_or_default();
840
841 let registry = prometheus_filtered::Registry::new();
842 let reader = PgManager::reader_with_config(
843 &connection_config.db_url,
844 connection_config.db_pool_size,
845 service_config.limits.request_timeout_ms.into(),
846 IndexerMetrics::new(®istry),
847 CancellationToken::new(),
848 &HistoricFallbackOptions::default(),
849 ®istry,
850 )
851 .expect("failed to create pg connection pool");
852
853 let version = Version::for_testing();
854 let metrics = metrics();
855
856 let db = Db::new(
857 reader.clone(),
858 service_config.limits.clone(),
859 metrics.clone(),
860 connection_config.max_available_range,
861 );
862 let loader = DataLoader::new(db.clone());
863 let pg_conn_pool = PgManager::new(reader);
864 let cancellation_token = CancellationToken::new();
865 let watermark = Watermark {
866 checkpoint: 1,
867 checkpoint_timestamp_ms: 1,
868 epoch: 0,
869 };
870 let state = AppState::new(
871 connection_config,
872 service_config.clone(),
873 metrics.clone(),
874 cancellation_token,
875 version,
876 );
877 ServerBuilder::new(state)
878 .context_data(db)
879 .context_data(loader)
880 .context_data(pg_conn_pool)
881 .context_data(service_config)
882 .context_data(query_id())
883 .context_data(ip_address())
884 .context_data(watermark)
885 .context_data(ChainIdentifierCache::default())
886 .context_data(metrics)
887 }
888
889 fn metrics() -> Metrics {
890 let binding_address: SocketAddr = "0.0.0.0:9185".parse().unwrap();
891 let registry = iota_metrics::start_prometheus_server(binding_address).default_registry();
892 Metrics::new(®istry)
893 }
894
895 fn ip_address() -> SocketAddr {
896 let binding_address: SocketAddr = "0.0.0.0:51515".parse().unwrap();
897 binding_address
898 }
899
900 fn query_id() -> Uuid {
901 Uuid::new_v4()
902 }
903
904 pub async fn test_timeout_impl(cluster: &Cluster) {
905 struct TimedExecuteExt {
906 pub min_req_delay: Duration,
907 }
908
909 impl ExtensionFactory for TimedExecuteExt {
910 fn create(&self) -> Arc<dyn Extension> {
911 Arc::new(TimedExecuteExt {
912 min_req_delay: self.min_req_delay,
913 })
914 }
915 }
916
917 #[async_trait::async_trait]
918 impl Extension for TimedExecuteExt {
919 async fn execute(
920 &self,
921 ctx: &ExtensionContext<'_>,
922 operation_name: Option<&str>,
923 next: NextExecute<'_>,
924 ) -> Response {
925 tokio::time::sleep(self.min_req_delay).await;
926 next.run(ctx, operation_name).await
927 }
928 }
929
930 async fn test_timeout(
931 delay: Duration,
932 timeout: Duration,
933 query: &str,
934 write_api: OptimisticWriteApi,
935 ) -> Response {
936 let mut cfg = ServiceConfig::default();
937 cfg.limits.request_timeout_ms = timeout.as_millis() as u32;
938 cfg.limits.mutation_timeout_ms = timeout.as_millis() as u32;
939
940 let schema = prep_schema(None, Some(cfg))
941 .context_data(write_api)
942 .extension(Timeout)
943 .extension(TimedExecuteExt {
944 min_req_delay: delay,
945 })
946 .build_schema();
947
948 schema.execute(query).await
949 }
950
951 let wallet = &cluster.validator_fullnode_handle.wallet;
952 let store = &cluster.indexer_store;
953 let fn_grpc_url = &cluster.validator_fullnode_handle.grpc_url();
954 let indexer_metrics = store.get_metrics();
955
956 let indexer_reader =
958 iota_indexer::read::IndexerReader::new_without_watermark_cache(store.blocking_cp());
959 let fullnode_gpc_client = GrpcClient::new(fn_grpc_url).unwrap();
960
961 let optimistic_tx_executor =
962 iota_indexer::optimistic_indexing::OptimisticTransactionExecutor::new(
963 fullnode_gpc_client.clone(),
964 indexer_reader.clone(),
965 store.clone(),
966 indexer_metrics,
967 )
968 .await
969 .unwrap();
970 let write_api = OptimisticWriteApi::new(
971 WriteApi::new(fullnode_gpc_client, indexer_reader),
972 optimistic_tx_executor,
973 );
974
975 let query = "{ chainIdentifier }";
976 let timeout = Duration::from_millis(1000);
977 let delay = Duration::from_millis(100);
978
979 test_timeout(delay, timeout, query, write_api.clone())
980 .await
981 .into_result()
982 .expect("should complete successfully");
983
984 let errs: Vec<_> = test_timeout(delay, delay, query, write_api.clone())
986 .await
987 .into_result()
988 .unwrap_err()
989 .into_iter()
990 .map(|e| e.message)
991 .collect();
992 let exp = format!("Query request timed out. Limit: {}s", delay.as_secs_f32());
993 assert_eq!(errs, vec![exp]);
994
995 let addresses = wallet.get_addresses();
999 let gas = wallet
1000 .get_one_gas_object_owned_by_address(addresses[0])
1001 .await
1002 .unwrap();
1003 let tx = Transaction::new_transfer_iota(
1004 addresses[1],
1005 addresses[0],
1006 Some(1000),
1007 gas.unwrap(),
1008 1_000_000,
1009 wallet.get_reference_gas_price().await.unwrap(),
1010 );
1011
1012 let tx = wallet.sign_transaction(&tx);
1013 let (tx_bytes, signatures) = tx.to_tx_bytes_and_signatures();
1014
1015 let signature_base64 = &signatures[0];
1016 let query = format!(
1017 r#"
1018 mutation {{
1019 executeTransactionBlock(txBytes: "{}", signatures: "{}") {{
1020 effects {{
1021 status
1022 }}
1023 }}
1024 }}"#,
1025 tx_bytes.encoded(),
1026 signature_base64.encoded()
1027 );
1028 let errs: Vec<_> = test_timeout(delay, delay, &query, write_api.clone())
1029 .await
1030 .into_result()
1031 .unwrap_err()
1032 .into_iter()
1033 .map(|e| e.message)
1034 .collect();
1035 let exp = format!(
1036 "Mutation request timed out. Limit: {}s",
1037 delay.as_secs_f32()
1038 );
1039 assert_eq!(errs, vec![exp]);
1040 }
1041
1042 pub async fn test_query_depth_limit_impl() {
1043 async fn exec_query_depth_limit(depth: u32, query: &str) -> Response {
1044 let service_config = ServiceConfig {
1045 limits: Limits {
1046 max_query_depth: depth,
1047 ..Default::default()
1048 },
1049 ..Default::default()
1050 };
1051
1052 let schema = prep_schema(None, Some(service_config))
1053 .context_data(PayloadSize(100))
1054 .extension(QueryLimitsChecker)
1055 .build_schema();
1056 schema.execute(query).await
1057 }
1058
1059 exec_query_depth_limit(1, "{ chainIdentifier }")
1060 .await
1061 .into_result()
1062 .expect("should complete successfully");
1063
1064 exec_query_depth_limit(
1065 5,
1066 "{ chainIdentifier protocolConfig { configs { value key }} }",
1067 )
1068 .await
1069 .into_result()
1070 .expect("should complete successfully");
1071
1072 let errs: Vec<_> = exec_query_depth_limit(0, "{ chainIdentifier }")
1074 .await
1075 .into_result()
1076 .unwrap_err()
1077 .into_iter()
1078 .map(|e| e.message)
1079 .collect();
1080
1081 assert_eq!(errs, vec!["Query nesting is over 0".to_string()]);
1082 let errs: Vec<_> = exec_query_depth_limit(
1083 2,
1084 "{ chainIdentifier protocolConfig { configs { value key }} }",
1085 )
1086 .await
1087 .into_result()
1088 .unwrap_err()
1089 .into_iter()
1090 .map(|e| e.message)
1091 .collect();
1092 assert_eq!(errs, vec!["Query nesting is over 2".to_string()]);
1093 }
1094
1095 pub async fn test_query_node_limit_impl() {
1096 async fn exec_query_node_limit(nodes: u32, query: &str) -> Response {
1097 let service_config = ServiceConfig {
1098 limits: Limits {
1099 max_query_nodes: nodes,
1100 ..Default::default()
1101 },
1102 ..Default::default()
1103 };
1104
1105 let schema = prep_schema(None, Some(service_config))
1106 .context_data(PayloadSize(100))
1107 .extension(QueryLimitsChecker)
1108 .build_schema();
1109 schema.execute(query).await
1110 }
1111
1112 exec_query_node_limit(1, "{ chainIdentifier }")
1113 .await
1114 .into_result()
1115 .expect("should complete successfully");
1116
1117 exec_query_node_limit(
1118 5,
1119 "{ chainIdentifier protocolConfig { configs { value key }} }",
1120 )
1121 .await
1122 .into_result()
1123 .expect("should complete successfully");
1124
1125 let err: Vec<_> = exec_query_node_limit(0, "{ chainIdentifier }")
1127 .await
1128 .into_result()
1129 .unwrap_err()
1130 .into_iter()
1131 .map(|e| e.message)
1132 .collect();
1133 assert_eq!(err, vec!["Query has over 0 nodes".to_string()]);
1134
1135 let err: Vec<_> = exec_query_node_limit(
1136 4,
1137 "{ chainIdentifier protocolConfig { configs { value key }} }",
1138 )
1139 .await
1140 .into_result()
1141 .unwrap_err()
1142 .into_iter()
1143 .map(|e| e.message)
1144 .collect();
1145 assert_eq!(err, vec!["Query has over 4 nodes".to_string()]);
1146 }
1147
1148 pub async fn test_query_default_page_limit_impl(connection_config: ConnectionConfig) {
1149 let service_config = ServiceConfig {
1150 limits: Limits {
1151 default_page_size: 1,
1152 ..Default::default()
1153 },
1154 ..Default::default()
1155 };
1156 let schema = prep_schema(Some(connection_config), Some(service_config)).build_schema();
1157
1158 let resp = schema
1159 .execute("{ checkpoints { nodes { sequenceNumber } } }")
1160 .await;
1161 let data = resp.data.clone().into_json().unwrap();
1162 let checkpoints = data
1163 .get("checkpoints")
1164 .unwrap()
1165 .get("nodes")
1166 .unwrap()
1167 .as_array()
1168 .unwrap();
1169 assert_eq!(
1170 checkpoints.len(),
1171 1,
1172 "Checkpoints should have exactly one element"
1173 );
1174
1175 let resp = schema
1176 .execute("{ checkpoints(first: 2) { nodes { sequenceNumber } } }")
1177 .await;
1178 let data = resp.data.into_json().unwrap();
1179 let checkpoints = data
1180 .get("checkpoints")
1181 .unwrap()
1182 .get("nodes")
1183 .unwrap()
1184 .as_array()
1185 .unwrap();
1186 assert_eq!(
1187 checkpoints.len(),
1188 2,
1189 "Checkpoints should return two elements"
1190 );
1191 }
1192
1193 pub async fn test_query_max_page_limit_impl() {
1194 let schema = prep_schema(None, None).build_schema();
1195
1196 schema
1197 .execute("{ objects(first: 1) { nodes { version } } }")
1198 .await
1199 .into_result()
1200 .expect("should complete successfully");
1201
1202 let err: Vec<_> = schema
1204 .execute("{ objects(first: 51) { nodes { version } } }")
1205 .await
1206 .into_result()
1207 .unwrap_err()
1208 .into_iter()
1209 .map(|e| e.message)
1210 .collect();
1211 assert_eq!(
1212 err,
1213 vec!["Connection's page size of 51 exceeds max of 50".to_string()]
1214 );
1215 }
1216
1217 pub async fn test_query_complexity_metrics_impl() {
1218 let server_builder = prep_schema(None, None).context_data(PayloadSize(100));
1219 let metrics = server_builder.state.metrics.clone();
1220 let schema = server_builder
1221 .extension(QueryLimitsChecker) .build_schema();
1223
1224 schema
1225 .execute("{ chainIdentifier }")
1226 .await
1227 .into_result()
1228 .expect("should complete successfully");
1229
1230 let req_metrics = metrics.request_metrics;
1231 assert_eq!(req_metrics.input_nodes.get_sample_count(), 1);
1232 assert_eq!(req_metrics.output_nodes.get_sample_count(), 1);
1233 assert_eq!(req_metrics.query_depth.get_sample_count(), 1);
1234 assert_eq!(req_metrics.input_nodes.get_sample_sum(), 1.);
1235 assert_eq!(req_metrics.output_nodes.get_sample_sum(), 1.);
1236 assert_eq!(req_metrics.query_depth.get_sample_sum(), 1.);
1237
1238 schema
1239 .execute("{ chainIdentifier protocolConfig { configs { value key }} }")
1240 .await
1241 .into_result()
1242 .expect("should complete successfully");
1243
1244 assert_eq!(req_metrics.input_nodes.get_sample_count(), 2);
1245 assert_eq!(req_metrics.output_nodes.get_sample_count(), 2);
1246 assert_eq!(req_metrics.query_depth.get_sample_count(), 2);
1247 assert_eq!(req_metrics.input_nodes.get_sample_sum(), 2. + 4.);
1248 assert_eq!(req_metrics.output_nodes.get_sample_sum(), 2. + 4.);
1249 assert_eq!(req_metrics.query_depth.get_sample_sum(), 1. + 3.);
1250 }
1251
1252 pub async fn test_health_check_impl() {
1253 let server_builder = prep_schema(None, None);
1254 let url = format!(
1255 "http://{}:{}/health",
1256 server_builder.state.connection.host, server_builder.state.connection.port
1257 );
1258 server_builder.build_schema();
1259
1260 let _ = rustls::crypto::ring::default_provider().install_default();
1261 let resp = reqwest::get(&url).await.unwrap();
1262 assert_eq!(resp.status(), reqwest::StatusCode::OK);
1263
1264 let url_with_param = format!("{url}?max_checkpoint_lag_ms=1");
1265 let resp = reqwest::get(&url_with_param).await.unwrap();
1266 assert_eq!(resp.status(), reqwest::StatusCode::GATEWAY_TIMEOUT);
1267 }
1268
1269 async fn execute_for_error(limits: Limits, request: Request) -> String {
1272 let service_config = ServiceConfig {
1273 limits,
1274 ..Default::default()
1275 };
1276
1277 let schema = prep_schema(None, Some(service_config))
1278 .context_data(PayloadSize(
1279 serde_json::to_string(&request).unwrap().len() as u64,
1284 ))
1285 .extension(QueryLimitsChecker)
1286 .build_schema();
1287
1288 let errs: Vec<_> = schema
1289 .execute(request)
1290 .await
1291 .into_result()
1292 .unwrap_err()
1293 .into_iter()
1294 .map(|e| e.message)
1295 .collect();
1296
1297 errs.join("\n")
1298 }
1299
1300 pub async fn test_payload_read_exceeded_impl() {
1301 assert_eq!(
1302 execute_for_error(
1303 Limits {
1304 max_tx_payload_size: 400,
1305 max_query_payload_size: 10,
1306 ..Default::default()
1307 },
1308 r#"
1309 mutation {
1310 executeTransactionBlock(txBytes: "AAA", signatures: ["BBB"]) {
1311 effects {
1312 status
1313 }
1314 }
1315 }
1316 "#
1317 .into()
1318 )
1319 .await,
1320 "Query part too large: 354 bytes. Requests are limited to 400 bytes or fewer on \
1321 transaction payloads (all inputs to executeTransactionBlock or \
1322 dryRunTransactionBlock) and the rest of the request (the query part) must be 10 \
1323 bytes or fewer."
1324 );
1325 }
1326
1327 pub async fn test_payload_mutation_exceeded_impl() {
1328 assert_eq!(
1329 execute_for_error(
1330 Limits {
1331 max_tx_payload_size: 10,
1332 max_query_payload_size: 400,
1333 ..Default::default()
1334 },
1335 r#"
1336 mutation {
1337 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: ["BBB"]) {
1338 effects {
1339 status
1340 }
1341 }
1342 }
1343 "#
1344 .into()
1345 )
1346 .await,
1347 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1348 transaction payloads (all inputs to executeTransactionBlock or \
1349 dryRunTransactionBlock) and the rest of the request (the query part) must be 400 \
1350 bytes or fewer."
1351 );
1352 }
1353
1354 pub async fn test_payload_dry_run_exceeded_impl() {
1355 assert_eq!(
1356 execute_for_error(
1357 Limits {
1358 max_tx_payload_size: 10,
1359 max_query_payload_size: 400,
1360 ..Default::default()
1361 },
1362 r#"
1363 query {
1364 dryRunTransactionBlock(txBytes: "AAABBBCCC") {
1365 error
1366 transaction {
1367 digest
1368 }
1369 }
1370 }
1371 "#
1372 .into(),
1373 )
1374 .await,
1375 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1376 transaction payloads (all inputs to executeTransactionBlock or \
1377 dryRunTransactionBlock) and the rest of the request (the query part) must be 400 bytes \
1378 or fewer."
1379 );
1380 }
1381
1382 pub async fn test_payload_total_exceeded_impl() {
1383 assert_eq!(
1384 execute_for_error(
1385 Limits {
1386 max_tx_payload_size: 10,
1387 max_query_payload_size: 10,
1388 ..Default::default()
1389 },
1390 r#"
1391 query {
1392 dryRunTransactionBlock(txByte: "AAABBB") {
1393 error
1394 transaction {
1395 digest
1396 }
1397 }
1398 }
1399 "#
1400 .into(),
1401 )
1402 .await,
1403 "Overall request too large: 380 bytes. Requests are limited to 10 bytes or fewer on \
1404 transaction payloads (all inputs to executeTransactionBlock or dryRunTransactionBlock) \
1405 and the rest of the request (the query part) must be 10 bytes or fewer."
1406 );
1407 }
1408
1409 pub async fn test_payload_using_vars_mutation_exceeded_impl() {
1410 assert_eq!(
1411 execute_for_error(
1412 Limits {
1413 max_tx_payload_size: 10,
1414 max_query_payload_size: 500,
1415 ..Default::default()
1416 },
1417 Request::new(
1418 r#"
1419 mutation ($tx: String!, $sigs: [String!]!) {
1420 executeTransactionBlock(txBytes: $tx, signatures: $sigs) {
1421 effects {
1422 status
1423 }
1424 }
1425 }
1426 "#
1427 )
1428 .variables(Variables::from_json(json!({
1429 "tx": "AAABBBCCC",
1430 "sigs": ["BBB"]
1431 })))
1432 )
1433 .await,
1434 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1435 transaction payloads (all inputs to executeTransactionBlock or \
1436 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
1437 bytes or fewer."
1438 );
1439 }
1440
1441 pub async fn test_payload_using_vars_read_exceeded_impl() {
1442 assert_eq!(
1443 execute_for_error(
1444 Limits {
1445 max_tx_payload_size: 500,
1446 max_query_payload_size: 10,
1447 ..Default::default()
1448 },
1449 Request::new(
1450 r#"
1451 mutation ($tx: String!, $sigs: [String!]!) {
1452 executeTransactionBlock(txBytes: $tx, signatures: $sigs) {
1453 effects {
1454 status
1455 }
1456 }
1457 }
1458 "#
1459 )
1460 .variables(Variables::from_json(json!({
1461 "tx": "AAA",
1462 "sigs": ["BBB"]
1463 })))
1464 )
1465 .await,
1466 "Query part too large: 409 bytes. Requests are limited to 500 bytes or fewer on \
1467 transaction payloads (all inputs to executeTransactionBlock or \
1468 dryRunTransactionBlock) and the rest of the request (the query part) must be 10 bytes \
1469 or fewer."
1470 );
1471 }
1472
1473 pub async fn test_payload_using_vars_dry_run_exceeded_impl() {
1474 assert_eq!(
1475 execute_for_error(
1476 Limits {
1477 max_tx_payload_size: 10,
1478 max_query_payload_size: 400,
1479 ..Default::default()
1480 },
1481 Request::new(
1482 r#"
1483 query ($tx: String!) {
1484 dryRunTransactionBlock(txBytes: $tx) {
1485 error
1486 transaction {
1487 digest
1488 }
1489 }
1490 }
1491 "#
1492 )
1493 .variables(Variables::from_json(json!({
1494 "tx": "AAABBBCCC"
1495 }))),
1496 )
1497 .await,
1498 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1499 transaction payloads (all inputs to executeTransactionBlock or \
1500 dryRunTransactionBlock) and the rest of the request (the query part) must be 400 \
1501 bytes or fewer."
1502 );
1503 }
1504
1505 pub async fn test_payload_using_vars_dry_run_read_exceeded_impl() {
1506 assert_eq!(
1507 execute_for_error(
1508 Limits {
1509 max_tx_payload_size: 400,
1510 max_query_payload_size: 10,
1511 ..Default::default()
1512 },
1513 Request::new(
1514 r#"
1515 query ($tx: String!) {
1516 dryRunTransactionBlock(txBytes: $tx) {
1517 error
1518 transaction {
1519 digest
1520 }
1521 }
1522 }
1523 "#
1524 )
1525 .variables(Variables::from_json(json!({
1526 "tx": "AAABBBCCC"
1527 }))),
1528 )
1529 .await,
1530 "Query part too large: 398 bytes. Requests are limited to 400 bytes or fewer on \
1531 transaction payloads (all inputs to executeTransactionBlock or \
1532 dryRunTransactionBlock) and the rest of the request (the query part) must be 10 bytes \
1533 or fewer."
1534 );
1535 }
1536
1537 pub async fn test_payload_multiple_execution_exceeded_impl() {
1538 let err = execute_for_error(
1541 Limits {
1542 max_tx_payload_size: 30,
1543 max_query_payload_size: 320,
1544 ..Default::default()
1545 },
1546 r#"
1547 mutation {
1548 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: ["DDD"]) {
1549 effects {
1550 status
1551 }
1552 }
1553 }
1554 "#
1555 .into(),
1556 )
1557 .await;
1558 assert!(err.starts_with("Query part too large"), "{err}");
1559
1560 assert_eq!(
1561 execute_for_error(
1562 Limits {
1563 max_tx_payload_size: 30,
1564 max_query_payload_size: 800,
1565 ..Default::default()
1566 },
1567 r#"
1568 mutation {
1569 e0: executeTransactionBlock(txBytes: "AAABBBCCC", signatures: ["DDD"]) {
1570 effects {
1571 status
1572 }
1573 }
1574 e1: executeTransactionBlock(txBytes: "EEEFFFGGG", signatures: ["HHH"]) {
1575 effects {
1576 status
1577 }
1578 }
1579 }
1580 "#
1581 .into()
1582 )
1583 .await,
1584 "Transaction payload too large. Requests are limited to 30 bytes or fewer on \
1585 transaction payloads (all inputs to executeTransactionBlock or \
1586 dryRunTransactionBlock) and the rest of the request (the query part) must be 800 \
1587 bytes or fewer."
1588 );
1589 }
1590
1591 pub async fn test_payload_multiple_dry_run_exceeded_impl() {
1592 let err = execute_for_error(
1595 Limits {
1596 max_tx_payload_size: 20,
1597 max_query_payload_size: 330,
1598 ..Default::default()
1599 },
1600 r#"
1601 query {
1602 dryRunTransactionBlock(txBytes: "AAABBBCCC") {
1603 error
1604 transaction {
1605 digest
1606 }
1607 }
1608 }
1609 "#
1610 .into(),
1611 )
1612 .await;
1613 assert!(err.starts_with("Query part too large"), "{err}");
1614
1615 assert_eq!(
1616 execute_for_error(
1617 Limits {
1618 max_tx_payload_size: 20,
1619 max_query_payload_size: 800,
1620 ..Default::default()
1621 },
1622 r#"
1623 query {
1624 d0: dryRunTransactionBlock(txBytes: "AAABBBCCC") {
1625 error
1626 transaction {
1627 digest
1628 }
1629 }
1630 d1: dryRunTransactionBlock(txBytes: "DDDEEEFFF") {
1631 error
1632 transaction {
1633 digest
1634 }
1635 }
1636 }
1637 "#
1638 .into()
1639 )
1640 .await,
1641 "Transaction payload too large. Requests are limited to 20 bytes or fewer on \
1642 transaction payloads (all inputs to executeTransactionBlock or \
1643 dryRunTransactionBlock) and the rest of the request (the query part) must be 800 \
1644 bytes or fewer."
1645 );
1646 }
1647
1648 pub async fn test_payload_execution_multiple_sigs_exceeded_impl() {
1649 let err = execute_for_error(
1652 Limits {
1653 max_tx_payload_size: 30,
1654 max_query_payload_size: 320,
1655 ..Default::default()
1656 },
1657 r#"
1658 mutation {
1659 executeTransactionBlock(txBytes: "AAA", signatures: ["BBB"]) {
1660 effects {
1661 status
1662 }
1663 }
1664 }
1665 "#
1666 .into(),
1667 )
1668 .await;
1669
1670 assert!(err.starts_with("Query part too large"), "{err}");
1671
1672 assert_eq!(
1673 execute_for_error(
1674 Limits {
1675 max_tx_payload_size: 30,
1676 max_query_payload_size: 500,
1677 ..Default::default()
1678 },
1679 r#"
1680 mutation {
1681 executeTransactionBlock(
1682 txBytes: "AAA",
1683 signatures: ["BBB", "CCC", "DDD", "EEE", "FFF"]
1684 ) {
1685 effects {
1686 status
1687 }
1688 }
1689 }
1690 "#
1691 .into(),
1692 )
1693 .await,
1694 "Transaction payload too large. Requests are limited to 30 bytes or fewer on \
1695 transaction payloads (all inputs to executeTransactionBlock or \
1696 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
1697 bytes or fewer.",
1698 )
1699 }
1700
1701 pub async fn test_payload_sig_var_execution_exceeded_impl() {
1702 assert_eq!(
1705 execute_for_error(
1706 Limits {
1707 max_tx_payload_size: 10,
1708 max_query_payload_size: 500,
1709 ..Default::default()
1710 },
1711 Request::new(
1712 r#"
1713 mutation ($tx: String!, $sig: String!) {
1714 executeTransactionBlock(txBytes: $tx, signatures: [$sig]) {
1715 effects {
1716 status
1717 }
1718 }
1719 }
1720 "#
1721 )
1722 .variables(Variables::from_json(json!({
1723 "tx": "AAA",
1724 "sig": "BBB"
1725 })))
1726 )
1727 .await,
1728 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1729 transaction payloads (all inputs to executeTransactionBlock or \
1730 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
1731 bytes or fewer."
1732 );
1733 }
1734
1735 fn passed_tx_checks(err: &str) -> bool {
1738 !err.starts_with("Overall request too large")
1739 && !err.starts_with("Transaction payload too large")
1740 }
1741
1742 pub async fn test_payload_reusing_vars_execution_impl() {
1743 assert!(!passed_tx_checks(
1749 &execute_for_error(
1750 Limits {
1751 max_tx_payload_size: 1,
1752 max_query_payload_size: 1,
1753 ..Default::default()
1754 },
1755 r#"
1756 mutation {
1757 executeTransactionBlock(txBytes: "AAA", signatures: ["BBB"]) {
1758 effects {
1759 status
1760 }
1761 }
1762 }
1763 "#
1764 .into()
1765 )
1766 .await
1767 ));
1768
1769 let limits = Limits {
1770 max_tx_payload_size: 20,
1771 max_query_payload_size: 1000,
1772 ..Default::default()
1773 };
1774
1775 assert!(passed_tx_checks(
1778 &execute_for_error(
1779 limits.clone(),
1780 Request::new(
1781 r#"
1782 mutation ($sig: String!) {
1783 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: [$sig]) {
1784 effects {
1785 status
1786 }
1787 }
1788 }
1789 "#,
1790 )
1791 .variables(Variables::from_json(json!({
1792 "sig": "BBB"
1793 })))
1794 )
1795 .await
1796 ));
1797
1798 let execution_result = execute_for_error(
1801 limits.clone(),
1802 Request::new(
1803 r#"
1804 mutation ($sig: String!) {
1805 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: [$sig, "BBB"]) {
1806 effects {
1807 status
1808 }
1809 }
1810 }
1811 "#,
1812 )
1813 .variables(Variables::from_json(json!({
1814 "sig": "BBB"
1815 }))),
1816 )
1817 .await;
1818 assert!(!passed_tx_checks(&execution_result), "{execution_result}");
1819
1820 let execution_result = execute_for_error(
1823 limits,
1824 Request::new(
1825 r#"
1826 mutation ($sig: String!) {
1827 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: [$sig, $sig]) {
1828 effects {
1829 status
1830 }
1831 }
1832 }
1833 "#,
1834 )
1835 .variables(Variables::from_json(json!({
1836 "sig": "BBB"
1837 }))),
1838 )
1839 .await;
1840 assert!(passed_tx_checks(&execution_result), "{execution_result}");
1841 }
1842
1843 pub async fn test_payload_reusing_vars_dry_run_impl() {
1844 let limits = Limits {
1848 max_tx_payload_size: 20,
1849 max_query_payload_size: 1000,
1850 ..Default::default()
1851 };
1852
1853 assert!(passed_tx_checks(
1855 &execute_for_error(
1856 limits.clone(),
1857 Request::new(
1858 r#"
1859 query ($tx: String!) {
1860 dryRunTransactionBlock(txBytes: $tx) {
1861 error
1862 transaction {
1863 digest
1864 }
1865 }
1866 }
1867 "#,
1868 )
1869 .variables(Variables::from_json(json!({
1870 "tx": "AAABBBCCC"
1871 })))
1872 )
1873 .await
1874 ));
1875
1876 assert!(!passed_tx_checks(
1878 &execute_for_error(
1879 limits.clone(),
1880 Request::new(
1881 r#"
1882 query ($tx: String!) {
1883 d0: dryRunTransactionBlock(txBytes: $tx) {
1884 error
1885 transaction {
1886 digest
1887 }
1888 }
1889
1890 d1: dryRunTransactionBlock(txBytes: "AAABBBCCC") {
1891 error
1892 transaction {
1893 digest
1894 }
1895 }
1896 }
1897 "#,
1898 )
1899 .variables(Variables::from_json(json!({
1900 "tx": "AAABBBCCC"
1901 })))
1902 )
1903 .await
1904 ));
1905
1906 assert!(passed_tx_checks(
1908 &execute_for_error(
1909 limits,
1910 Request::new(
1911 r#"
1912 query ($tx: String!) {
1913 d0: dryRunTransactionBlock(txBytes: $tx) {
1914 error
1915 transaction {
1916 digest
1917 }
1918 }
1919
1920 d1: dryRunTransactionBlock(txBytes: $tx) {
1921 error
1922 transaction {
1923 digest
1924 }
1925 }
1926 }
1927 "#,
1928 )
1929 .variables(Variables::from_json(json!({
1930 "tx": "AAABBBCCC"
1931 })))
1932 )
1933 .await
1934 ));
1935 }
1936
1937 pub async fn test_payload_named_fragment_execution_exceeded_impl() {
1938 assert_eq!(
1939 execute_for_error(
1940 Limits {
1941 max_tx_payload_size: 10,
1942 max_query_payload_size: 500,
1943 ..Default::default()
1944 },
1945 r#"
1946 mutation {
1947 ...Tx
1948 }
1949
1950 fragment Tx on Mutation {
1951 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: ["BBB"]) {
1952 effects {
1953 status
1954 }
1955 }
1956 }
1957 "#
1958 .into()
1959 )
1960 .await,
1961 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1962 transaction payloads (all inputs to executeTransactionBlock or \
1963 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
1964 bytes or fewer."
1965 );
1966 }
1967
1968 pub async fn test_payload_inline_fragment_execution_exceeded_impl() {
1969 assert_eq!(
1970 execute_for_error(
1971 Limits {
1972 max_tx_payload_size: 10,
1973 max_query_payload_size: 500,
1974 ..Default::default()
1975 },
1976 r#"
1977 mutation {
1978 ... on Mutation {
1979 executeTransactionBlock(txBytes: "AAABBBCCC", signatures: ["BBB"]) {
1980 effects {
1981 status
1982 }
1983 }
1984 }
1985 }
1986 "#
1987 .into()
1988 )
1989 .await,
1990 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
1991 transaction payloads (all inputs to executeTransactionBlock or \
1992 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
1993 bytes or fewer."
1994 );
1995 }
1996
1997 pub async fn test_payload_named_fragment_dry_run_exceeded_impl() {
1998 assert_eq!(
1999 execute_for_error(
2000 Limits {
2001 max_tx_payload_size: 10,
2002 max_query_payload_size: 500,
2003 ..Default::default()
2004 },
2005 r#"
2006 query {
2007 ...DryRun
2008 }
2009
2010 fragment DryRun on Query {
2011 dryRunTransactionBlock(txBytes: "AAABBBCCC") {
2012 error
2013 transaction {
2014 digest
2015 }
2016 }
2017 }
2018 "#
2019 .into(),
2020 )
2021 .await,
2022 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
2023 transaction payloads (all inputs to executeTransactionBlock or \
2024 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
2025 bytes or fewer."
2026 );
2027 }
2028
2029 pub async fn test_payload_inline_fragment_dry_run_exceeded_impl() {
2030 assert_eq!(
2031 execute_for_error(
2032 Limits {
2033 max_tx_payload_size: 10,
2034 max_query_payload_size: 500,
2035 ..Default::default()
2036 },
2037 r#"
2038 query {
2039 ... on Query {
2040 dryRunTransactionBlock(txBytes: "AAABBBCCC") {
2041 error
2042 transaction {
2043 digest
2044 }
2045 }
2046 }
2047 }
2048 "#
2049 .into(),
2050 )
2051 .await,
2052 "Transaction payload too large. Requests are limited to 10 bytes or fewer on \
2053 transaction payloads (all inputs to executeTransactionBlock or \
2054 dryRunTransactionBlock) and the rest of the request (the query part) must be 500 \
2055 bytes or fewer."
2056 );
2057 }
2058
2059 pub async fn test_payload_object_input_dry_run_exceeded_impl() {
2060 assert_eq!(
2065 execute_for_error(
2066 Limits {
2067 max_tx_payload_size: 500,
2068 max_query_payload_size: 10_000,
2069 ..Default::default()
2070 },
2071 Request::new(
2072 r#"
2073 query DryRunQuery(
2074 $txBytes: String!,
2075 $skipChecks: Boolean!,
2076 $txMeta: TransactionMetadata
2077 ) {
2078 dryRunTransactionBlock(
2079 txBytes: $txBytes,
2080 skipChecks: $skipChecks,
2081 txMeta: $txMeta
2082 ) {
2083 error
2084 transaction {
2085 digest
2086 }
2087 }
2088 }
2089 "#,
2090 )
2091 .variables(Variables::from_json(json!({
2092 "txBytes": "AAIAINdb/wdoZKClk3YWgTp2SRpvVYkX9XUMu0Im6dGMv5g1AAjoAwAAAAAAAAICAAEBAQABAQIAAAEAAA==",
2093 "skipChecks": false,
2094 "txMeta": {
2095 "gasBudget": null,
2096 "gasPrice": 1000,
2097 "gasSponsor": "0x2c4a2ee4a1cb67c2ae7a6215784a26bb9c5051ea3f9aa5f60ccc58613a94f70b",
2098 "sender": "0x2c4a2ee4a1cb67c2ae7a6215784a26bb9c5051ea3f9aa5f60ccc58613a94f70b",
2099 "gasObjects": [
2100 { "address": "0x0f49b390361c7b85f50ea436dd2f78d07794450162fa543d9b37d4fd5d3c9884", "digest": "3Hij17jtvK4o7VxarnBJkX31LYLtV7KDecLBhmBJuapU", "version": 3 },
2101 { "address": "0x115d51e2df3dae3fcb4a8d71ea9e357cec509e2f8e2f604eea53bb7abcd68c2b", "digest": "9t4C9FXKFdpgpZa8L968KtgkCPHwRA2QFHz6gVrymVJu", "version": 3 },
2102 { "address": "0x18c9d0de646c02aaea280eb4aafabbc6855cf6efb7c423714428318c42fb35be", "digest": "FguxMqj4pLitqCQLhGrEja65HT5r7749h8CPd3JzSnnk", "version": 3 }
2103 ]
2104 }
2105 })))
2106 )
2107 .await,
2108 "Transaction payload too large. Requests are limited to 500 bytes or fewer on \
2109 transaction payloads (all inputs to executeTransactionBlock or \
2110 dryRunTransactionBlock) and the rest of the request (the query part) must be 10000 \
2111 bytes or fewer."
2112 );
2113 }
2114}