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
use crate::{
account_address::AccountAddress,
account_config::aptos_root_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 epoch(&self) -> u64 {
self.epoch
}
pub fn round(&self) -> u64 {
self.round
}
}
pub fn new_block_event_key() -> EventKey {
EventKey::new_from_address(&aptos_root_address(), 6)
}
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 {}
#[derive(Clone, Deserialize, Serialize)]
pub struct NewBlockEvent {
epoch: u64,
round: u64,
previous_block_votes: Vec<bool>,
proposer: AccountAddress,
failed_proposer_indices: Vec<u64>,
timestamp: u64,
}
impl NewBlockEvent {
pub fn new(
epoch: u64,
round: u64,
previous_block_votes: Vec<bool>,
proposer: AccountAddress,
failed_proposer_indices: Vec<u64>,
timestamp: u64,
) -> Self {
Self {
epoch,
round,
previous_block_votes,
proposer,
failed_proposer_indices,
timestamp,
}
}
pub fn epoch(&self) -> u64 {
self.epoch
}
pub fn round(&self) -> u64 {
self.round
}
pub fn previous_block_votes(&self) -> &Vec<bool> {
&self.previous_block_votes
}
pub fn proposer(&self) -> AccountAddress {
self.proposer
}
pub fn failed_proposer_indices(&self) -> &Vec<u64> {
&self.failed_proposer_indices
}
}