Skip to content

Commit 1ef9d9c

Browse files
rafal-chAurelienFTMitchTurner
authored
Expose indexation status in NodeInfo endpoint (#2595)
Closes #2576 ## Description This PR extends the `nodeInfo` GraphQL endpoint by adding the `indexation` field which allows to check whether a given indexation is enabled. Example response: ```json { "data": { "nodeInfo": { "utxoValidation": false, "indexation": { "balances": true, "coinsToSpend": false, "assetMetadata": false } } } } ``` ### Before requesting review - [X] I have reviewed the code myself ### After merging, notify other teams - [X] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) --------- Co-authored-by: AurelienFT <[email protected]> Co-authored-by: Mitchell Turner <[email protected]> Co-authored-by: AurelienFT <[email protected]> Co-authored-by: AurelienFT <[email protected]>
1 parent eafb4a6 commit 1ef9d9c

File tree

11 files changed

+167
-33
lines changed

11 files changed

+167
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### Added
1010
- [2551](https://github.com/FuelLabs/fuel-core/pull/2551): Enhanced the DA compressed block header to include block id.
11+
- [2595](https://github.com/FuelLabs/fuel-core/pull/2595): Added `indexation` field to the `nodeInfo` GraphQL endpoint to allow checking if a specific indexation is enabled.
1112

1213
### Changed
1314
- [2603](https://github.com/FuelLabs/fuel-core/pull/2603): Sets the latest recorded height on initialization, not just when DA costs are received

crates/client/assets/schema.sdl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,21 @@ type HeavyOperation {
580580
scalar HexString
581581

582582

583+
type IndexationFlags {
584+
"""
585+
Is balances indexation enabled
586+
"""
587+
balances: Boolean!
588+
"""
589+
Is coins to spend indexation enabled
590+
"""
591+
coinsToSpend: Boolean!
592+
"""
593+
Is asset metadata indexation enabled
594+
"""
595+
assetMetadata: Boolean!
596+
}
597+
583598
union Input = InputCoin | InputContract | InputMessage
584599

585600
type InputCoin {
@@ -764,6 +779,7 @@ type NodeInfo {
764779
maxSize: U64!
765780
maxDepth: U64!
766781
nodeVersion: String!
782+
indexation: IndexationFlags!
767783
txPoolStats: TxPoolStats!
768784
peers: [PeerInfo!]!
769785
}

crates/client/src/client/schema/node_info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct NodeInfo {
2727
pub max_size: U64,
2828
pub max_depth: U64,
2929
pub node_version: String,
30+
pub indexation: IndexationFlags,
3031
pub tx_pool_stats: TxPoolStats,
3132
}
3233

@@ -88,6 +89,14 @@ pub struct TxPoolStats {
8889
pub total_size: U64,
8990
}
9091

92+
#[derive(cynic::QueryFragment, Clone, Debug, PartialEq, Eq)]
93+
#[cynic(schema_path = "./assets/schema.sdl")]
94+
pub struct IndexationFlags {
95+
pub balances: bool,
96+
pub coins_to_spend: bool,
97+
pub asset_metadata: bool,
98+
}
99+
91100
#[cfg(test)]
92101
mod tests {
93102
use super::*;

crates/client/src/client/schema/snapshots/fuel_core_client__client__schema__node_info__tests__node_info_query_gql_output.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ query QueryNodeInfo {
1212
maxSize
1313
maxDepth
1414
nodeVersion
15+
indexation {
16+
balances
17+
coinsToSpend
18+
assetMetadata
19+
}
1520
txPoolStats {
1621
txCount
1722
totalGas

crates/client/src/client/types/node_info.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::client::schema::{
22
self,
3-
node_info::TxPoolStats,
3+
node_info::{
4+
IndexationFlags,
5+
TxPoolStats,
6+
},
47
};
58

69
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -12,6 +15,7 @@ pub struct NodeInfo {
1215
pub max_size: u64,
1316
pub max_depth: u64,
1417
pub node_version: String,
18+
pub indexation: IndexationFlags,
1519
pub tx_pool_stats: TxPoolStats,
1620
}
1721

@@ -27,6 +31,7 @@ impl From<schema::node_info::NodeInfo> for NodeInfo {
2731
max_size: value.max_size.into(),
2832
max_depth: value.max_depth.into(),
2933
node_version: value.node_version,
34+
indexation: value.indexation,
3035
tx_pool_stats: value.tx_pool_stats,
3136
}
3237
}

crates/fuel-core/src/graphql_api/database.rs

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use crate::fuel_core_graphql_api::{
2-
database::arc_wrapper::ArcWrapper,
3-
ports::{
4-
OffChainDatabase,
5-
OnChainDatabase,
1+
use crate::{
2+
database::database_description::IndexationKind,
3+
fuel_core_graphql_api::{
4+
database::arc_wrapper::ArcWrapper,
5+
ports::{
6+
OffChainDatabase,
7+
OnChainDatabase,
8+
},
69
},
710
};
811
use fuel_core_services::yield_stream::StreamYieldExt;
@@ -67,6 +70,7 @@ use std::{
6770
borrow::Cow,
6871
sync::Arc,
6972
};
73+
use strum::IntoEnumIterator;
7074

7175
use super::ports::worker;
7276

@@ -88,12 +92,31 @@ pub struct ReadDatabase {
8892
on_chain: Box<dyn AtomicView<LatestView = OnChainView>>,
8993
/// The off-chain database view provider.
9094
off_chain: Box<dyn AtomicView<LatestView = OffChainView>>,
91-
/// The flag that indicates whether the Balances indexation is enabled.
92-
balances_indexation_enabled: bool,
93-
/// The flag that indicates whether the CoinsToSpend indexation is enabled.
94-
coins_to_spend_indexation_enabled: bool,
95-
/// The flag that indicates whether the AssetMetadata indexation is enabled.
96-
asset_metadata_indexation_enabled: bool,
95+
/// The flag indicating which indexation is enabled.
96+
indexation_flags: IndexationFlags,
97+
}
98+
99+
#[derive(Clone)]
100+
pub struct IndexationFlags(u8);
101+
102+
impl IndexationFlags {
103+
pub fn new() -> Self {
104+
Self(0)
105+
}
106+
107+
pub fn contains(&self, kind: &IndexationKind) -> bool {
108+
self.0 & (1 << *kind as u8) != 0
109+
}
110+
111+
pub fn insert(&mut self, kind: IndexationKind) {
112+
self.0 |= 1 << kind as u8;
113+
}
114+
}
115+
116+
impl Default for IndexationFlags {
117+
fn default() -> Self {
118+
Self::new()
119+
}
97120
}
98121

99122
impl ReadDatabase {
@@ -110,20 +133,32 @@ impl ReadDatabase {
110133
OnChain::LatestView: OnChainDatabase,
111134
OffChain::LatestView: OffChainDatabase,
112135
{
113-
let balances_indexation_enabled = off_chain.balances_indexation_enabled()?;
114-
let coins_to_spend_indexation_enabled =
115-
off_chain.coins_to_spend_indexation_enabled()?;
116-
let asset_metadata_indexation_enabled =
117-
off_chain.asset_metadata_indexation_enabled()?;
118-
136+
let mut indexation_flags = IndexationFlags::new();
137+
for kind in IndexationKind::iter() {
138+
match kind {
139+
IndexationKind::Balances => {
140+
if off_chain.balances_indexation_enabled()? {
141+
indexation_flags.insert(kind);
142+
}
143+
}
144+
IndexationKind::CoinsToSpend => {
145+
if off_chain.coins_to_spend_indexation_enabled()? {
146+
indexation_flags.insert(kind);
147+
}
148+
}
149+
IndexationKind::AssetMetadata => {
150+
if off_chain.asset_metadata_indexation_enabled()? {
151+
indexation_flags.insert(kind);
152+
}
153+
}
154+
}
155+
}
119156
Ok(Self {
120157
batch_size,
121158
genesis_height,
122159
on_chain: Box::new(ArcWrapper::new(on_chain)),
123160
off_chain: Box::new(ArcWrapper::new(off_chain)),
124-
balances_indexation_enabled,
125-
coins_to_spend_indexation_enabled,
126-
asset_metadata_indexation_enabled,
161+
indexation_flags,
127162
})
128163
}
129164

@@ -137,9 +172,7 @@ impl ReadDatabase {
137172
genesis_height: self.genesis_height,
138173
on_chain: self.on_chain.latest_view()?,
139174
off_chain: self.off_chain.latest_view()?,
140-
balances_indexation_enabled: self.balances_indexation_enabled,
141-
coins_to_spend_indexation_enabled: self.coins_to_spend_indexation_enabled,
142-
asset_metadata_indexation_enabled: self.asset_metadata_indexation_enabled,
175+
indexation_flags: self.indexation_flags.clone(),
143176
})
144177
}
145178

@@ -155,9 +188,7 @@ pub struct ReadView {
155188
pub(crate) genesis_height: BlockHeight,
156189
pub(crate) on_chain: OnChainView,
157190
pub(crate) off_chain: OffChainView,
158-
pub(crate) balances_indexation_enabled: bool,
159-
pub(crate) coins_to_spend_indexation_enabled: bool,
160-
pub(crate) asset_metadata_indexation_enabled: bool,
191+
pub(crate) indexation_flags: IndexationFlags,
161192
}
162193

163194
impl ReadView {
@@ -408,3 +439,26 @@ impl ReadView {
408439
self.off_chain.message_is_spent(nonce)
409440
}
410441
}
442+
443+
#[test]
444+
fn test_indexation_flags() {
445+
let mut indexation = IndexationFlags::new();
446+
assert!(!indexation.contains(&IndexationKind::Balances));
447+
assert!(!indexation.contains(&IndexationKind::CoinsToSpend));
448+
assert!(!indexation.contains(&IndexationKind::AssetMetadata));
449+
450+
indexation.insert(IndexationKind::Balances);
451+
assert!(indexation.contains(&IndexationKind::Balances));
452+
assert!(!indexation.contains(&IndexationKind::CoinsToSpend));
453+
assert!(!indexation.contains(&IndexationKind::AssetMetadata));
454+
455+
indexation.insert(IndexationKind::CoinsToSpend);
456+
assert!(indexation.contains(&IndexationKind::Balances));
457+
assert!(indexation.contains(&IndexationKind::CoinsToSpend));
458+
assert!(!indexation.contains(&IndexationKind::AssetMetadata));
459+
460+
indexation.insert(IndexationKind::AssetMetadata);
461+
assert!(indexation.contains(&IndexationKind::Balances));
462+
assert!(indexation.contains(&IndexationKind::CoinsToSpend));
463+
assert!(indexation.contains(&IndexationKind::AssetMetadata));
464+
}

crates/fuel-core/src/query/assets.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
database::database_description::IndexationKind,
23
fuel_core_graphql_api::database::ReadView,
34
graphql_api::storage::assets::AssetDetails,
45
};
@@ -10,7 +11,10 @@ use fuel_core_types::fuel_tx::AssetId;
1011

1112
impl ReadView {
1213
pub fn get_asset_details(&self, id: &AssetId) -> StorageResult<AssetDetails> {
13-
if self.asset_metadata_indexation_enabled {
14+
if self
15+
.indexation_flags
16+
.contains(&IndexationKind::AssetMetadata)
17+
{
1418
Ok(self
1519
.off_chain
1620
.asset_info(id)?

crates/fuel-core/src/query/balance.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
};
55

66
use crate::{
7+
database::database_description::IndexationKind,
78
fuel_core_graphql_api::database::ReadView,
89
graphql_api::storage::balances::TotalBalanceAmount,
910
};
@@ -41,7 +42,7 @@ impl ReadView {
4142
asset_id: AssetId,
4243
base_asset_id: AssetId,
4344
) -> StorageResult<AddressBalance> {
44-
let amount = if self.balances_indexation_enabled {
45+
let amount = if self.indexation_flags.contains(&IndexationKind::Balances) {
4546
self.off_chain.balance(&owner, &asset_id, &base_asset_id)?
4647
} else {
4748
AssetQuery::new(
@@ -73,7 +74,7 @@ impl ReadView {
7374
direction: IterDirection,
7475
base_asset_id: &'a AssetId,
7576
) -> impl Stream<Item = StorageResult<AddressBalance>> + 'a {
76-
if self.balances_indexation_enabled {
77+
if self.indexation_flags.contains(&IndexationKind::Balances) {
7778
futures::future::Either::Left(self.balances_with_cache(
7879
owner,
7980
start,

crates/fuel-core/src/schema/balance.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
database::database_description::IndexationKind,
23
fuel_core_graphql_api::{
34
api_service::ConsensusProvider,
45
query_costs,
@@ -111,7 +112,9 @@ impl BalanceQuery {
111112
) -> async_graphql::Result<Connection<AssetId, Balance, EmptyFields, EmptyFields>>
112113
{
113114
let query = ctx.read_view()?;
114-
if !query.balances_indexation_enabled && (before.is_some() || after.is_some()) {
115+
if !query.indexation_flags.contains(&IndexationKind::Balances)
116+
&& (before.is_some() || after.is_some())
117+
{
115118
return Err(anyhow!(
116119
"Can not use pagination when balances indexation is not available"
117120
)

crates/fuel-core/src/schema/coins.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
ExcludedCoinIds,
99
SpendQuery,
1010
},
11+
database::database_description::IndexationKind,
1112
fuel_core_graphql_api::{
1213
query_costs,
1314
storage::coins::CoinsToSpendIndexKey,
@@ -289,7 +290,9 @@ impl CoinQuery {
289290
query_per_asset.truncate(max_input as usize);
290291

291292
let read_view = ctx.read_view()?;
292-
let indexation_available = read_view.coins_to_spend_indexation_enabled;
293+
let indexation_available = read_view
294+
.indexation_flags
295+
.contains(&IndexationKind::CoinsToSpend);
293296
if indexation_available {
294297
coins_to_spend_with_cache(
295298
owner,

0 commit comments

Comments
 (0)