Skip to content

Commit

Permalink
Extraction of the FuelBlockSecondaryKeyBlockHeights table to an off…
Browse files Browse the repository at this point in the history
…-chain worker (#1671)

Closes #1583

The change moves the `FuelBlockSecondaryKeyBlockHeights` table and
related logic to the off-chain worker. Along with this, the change adds
a new `Merklelized` blueprint that maintains the binary Merkle tree over
the storage data. It supports only the insertion of the objects without
removing them. This blueprint replaces the logic that previously was
defined in the `Database`.

Some side changes caused by the main change:
- Now, each blueprint provides its own set of tests related to the logic
of this blueprint.
- The `TransactionStatus` uses `block_height` inside instead of the
`block_id`.
- The key for the dense merkle tree looks like:
  ```rust
  pub enum DenseMetadataKey<PrimaryKey> {
      /// The primary key of the `DenseMerkleMetadata`.
      Primary(PrimaryKey),
      #[default]
      /// The latest `DenseMerkleMetadata` of the table.
      Latest,
  }
  ```

---------

Co-authored-by: Voxelot <brandonkite92@gmail.com>
  • Loading branch information
xgreenx and Voxelot authored Feb 22, 2024
1 parent 7d49387 commit ed92738
Show file tree
Hide file tree
Showing 47 changed files with 1,423 additions and 789 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Description of the upcoming release here.

### Added

- [#1671](https://github.com/FuelLabs/fuel-core/pull/1671): Added a new `Merklized` blueprint that maintains the binary Merkle tree over the storage data. It supports only the insertion of the objects without removing them.

### Changed

- [#1671](https://github.com/FuelLabs/fuel-core/pull/1671): The logic related to the `FuelBlockIdsToHeights` is moved to the off-chain worker.
- [#1663](https://github.com/FuelLabs/fuel-core/pull/1663): Reduce the punishment criteria for mempool gossipping.
- [#1658](https://github.com/FuelLabs/fuel-core/pull/1658): Removed `Receipts` table. Instead, receipts are part of the `TransactionStatuses` table.
- [#1640](https://github.com/FuelLabs/fuel-core/pull/1640): Upgrade to fuel-vm 0.45.0.
Expand All @@ -33,6 +38,7 @@ Description of the upcoming release here.
- [#1636](https://github.com/FuelLabs/fuel-core/pull/1636): Add more docs to GraphQL DAP API.

#### Breaking
- [#1671](https://github.com/FuelLabs/fuel-core/pull/1671): The GraphQL API uses block height instead of the block id where it is possible. The transaction status contains `block_height` instead of the `block_id`.
- [#1675](https://github.com/FuelLabs/fuel-core/pull/1675): Simplify GQL schema by disabling contract resolvers in most cases, and just return a ContractId scalar instead.
- [#1658](https://github.com/FuelLabs/fuel-core/pull/1658): Receipts are part of the transaction status.
Removed `reason` from the `TransactionExecutionResult::Failed`. It can be calculated based on the program state and receipts.
Expand Down
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.

1 change: 1 addition & 0 deletions crates/client/assets/schema.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ input BalanceFilterInput {

type Block {
id: BlockId!
height: U32!
header: Header!
consensus: Consensus!
transactions: [Transaction!]!
Expand Down
7 changes: 5 additions & 2 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,12 @@ impl FuelClient {
Ok(block)
}

pub async fn block_by_height(&self, height: u32) -> io::Result<Option<types::Block>> {
pub async fn block_by_height(
&self,
height: BlockHeight,
) -> io::Result<Option<types::Block>> {
let query = schema::block::BlockByHeightQuery::build(BlockByHeightArgs {
height: Some(U32(height)),
height: Some(U32(height.into())),
});

let block = self.query(query).await?.block.map(Into::into);
Expand Down
17 changes: 16 additions & 1 deletion crates/client/src/client/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::client::schema::{
U32,
U64,
};
use fuel_core_types::fuel_crypto;
use fuel_core_types::{
fuel_crypto,
fuel_types::BlockHeight,
};

use super::{
tx::TransactionIdFragment,
Expand Down Expand Up @@ -87,6 +90,12 @@ pub struct BlockIdFragment {
pub id: BlockId,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Block")]
pub struct BlockHeightFragment {
pub height: U32,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ProduceBlockArgs {
pub start_timestamp: Option<Tai64Timestamp>,
Expand Down Expand Up @@ -159,6 +168,12 @@ impl Block {
}
}

impl From<BlockHeightFragment> for BlockHeight {
fn from(fragment: BlockHeightFragment) -> Self {
BlockHeight::new(fragment.height.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ query($id: TransactionId!) {
... on SuccessStatus {
transactionId
block {
id
height
}
time
programState {
Expand Down Expand Up @@ -57,7 +57,7 @@ query($id: TransactionId!) {
... on FailureStatus {
transactionId
block {
id
height
}
time
reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ query($owner: Address!, $after: String, $before: String, $first: Int, $last: Int
... on SuccessStatus {
transactionId
block {
id
height
}
time
programState {
Expand Down Expand Up @@ -60,7 +60,7 @@ query($owner: Address!, $after: String, $before: String, $first: Int, $last: Int
... on FailureStatus {
transactionId
block {
id
height
}
time
reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ query($after: String, $before: String, $first: Int, $last: Int) {
... on SuccessStatus {
transactionId
block {
id
height
}
time
programState {
Expand Down Expand Up @@ -60,7 +60,7 @@ query($after: String, $before: String, $first: Int, $last: Int) {
... on FailureStatus {
transactionId
block {
id
height
}
time
reason
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ query($id: TransactionId!) {
... on SuccessStatus {
transactionId
block {
id
height
}
time
programState {
Expand Down Expand Up @@ -139,7 +139,7 @@ query($id: TransactionId!) {
... on FailureStatus {
transactionId
block {
id
height
}
time
reason
Expand Down
6 changes: 3 additions & 3 deletions crates/client/src/client/schema/tx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::block::BlockIdFragment;
use super::block::BlockHeightFragment;
use crate::client::{
schema::{
schema,
Expand Down Expand Up @@ -178,7 +178,7 @@ pub struct SubmittedStatus {
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct SuccessStatus {
pub transaction_id: TransactionId,
pub block: BlockIdFragment,
pub block: BlockHeightFragment,
pub time: Tai64Timestamp,
pub program_state: Option<ProgramState>,
pub receipts: Vec<Receipt>,
Expand All @@ -188,7 +188,7 @@ pub struct SuccessStatus {
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct FailureStatus {
pub transaction_id: TransactionId,
pub block: BlockIdFragment,
pub block: BlockHeightFragment,
pub time: Tai64Timestamp,
pub reason: String,
pub program_state: Option<ProgramState>,
Expand Down
13 changes: 8 additions & 5 deletions crates/client/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ use fuel_core_types::{
Receipt,
Transaction,
},
fuel_types::canonical::Deserialize,
fuel_types::{
canonical::Deserialize,
BlockHeight,
},
fuel_vm::ProgramState,
};
use tai64::Tai64;
Expand Down Expand Up @@ -92,7 +95,7 @@ pub enum TransactionStatus {
submitted_at: Tai64,
},
Success {
block_id: String,
block_height: BlockHeight,
time: Tai64,
program_state: Option<ProgramState>,
receipts: Vec<Receipt>,
Expand All @@ -101,7 +104,7 @@ pub enum TransactionStatus {
reason: String,
},
Failure {
block_id: String,
block_height: BlockHeight,
time: Tai64,
reason: String,
program_state: Option<ProgramState>,
Expand All @@ -118,7 +121,7 @@ impl TryFrom<SchemaTxStatus> for TransactionStatus {
submitted_at: s.time.0,
},
SchemaTxStatus::SuccessStatus(s) => TransactionStatus::Success {
block_id: s.block.id.0.to_string(),
block_height: s.block.height.into(),
time: s.time.0,
program_state: s.program_state.map(TryInto::try_into).transpose()?,
receipts: s
Expand All @@ -128,7 +131,7 @@ impl TryFrom<SchemaTxStatus> for TransactionStatus {
.collect::<Result<Vec<_>, _>>()?,
},
SchemaTxStatus::FailureStatus(s) => TransactionStatus::Failure {
block_id: s.block.id.0.to_string(),
block_height: s.block.height.into(),
time: s.time.0,
reason: s.reason,
program_state: s.program_state.map(TryInto::try_into).transpose()?,
Expand Down
5 changes: 3 additions & 2 deletions crates/client/src/client/types/gas_price.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::client::schema;
use fuel_core_types::fuel_types::BlockHeight;

pub struct LatestGasPrice {
pub gas_price: u64,
pub block_height: u32,
pub block_height: BlockHeight,
}

// GraphQL Translation
impl From<schema::gas_price::LatestGasPrice> for LatestGasPrice {
fn from(value: schema::gas_price::LatestGasPrice) -> Self {
Self {
gas_price: value.gas_price.into(),
block_height: value.block_height.into(),
block_height: BlockHeight::new(value.block_height.into()),
}
}
}
Expand Down
Loading

0 comments on commit ed92738

Please sign in to comment.