use crate::{
account_address::AccountAddress,
account_config::CORE_CODE_ADDRESS,
event::{EventHandle, EventKey},
};
use aptos_crypto::HashValue;
use move_deps::move_core_types::{
ident_str,
identifier::IdentStr,
move_resource::{MoveResource, MoveStructType},
};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockMetadata {
id: HashValue,
epoch: u64,
round: u64,
previous_block_votes: Vec<bool>,
proposer: AccountAddress,
failed_proposer_indices: Vec<u32>,
timestamp_usecs: u64,
}
impl BlockMetadata {
pub fn new(
id: HashValue,
epoch: u64,
round: u64,
previous_block_votes: Vec<bool>,
proposer: AccountAddress,
failed_proposer_indices: Vec<u32>,
timestamp_usecs: u64,
) -> Self {
Self {
id,
epoch,
round,
previous_block_votes,
proposer,
failed_proposer_indices,
timestamp_usecs,
}
}
pub fn id(&self) -> HashValue {
self.id
}
pub fn into_inner(self) -> (u64, u64, u64, Vec<bool>, AccountAddress, Vec<u32>) {
(
self.epoch,
self.round,
self.timestamp_usecs,
self.previous_block_votes.clone(),
self.proposer,
self.failed_proposer_indices,
)
}
pub fn timestamp_usecs(&self) -> u64 {
self.timestamp_usecs
}
pub fn proposer(&self) -> AccountAddress {
self.proposer
}
pub fn previous_block_votes(&self) -> &Vec<bool> {
&self.previous_block_votes
}
pub fn failed_proposer_indices(&self) -> &Vec<u32> {
&self.failed_proposer_indices
}
pub fn epoch(&self) -> u64 {
self.epoch
}
pub fn round(&self) -> u64 {
self.round
}
}
pub fn new_block_event_key() -> EventKey {
EventKey::new(3, CORE_CODE_ADDRESS)
}
pub static NEW_BLOCK_EVENT_PATH: Lazy<Vec<u8>> = Lazy::new(|| {
let mut path = BlockResource::resource_path();
path.extend_from_slice(b"/new_block_event/");
path
});
#[derive(Deserialize, Serialize)]
pub struct BlockResource {
height: u64,
new_block_events: EventHandle,
}
impl BlockResource {
pub fn new_block_events(&self) -> &EventHandle {
&self.new_block_events
}
pub fn height(&self) -> u64 {
self.height
}
}
impl MoveStructType for BlockResource {
const MODULE_NAME: &'static IdentStr = ident_str!("block");
const STRUCT_NAME: &'static IdentStr = ident_str!("BlockMetadata");
}
impl MoveResource for BlockResource {}