Skip to content

Commit

Permalink
Change complexity block API (#2276)
Browse files Browse the repository at this point in the history
## Description
Now, it's possible to get 10 blocks with all statuses at maximum before
making a too complex query.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [x] I have created follow-up issues caused by this PR and linked them
here

---------

Co-authored-by: green <xgreenx9999@gmail.com>
  • Loading branch information
AurelienFT and xgreenx authored Oct 4, 2024
1 parent b5d5ff8 commit f1973cb
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2204](https://github.com/FuelLabs/fuel-core/pull/2204): Added `dnsaddr` resolution for TLD without suffixes.

### Changed
- [2276](https://github.com/FuelLabs/fuel-core/pull/2276): Change complexity block query to allow fetching of 10 blocks

#### Breaking
- [2199](https://github.com/FuelLabs/fuel-core/pull/2199): Applying several breaking changes to the WASM interface from backlog:
Expand Down
2 changes: 1 addition & 1 deletion bin/fuel-core/src/cli/run/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct GraphQLArgs {
pub graphql_max_depth: usize,

/// The max complexity of GraphQL queries.
#[clap(long = "graphql-max-complexity", default_value = "20000", env)]
#[clap(long = "graphql-max-complexity", default_value = "80000", env)]
pub graphql_max_complexity: usize,

/// The max recursive depth of GraphQL queries.
Expand Down
32 changes: 23 additions & 9 deletions crates/fuel-core/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,41 @@ pub struct Costs {
pub status_change: usize,
pub raw_payload: usize,
pub storage_read: usize,
pub tx_get: usize,
pub tx_status_read: usize,
pub tx_raw_payload: usize,
pub block_header: usize,
pub block_transactions: usize,
pub block_transactions_ids: usize,
pub storage_iterator: usize,
pub bytecode_read: usize,
pub state_transition_bytecode_read: usize,
}

pub const QUERY_COSTS: Costs = Costs {
// balance_query: 4000,
balance_query: 10001,
coins_to_spend: 10001,
balance_query: 40001,
coins_to_spend: 40001,
// get_peers: 2000,
get_peers: 10001,
get_peers: 40001,
// estimate_predicates: 3000,
estimate_predicates: 10001,
dry_run: 3000,
estimate_predicates: 40001,
dry_run: 12000,
// submit: 5000,
submit: 10001,
submit_and_await: 10001,
status_change: 10001,
submit: 40001,
submit_and_await: 40001,
status_change: 40001,
raw_payload: 10,
storage_read: 10,
tx_get: 50,
tx_status_read: 50,
tx_raw_payload: 150,
block_header: 150,
block_transactions: 1500,
block_transactions_ids: 50,
storage_iterator: 100,
bytecode_read: 2000,
bytecode_read: 8000,
state_transition_bytecode_read: 76_000,
};

#[derive(Clone, Debug)]
Expand Down
21 changes: 10 additions & 11 deletions crates/fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Block {
Ok(query.consensus(height)?.try_into()?)
}

#[graphql(complexity = "QUERY_COSTS.block_transactions_ids")]
async fn transaction_ids(&self) -> Vec<TransactionId> {
self.0
.transactions()
Expand All @@ -134,8 +135,7 @@ impl Block {
}

// Assume that in average we have 32 transactions per block.
#[graphql(complexity = "QUERY_COSTS.storage_iterator\
+ (QUERY_COSTS.storage_read + child_complexity) * 32")]
#[graphql(complexity = "QUERY_COSTS.block_transactions + child_complexity")]
async fn transactions(
&self,
ctx: &Context<'_>,
Expand Down Expand Up @@ -246,7 +246,7 @@ pub struct BlockQuery;

#[Object]
impl BlockQuery {
#[graphql(complexity = "2 * QUERY_COSTS.storage_read + child_complexity")]
#[graphql(complexity = "QUERY_COSTS.block_header + child_complexity")]
async fn block(
&self,
ctx: &Context<'_>,
Expand Down Expand Up @@ -276,9 +276,8 @@ impl BlockQuery {
}

#[graphql(complexity = "{\
QUERY_COSTS.storage_iterator\
+ (QUERY_COSTS.storage_read + first.unwrap_or_default() as usize) * child_complexity \
+ (QUERY_COSTS.storage_read + last.unwrap_or_default() as usize) * child_complexity\
(QUERY_COSTS.block_header + child_complexity) \
* (first.unwrap_or_default() as usize + last.unwrap_or_default() as usize) \
}")]
async fn blocks(
&self,
Expand All @@ -305,7 +304,7 @@ pub struct HeaderQuery;

#[Object]
impl HeaderQuery {
#[graphql(complexity = "QUERY_COSTS.storage_read + child_complexity")]
#[graphql(complexity = "QUERY_COSTS.block_header + child_complexity")]
async fn header(
&self,
ctx: &Context<'_>,
Expand All @@ -319,9 +318,8 @@ impl HeaderQuery {
}

#[graphql(complexity = "{\
QUERY_COSTS.storage_iterator\
+ (QUERY_COSTS.storage_read + first.unwrap_or_default() as usize) * child_complexity \
+ (QUERY_COSTS.storage_read + last.unwrap_or_default() as usize) * child_complexity\
(QUERY_COSTS.block_header + child_complexity) \
* (first.unwrap_or_default() as usize + last.unwrap_or_default() as usize) \
}")]
async fn headers(
&self,
Expand Down Expand Up @@ -374,13 +372,14 @@ impl BlockMutation {
start_timestamp: Option<Tai64Timestamp>,
blocks_to_produce: U32,
) -> async_graphql::Result<U32> {
let consensus_module = ctx.data_unchecked::<ConsensusModule>();
let config = ctx.data_unchecked::<GraphQLConfig>().clone();

if !config.debug {
return Err(anyhow!("`debug` must be enabled to use this endpoint").into())
}

let consensus_module = ctx.data_unchecked::<ConsensusModule>();

let start_time = start_timestamp.map(|timestamp| timestamp.0);
let blocks_to_produce: u32 = blocks_to_produce.into();
consensus_module
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/schema/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct LatestGasPriceQuery {}

#[Object]
impl LatestGasPriceQuery {
#[graphql(complexity = "2 * QUERY_COSTS.storage_read")]
#[graphql(complexity = "QUERY_COSTS.block_header")]
async fn latest_gas_price(
&self,
ctx: &Context<'_>,
Expand Down
6 changes: 3 additions & 3 deletions crates/fuel-core/src/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ impl TxQuery {
}
}

// We assume that each block has 100 transactions.
#[graphql(complexity = "{\
QUERY_COSTS.storage_iterator\
+ (QUERY_COSTS.storage_read + first.unwrap_or_default() as usize) * child_complexity \
+ (QUERY_COSTS.storage_read + last.unwrap_or_default() as usize) * child_complexity\
(QUERY_COSTS.tx_get + child_complexity) \
* (first.unwrap_or_default() as usize + last.unwrap_or_default() as usize)
}")]
async fn transactions(
&self,
Expand Down
6 changes: 3 additions & 3 deletions crates/fuel-core/src/schema/tx/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl SuccessStatus {
self.block_height.into()
}

#[graphql(complexity = "QUERY_COSTS.storage_read + child_complexity")]
#[graphql(complexity = "QUERY_COSTS.block_header + child_complexity")]
async fn block(&self, ctx: &Context<'_>) -> async_graphql::Result<Block> {
let query = ctx.read_view()?;
let block = query.block(&self.block_height)?;
Expand Down Expand Up @@ -238,7 +238,7 @@ impl FailureStatus {
self.block_height.into()
}

#[graphql(complexity = "QUERY_COSTS.storage_read + child_complexity")]
#[graphql(complexity = "QUERY_COSTS.block_header + child_complexity")]
async fn block(&self, ctx: &Context<'_>) -> async_graphql::Result<Block> {
let query = ctx.read_view()?;
let block = query.block(&self.block_height)?;
Expand Down Expand Up @@ -693,7 +693,7 @@ impl Transaction {
}
}

#[graphql(complexity = "QUERY_COSTS.storage_read + child_complexity")]
#[graphql(complexity = "QUERY_COSTS.tx_status_read + child_complexity")]
async fn status(
&self,
ctx: &Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-core/src/schema/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl StateTransitionBytecode {
HexString(self.root.to_vec())
}

#[graphql(complexity = "QUERY_COSTS.state_transition_bytecode_read")]
async fn bytecode(
&self,
ctx: &Context<'_>,
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Config {
0,
),
max_queries_depth: 16,
max_queries_complexity: 20000,
max_queries_complexity: 80000,
max_queries_recursive_depth: 16,
request_body_bytes_limit: 16 * 1024 * 1024,
query_log_threshold_time: Duration::from_secs(2),
Expand Down
Loading

0 comments on commit f1973cb

Please sign in to comment.