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

Add versioning of all object id and some tests #4226

Merged
merged 16 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion massa-bootstrap/src/tests/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub fn get_random_final_state_bootstrap(
}

pub fn get_dummy_block_id(s: &str) -> BlockId {
BlockId(Hash::compute_from(s.as_bytes()))
BlockId::generate_from_hash(Hash::compute_from(s.as_bytes()))
}

pub fn get_random_address() -> Address {
Expand Down
2 changes: 2 additions & 0 deletions massa-consensus-exports/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum ConsensusError {
ExecutionError(#[from] ExecutionError),
/// models error: {0}
ModelsError(#[from] ModelsError),
/// Serialization error: {0}
SerializationError(String),
/// Could not create genesis block {0}
GenesisCreationError(String),
/// missing block {0}
Expand Down
21 changes: 10 additions & 11 deletions massa-consensus-exports/src/export_active_block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::error::ConsensusError;
use massa_hash::HashDeserializer;
use massa_models::{
active_block::ActiveBlock,
block::{Block, BlockDeserializer, BlockDeserializerArgs, SecureShareBlock},
block_id::BlockId,
block_id::{BlockId, BlockIdDeserializer, BlockIdSerializer},
prehash::PreHashMap,
secure_share::{SecureShareDeserializer, SecureShareSerializer},
};
Expand Down Expand Up @@ -92,6 +91,7 @@ impl ExportActiveBlock {
pub struct ExportActiveBlockSerializer {
sec_share_serializer: SecureShareSerializer,
period_serializer: U64VarIntSerializer,
block_id_serializer: BlockIdSerializer,
}

impl ExportActiveBlockSerializer {
Expand All @@ -100,6 +100,7 @@ impl ExportActiveBlockSerializer {
ExportActiveBlockSerializer {
sec_share_serializer: SecureShareSerializer::new(),
period_serializer: U64VarIntSerializer::new(),
block_id_serializer: BlockIdSerializer::new(),
}
}
}
Expand All @@ -117,7 +118,7 @@ impl Serializer<ExportActiveBlock> for ExportActiveBlockSerializer {
// note: there should be no parents for genesis blocks
buffer.push(u8::from(!value.parents.is_empty()));
for (hash, period) in value.parents.iter() {
buffer.extend(hash.0.to_bytes());
self.block_id_serializer.serialize(hash, buffer)?;
self.period_serializer.serialize(period, buffer)?;
}

Expand All @@ -131,7 +132,7 @@ impl Serializer<ExportActiveBlock> for ExportActiveBlockSerializer {
/// Basic deserializer of `ExportActiveBlock`
pub struct ExportActiveBlockDeserializer {
sec_share_block_deserializer: SecureShareDeserializer<Block, BlockDeserializer>,
hash_deserializer: HashDeserializer,
block_id_deserializer: BlockIdDeserializer,
period_deserializer: U64VarIntDeserializer,
thread_count: u8,
}
Expand All @@ -146,7 +147,7 @@ impl ExportActiveBlockDeserializer {
sec_share_block_deserializer: SecureShareDeserializer::new(BlockDeserializer::new(
block_der_args,
)),
hash_deserializer: HashDeserializer::new(),
block_id_deserializer: BlockIdDeserializer::new(),
period_deserializer: U64VarIntDeserializer::new(Included(0), Included(u64::MAX)),
thread_count,
}
Expand All @@ -168,7 +169,7 @@ impl Deserializer<ExportActiveBlock> for ExportActiveBlockDeserializer {
///
/// let keypair = KeyPair::generate(0).unwrap();
/// let parents = (0..THREAD_COUNT)
/// .map(|i| BlockId(Hash::compute_from(&[i])))
/// .map(|i| BlockId::generate_from_hash(Hash::compute_from(&[i])))
/// .collect();
///
/// // create block header
Expand All @@ -184,7 +185,7 @@ impl Deserializer<ExportActiveBlock> for ExportActiveBlockDeserializer {
/// Endorsement {
/// slot: Slot::new(1, 1),
/// index: 1,
/// endorsed_block: BlockId(Hash::compute_from(&[1])),
/// endorsed_block: BlockId::generate_from_hash(Hash::compute_from(&[1])),
/// },
/// EndorsementSerializer::new(),
/// &keypair,
Expand All @@ -194,7 +195,7 @@ impl Deserializer<ExportActiveBlock> for ExportActiveBlockDeserializer {
/// Endorsement {
/// slot: Slot::new(1, 1),
/// index: 3,
/// endorsed_block: BlockId(Hash::compute_from(&[1])),
/// endorsed_block: BlockId::generate_from_hash(Hash::compute_from(&[1])),
/// },
/// EndorsementSerializer::new(),
/// &keypair,
Expand Down Expand Up @@ -250,9 +251,7 @@ impl Deserializer<ExportActiveBlock> for ExportActiveBlockDeserializer {
count(
tuple((
context("Failed block_id deserialization", |input| {
self.hash_deserializer
.deserialize(input)
.map(|(rest, hash)| (rest, BlockId(hash)))
self.block_id_deserializer.deserialize(input)
}),
context("Failed period deserialization", |input| {
self.period_deserializer.deserialize(input)
Expand Down
1 change: 1 addition & 0 deletions massa-consensus-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ massa_channel = {workspace = true}
massa_metrics = {workspace = true}
massa_consensus_exports = {workspace = true}
massa_models = {workspace = true}
massa_serialization = {workspace = true}
massa_storage = {workspace = true}
massa_signature = {workspace = true}
massa_time = {workspace = true}
Expand Down
16 changes: 7 additions & 9 deletions massa-consensus-worker/src/state/clique_computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ mod tests {
let mut gi_head = PreHashMap::default();
for i in 0..size {
gi_head.insert(
BlockId::from_bytes(
massa_hash::Hash::compute_from(&i.to_be_bytes()).to_bytes(),
),
BlockId::generate_from_hash(massa_hash::Hash::compute_from(&i.to_be_bytes())),
PreHashSet::default(),
);
}
Expand All @@ -97,12 +95,12 @@ mod tests {
let is_compatible = rng.gen_bool(0.5);

if !is_compatible {
let i_id = BlockId::from_bytes(
massa_hash::Hash::compute_from(&i.to_be_bytes()).to_bytes(),
);
let j_id = BlockId::from_bytes(
massa_hash::Hash::compute_from(&j.to_be_bytes()).to_bytes(),
);
let i_id = BlockId::generate_from_hash(massa_hash::Hash::compute_from(
&i.to_be_bytes(),
));
let j_id = BlockId::generate_from_hash(massa_hash::Hash::compute_from(
&j.to_be_bytes(),
));
// Add the incompatibility relationship to gi_head
gi_head.entry(i_id).or_default().insert(j_id);
gi_head.entry(j_id).or_default().insert(i_id);
Expand Down
15 changes: 13 additions & 2 deletions massa-consensus-worker/src/state/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use massa_consensus_exports::{
error::ConsensusError,
};
use massa_logging::massa_trace;
use massa_models::{block_id::BlockId, clique::Clique, prehash::PreHashSet, slot::Slot};
use massa_models::{
block_id::{BlockId, BlockIdSerializer},
clique::Clique,
prehash::PreHashSet,
slot::Slot,
};
use massa_serialization::Serializer;

use super::ConsensusState;

Expand Down Expand Up @@ -49,6 +55,7 @@ impl ConsensusState {
&mut self,
add_block_id: &BlockId,
) -> Result<usize, ConsensusError> {
let block_id_serializer = BlockIdSerializer::new();
let mut blockclique_i = 0usize;
let mut max_clique_fitness = (0u64, num::BigInt::default());
for (clique_i, clique) in self.max_cliques.iter_mut().enumerate() {
Expand All @@ -64,7 +71,11 @@ impl ConsensusState {
.fitness
.checked_add(fitness)
.ok_or(ConsensusError::FitnessOverflow)?;
sum_hash -= num::BigInt::from_bytes_be(num::bigint::Sign::Plus, block_h.to_bytes());
let mut bytes = Vec::new();
block_id_serializer
.serialize(block_h, &mut bytes)
.map_err(|err| ConsensusError::SerializationError(err.to_string()))?;
sum_hash -= num::BigInt::from_bytes_be(num::bigint::Sign::Plus, &bytes);
}
let cur_fit = (clique.fitness, sum_hash);
if cur_fit > max_clique_fitness {
Expand Down
6 changes: 4 additions & 2 deletions massa-executed-ops/src/executed_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl ExecutedOps {
return false;
}

let Ok((rest, _id)) = self.operation_id_deserializer.deserialize::<DeserializeError>(&serialized_key[EXECUTED_OPS_PREFIX.len()..]) else {
let Ok((rest, _id)): Result<(&[u8], OperationId), nom::Err<DeserializeError>> = self.operation_id_deserializer.deserialize::<DeserializeError>(&serialized_key[EXECUTED_OPS_PREFIX.len()..]) else {
return false;
};
if !rest.is_empty() {
Expand Down Expand Up @@ -377,6 +377,7 @@ fn test_executed_ops_hash_computing() {
pub struct ExecutedOpsSerializer {
slot_serializer: SlotSerializer,
u64_serializer: U64VarIntSerializer,
op_id_serializer: OperationIdSerializer,
}

impl Default for ExecutedOpsSerializer {
Expand All @@ -391,6 +392,7 @@ impl ExecutedOpsSerializer {
ExecutedOpsSerializer {
slot_serializer: SlotSerializer::new(),
u64_serializer: U64VarIntSerializer::new(),
op_id_serializer: OperationIdSerializer::new(),
}
}
}
Expand All @@ -412,7 +414,7 @@ impl Serializer<BTreeMap<Slot, PreHashSet<OperationId>>> for ExecutedOpsSerializ
self.u64_serializer.serialize(&(ids.len() as u64), buffer)?;
// slots ids
for op_id in ids {
buffer.extend(op_id.to_bytes());
self.op_id_serializer.serialize(op_id, buffer)?;
}
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions massa-execution-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ massa_hash = {workspace = true}
massa-sc-runtime = {workspace = true}
massa_metrics = {workspace = true}
massa_module_cache = {workspace = true}
massa_serialization = {workspace = true}
massa_signature = {workspace = true}
massa_time = {workspace = true}
massa_ledger_exports = {workspace = true}
Expand Down
19 changes: 13 additions & 6 deletions massa-execution-worker/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use massa_final_state::{FinalState, StateChanges};
use massa_hash::Hash;
use massa_ledger_exports::{LedgerChanges, SetOrKeep};
use massa_models::address::ExecutionAddressCycleInfo;
use massa_models::block_id::BlockIdSerializer;
use massa_models::bytecode::Bytecode;
use massa_models::denunciation::DenunciationIndex;
use massa_models::timeslots::get_block_slot_timestamp;
Expand All @@ -37,6 +38,7 @@ use massa_models::{
};
use massa_module_cache::controller::ModuleCache;
use massa_pos_exports::PoSChanges;
use massa_serialization::Serializer;
use massa_versioning::address_factory::{AddressArgs, AddressFactory};
use massa_versioning::versioning::MipStore;
use massa_versioning::versioning_factory::{FactoryStrategy, VersioningFactory};
Expand Down Expand Up @@ -1087,12 +1089,17 @@ fn generate_execution_trail_hash(
&slot.to_bytes_key(),
&[if read_only { 1u8 } else { 0u8 }, 0u8],
]),
Some(block_id) => massa_hash::Hash::compute_from_tuple(&[
previous_execution_trail_hash.to_bytes(),
&slot.to_bytes_key(),
&[if read_only { 1u8 } else { 0u8 }, 1u8],
block_id.to_bytes(),
]),
Some(block_id) => {
let mut bytes = Vec::new();
let block_id_serializer = BlockIdSerializer::new();
block_id_serializer.serialize(block_id, &mut bytes).unwrap();
massa_hash::Hash::compute_from_tuple(&[
previous_execution_trail_hash.to_bytes(),
&slot.to_bytes_key(),
&[if read_only { 1u8 } else { 0u8 }, 1u8],
&bytes,
])
}
}
}

Expand Down
1 change: 1 addition & 0 deletions massa-factory-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tracing = {workspace = true}
massa_channel = {workspace = true}
massa_models = {workspace = true}
massa_factory_exports = {workspace = true}
massa_serialization = {workspace = true}
massa_signature = {workspace = true}
massa_storage = {workspace = true}
massa_time = {workspace = true}
Expand Down
11 changes: 10 additions & 1 deletion massa-factory-worker/src/block_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use massa_models::{
block_header::{BlockHeader, BlockHeaderSerializer, SecuredHeader},
block_id::BlockId,
endorsement::SecureShareEndorsement,
operation::OperationIdSerializer,
prehash::PreHashSet,
secure_share::SecureShareContent,
slot::Slot,
timeslots::{get_block_slot_timestamp, get_closest_slot_to_timestamp},
};
use massa_serialization::Serializer;
use massa_time::MassaTime;
use massa_versioning::versioning::MipStore;
use massa_wallet::Wallet;
Expand All @@ -27,6 +29,7 @@ pub(crate) struct BlockFactoryWorker {
channels: FactoryChannels,
factory_receiver: MassaReceiver<()>,
mip_store: MipStore,
op_id_serializer: OperationIdSerializer,
}

impl BlockFactoryWorker {
Expand All @@ -48,6 +51,7 @@ impl BlockFactoryWorker {
channels,
factory_receiver,
mip_store,
op_id_serializer: OperationIdSerializer::new(),
};
this.run();
})
Expand Down Expand Up @@ -223,7 +227,12 @@ impl BlockFactoryWorker {
let global_operations_hash = Hash::compute_from(
&op_ids
.iter()
.flat_map(|op_id| *op_id.to_bytes())
.flat_map(|op_id| {
let mut buffer = Vec::new();
//It was a to_bytes() there before, we know the op is valid because it comes from the pool
self.op_id_serializer.serialize(op_id, &mut buffer).unwrap();
buffer
})
.collect::<Vec<u8>>(),
);

Expand Down
Loading
Loading