Skip to content

Commit

Permalink
Propagated the only_use_xor bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon committed Jun 15, 2023
1 parent 29ffba4 commit b368beb
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 157 deletions.
4 changes: 2 additions & 2 deletions massa-async-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ impl AsyncPool {
/// Resets the pool to its initial state
///
/// USED ONLY FOR BOOTSTRAP
pub fn reset(&mut self) {
pub fn reset(&mut self, only_use_xor: bool) {
self.db
.write()
.delete_prefix(ASYNC_POOL_PREFIX, STATE_CF, None);
.delete_prefix(ASYNC_POOL_PREFIX, STATE_CF, None, only_use_xor);
self.recompute_message_info_cache();
}

Expand Down
14 changes: 9 additions & 5 deletions massa-bootstrap/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,13 @@ pub fn get_state(
{
let mut final_state_guard = final_state.write();

let only_use_xor = final_state_guard.get_only_use_xor();

if !bootstrap_config.keep_ledger {
// load ledger from initial ledger file
final_state_guard
.ledger
.load_initial_ledger()
.load_initial_ledger(only_use_xor)
.map_err(|err| {
BootstrapError::GeneralError(format!(
"could not load initial ledger: {}",
Expand All @@ -439,10 +441,12 @@ pub fn get_state(
);

// TODO: should receive ver batch here?
final_state_guard
.db
.write()
.write_batch(batch, Default::default(), Some(slot));
final_state_guard.db.write().write_batch(
batch,
Default::default(),
Some(slot),
only_use_xor,
);
}
return Ok(GlobalBootstrapState::new(final_state));
}
Expand Down
8 changes: 4 additions & 4 deletions massa-bootstrap/src/tests/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ fn test_bootstrap_server() {
final_write
.db
.write()
.write_batch(batch, Default::default(), Some(next));
.write_batch(batch, Default::default(), Some(next), false);

let final_state_hash = final_write.db.read().get_db_hash();
let cycle = next.get_cycle(final_state_local_config.periods_per_cycle.clone());
final_write
.pos_state
.feed_cycle_state_hash(cycle, final_state_hash);
.feed_cycle_state_hash(cycle, final_state_hash, false);

current_slot = next;
}
Expand Down Expand Up @@ -462,13 +462,13 @@ fn test_bootstrap_server() {
final_write
.db
.write()
.write_batch(batch, Default::default(), Some(next));
.write_batch(batch, Default::default(), Some(next), false);

let final_state_hash = final_write.db.read().get_db_hash();
let cycle = next.get_cycle(final_state_local_config.periods_per_cycle.clone());
final_write
.pos_state
.feed_cycle_state_hash(cycle, final_state_hash);
.feed_cycle_state_hash(cycle, final_state_hash, false);

let mut list_changes_write = list_changes_clone.write();
list_changes_write.push((next, changes));
Expand Down
15 changes: 10 additions & 5 deletions massa-bootstrap/src/tests/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,18 @@ fn get_random_pos_state(r_limit: u64, mut pos: PoSFinalState) -> PoSFinalState {

pos.create_initial_cycle(&mut batch);

pos.db.write().write_batch(batch, Default::default(), None);
pos.db
.write()
.write_batch(batch, Default::default(), None, false);

let mut batch = DBBatch::new();

pos.apply_changes_to_batch(changes, Slot::new(0, 0), false, &mut batch)
.expect("Critical: Error while applying changes to pos_state");

pos.db.write().write_batch(batch, Default::default(), None);
pos.db
.write()
.write_batch(batch, Default::default(), None, false);

pos
}
Expand Down Expand Up @@ -230,7 +234,8 @@ pub fn get_random_executed_ops(
let mut executed_ops = ExecutedOps::new(config.clone(), db.clone());
let mut batch = DBBatch::new();
executed_ops.apply_changes_to_batch(get_random_executed_ops_changes(10), slot, &mut batch);
db.write().write_batch(batch, Default::default(), None);
db.write()
.write_batch(batch, Default::default(), None, false);
executed_ops
}

Expand Down Expand Up @@ -264,7 +269,7 @@ pub fn get_random_executed_de(
executed_de
.db
.write()
.write_batch(batch, Default::default(), None);
.write_batch(batch, Default::default(), None, false);

executed_de
}
Expand Down Expand Up @@ -318,7 +323,7 @@ pub fn get_random_final_state_bootstrap(
async_pool
.db
.write()
.write_batch(batch, versioning_batch, None);
.write_batch(batch, versioning_batch, None, false);

let executed_ops = get_random_executed_ops(
r_limit,
Expand Down
1 change: 1 addition & 0 deletions massa-db/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const STATE_CF: &str = "state";
pub const VERSIONING_CF: &str = "versioning";

pub const STATE_HASH_KEY: &[u8; 1] = b"h";
pub const STATE_HASH_XOR_KEY: &[u8; 1] = b"x";
pub const STATE_HASH_INITIAL_BYTES: &[u8; 32] = &[0; HASH_SIZE_BYTES];
pub const CHANGE_ID_KEY: &[u8; 1] = b"c";

Expand Down
61 changes: 47 additions & 14 deletions massa-db/src/massa_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
MassaDBError, CF_ERROR, CHANGE_ID_DESER_ERROR, CHANGE_ID_KEY, CHANGE_ID_SER_ERROR, CRUD_ERROR,
LSMTREE_ERROR, LSMTREE_NODES_CF, LSMTREE_VALUES_CF, METADATA_CF, OPEN_ERROR, STATE_CF,
STATE_HASH_ERROR, STATE_HASH_INITIAL_BYTES, STATE_HASH_KEY, VERSIONING_CF,
STATE_HASH_ERROR, STATE_HASH_INITIAL_BYTES, STATE_HASH_KEY, STATE_HASH_XOR_KEY, VERSIONING_CF,
};
use lsmtree::{bytes::Bytes, BadProof, KVStore, SparseMerkleTree};
use massa_hash::{Hash, SmtHasher};
Expand Down Expand Up @@ -430,6 +430,7 @@ where
versioning_changes: BTreeMap<Key, Option<Value>>,
change_id: Option<ChangeID>,
reset_history: bool,
only_use_xor: bool,
) -> Result<(), MassaDBError> {
debug!("AURELIEN: MassaDB: start write changes");
if let Some(change_id) = change_id.clone() {
Expand All @@ -445,6 +446,8 @@ where
let handle_versioning = self.db.cf_handle(VERSIONING_CF).expect(CF_ERROR);
debug!("AURELIEN: MassaDB: after get handles");

let _current_xor_hash = self.get_db_hash_xor();

*self.current_batch.lock() = WriteBatch::default();

for (key, value) in changes.iter() {
Expand All @@ -453,16 +456,20 @@ where
self.current_batch.lock().put_cf(handle_state, key, value);
let key_hash = Hash::compute_from(key);
let value_hash = Hash::compute_from(value);
let _key_value_hash =
Hash::compute_from(&[key.as_slice(), value.as_slice()].concat());
debug!("AURELIEN: MassaDB: after insert value");

debug!("AURELIEN: MassaDB: before tree update");
self.lsmtree
.update(
key_hash.to_bytes(),
Bytes::from(value_hash.to_bytes().to_vec()),
)
.expect(LSMTREE_ERROR);
debug!("AURELIEN: MassaDB: after tree update");
if !only_use_xor {
debug!("AURELIEN: MassaDB: before tree update");
self.lsmtree
.update(
key_hash.to_bytes(),
Bytes::from(value_hash.to_bytes().to_vec()),
)
.expect(LSMTREE_ERROR);
debug!("AURELIEN: MassaDB: after tree update");
}
} else {
debug!("AURELIEN: MassaDB: before delete");
self.current_batch.lock().delete_cf(handle_state, key);
Expand Down Expand Up @@ -529,7 +536,6 @@ where
.or_insert(versioning_changes);
debug!("AURELIEN: MassaDB: after changes history update");


debug!("AURELIEN: MassaDB: before cleaning");
if reset_history {
self.change_history.clear();
Expand Down Expand Up @@ -633,6 +639,7 @@ where
versioning_changes,
Some(stream_changes.change_id),
true,
false,
)?;

Ok((new_cursor, new_cursor_versioning))
Expand All @@ -656,6 +663,25 @@ where
Hash::from_bytes(state_hash_bytes.try_into().expect(STATE_HASH_ERROR))
})
}

/// Get the current state hash xor of the database
pub fn get_db_hash_xor(&self) -> Hash {
self.get_db_hash_opt_xor()
.unwrap_or(Hash::from_bytes(STATE_HASH_INITIAL_BYTES))
}

/// Get the current state hash xor of the database
fn get_db_hash_opt_xor(&self) -> Option<Hash> {
let db = &self.db;
let handle = db.cf_handle(METADATA_CF).expect(CF_ERROR);

db.get_cf(handle, STATE_HASH_XOR_KEY)
.expect(CRUD_ERROR)
.as_deref()
.map(|state_hash_bytes| {
Hash::from_bytes(state_hash_bytes.try_into().expect(STATE_HASH_ERROR))
})
}
}

impl RawMassaDB<Slot, SlotSerializer, SlotDeserializer> {
Expand Down Expand Up @@ -749,8 +775,9 @@ impl RawMassaDB<Slot, SlotSerializer, SlotDeserializer> {
batch: DBBatch,
versioning_batch: DBBatch,
change_id: Option<Slot>,
only_use_xor: bool,
) {
self.write_changes(batch, versioning_batch, change_id, false)
self.write_changes(batch, versioning_batch, change_id, false, only_use_xor)
.expect(CRUD_ERROR);
}

Expand All @@ -765,7 +792,13 @@ impl RawMassaDB<Slot, SlotSerializer, SlotDeserializer> {
}

/// Utility function to delete all keys in a prefix
pub fn delete_prefix(&mut self, prefix: &str, handle_str: &str, change_id: Option<Slot>) {
pub fn delete_prefix(
&mut self,
prefix: &str,
handle_str: &str,
change_id: Option<Slot>,
only_use_xor: bool,
) {
let db = &self.db;

let handle = db.cf_handle(handle_str).expect(CF_ERROR);
Expand All @@ -780,10 +813,10 @@ impl RawMassaDB<Slot, SlotSerializer, SlotDeserializer> {

match handle_str {
STATE_CF => {
self.write_batch(batch, DBBatch::new(), change_id);
self.write_batch(batch, DBBatch::new(), change_id, only_use_xor);
}
VERSIONING_CF => {
self.write_batch(DBBatch::new(), batch, change_id);
self.write_batch(DBBatch::new(), batch, change_id, only_use_xor);
}
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions massa-executed-ops/src/executed_denunciations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl ExecutedDenunciations {
/// Reset the executed denunciations
///
/// USED FOR BOOTSTRAP ONLY
pub fn reset(&mut self) {
pub fn reset(&mut self, only_use_xor: bool) {
{
let mut db = self.db.write();
db.delete_prefix(EXECUTED_DENUNCIATIONS_PREFIX, STATE_CF, None);
db.delete_prefix(EXECUTED_DENUNCIATIONS_PREFIX, STATE_CF, None, only_use_xor);
}

self.recompute_sorted_denunciations();
Expand Down
16 changes: 10 additions & 6 deletions massa-executed-ops/src/executed_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ impl ExecutedOps {
/// Reset the executed operations
///
/// USED FOR BOOTSTRAP ONLY
pub fn reset(&mut self) {
pub fn reset(&mut self, only_use_xor: bool) {
self.db
.write()
.delete_prefix(EXECUTED_OPS_PREFIX, STATE_CF, None);
.delete_prefix(EXECUTED_OPS_PREFIX, STATE_CF, None, only_use_xor);

self.recompute_sorted_ops_and_op_exec_status();
}
Expand Down Expand Up @@ -322,15 +322,18 @@ fn test_executed_ops_hash_computing() {

let mut batch_a = DBBatch::new();
a.apply_changes_to_batch(change_a, apply_slot, &mut batch_a);
db_a.write().write_batch(batch_a, Default::default(), None);
db_a.write()
.write_batch(batch_a, Default::default(), None, false);

let mut batch_b = DBBatch::new();
a.apply_changes_to_batch(change_b, apply_slot, &mut batch_b);
db_a.write().write_batch(batch_b, Default::default(), None);
db_a.write()
.write_batch(batch_b, Default::default(), None, false);

let mut batch_c = DBBatch::new();
c.apply_changes_to_batch(change_c, apply_slot, &mut batch_c);
db_c.write().write_batch(batch_c, Default::default(), None);
db_c.write()
.write_batch(batch_c, Default::default(), None, false);

// check that a.hash ^ $(change_b) = c.hash
assert_ne!(
Expand All @@ -350,7 +353,8 @@ fn test_executed_ops_hash_computing() {
};
let mut batch_a = DBBatch::new();
a.prune_to_batch(prune_slot, &mut batch_a);
db_a.write().write_batch(batch_a, Default::default(), None);
db_a.write()
.write_batch(batch_a, Default::default(), None, false);

// at this point the hash should have been reset to its original value
assert_eq!(
Expand Down
Loading

0 comments on commit b368beb

Please sign in to comment.