Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayprabhu committed Nov 27, 2024
1 parent aec0006 commit 723b3f9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
16 changes: 6 additions & 10 deletions src/storage/store/account/verification_store.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use tracing::info;

use super::{
get_many_messages_as_bytes, get_message, make_fid_key, make_message_primary_key, make_ts_hash,
make_user_key, message_decode, read_fid_key,
make_fid_key, make_ts_hash, make_user_key, message_decode, read_fid_key,
store::{Store, StoreDef},
MessagesPage, StoreEventHandler, FID_BYTES, PAGE_SIZE_MAX, TS_HASH_LENGTH,
MessagesPage, StoreEventHandler, FID_BYTES, TS_HASH_LENGTH,
};
use crate::storage::util::increment_vec_u8;
use crate::{
core::error::HubError,
proto::msg::{
link_body::Target, Protocol, SignatureScheme, VerificationAddAddressBody,
VerificationRemoveBody,
},
storage::{store::account::delete_message_transaction, util::vec_to_u8_24},
proto::msg::{Protocol, SignatureScheme, VerificationAddAddressBody, VerificationRemoveBody},
storage::store::account::delete_message_transaction,
};
use crate::{proto::msg::message_data::Body, storage::db::PageOptions};
use crate::{proto::msg::LinkBody, storage::util::increment_vec_u8};
use crate::{
proto::msg::MessageData,
storage::constants::{RootPrefix, UserPostfix},
Expand All @@ -24,7 +20,7 @@ use crate::{
proto::msg::{Message, MessageType},
storage::db::{RocksDB, RocksDbTransactionBatch},
};
use std::{borrow::Borrow, convert::TryInto, sync::Arc};
use std::sync::Arc;

#[derive(Clone)]
pub struct VerificationStoreDef {
Expand Down
62 changes: 40 additions & 22 deletions src/storage/store/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,29 @@ impl ShardEngine {
) -> Result<Vec<HubEvent>, EngineError> {
let mut events = vec![];
for snapchain_txn in transactions {
let (_, txn_events) = self.replay_snapchain_txn(snapchain_txn, txn_batch)?;
let (account_root, txn_events) = self.replay_snapchain_txn(snapchain_txn, txn_batch)?;
// Reject early if account roots fail to match (shard roots will definitely fail)
if &account_root != &snapchain_txn.account_root {
warn!(
fid = snapchain_txn.fid,
new_account_root = hex::encode(&account_root),
tx_account_root = hex::encode(&snapchain_txn.account_root),
"Account root mismatch"
);
return Err(EngineError::HashMismatch);
}
events.extend(txn_events);
}

let root1 = self.stores.trie.root_hash()?;

if &root1 != shard_root {
warn!(
shard_id = self.shard_id,
new_shard_root = hex::encode(&root1),
tx_shard_root = hex::encode(shard_root),
"Shard root mismatch"
);
return Err(EngineError::HashMismatch);
}

Expand All @@ -296,6 +312,27 @@ impl ShardEngine {
let mut events = vec![];
let mut message_types = HashSet::new();

// System messages first, then user messages and finally prunes
for msg in &snapchain_txn.system_messages {
if let Some(onchain_event) = &msg.on_chain_event {
let event = self
.stores
.onchain_event_store
.merge_onchain_event(onchain_event.clone(), txn_batch);

match event {
Ok(hub_event) => {
self.update_trie(&hub_event, txn_batch)?;
events.push(hub_event.clone());
system_messages_count += 1;
}
Err(err) => {
warn!("Error merging onchain event: {:?}", err);
}
}
}
}

for msg in &snapchain_txn.user_messages {
// Errors are validated based on the shard root
let result = self.merge_message(msg, txn_batch);
Expand Down Expand Up @@ -338,26 +375,6 @@ impl ShardEngine {
}
}

for msg in &snapchain_txn.system_messages {
if let Some(onchain_event) = &msg.on_chain_event {
let event = self
.stores
.onchain_event_store
.merge_onchain_event(onchain_event.clone(), txn_batch);

match event {
Ok(hub_event) => {
self.update_trie(&hub_event, txn_batch)?;
events.push(hub_event.clone());
system_messages_count += 1;
}
Err(err) => {
warn!("Error merging onchain event: {:?}", err);
}
}
}
}

let account_root = self.stores.trie.get_hash(
&self.db,
txn_batch,
Expand All @@ -369,7 +386,8 @@ impl ShardEngine {
num_system_messages = total_system_messages,
user_messages_merged = user_messages_count,
system_messages_merged = system_messages_count,
account_root = hex::encode(&account_root),
new_account_root = hex::encode(&account_root),
tx_account_root = hex::encode(&snapchain_txn.account_root),
"Replayed transaction"
);

Expand Down
2 changes: 1 addition & 1 deletion src/storage/store/stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::proto::msg::MessageType;
use crate::storage::db::{RocksDB, RocksDbTransactionBatch};
use crate::storage::store::account::{
CastStore, CastStoreDef, IntoU8, LinkStore, OnchainEventStorageError, OnchainEventStore, Store,
StoreDef, StoreEventHandler,
StoreEventHandler,
};
use crate::storage::store::shard::ShardStore;
use crate::storage::trie::merkle_trie;
Expand Down
5 changes: 1 addition & 4 deletions src/utils/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ pub mod messages_factory {
}

pub mod verifications {
use message::{
reaction_body::Target, ReactionBody, ReactionType, VerificationAddAddressBody,
VerificationRemoveBody,
};
use message::{VerificationAddAddressBody, VerificationRemoveBody};

use super::*;

Expand Down

0 comments on commit 723b3f9

Please sign in to comment.