1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
use crate::{block_status::StorageOrBlock, error::ConsensusError};
use massa_models::{
active_block::ActiveBlock,
block::{Block, BlockDeserializer, BlockDeserializerArgs, SecureShareBlock},
block_id::{BlockId, BlockIdDeserializer, BlockIdSerializer},
prehash::PreHashMap,
secure_share::{SecureShareDeserializer, SecureShareSerializer},
};
use massa_serialization::{
Deserializer, SerializeError, Serializer, U64VarIntDeserializer, U64VarIntSerializer,
};
use nom::branch::alt;
use nom::{
bytes::complete::tag,
combinator::value,
error::{ContextError, ParseError},
multi::count,
sequence::{preceded, tuple},
};
use nom::{error::context, IResult, Parser};
use serde::{Deserialize, Serialize};
use std::ops::Bound::Included;
/// Exportable version of `ActiveBlock`
/// Fields that can be easily recomputed were left out
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportActiveBlock {
/// The block.
pub block: SecureShareBlock,
/// one `(block id, period)` per thread ( if not genesis )
pub parents: Vec<(BlockId, u64)>,
/// for example has its fitness reached the given threshold
pub is_final: bool,
}
impl ExportActiveBlock {
/// conversion from active block to export active block
pub fn from_active_block(a_block: &ActiveBlock, storage_or_block: &StorageOrBlock) -> Self {
// TODO: if we decide that endorsements are separate, also gather endorsements here
ExportActiveBlock {
parents: a_block.parents.clone(),
is_final: a_block.is_final,
block: storage_or_block.clone_block(&a_block.block_id),
}
}
/// consuming conversion from `ExportActiveBlock` to `ActiveBlock`
pub fn to_active_block(
self,
thread_count: u8,
) -> Result<(ActiveBlock, StorageOrBlock), ConsensusError> {
// create ActiveBlock
let active_block = ActiveBlock {
creator_address: self.block.content_creator_address,
block_id: self.block.id,
parents: self.parents.clone(),
children: vec![PreHashMap::default(); thread_count as usize], // will be computed once the full graph is available
descendants: Default::default(), // will be computed once the full graph is available
is_final: self.is_final,
slot: self.block.content.header.content.slot,
fitness: self.block.get_fitness(),
same_thread_parent_creator: None, // will be computed once the full graph is available
};
Ok((active_block, StorageOrBlock::Block(Box::new(self.block))))
}
}
/// Basic serializer of `ExportActiveBlock`
#[derive(Default)]
pub struct ExportActiveBlockSerializer {
sec_share_serializer: SecureShareSerializer,
period_serializer: U64VarIntSerializer,
block_id_serializer: BlockIdSerializer,
}
impl ExportActiveBlockSerializer {
/// Create a new `ExportActiveBlockSerializer`
pub fn new() -> Self {
ExportActiveBlockSerializer {
sec_share_serializer: SecureShareSerializer::new(),
period_serializer: U64VarIntSerializer::new(),
block_id_serializer: BlockIdSerializer::new(),
}
}
}
impl Serializer<ExportActiveBlock> for ExportActiveBlockSerializer {
fn serialize(
&self,
value: &ExportActiveBlock,
buffer: &mut Vec<u8>,
) -> Result<(), SerializeError> {
// block
self.sec_share_serializer.serialize(&value.block, buffer)?;
// parents with periods
// note: there should be no parents for genesis blocks
buffer.push(u8::from(!value.parents.is_empty()));
for (hash, period) in value.parents.iter() {
self.block_id_serializer.serialize(hash, buffer)?;
self.period_serializer.serialize(period, buffer)?;
}
// finality
buffer.push(u8::from(value.is_final));
Ok(())
}
}
/// Basic deserializer of `ExportActiveBlock`
pub struct ExportActiveBlockDeserializer {
sec_share_block_deserializer: SecureShareDeserializer<Block, BlockDeserializer>,
block_id_deserializer: BlockIdDeserializer,
period_deserializer: U64VarIntDeserializer,
thread_count: u8,
}
impl ExportActiveBlockDeserializer {
/// Create a new `ExportActiveBlockDeserializer`
// TODO: check if we can remove this?
#[allow(clippy::too_many_arguments)]
pub fn new(block_der_args: BlockDeserializerArgs) -> Self {
let thread_count = block_der_args.thread_count;
let chain_id = block_der_args.chain_id;
ExportActiveBlockDeserializer {
sec_share_block_deserializer: SecureShareDeserializer::new(
BlockDeserializer::new(block_der_args),
chain_id,
),
block_id_deserializer: BlockIdDeserializer::new(),
period_deserializer: U64VarIntDeserializer::new(Included(0), Included(u64::MAX)),
thread_count,
}
}
}
impl Deserializer<ExportActiveBlock> for ExportActiveBlockDeserializer {
/// ## Example:
/// ```rust
/// use massa_consensus_exports::export_active_block::{ExportActiveBlock, ExportActiveBlockDeserializer, ExportActiveBlockSerializer};
/// use massa_models::{ledger::LedgerChanges, config::THREAD_COUNT, rolls::RollUpdates, block::{Block, BlockSerializer}, prehash::PreHashSet, endorsement::{Endorsement, EndorsementSerializer}, slot::Slot, secure_share::SecureShareContent};
/// use massa_models::block_id::BlockId;
/// use massa_models::block_header::{BlockHeader, BlockHeaderSerializer};
/// use massa_hash::Hash;
/// use std::collections::HashSet;
/// use massa_models::block::BlockDeserializerArgs;
/// use massa_models::config::CHAINID;
/// use massa_signature::KeyPair;
/// use massa_serialization::{Serializer, Deserializer, DeserializeError};
///
/// let keypair = KeyPair::generate(0).unwrap();
/// let parents = (0..THREAD_COUNT)
/// .map(|i| BlockId::generate_from_hash(Hash::compute_from(&[i])))
/// .collect();
///
/// // create block header
/// let orig_header = BlockHeader::new_verifiable(
/// BlockHeader {
/// current_version: 0,
/// announced_version: None,
/// slot: Slot::new(1, 1),
/// parents,
/// operation_merkle_root: Hash::compute_from("mno".as_bytes()),
/// endorsements: vec![
/// Endorsement::new_verifiable(
/// Endorsement {
/// slot: Slot::new(1, 1),
/// index: 1,
/// endorsed_block: BlockId::generate_from_hash(Hash::compute_from(&[1])),
/// },
/// EndorsementSerializer::new(),
/// &keypair,
/// *CHAINID
/// )
/// .unwrap(),
/// Endorsement::new_verifiable(
/// Endorsement {
/// slot: Slot::new(1, 1),
/// index: 3,
/// endorsed_block: BlockId::generate_from_hash(Hash::compute_from(&[1])),
/// },
/// EndorsementSerializer::new(),
/// &keypair,
/// *CHAINID
/// )
/// .unwrap(),
/// ],
/// denunciations: vec![],},
/// BlockHeaderSerializer::new(),
/// &keypair,
/// *CHAINID
/// )
/// .unwrap();
///
/// // create block
/// let orig_block = Block {
/// header: orig_header,
/// operations: Vec::new(),
/// };
///
/// let full_block = Block::new_verifiable(orig_block, BlockSerializer::new(), &keypair, *CHAINID).unwrap();
/// let export_active_block = ExportActiveBlock {
/// block: full_block.clone(),
/// parents: vec![],
/// is_final: false,
/// };
///
/// let mut serialized = Vec::new();
/// ExportActiveBlockSerializer::new().serialize(&export_active_block, &mut serialized).unwrap();
/// let args = BlockDeserializerArgs {
/// thread_count: 32, max_operations_per_block: 16, endorsement_count: 1000,max_denunciations_per_block_header: 128,last_start_period: Some(0),chain_id: *CHAINID};
/// let (rest, export_deserialized) = ExportActiveBlockDeserializer::new(args).deserialize::<DeserializeError>(&serialized).unwrap();
/// assert_eq!(export_deserialized.block.id, export_active_block.block.id);
/// assert_eq!(export_deserialized.block.serialized_data, export_active_block.block.serialized_data);
/// assert_eq!(rest.len(), 0);
/// ```
fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
&self,
buffer: &'a [u8],
) -> IResult<&'a [u8], ExportActiveBlock, E> {
context(
"Failed ExportActiveBlock deserialization",
tuple((
// block
context("Failed block deserialization", |input| {
self.sec_share_block_deserializer.deserialize(input)
}),
// parents
context(
"Failed parents deserialization",
alt((
value(Vec::new(), tag(&[0])),
preceded(
tag(&[1]),
count(
tuple((
context("Failed block_id deserialization", |input| {
self.block_id_deserializer.deserialize(input)
}),
context("Failed period deserialization", |input| {
self.period_deserializer.deserialize(input)
}),
)),
self.thread_count as usize,
),
),
)),
),
// finality
context(
"Failed is_final deserialization",
alt((value(true, tag(&[1])), value(false, tag(&[0])))),
),
)),
)
.map(|(block, parents, is_final)| ExportActiveBlock {
block,
parents,
is_final,
})
.parse(buffer)
}
}