Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.9.3"
version = "0.9.4"
edition = "2024"
rust-version = "1.88"
authors = ["init4"]
Expand Down
6 changes: 6 additions & 0 deletions crates/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ tracing.workspace = true
futures-util = "0.3.31"
tokio.workspace = true
auto_impl = "1.3.0"

[dev-dependencies]
serde_json.workspace = true
reth-db = { workspace = true, features = ["test-utils"] }
reth-exex-test-utils.workspace = true
signet-constants = { workspace = true, features = ["test-utils"] }
5 changes: 3 additions & 2 deletions crates/db/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Type conversion traits and implementations for converting between Reth, Alloy, and Signet types.
//!
//! This module provides a set of conversion traits that enable seamless interoperability
//! between different type systems used in the Ethereum ecosystem:
//! This module provides a set of conversion traits that enable seamless
//! interoperability between different type systems used in the Ethereum
//! ecosystem:
//!
//! - **Reth types**: Core primitives from the Reth Ethereum client
//! - **Alloy types**: Modern Ethereum types from the Alloy framework
Expand Down
35 changes: 4 additions & 31 deletions crates/db/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ use alloy::{
primitives::{Address, B256, BlockNumber, U256, map::HashSet},
};
use reth::{
primitives::{Account, StaticFileSegment},
primitives::StaticFileSegment,
providers::{
AccountReader, BlockBodyIndicesProvider, BlockNumReader, BlockReader, BlockWriter, Chain,
DBProvider, HistoryWriter, OriginalValuesKnown, ProviderError, ProviderResult,
StageCheckpointWriter, StateWriter, StaticFileProviderFactory, StaticFileWriter,
StorageLocation,
BlockBodyIndicesProvider, BlockNumReader, BlockReader, BlockWriter, Chain, DBProvider,
HistoryWriter, OriginalValuesKnown, ProviderError, ProviderResult, StageCheckpointWriter,
StateWriter, StaticFileProviderFactory, StaticFileWriter, StorageLocation,
},
};
use reth_db::{
PlainAccountState,
cursor::{DbCursorRO, DbCursorRW},
models::{BlockNumberAddress, StoredBlockBodyIndices},
tables,
Expand Down Expand Up @@ -104,31 +102,6 @@ where
.map_err(Into::into)
}

/// Increase the balance of an account.
fn mint_eth(&self, address: Address, amount: U256) -> ProviderResult<Account> {
let mut account = self.basic_account(&address)?.unwrap_or_default();
account.balance = account.balance.saturating_add(amount);
self.tx_ref().put::<PlainAccountState>(address, account)?;
trace!(%address, balance = %account.balance, "minting ETH");
Ok(account)
}

/// Decrease the balance of an account.
fn burn_eth(&self, address: Address, amount: U256) -> ProviderResult<Account> {
let mut account = self.basic_account(&address)?.unwrap_or_default();
if amount > account.balance {
warn!(
balance = %account.balance,
amount = %amount,
"burning more than balance"
);
}
account.balance = account.balance.saturating_sub(amount);
self.tx_ref().put::<PlainAccountState>(address, account)?;
trace!(%address, balance = %account.balance, "burning ETH");
Ok(account)
}

fn insert_signet_header(
&self,
header: Zenith::BlockHeader,
Expand Down
13 changes: 2 additions & 11 deletions crates/db/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{DbExtractionResults, DbSignetEvent, RuChain, SignetDbRw};
use alloy::primitives::{Address, B256, BlockNumber, U256};
use alloy::primitives::{B256, BlockNumber};
use itertools::Itertools;
use reth::{
primitives::Account,
providers::{OriginalValuesKnown, ProviderResult, StorageLocation},
};
use reth::providers::{OriginalValuesKnown, ProviderResult, StorageLocation};
use reth_db::models::StoredBlockBodyIndices;
use signet_evm::BlockResult;
use signet_node_types::NodeTypesDbTrait;
Expand All @@ -31,12 +28,6 @@ pub trait RuWriter {
/// Get the latest journal hash from the DB.
fn latest_journal_hash(&self) -> ProviderResult<B256>;

/// Increase the balance of an account.
fn mint_eth(&self, address: Address, amount: U256) -> ProviderResult<Account>;

/// Decrease the balance of an account.
fn burn_eth(&self, address: Address, amount: U256) -> ProviderResult<Account>;

/// Store a zenith header in the DB
fn insert_signet_header(
&self,
Expand Down
33 changes: 33 additions & 0 deletions crates/db/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use alloy::genesis::Genesis;
use reth::{
chainspec::ChainSpec,
providers::{ProviderFactory, providers::StaticFileProvider},
};
use reth_db::test_utils::{create_test_rw_db, create_test_static_files_dir};
use reth_exex_test_utils::TmpDB as TmpDb;
use signet_node_types::SignetNodeTypes;
use std::sync::{Arc, OnceLock};

static GENESIS_JSON: &str = include_str!("../../../../tests/artifacts/local.genesis.json");

static SPEC: OnceLock<Arc<ChainSpec>> = OnceLock::new();

/// Returns a chain spec for tests.
pub fn chain_spec() -> Arc<ChainSpec> {
SPEC.get_or_init(|| {
let genesis: Genesis = serde_json::from_str(GENESIS_JSON).expect("valid genesis json");
Arc::new(genesis.into())
})
.clone()
}

/// Create a provider factory with a chain spec
pub fn create_test_provider_factory() -> ProviderFactory<SignetNodeTypes<TmpDb>> {
let (static_dir, _) = create_test_static_files_dir();
let db = create_test_rw_db();
ProviderFactory::new(
db,
chain_spec(),
StaticFileProvider::read_write(static_dir.keep()).expect("static file provider"),
)
}
82 changes: 82 additions & 0 deletions crates/db/tests/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#[path = "./common/mod.rs"]
mod test_common;

use alloy::{
consensus::{BlockBody, BlockHeader, Signed, TxEip1559, TxEnvelope},
primitives::{Address, B256, U256},
signers::Signature,
};
use reth::providers::{BlockNumReader, BlockReader};
use signet_constants::test_utils::{DEPLOY_HEIGHT, RU_CHAIN_ID};
use signet_db::RuWriter;
use signet_types::primitives::{RecoveredBlock, SealedBlock, SealedHeader};
use signet_zenith::Zenith;

#[test]
fn test_ru_writer() {
let factory = test_common::create_test_provider_factory();

let writer = factory.provider_rw().unwrap();

dbg!(writer.last_block_number().unwrap());
}

#[test]
fn test_insert_signet_block() {
let factory = test_common::create_test_provider_factory();
let writer = factory.provider_rw().unwrap();

let journal_hash = B256::repeat_byte(0x55);
let header = Some(Zenith::BlockHeader {
rollupChainId: U256::from(RU_CHAIN_ID),
hostBlockNumber: U256::from(DEPLOY_HEIGHT),
gasLimit: U256::from(30_000_000),
rewardAddress: Address::repeat_byte(0x11),
blockDataHash: B256::repeat_byte(0x22),
});

let block = RecoveredBlock {
block: SealedBlock {
header: SealedHeader::new(alloy::consensus::Header::default()),
body: BlockBody {
transactions: std::iter::repeat_n(
TxEnvelope::Eip1559(Signed::new_unhashed(
TxEip1559::default(),
Signature::test_signature(),
))
.into(),
10,
)
.collect(),
ommers: vec![],
withdrawals: None,
},
},
senders: std::iter::repeat_n(Address::repeat_byte(0x33), 10).collect(),
};

writer
.insert_signet_block(header, &block, journal_hash, reth::providers::StorageLocation::Both)
.unwrap();

// Check basic updates
assert_eq!(writer.last_block_number().unwrap(), block.number());
assert_eq!(writer.latest_journal_hash().unwrap(), journal_hash);
assert_eq!(writer.get_journal_hash(block.number()).unwrap(), Some(journal_hash));
// This tests resolving `BlockId::Latest`
assert_eq!(writer.best_block_number().unwrap(), block.number());

// Check that the block can be loaded back
let loaded_block = writer
.recovered_block_range(block.number()..=block.number())
.unwrap()
.first()
.cloned()
.unwrap();
assert_eq!(loaded_block.header(), block.block.header.header());
assert_eq!(loaded_block.body().transactions.len(), block.block.body.transactions.len());

// Check that the ZenithHeader can be loaded back
let loaded_header = writer.get_signet_header(block.number()).unwrap();
assert_eq!(loaded_header, header);
}
186 changes: 186 additions & 0 deletions tests/artifacts/local.genesis.json

Large diffs are not rendered by default.