refactor: make SharableDatabase more relax (#1274)

This commit is contained in:
rakita
2023-02-10 15:46:54 +01:00
committed by GitHub
parent 07ed660cae
commit b731bb9bb3
4 changed files with 39 additions and 7 deletions

View File

@@ -239,7 +239,7 @@ impl Command {
&self,
config: &Config,
db: &Arc<Env<WriteMap>>,
) -> NetworkConfig<ShareableDatabase<Env<WriteMap>>> {
) -> NetworkConfig<ShareableDatabase<Arc<Env<WriteMap>>>> {
let peers_file = (!self.network.no_persist_peers).then_some(&self.network.peers_file);
config.network_config(
db.clone(),

View File

@@ -30,7 +30,7 @@ impl Config {
/// Initializes network config from read data
pub fn network_config<DB: Database>(
&self,
db: Arc<DB>,
db: DB,
chain_spec: ChainSpec,
disable_discovery: bool,
bootnodes: Option<Vec<NodeRecord>>,

View File

@@ -3,6 +3,7 @@ use crate::{
transaction::{DbTx, DbTxMut},
Error,
};
use std::sync::Arc;
/// Implements the GAT method from:
/// <https://sabrinajewson.org/blog/the-better-alternative-to-lifetime-gats#the-better-gats>.
@@ -51,3 +52,35 @@ pub trait Database: for<'a> DatabaseGAT<'a> {
Ok(res)
}
}
// Generic over Arc
impl<'a, DB: Database> DatabaseGAT<'a> for Arc<DB> {
type TX = <DB as DatabaseGAT<'a>>::TX;
type TXMut = <DB as DatabaseGAT<'a>>::TXMut;
}
impl<DB: Database> Database for Arc<DB> {
fn tx(&self) -> Result<<Self as DatabaseGAT<'_>>::TX, Error> {
<DB as Database>::tx(self)
}
fn tx_mut(&self) -> Result<<Self as DatabaseGAT<'_>>::TXMut, Error> {
<DB as Database>::tx_mut(self)
}
}
// Generic over reference
impl<'a, DB: Database> DatabaseGAT<'a> for &DB {
type TX = <DB as DatabaseGAT<'a>>::TX;
type TXMut = <DB as DatabaseGAT<'a>>::TXMut;
}
impl<DB: Database> Database for &DB {
fn tx(&self) -> Result<<Self as DatabaseGAT<'_>>::TX, Error> {
<DB as Database>::tx(self)
}
fn tx_mut(&self) -> Result<<Self as DatabaseGAT<'_>>::TXMut, Error> {
<DB as Database>::tx_mut(self)
}
}

View File

@@ -6,7 +6,6 @@ use reth_db::{
};
use reth_interfaces::Result;
use reth_primitives::{rpc::BlockId, Block, BlockHash, BlockNumber, ChainInfo, Header, H256, U256};
use std::sync::Arc;
mod state;
pub use state::{
@@ -20,19 +19,19 @@ pub use state::{
/// This provider implements most provider or provider factory traits.
pub struct ShareableDatabase<DB> {
/// Database
db: Arc<DB>,
db: DB,
}
impl<DB> ShareableDatabase<DB> {
/// create new database provider
pub fn new(db: Arc<DB>) -> Self {
pub fn new(db: DB) -> Self {
Self { db }
}
}
impl<DB> Clone for ShareableDatabase<DB> {
impl<DB: Clone> Clone for ShareableDatabase<DB> {
fn clone(&self) -> Self {
Self { db: Arc::clone(&self.db) }
Self { db: self.db.clone() }
}
}