diem_types/
block_metadata.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    account_address::AccountAddress, account_config::diem_root_address,
10    event::EventKey,
11};
12use diem_crypto::HashValue;
13use serde::{Deserialize, Serialize};
14
15/// Struct that will be persisted on chain to store the information of the
16/// current block.
17///
18/// The flow will look like following:
19/// 1. The executor will pass this struct to VM at the end of a block proposal.
20/// 2. The VM will use this struct to create a special system transaction that
21/// will emit an event    represents the information of the current block. This
22/// transaction can't    be emitted by regular users and is generated by each of
23/// the validators on the fly. Such    transaction will be executed before all
24/// of the user-submitted transactions in the blocks. 3. Once that special
25/// resource is modified, the other user transactions can read the consensus
26///    info by calling into the read method of that resource, which would thus
27/// give users the    information such as the current leader.
28#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
29pub struct BlockMetadata {
30    id: HashValue,
31    round: u64,
32    timestamp_usecs: u64,
33    // The vector has to be sorted to ensure consistent result among all nodes
34    previous_block_votes: Vec<AccountAddress>,
35    proposer: AccountAddress,
36}
37
38impl BlockMetadata {
39    pub fn new(
40        id: HashValue, round: u64, timestamp_usecs: u64,
41        previous_block_votes: Vec<AccountAddress>, proposer: AccountAddress,
42    ) -> Self {
43        Self {
44            id,
45            round,
46            timestamp_usecs,
47            previous_block_votes,
48            proposer,
49        }
50    }
51
52    pub fn id(&self) -> HashValue { self.id }
53
54    pub fn into_inner(self) -> (u64, u64, Vec<AccountAddress>, AccountAddress) {
55        (
56            self.round,
57            self.timestamp_usecs,
58            self.previous_block_votes.clone(),
59            self.proposer,
60        )
61    }
62
63    pub fn timestamp_usec(&self) -> u64 { self.timestamp_usecs }
64
65    pub fn proposer(&self) -> AccountAddress { self.proposer }
66}
67
68pub fn new_block_event_key() -> EventKey {
69    EventKey::new_from_address(&diem_root_address(), 17)
70}
71
72#[derive(Clone, Deserialize, Serialize)]
73pub struct NewBlockEvent {
74    round: u64,
75    proposer: AccountAddress,
76    votes: Vec<AccountAddress>,
77    timestamp: u64,
78}
79
80impl NewBlockEvent {
81    pub fn new(
82        round: u64, proposer: AccountAddress, votes: Vec<AccountAddress>,
83        timestamp: u64,
84    ) -> Self {
85        Self {
86            round,
87            proposer,
88            votes,
89            timestamp,
90        }
91    }
92
93    pub fn round(&self) -> u64 { self.round }
94
95    pub fn proposer(&self) -> AccountAddress { self.proposer }
96
97    pub fn votes(&self) -> Vec<AccountAddress> { self.votes.clone() }
98}