Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write the dag relationship in batch #4304

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
7 changes: 7 additions & 0 deletions flexidag/src/consensusdb/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ where
Ok(())
}

pub fn flush_cache(&self, data: &[(S::Key, S::Value)]) -> Result<(), StoreError> {
for (key, value) in data {
self.cache.insert(key.clone(), value.clone());
}
Ok(())
}

/// Write directly from an iterator and do not cache any data. NOTE: this action also clears the cache
pub fn write_many_without_cache(
&self,
Expand Down
114 changes: 57 additions & 57 deletions flexidag/src/consensusdb/consensus_relations.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::schema::{KeyCodec, ValueCodec};
use super::{
db::DBStorage,
prelude::{BatchDbWriter, CachedDbAccess, DirectDbWriter, StoreError},
prelude::{CachedDbAccess, StoreError},
};
use crate::define_schema;
use rocksdb::WriteBatch;
use starcoin_crypto::HashValue as Hash;
use starcoin_storage::batch::{WriteBatch, WriteBatchData, WriteBatchWithColumn};
use starcoin_storage::storage::{InnerStore, WriteOp};
use starcoin_types::blockhash::{BlockHashes, BlockLevel};
use std::collections::HashMap;
use std::sync::Arc;

/// Reader API for `RelationsStore`.
pub trait RelationsStoreReader {
fn get_parents(&self, hash: Hash) -> Result<BlockHashes, StoreError>;
Expand Down Expand Up @@ -90,41 +91,6 @@ impl DbRelationsStore {
pub fn clone_with_new_cache(&self, cache_size: usize) -> Self {
Self::new(Arc::clone(&self.db), self.level, cache_size)
}

pub fn insert_batch(
&mut self,
batch: &mut WriteBatch,
hash: Hash,
parents: BlockHashes,
) -> Result<(), StoreError> {
if self.has(hash)? {
return Err(StoreError::KeyAlreadyExists(hash.to_string()));
}

// Insert a new entry for `hash`
self.parents_access
.write(BatchDbWriter::new(batch, &self.db), hash, parents.clone())?;

// The new hash has no children yet
self.children_access.write(
BatchDbWriter::new(batch, &self.db),
hash,
BlockHashes::new(Vec::new()),
)?;

// Update `children` for each parent
for parent in parents.iter().cloned() {
let mut children = (*self.get_children(parent)?).clone();
children.push(hash);
self.children_access.write(
BatchDbWriter::new(batch, &self.db),
parent,
BlockHashes::new(children),
)?;
}

Ok(())
}
}

impl RelationsStoreReader for DbRelationsStore {
Expand All @@ -147,35 +113,69 @@ impl RelationsStoreReader for DbRelationsStore {
}

impl RelationsStore for DbRelationsStore {
/// See `insert_batch` as well
/// TODO: use one function with DbWriter for both this function and insert_batch
fn insert(&self, hash: Hash, parents: BlockHashes) -> Result<(), StoreError> {
if self.has(hash)? {
return Err(StoreError::KeyAlreadyExists(hash.to_string()));
}

// Insert a new entry for `hash`
self.parents_access
.write(DirectDbWriter::new(&self.db), hash, parents.clone())?;
let mut parent_to_children = HashMap::new();
parent_to_children.insert(hash, vec![]);

// The new hash has no children yet
self.children_access.write(
DirectDbWriter::new(&self.db),
hash,
BlockHashes::new(Vec::new()),
)?;

// Update `children` for each parent
for parent in parents.iter().cloned() {
let mut children = (*self.get_children(parent)?).clone();
let mut children = match self.get_children(parent) {
Ok(children) => (*children).clone(),
Err(e) => match e {
StoreError::KeyNotFound(_) => vec![],
_ => return std::result::Result::Err(e),
},
};
children.push(hash);
self.children_access.write(
DirectDbWriter::new(&self.db),
parent,
BlockHashes::new(children),
)?;
parent_to_children.insert(parent, children);
Comment on lines +121 to +133
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize HashMap initialization and error handling

A few suggestions for improvement:

  1. The initial empty children vector for the new hash (line 122) is unnecessary as it's not used anywhere.
  2. The error handling for missing parents could be more explicit.

Consider this improved implementation:

-        let mut parent_to_children = HashMap::new();
-        parent_to_children.insert(hash, vec![]);
+        let mut parent_to_children = HashMap::with_capacity(parents.len());

         for parent in parents.iter().cloned() {
             let mut children = match self.get_children(parent) {
                 Ok(children) => (*children).clone(),
                 Err(e) => match e {
-                    StoreError::KeyNotFound(_) => vec![],
+                    StoreError::KeyNotFound(_) => {
+                        return Err(StoreError::KeyNotFound(
+                            format!("Parent block {} not found", parent)
+                        ));
+                    }
                     _ => return std::result::Result::Err(e),
                 },
             };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let mut parent_to_children = HashMap::new();
parent_to_children.insert(hash, vec![]);
// The new hash has no children yet
self.children_access.write(
DirectDbWriter::new(&self.db),
hash,
BlockHashes::new(Vec::new()),
)?;
// Update `children` for each parent
for parent in parents.iter().cloned() {
let mut children = (*self.get_children(parent)?).clone();
let mut children = match self.get_children(parent) {
Ok(children) => (*children).clone(),
Err(e) => match e {
StoreError::KeyNotFound(_) => vec![],
_ => return std::result::Result::Err(e),
},
};
children.push(hash);
self.children_access.write(
DirectDbWriter::new(&self.db),
parent,
BlockHashes::new(children),
)?;
parent_to_children.insert(parent, children);
let mut parent_to_children = HashMap::with_capacity(parents.len());
for parent in parents.iter().cloned() {
let mut children = match self.get_children(parent) {
Ok(children) => (*children).clone(),
Err(e) => match e {
StoreError::KeyNotFound(_) => {
return Err(StoreError::KeyNotFound(
format!("Parent block {} not found", parent)
));
}
_ => return std::result::Result::Err(e),
},
};
children.push(hash);
parent_to_children.insert(parent, children);

}

let batch = WriteBatchWithColumn {
data: vec![
WriteBatchData {
column: PARENTS_CF.to_string(),
row_data: WriteBatch::new_with_rows(vec![(
hash.to_vec(),
WriteOp::Value(
<Arc<Vec<Hash>> as ValueCodec<RelationParent>>::encode_value(&parents)?,
),
)]),
},
WriteBatchData {
column: CHILDREN_CF.to_string(),
row_data: WriteBatch::new_with_rows(
parent_to_children
.iter()
.map(|(key, value)| {
std::result::Result::Ok((
key.to_vec(),
WriteOp::Value(<Arc<Vec<Hash>> as ValueCodec<
RelationChildren,
>>::encode_value(
&Arc::new(value.clone())
)?),
))
})
.collect::<std::result::Result<Vec<_>, StoreError>>()?,
),
},
],
};
self.db
.write_batch_with_column(batch)
.map_err(|e| StoreError::DBIoError(format!("Failed to write batch when writing batch with column for the dag releationship: {:?}", e)))?;

self.parents_access.flush_cache(&[(hash, parents)])?;
self.children_access.flush_cache(
&parent_to_children
.into_iter()
.map(|(key, value)| (key, BlockHashes::new(value)))
.collect::<Vec<_>>(),
)?;

Ok(())
}
}
Expand Down
11 changes: 4 additions & 7 deletions flexidag/src/ghostdag/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::util::Refs;
use crate::consensusdb::schemadb::{GhostdagStoreReader, HeaderStoreReader, RelationsStoreReader};
use crate::reachability::reachability_service::ReachabilityService;
use crate::types::{ghostdata::GhostdagData, ordering::*};
use anyhow::{bail, ensure, Context, Result};
use anyhow::{ensure, Context, Result};
use parking_lot::RwLock;
use starcoin_crypto::HashValue as Hash;
use starcoin_logger::prelude::*;
Expand Down Expand Up @@ -221,12 +221,9 @@ impl<
.map(|header| header.id())
.collect::<HashSet<_>>()
{
if header.number() < 10000000 {
// no bail before 10000000
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
} else {
bail!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
}
warn!("The data of blue set is not equal when executing the block: {:?}, for {:?}, checking data: {:?}", header.id(), blue_blocks.iter().map(|header| header.id()).collect::<Vec<_>>(), new_block_data.mergeset_blues);
new_block_data.mergeset_blues =
Arc::new(blue_blocks.iter().map(|header| header.id()).collect());
}

let blue_score = self
Expand Down
5 changes: 5 additions & 0 deletions flexidag/src/prune/pruning_point_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ impl<T: ReachabilityStoreReader + Clone> PruningPointManagerT<T> {
min_required_blue_score_for_next_pruning_point
);

debug!("previous_pruning_point: {:?}, previous_ghostdata: {:?}, next_ghostdata: {:?}, pruning_depth: {:?}, pruning_finality: {:?}",
previous_pruning_point, previous_ghostdata, next_ghostdata,
pruning_depth, pruning_finality,
);

let mut latest_pruning_ghost_data = previous_ghostdata.to_compact();
if min_required_blue_score_for_next_pruning_point + pruning_depth
<= next_ghostdata.blue_score
Expand Down
Loading
Loading