Skip to content

Commit

Permalink
Merge #4090
Browse files Browse the repository at this point in the history
4090: Include xor versioning r=AurelienFT a=Leo-Besancon

* [ ] document all added functions
* [ ] try in sandbox /simulation/labnet
* [ ] unit tests on the added/changed features
  * [ ] make tests compile
  * [ ] make tests pass 
* [ ] add logs allowing easy debugging in case the changes caused problems
* [ ] if the API has changed, update the API specification

Co-authored-by: modship <yeskinokay@gmail.com>
Co-authored-by: AurelienFT <aurelien.foucault@epitech.eu>
Co-authored-by: sydhds <sydhds@gmail.com>
Co-authored-by: Leo-Besancon <lb@massa.net>
  • Loading branch information
5 people authored Jun 15, 2023
2 parents dc353ea + 8975bfd commit 51aed98
Show file tree
Hide file tree
Showing 31 changed files with 581 additions and 216 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(&Slot::new(0, 31));

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
35 changes: 34 additions & 1 deletion massa-channel/src/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{
ops::{Deref, DerefMut},
sync::Arc,
time::{Duration, Instant},
};

use crossbeam::channel::{Receiver, RecvError, TryRecvError};
use crossbeam::channel::{Receiver, RecvError, RecvTimeoutError, TryRecvError};
use prometheus::{Counter, Gauge};

#[derive(Clone)]
Expand Down Expand Up @@ -63,6 +64,38 @@ impl<T> MassaReceiver<T> {
}
}

pub fn recv_deadline(&self, deadline: Instant) -> Result<T, RecvTimeoutError> {
match self.receiver.recv_deadline(deadline) {
Ok(msg) => {
self.inc_metrics();

Ok(msg)
}
Err(e) => {
let _ = prometheus::unregister(Box::new(self.actual_len.clone()));
let _ = prometheus::unregister(Box::new(self.received.clone()));

Err(e)
}
}
}

pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError> {
match self.receiver.recv_timeout(timeout) {
Ok(msg) => {
self.inc_metrics();

Ok(msg)
}
Err(e) => {
let _ = prometheus::unregister(Box::new(self.actual_len.clone()));
let _ = prometheus::unregister(Box::new(self.received.clone()));

Err(e)
}
}
}

pub fn recv(&self) -> Result<T, RecvError> {
match self.receiver.recv() {
Ok(msg) => {
Expand Down
15 changes: 14 additions & 1 deletion massa-channel/src/sender.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{ops::Deref, time::Duration};
use std::{
ops::Deref,
time::{Duration, Instant},
};

use crossbeam::channel::{SendError, SendTimeoutError, Sender, TrySendError};
use prometheus::Gauge;
Expand Down Expand Up @@ -34,6 +37,16 @@ impl<T> MassaSender<T> {
}
}

pub fn send_deadline(&self, msg: T, deadline: Instant) -> Result<(), SendTimeoutError<T>> {
match self.sender.send_deadline(msg, deadline) {
Ok(()) => {
self.actual_len.inc();
Ok(())
}
Err(e) => Err(e),
}
}

pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
match self.sender.try_send(msg) {
Ok(()) => {
Expand Down
28 changes: 15 additions & 13 deletions massa-consensus-worker/src/state/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,6 @@ impl ConsensusState {
"block_id": add_block_id
});

{
// set metrics
let add_slot_timestamp = timeslots::get_block_slot_timestamp(
self.config.thread_count,
self.config.t0,
self.config.genesis_timestamp,
add_block_slot,
)?;
let now = MassaTime::now()?;
let diff = now.saturating_sub(add_slot_timestamp);
self.massa_metrics.set_block_graph_diff_ms(diff.to_millis());
}

// Ensure block parents are claimed by the block's storage.
// Note that operations and endorsements should already be there (claimed in Protocol).
storage.claim_block_refs(&parents_hash_period.iter().map(|(p_id, _)| *p_id).collect());
Expand Down Expand Up @@ -498,6 +485,21 @@ impl ConsensusState {
self.mark_final_blocks(&add_block_id, final_blocks)?;

massa_trace!("consensus.block_graph.add_block_to_graph.end", {});

{
// set metrics
let add_slot_timestamp = timeslots::get_block_slot_timestamp(
self.config.thread_count,
self.config.t0,
self.config.genesis_timestamp,
add_block_slot,
)?;
let now = MassaTime::now()?;
let diff = now.saturating_sub(add_slot_timestamp);
self.massa_metrics.inc_block_graph_counter();
self.massa_metrics.inc_block_graph_ms(diff.to_millis());
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions massa-db/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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_KEY_IS_XOR_KEY: &[u8; 6] = b"is_xor";
pub const STATE_HASH_INITIAL_BYTES: &[u8; 32] = &[0; HASH_SIZE_BYTES];
pub const CHANGE_ID_KEY: &[u8; 1] = b"c";

Expand Down
Loading

0 comments on commit 51aed98

Please sign in to comment.