Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new index for tracking Asset metadata #2445

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- [2445](https://github.com/FuelLabs/fuel-core/pull/2445): Added GQL endpoint for querying asset details.
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `Unknown` variant to `ConsensusParameters` graphql queries
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `Unknown` variant to `Block` graphql queries
- [2154](https://github.com/FuelLabs/fuel-core/pull/2154): Added `TransactionType` type in `fuel-client`
Expand Down
18 changes: 18 additions & 0 deletions crates/client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ scalar Address

scalar AssetId

type AssetInfoDetails {
contractId: HexString!
subId: HexString!
totalSupply: U64!
}

type Balance {
owner: Address!
amount: U64!
Expand Down Expand Up @@ -850,6 +856,18 @@ type ProgramState {
}

type Query {
assetDetails(
"""
ID of the Asset
"""
id: AssetId!
): AssetInfoDetails
assetId(
"""
ID of the Asset
"""
id: AssetId!
): Boolean
"""
Read register value by index.
"""
Expand Down
15 changes: 15 additions & 0 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ use pagination::{
PaginationRequest,
};
use schema::{
assets::{
AssetInfoArg,
AssetInfoDetails,
},
balance::BalanceArgs,
blob::BlobByIdArgs,
block::BlockByIdArgs,
Expand Down Expand Up @@ -1191,6 +1195,17 @@ impl FuelClient {
.transpose()?;
Ok(status)
}

pub async fn asset_info(
&self,
asset_id: &AssetId,
) -> io::Result<Option<AssetInfoDetails>> {
let query = schema::assets::AssetInfoQuery::build(AssetInfoArg {
id: (*asset_id).into(),
});
let asset_info = self.query(query).await?.asset_details.map(Into::into);
Ok(asset_info)
}
}

#[cfg(any(test, feature = "test-helpers"))]
Expand Down
1 change: 1 addition & 0 deletions crates/client/src/client/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::client::pagination::{
};
pub use primitives::*;

pub mod assets;
pub mod balance;
pub mod blob;
pub mod block;
Expand Down
30 changes: 30 additions & 0 deletions crates/client/src/client/schema/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::client::schema::{
schema,
AssetId,
HexString,
U64,
};

#[derive(cynic::QueryVariables, Debug)]
pub struct AssetInfoArg {
pub id: AssetId,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
variables = "AssetInfoArg"
)]
pub struct AssetInfoQuery {
#[arguments(id: $id)]
pub asset_details: Option<AssetInfoDetails>,
}

#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct AssetInfoDetails {
pub sub_id: HexString,
pub contract_id: HexString,
pub total_supply: U64,
}
8 changes: 8 additions & 0 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ use fuel_core_types::{
};
use std::sync::Arc;

use super::storage::assets::AssetDetails;

pub trait OffChainDatabase: Send + Sync {
fn block_height(&self, block_id: &BlockId) -> StorageResult<BlockHeight>;

Expand Down Expand Up @@ -112,6 +114,10 @@ pub trait OffChainDatabase: Send + Sync {
) -> StorageResult<Option<RelayedTransactionStatus>>;

fn message_is_spent(&self, nonce: &Nonce) -> StorageResult<bool>;

fn asset_info(&self, asset_id: &AssetId) -> StorageResult<Option<AssetDetails>>;

fn asset_exists(&self, asset_id: &AssetId) -> StorageResult<bool>;
}

/// The on chain database port expected by GraphQL API service.
Expand Down Expand Up @@ -273,6 +279,7 @@ pub mod worker {
},
},
graphql_api::storage::{
assets::AssetsInfo,
da_compression::*,
old::{
OldFuelBlockConsensus,
Expand Down Expand Up @@ -336,6 +343,7 @@ pub mod worker {
+ StorageMutate<DaCompressionTemporalRegistryIndex, Error = StorageError>
+ StorageMutate<DaCompressionTemporalRegistryTimestamps, Error = StorageError>
+ StorageMutate<DaCompressionTemporalRegistryEvictorCache, Error = StorageError>
+ StorageMutate<AssetsInfo, Error = StorageError>
{
fn record_tx_id_owner(
&mut self,
Expand Down
3 changes: 3 additions & 0 deletions crates/fuel-core/src/graphql_api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use fuel_core_types::{
};
use statistic::StatisticTable;

pub mod assets;
pub mod blocks;
pub mod coins;
pub mod contracts;
Expand Down Expand Up @@ -113,6 +114,8 @@ pub enum Column {
DaCompressionTemporalRegistryScriptCode = 21,
/// See [`DaCompressionTemporalRegistryPredicateCode`](da_compression::DaCompressionTemporalRegistryPredicateCode)
DaCompressionTemporalRegistryPredicateCode = 22,
/// See [`AssetsInfo`](assets::AssetsInfo)
AssetsInfo = 23,
}

impl Column {
Expand Down
48 changes: 48 additions & 0 deletions crates/fuel-core/src/graphql_api/storage/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use fuel_core_storage::{
blueprint::plain::Plain,
codec::{
postcard::Postcard,
raw::Raw,
},
structured_storage::TableWithBlueprint,
Mappable,
};
use fuel_core_types::{
fuel_merkle::common::Bytes32,
fuel_tx::{
AssetId,
ContractId,
},
};

/// Contract info
pub struct AssetsInfo;

pub type AssetDetails = (ContractId, Bytes32, u64); // (contract_id, sub_id, total_amount)

impl Mappable for AssetsInfo {
type Key = AssetId;
type OwnedKey = Self::Key;
type Value = Self::OwnedValue;
type OwnedValue = AssetDetails;
}

impl TableWithBlueprint for AssetsInfo {
type Blueprint = Plain<Raw, Postcard>;
type Column = super::Column;

fn column() -> Self::Column {
Self::Column::AssetsInfo
}
}

#[cfg(test)]
mod test {
use super::*;

fuel_core_storage::basic_storage_tests!(
AssetsInfo,
<AssetsInfo as Mappable>::Key::default(),
<AssetsInfo as Mappable>::Value::default()
);
}
39 changes: 35 additions & 4 deletions crates/fuel-core/src/graphql_api/worker_service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::{
da_compression::da_compress_block,
storage::old::{
OldFuelBlockConsensus,
OldFuelBlocks,
OldTransactions,
storage::{
assets::AssetsInfo,
old::{
OldFuelBlockConsensus,
OldFuelBlocks,
OldTransactions,
},
},
};
use crate::{
Expand Down Expand Up @@ -63,9 +66,11 @@ use fuel_core_types::{
CoinPredicate,
CoinSigned,
},
AssetId,
Contract,
Input,
Output,
Receipt,
Transaction,
TxId,
UniqueIdentifier,
Expand Down Expand Up @@ -356,6 +361,32 @@ where
)
.into());
}

for receipt in result.receipts() {
match receipt {
Receipt::Mint {
sub_id,
contract_id,
..
}
| Receipt::Burn {
sub_id,
contract_id,
..
} => {
let asset_id = AssetId::from(**contract_id);
let current_count = db
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to increase/decrease based on the minted/burned amount=)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 😄 ab09b8d

.storage::<AssetsInfo>()
.get(&asset_id)?
.map(|info| info.2 + 1)
.unwrap_or(1);

db.storage::<AssetsInfo>()
.insert(&asset_id, &(*contract_id, **sub_id, current_count))?;
}
_ => {}
}
}
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-core/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod assets;
mod balance;
mod blob;
mod block;
Expand Down
23 changes: 23 additions & 0 deletions crates/fuel-core/src/query/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{
fuel_core_graphql_api::database::ReadView,
graphql_api::storage::assets::AssetDetails,
};
use fuel_core_storage::{
not_found,
Result as StorageResult,
};
use fuel_core_types::fuel_tx::AssetId;

impl ReadView {
pub fn get_asset_exists(&self, id: AssetId) -> StorageResult<bool> {
self.off_chain.asset_exists(&id)
}

pub fn get_asset_details(&self, id: AssetId) -> StorageResult<AssetDetails> {
let asset = self
.off_chain
.asset_info(&id)?
.ok_or(not_found!(AssetDetails))?;
Ok(asset.into())
}
}
8 changes: 5 additions & 3 deletions crates/fuel-core/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use futures::{
use std::borrow::Cow;
use tokio_stream::StreamExt;

pub mod assets;
pub mod balance;
pub mod blob;
pub mod block;
Expand All @@ -51,6 +52,7 @@ pub mod relayed_tx;

#[derive(MergedObject, Default)]
pub struct Query(
assets::AssetInfoQuery,
dap::DapQuery,
balance::BalanceQuery,
blob::BlobQuery,
Expand Down Expand Up @@ -142,7 +144,7 @@ where
} else if let Some(last) = last {
(last, IterDirection::Reverse)
} else {
return Err(anyhow!("Either `first` or `last` should be provided"))
return Err(anyhow!("Either `first` or `last` should be provided"));
};

let start;
Expand Down Expand Up @@ -170,7 +172,7 @@ where
// Skip until start + 1
if key == start {
has_previous_page = true;
return true
return true;
}
}
}
Expand All @@ -184,7 +186,7 @@ where
// take until we've reached the end
if key == end {
has_next_page = true;
return false
return false;
}
}
count = count.saturating_sub(1);
Expand Down
Loading
Loading