Skip to content
Snippets Groups Projects
Commit 460e66e8 authored by Éloïs's avatar Éloïs
Browse files

[ref] bc-db-writer: rearrangement by storage type (blocks, indexes)

parent 3b488219
Branches
No related tags found
1 merge request!193Resolve "Migrate high-volume DBs to LMDB"
Showing
with 76 additions and 82 deletions
......@@ -13,7 +13,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Describe fork tree
//! Blocks fork tree: define entity and read requests.
use dubp_common_doc::{BlockHash, BlockNumber, Blockstamp};
use log::error;
......
......@@ -13,7 +13,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Blockchain stored indexes.
//! Blockchain stored indexes: definition and read requests.
pub mod balance;
pub mod certs;
......
......@@ -13,6 +13,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Blocks storage: defien write requests.
pub mod fork_tree;
use crate::DbError;
use crate::*;
use dubp_block_doc::block::BlockDocumentTrait;
......@@ -62,10 +66,8 @@ pub fn insert_new_head_block(
if let Some(fork_tree) = fork_tree {
// Insert head block in fork tree
let removed_blockstamps = crate::writers::fork_tree::insert_new_head_block(
fork_tree,
dal_block.blockstamp(),
)?;
let removed_blockstamps =
crate::blocks::fork_tree::insert_new_head_block(fork_tree, dal_block.blockstamp())?;
// Insert head block in ForkBlocks
let blockstamp_bytes: Vec<u8> = dal_block.blockstamp().into();
fork_blocks_store.put(
......@@ -101,7 +103,7 @@ pub fn insert_new_fork_block(
) -> Result<bool, DbError> {
let bin_dal_block = durs_dbs_tools::to_bytes(&dal_block)?;
let blockstamp_bytes: Vec<u8> = dal_block.blockstamp().into();
if crate::writers::fork_tree::insert_new_fork_block(
if fork_tree::insert_new_fork_block(
fork_tree,
dal_block.block.blockstamp(),
unwrap!(dal_block.block.previous_hash()),
......
......@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Blocks fork tree: define write requests.
use crate::*;
use dubp_common_doc::BlockHash;
use durs_bc_db_reader::blocks::fork_tree::ForkTree;
......@@ -252,16 +254,8 @@ mod test {
// Check tree state
assert_eq!(*DEFAULT_FORK_WINDOW_SIZE, fork_tree.size());
assert_eq!(
<<<<<<< dev:lib/modules/blockchain/blockchain-dal/src/writers/fork_tree.rs
vec![(TreeNodeId(1), blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 2])],
fork_tree_db.read(|tree| tree.get_sheets())?
=======
vec![(
TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE + 4),
blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 2]
)],
fork_tree.get_sheets()
>>>>>>> [ref] blockchain: migrate forks & identities in LMDB:lib/modules/blockchain/bc-db-writer/src/writers/fork_tree.rs
);
Ok(())
......
......@@ -13,5 +13,9 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Currency parameters DB name
pub const CURRENCY_PARAMS_DB_NAME: &str = "params.db";
//! Blockchain stored indexes: write requests.
pub mod certs;
pub mod dividends;
pub mod identities;
pub mod transactions;
......@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Certifications stored indexes: write requests.
use crate::{BinFreeStructDb, Db, DbError};
use dubp_common_doc::BlockNumber;
use dubp_currency_params::CurrencyParameters;
......
......@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Universal dividends stored indexes: write requests.
use crate::*;
use dubp_common_doc::BlockNumber;
use dubp_user_docs::documents::transaction::*;
......
......@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Identities stored indexes: write requests.
use crate::{BinFreeStructDb, Db, DbError, MsExpirV10Datas};
use dubp_common_doc::traits::Document;
use dubp_common_doc::{BlockNumber, Blockstamp};
......
......@@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Transactions stored indexes: write requests.
use dubp_user_docs::documents::transaction::*;
use durs_common_tools::fatal_error;
......@@ -495,7 +497,7 @@ mod tests {
// Open currencys_db in memory mode
let currency_dbs = CurrencyV10DBs::open(None);
// Create first g1 UD for cgeek and tortue
writers::dividend::create_du(
crate::indexes::dividends::create_du(
&currency_dbs.du_db,
&currency_dbs.balances_db,
&SourceAmount(TxAmount(1000), TxBase(0)),
......
......@@ -32,10 +32,8 @@ extern crate log;
#[macro_use]
extern crate serde_derive;
/// Define crate constants
pub mod constants;
/// Contains all write databases functions
pub mod blocks;
pub mod indexes;
pub mod writers;
pub use durs_dbs_tools::kv_db::{
......@@ -47,7 +45,7 @@ pub use durs_dbs_tools::{
};
pub use durs_dbs_tools::{BinFreeStructDb, DbError};
use crate::writers::transaction::DbTxV10;
use crate::indexes::transactions::DbTxV10;
use dubp_common_doc::{BlockNumber, Blockstamp};
use dubp_indexes::sindex::UniqueIdUTXOv10;
use dubp_user_docs::documents::transaction::*;
......
......@@ -13,23 +13,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Block writer functions
pub mod block;
//! DELETION IN PROGRESS
/// Certification apply functions
pub mod certification;
/// Dividend apply functions
pub mod dividend;
/// Fork tree writer functions
pub mod fork_tree;
/// Identities writer functions
pub mod identity;
/// Databases write requests
/// DELETION IN PROGRESS
pub mod requests;
/// Transaction apply functions
pub mod transaction;
......@@ -13,7 +13,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::writers::transaction::DbTxV10;
use crate::indexes::transactions::DbTxV10;
use crate::*;
use dubp_block_doc::block::{BlockDocument, BlockDocumentTrait};
use dubp_common_doc::Blockstamp;
......@@ -71,14 +71,14 @@ impl BlocksDBsWriteQuery {
|| dal_block.blockstamp().id.0 + fork_window_size as u32
>= sync_target.expect("safe unwrap").id.0
{
super::block::insert_new_head_block(db, Some(fork_tree), dal_block)?;
crate::blocks::insert_new_head_block(db, Some(fork_tree), dal_block)?;
} else {
super::block::insert_new_head_block(db, None, dal_block)?;
crate::blocks::insert_new_head_block(db, None, dal_block)?;
}
}
BlocksDBsWriteQuery::RevertBlock(dal_block) => {
trace!("BlocksDBsWriteQuery::WriteBlock...");
super::block::remove_block(db, dal_block.block.number())?;
crate::blocks::remove_block(db, dal_block.block.number())?;
trace!("BlocksDBsWriteQuery::WriteBlock...finish");
}
}
......@@ -138,7 +138,7 @@ impl WotsDBsWriteQuery {
ref idty_doc,
ref ms_created_block_id,
) => {
writers::identity::create_identity(
crate::indexes::identities::create_identity(
currency_params,
&db,
&databases.ms_db,
......@@ -150,7 +150,7 @@ impl WotsDBsWriteQuery {
)?;
}
WotsDBsWriteQuery::RevertCreateIdentity(ref pubkey) => {
writers::identity::revert_create_identity(&db, &databases.ms_db, pubkey)?;
crate::indexes::identities::revert_create_identity(&db, &databases.ms_db, pubkey)?;
}
WotsDBsWriteQuery::RenewalIdentity(
ref pubkey,
......@@ -159,7 +159,7 @@ impl WotsDBsWriteQuery {
ms_created_block_id,
) => {
trace!("WotsDBsWriteQuery::RenewalIdentity...");
writers::identity::renewal_identity(
crate::indexes::identities::renewal_identity(
currency_params,
&db,
&databases.ms_db,
......@@ -177,7 +177,7 @@ impl WotsDBsWriteQuery {
ref current_bc_time,
ms_created_block_id,
) => {
writers::identity::renewal_identity(
crate::indexes::identities::renewal_identity(
currency_params,
&db,
&databases.ms_db,
......@@ -189,16 +189,20 @@ impl WotsDBsWriteQuery {
)?;
}
WotsDBsWriteQuery::ExcludeIdentity(ref pubkey, ref blockstamp) => {
writers::identity::exclude_identity(&db, pubkey, blockstamp, false)?;
crate::indexes::identities::exclude_identity(&db, pubkey, blockstamp, false)?;
}
WotsDBsWriteQuery::RevertExcludeIdentity(ref pubkey, ref blockstamp) => {
writers::identity::exclude_identity(&db, pubkey, blockstamp, true)?;
crate::indexes::identities::exclude_identity(&db, pubkey, blockstamp, true)?;
}
WotsDBsWriteQuery::RevokeIdentity(ref pubkey, ref blockstamp, ref explicit) => {
writers::identity::revoke_identity(&db, pubkey, blockstamp, *explicit, false)?;
crate::indexes::identities::revoke_identity(
&db, pubkey, blockstamp, *explicit, false,
)?;
}
WotsDBsWriteQuery::RevertRevokeIdentity(ref pubkey, ref blockstamp, ref explicit) => {
writers::identity::revoke_identity(&db, pubkey, blockstamp, *explicit, true)?;
crate::indexes::identities::revoke_identity(
&db, pubkey, blockstamp, *explicit, true,
)?;
}
WotsDBsWriteQuery::CreateCert(
ref source_pubkey,
......@@ -208,7 +212,7 @@ impl WotsDBsWriteQuery {
ref median_time,
) => {
trace!("WotsDBsWriteQuery::CreateCert...");
writers::certification::write_certification(
crate::indexes::certs::write_certification(
currency_params,
&db,
&databases.certs_db,
......@@ -222,7 +226,7 @@ impl WotsDBsWriteQuery {
}
WotsDBsWriteQuery::RevertCert(ref compact_doc, ref source, ref target) => {
trace!("WotsDBsWriteQuery::CreateCert...");
writers::certification::revert_write_cert(
crate::indexes::certs::revert_write_cert(
&db,
&databases.certs_db,
*compact_doc,
......@@ -232,10 +236,10 @@ impl WotsDBsWriteQuery {
trace!("WotsDBsWriteQuery::CreateCert...finish");
}
WotsDBsWriteQuery::ExpireCerts(ref created_block_id) => {
super::certification::expire_certs(&databases.certs_db, *created_block_id)?;
crate::indexes::certs::expire_certs(&databases.certs_db, *created_block_id)?;
}
WotsDBsWriteQuery::RevertExpireCert(ref source, ref target, ref created_block_id) => {
super::certification::revert_expire_cert(
crate::indexes::certs::revert_expire_cert(
&databases.certs_db,
*source,
*target,
......@@ -269,13 +273,17 @@ impl CurrencyDBsWriteQuery {
) -> Result<(), DbError> {
match *self {
CurrencyDBsWriteQuery::WriteTx(ref tx_doc) => {
super::transaction::apply_and_write_tx(blockstamp, &databases, tx_doc.deref())?;
crate::indexes::transactions::apply_and_write_tx(
blockstamp,
&databases,
tx_doc.deref(),
)?;
}
CurrencyDBsWriteQuery::RevertTx(ref dal_tx) => {
super::transaction::revert_tx(blockstamp, &databases, dal_tx.deref())?;
crate::indexes::transactions::revert_tx(blockstamp, &databases, dal_tx.deref())?;
}
CurrencyDBsWriteQuery::CreateUD(ref du_amount, ref block_id, ref members) => {
super::dividend::create_du(
crate::indexes::dividends::create_du(
&databases.du_db,
&databases.balances_db,
du_amount,
......@@ -285,7 +293,7 @@ impl CurrencyDBsWriteQuery {
)?;
}
CurrencyDBsWriteQuery::RevertUD(ref du_amount, ref block_id, ref members) => {
super::dividend::create_du(
crate::indexes::dividends::create_du(
&databases.du_db,
&databases.balances_db,
du_amount,
......
......@@ -138,11 +138,7 @@ pub fn check_and_apply_block(
expire_certs: None,
};
if durs_bc_db_writer::writers::block::insert_new_fork_block(
&bc.db,
&mut bc.fork_tree,
dal_block,
)
if durs_bc_db_writer::blocks::insert_new_fork_block(&bc.db, &mut bc.fork_tree, dal_block)
.expect("durs_bc_db_writer::writers::block::insert_new_fork_block() : DbError")
{
Ok(CheckAndApplyBlockReturn::ForkBlock)
......
......@@ -131,11 +131,11 @@ pub fn receive_blocks(bc: &mut BlockchainModule, blocks: Vec<BlockDocument>) {
}
// Save databases
if save_blocks_dbs {
durs_bc_db_writer::blocks::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
bc.db
.save()
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
durs_bc_db_writer::writers::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
}
if save_wots_dbs {
bc.wot_databases.save_dbs();
......
......@@ -119,7 +119,7 @@ mod tests {
// Insert mock blocks in forks_dbs
for block in &main_branch {
durs_bc_db_writer::writers::block::insert_new_head_block(
durs_bc_db_writer::blocks::insert_new_head_block(
&db,
Some(&mut fork_tree),
DbBlock {
......@@ -186,7 +186,7 @@ mod tests {
};
assert_eq!(
true,
durs_bc_db_writer::writers::block::insert_new_fork_block(
durs_bc_db_writer::blocks::insert_new_fork_block(
&db,
&mut fork_tree,
DbBlock {
......@@ -265,7 +265,7 @@ mod tests {
for block in blocks {
assert_eq!(
true,
durs_bc_db_writer::writers::block::insert_new_fork_block(
durs_bc_db_writer::blocks::insert_new_fork_block(
db,
fork_tree,
DbBlock {
......
......@@ -23,8 +23,8 @@ use dubp_user_docs::documents::transaction::{TxAmount, TxBase};
use dup_crypto::keys::*;
use durs_bc_db_reader::blocks::DbBlock;
use durs_bc_db_reader::indexes::sources::SourceAmount;
use durs_bc_db_writer::indexes::transactions::DbTxV10;
use durs_bc_db_writer::writers::requests::*;
use durs_bc_db_writer::writers::transaction::DbTxV10;
use durs_bc_db_writer::{BinFreeStructDb, DbError, TxV10Datas};
use durs_common_tools::fatal_error;
use durs_wot::data::{NewLinkResult, RemLinkResult};
......
......@@ -138,7 +138,7 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>)
if new_branch_is_valid {
// update main branch in fork tree
if let Err(err) = durs_bc_db_writer::writers::fork_tree::change_main_branch(
if let Err(err) = durs_bc_db_writer::blocks::fork_tree::change_main_branch(
&bc.db,
&mut bc.fork_tree,
old_current_blockstamp,
......@@ -148,11 +148,11 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>)
}
// save dbs
durs_bc_db_writer::blocks::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
bc.db
.save()
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
durs_bc_db_writer::writers::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
bc.wot_databases.save_dbs();
bc.currency_databases.save_dbs(true, true);
// Send events stackUpValidBlock
......
......@@ -86,11 +86,11 @@ pub fn apply_stackable_blocks(bc: &mut BlockchainModule) {
}
}
// Save databases
durs_bc_db_writer::blocks::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
bc.db
.save()
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
durs_bc_db_writer::writers::fork_tree::save_fork_tree(&bc.db, &bc.fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
bc.wot_databases.save_dbs();
bc.currency_databases.save_dbs(true, true);
break 'blockchain;
......
......@@ -84,7 +84,7 @@ pub fn execute(
println!();
println!("Write indexs in files...");
info!("Save blockchain and forks databases in files...");
durs_bc_db_writer::writers::fork_tree::save_fork_tree(&db, &fork_tree)
durs_bc_db_writer::blocks::fork_tree::save_fork_tree(&db, &fork_tree)
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
db.save()
.unwrap_or_else(|_| fatal_error!("DB corrupted, please reset data."));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment