Skip to content

Commit

Permalink
added fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ogabrielides committed Aug 6, 2024
1 parent 9b9fb05 commit 2aa8416
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Drive Initialization
use crate::drive::Drive;
use crate::error::Error;

use crate::drive::system::{misc_path, misc_path_vec};
use crate::error::drive::DriveError;
use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods;
use crate::util::batch::GroveDbOpBatch;
use crate::util::grove_operations::QueryType;
use dpp::prelude::CoreBlockHeight;
use dpp::version::PlatformVersion;
use grovedb::{Element, TransactionArg};
use integer_encoding::VarInt;

Check warning on line 14 in packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unused import: `integer_encoding::VarInt`

warning: unused import: `integer_encoding::VarInt` --> packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs:14:5 | 14 | use integer_encoding::VarInt; | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 14 in packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unused import: `integer_encoding::VarInt`

warning: unused import: `integer_encoding::VarInt` --> packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs:14:5 | 14 | use integer_encoding::VarInt; | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

/// Genesis Core Height Key
#[cfg(any(feature = "server", feature = "verify"))]
pub const GENESIS_CORE_HEIGHT_KEY: &[u8; 1] = b"C";

impl Drive {
/// Stores the genesis core height in groveDB
pub fn store_genesis_core_height(
&self,
genesis_core_height: CoreBlockHeight,
transaction: TransactionArg,
platform_version: &PlatformVersion,
) -> Result<(), Error> {
let drive_version = &platform_version.drive;
let mut batch = GroveDbOpBatch::new();

// In Misc
batch.add_insert(
misc_path_vec(),
GENESIS_CORE_HEIGHT_KEY.to_vec(),
Element::Item(genesis_core_height.to_be_bytes().to_vec(), None),
);

self.grove_apply_batch(batch, false, transaction, drive_version)?;

Ok(())
}

/// Fetches the genesis core height in groveDB
pub fn fetch_genesis_core_height(
&self,
transaction: TransactionArg,
platform_version: &PlatformVersion,
) -> Result<CoreBlockHeight, Error> {
let drive_version = &platform_version.drive;

let genesis_core_height_vec = self
.grove_get(
(&misc_path()).into(),
GENESIS_CORE_HEIGHT_KEY,
QueryType::StatefulQuery,
transaction,
&mut vec![],
drive_version,
)?
.ok_or(Error::Drive(DriveError::CorruptedCodeExecution(
"genesis core height must exist",
)))
.and_then(|element| element.into_item_bytes().map_err(Error::GroveDB))?;

let genesis_core_height =
u32::from_be_bytes(genesis_core_height_vec.try_into().map_err(|_| {
Error::Drive(DriveError::CorruptedSerialization(
"storage value must be a u32".to_string(),
))
})?);

Ok((genesis_core_height))

Check warning on line 72 in packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

consider removing unnecessary double parentheses

warning: consider removing unnecessary double parentheses --> packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs:72:12 | 72 | Ok((genesis_core_height)) | ^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens = note: `#[warn(clippy::double_parens)]` on by default

Check warning on line 72 in packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unnecessary parentheses around function argument

warning: unnecessary parentheses around function argument --> packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs:72:12 | 72 | Ok((genesis_core_height)) | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 72 - Ok((genesis_core_height)) 72 + Ok(genesis_core_height) |

Check warning on line 72 in packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unnecessary parentheses around function argument

warning: unnecessary parentheses around function argument --> packages/rs-drive/src/drive/initialization/genesis_core_height/mod.rs:72:12 | 72 | Ok((genesis_core_height)) | ^ ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 72 - Ok((genesis_core_height)) 72 + Ok(genesis_core_height) |
}
}
2 changes: 1 addition & 1 deletion packages/rs-drive/src/drive/initialization/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Drive Initialization
mod store_genesis_core_height;
mod genesis_core_height;
mod v0;

use crate::drive::Drive;
Expand Down

This file was deleted.

0 comments on commit 2aa8416

Please sign in to comment.