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

[feat] bc-db: add current ud

parent 9e329beb
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
//! Current meta datas //! Current meta datas
pub mod current_ud;
use self::current_ud::{CurrentUdDb, CurrentUdDbInternal};
use crate::blocks::fork_tree::ForkTree; use crate::blocks::fork_tree::ForkTree;
use crate::constants::*; use crate::constants::*;
use crate::*; use crate::*;
...@@ -38,6 +41,8 @@ pub enum CurrentMetaDataKey { ...@@ -38,6 +41,8 @@ pub enum CurrentMetaDataKey {
ForkTree, ForkTree,
/// Greatest wot id /// Greatest wot id
NextWotId, NextWotId,
/// Current Universal Dividend
CurrentUd,
} }
impl CurrentMetaDataKey { impl CurrentMetaDataKey {
...@@ -50,6 +55,7 @@ impl CurrentMetaDataKey { ...@@ -50,6 +55,7 @@ impl CurrentMetaDataKey {
Self::CurrentBlockchainTime => 3, Self::CurrentBlockchainTime => 3,
Self::ForkTree => 4, Self::ForkTree => 4,
Self::NextWotId => 5, Self::NextWotId => 5,
Self::CurrentUd => 6,
} }
} }
} }
...@@ -155,3 +161,16 @@ pub fn get_greatest_wot_id_<DB: BcDbInReadTx>(db: &DB) -> Result<WotId, DbError> ...@@ -155,3 +161,16 @@ pub fn get_greatest_wot_id_<DB: BcDbInReadTx>(db: &DB) -> Result<WotId, DbError>
Ok(WotId(0)) Ok(WotId(0))
} }
} }
/// Get current UD
pub fn get_current_ud<DB: BcDbInReadTx>(db: &DB) -> Result<Option<CurrentUdDb>, DbError> {
if let Some(v) = db
.db()
.get_int_store(CURRENT_METADATA)
.get(db.r(), CurrentMetaDataKey::CurrentUd.to_u32())?
{
Ok(from_db_value::<CurrentUdDbInternal>(v)?.into())
} else {
Ok(None)
}
}
// Copyright (C) 2017-2019 The AXIOM TEAM Association.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// 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/>.
//! Define entity CurrentUdDb
use dubp_block_doc::BlockDocument;
use dubp_common_doc::BlockNumber;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct CurrentUdDb {
pub amount: usize,
pub base: usize,
pub block_number: BlockNumber,
pub members_count: usize,
pub monetary_mass: usize,
pub common_time: u64,
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
pub struct CurrentUdDbInternal {
current: Option<CurrentUdDb>,
previous: Option<CurrentUdDb>,
}
impl Into<Option<CurrentUdDb>> for CurrentUdDbInternal {
fn into(self) -> Option<CurrentUdDb> {
self.current
}
}
impl CurrentUdDbInternal {
pub fn update(&mut self, block_doc: &BlockDocument) {
let BlockDocument::V10(ref block_doc_v10) = block_doc;
if let Some(dividend) = block_doc_v10.dividend {
self.previous = self.current;
self.current = Some(CurrentUdDb {
amount: dividend,
base: block_doc_v10.unit_base,
block_number: block_doc_v10.number,
members_count: block_doc_v10.members_count,
monetary_mass: block_doc_v10.monetary_mass,
common_time: block_doc_v10.median_time,
})
}
}
pub fn revert(&mut self) {
self.current = self.previous;
self.previous = None;
}
}
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
// ! Define read only trait // ! Define read only trait
use crate::blocks::BlockDb; use crate::blocks::BlockDb;
use crate::current_metadata::current_ud::CurrentUdDb;
use crate::indexes::identities::{IdentityDb, IdentityStateDb}; use crate::indexes::identities::{IdentityDb, IdentityStateDb};
use crate::{BcDbWithReaderStruct, DbReadable, DbReader}; use crate::{BcDbWithReaderStruct, DbReadable, DbReader};
use dubp_common_doc::{BlockNumber, Blockstamp}; use dubp_common_doc::{BlockNumber, Blockstamp};
...@@ -87,6 +88,7 @@ pub trait BcDbInReadTx: BcDbWithReader { ...@@ -87,6 +88,7 @@ pub trait BcDbInReadTx: BcDbWithReader {
fn get_idty_state_by_pubkey(&self, pubkey: &PubKey) fn get_idty_state_by_pubkey(&self, pubkey: &PubKey)
-> Result<Option<IdentityStateDb>, DbError>; -> Result<Option<IdentityStateDb>, DbError>;
fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError>; fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError>;
fn get_current_ud(&self) -> Result<Option<CurrentUdDb>, DbError>;
} }
impl<T> BcDbInReadTx for T impl<T> BcDbInReadTx for T
...@@ -135,4 +137,8 @@ where ...@@ -135,4 +137,8 @@ where
fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError> { fn get_identity_by_pubkey(&self, pubkey: &PubKey) -> Result<Option<IdentityDb>, DbError> {
crate::indexes::identities::get_identity_by_pubkey(self, pubkey) crate::indexes::identities::get_identity_by_pubkey(self, pubkey)
} }
#[inline]
fn get_current_ud(&self) -> Result<Option<CurrentUdDb>, DbError> {
crate::current_metadata::get_current_ud(self)
}
} }
...@@ -20,7 +20,9 @@ use dubp_block_doc::block::BlockDocumentTrait; ...@@ -20,7 +20,9 @@ use dubp_block_doc::block::BlockDocumentTrait;
use dubp_block_doc::BlockDocument; use dubp_block_doc::BlockDocument;
use dubp_common_doc::traits::Document; use dubp_common_doc::traits::Document;
use durs_bc_db_reader::constants::CURRENT_METADATA; use durs_bc_db_reader::constants::CURRENT_METADATA;
use durs_bc_db_reader::current_metadata::current_ud::CurrentUdDbInternal;
use durs_bc_db_reader::current_metadata::CurrentMetaDataKey; use durs_bc_db_reader::current_metadata::CurrentMetaDataKey;
use durs_bc_db_reader::from_db_value;
use durs_bc_db_reader::DbValue; use durs_bc_db_reader::DbValue;
/// Update CURRENT_METADATA /// Update CURRENT_METADATA
...@@ -43,6 +45,64 @@ pub fn update_current_metadata( ...@@ -43,6 +45,64 @@ pub fn update_current_metadata(
CurrentMetaDataKey::CurrentBlockchainTime.to_u32(), CurrentMetaDataKey::CurrentBlockchainTime.to_u32(),
&DbValue::U64(new_current_block.common_time()), &DbValue::U64(new_current_block.common_time()),
)?; )?;
// Update current UD
let BlockDocument::V10(ref block_v10) = new_current_block;
if block_v10.dividend.is_some() {
let mut current_ud_internal = db
.get_int_store(CURRENT_METADATA)
.get(w.as_ref(), CurrentMetaDataKey::CurrentUd.to_u32())?
.map(from_db_value::<CurrentUdDbInternal>)
.transpose()?
.unwrap_or_default();
current_ud_internal.update(new_current_block);
let current_ud_internal_bytes = durs_dbs_tools::to_bytes(&current_ud_internal)?;
db.get_int_store(CURRENT_METADATA).put(
w.as_mut(),
CurrentMetaDataKey::CurrentUd.to_u32(),
&DbValue::Blob(&current_ud_internal_bytes),
)?;
}
Ok(())
}
/// Revert CURRENT_METADATA
pub fn revert_current_metadata(
db: &Db,
w: &mut DbWriter,
new_current_block: &BlockDocument,
) -> Result<(), DbError> {
let new_current_blockstamp_bytes: Vec<u8> = new_current_block.blockstamp().into();
// Update current blockstamp
db.get_int_store(CURRENT_METADATA).put(
w.as_mut(),
CurrentMetaDataKey::CurrentBlockstamp.to_u32(),
&DbValue::Blob(&new_current_blockstamp_bytes),
)?;
// Update current common time (also named "blockchain time")
db.get_int_store(CURRENT_METADATA).put(
w.as_mut(),
CurrentMetaDataKey::CurrentBlockchainTime.to_u32(),
&DbValue::U64(new_current_block.common_time()),
)?;
// Revert current UD
let BlockDocument::V10(ref block_v10) = new_current_block;
if block_v10.dividend.is_some() {
let mut current_ud_internal = db
.get_int_store(CURRENT_METADATA)
.get(w.as_ref(), CurrentMetaDataKey::CurrentUd.to_u32())?
.map(from_db_value::<CurrentUdDbInternal>)
.transpose()?
.unwrap_or_default();
current_ud_internal.revert();
let current_ud_internal_bytes = durs_dbs_tools::to_bytes(&current_ud_internal)?;
db.get_int_store(CURRENT_METADATA).put(
w.as_mut(),
CurrentMetaDataKey::CurrentUd.to_u32(),
&DbValue::Blob(&current_ud_internal_bytes),
)?;
}
Ok(()) Ok(())
} }
...@@ -79,6 +79,7 @@ impl BlocksDBsWriteQuery { ...@@ -79,6 +79,7 @@ impl BlocksDBsWriteQuery {
} }
BlocksDBsWriteQuery::RevertBlock(block_db) => { BlocksDBsWriteQuery::RevertBlock(block_db) => {
trace!("BlocksDBsWriteQuery::WriteBlock..."); trace!("BlocksDBsWriteQuery::WriteBlock...");
crate::current_metadata::revert_current_metadata(db, w, &block_db.block)?;
crate::blocks::remove_block(db, w, block_db.block.number())?; crate::blocks::remove_block(db, w, block_db.block.number())?;
trace!("BlocksDBsWriteQuery::WriteBlock...finish"); trace!("BlocksDBsWriteQuery::WriteBlock...finish");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment