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
266
267
268
269
270
use crate::bandwidth_scheduler::{
Bandwidth, BandwidthRequest, BandwidthRequestValues, BandwidthRequests,
BandwidthSchedulerParams, BlockBandwidthRequests,
};
use borsh::{BorshDeserialize, BorshSerialize};
use near_primitives_core::types::{Balance, BlockHeight, Gas, ShardId};
use near_schema_checker_lib::ProtocolSchema;
use std::collections::BTreeMap;
/// Information gathered during chunk application.
/// Provides insight into what happened when the chunk was applied.
/// How many transactions and receipts were processed, buffered, forwarded, etc.
/// Useful for debugging, metrics and sanity checks.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ProtocolSchema)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum ChunkApplyStats {
V0(ChunkApplyStatsV0) = 0,
V1(ChunkApplyStatsV1) = 1,
}
/// Information gathered during chunk application.
/// V0 is kept only to deserialize rows written by older binaries; new code
/// constructs and persists [`ChunkApplyStatsV1`] instead.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct ChunkApplyStatsV0 {
/// Height at which the chunk was applied
pub height: BlockHeight,
/// Shard ID of the chunk
pub shard_id: ShardId,
/// Was this chunk applied as a new (non-missing) chunk or a missing one (apply_old_chunk)?
pub is_new_chunk: bool,
/// Number of new transactions in this chunk
pub transactions_num: u64,
/// Number of incoming receipts to this chunk
pub incoming_receipts_num: u64,
/// Receipt sink stats - forwarded receipts, buffered receipts, outgoing limits
pub receipt_sink: ReceiptSinkStats,
/// Bandwidth scheduler stats
pub bandwidth_scheduler: BandwidthSchedulerStats,
/// Balance stats - used in balance checker.
pub balance: BalanceStats,
}
/// Information gathered during chunk application.
/// This feature is still in development. Consider V1 as unstable, fields might be added or removed
/// from it at any time. We will do proper versioning after stabilization when there will be other
/// services depending on this structure.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct ChunkApplyStatsV1 {
/// Height at which the chunk was applied
pub height: BlockHeight,
/// Shard ID of the chunk
pub shard_id: ShardId,
/// Was this chunk applied as a new (non-missing) chunk or a missing one (apply_old_chunk)?
pub is_new_chunk: bool,
/// Number of new transactions in this chunk
pub transactions_num: u64,
/// Number of incoming receipts to this chunk
pub incoming_receipts_num: u64,
/// Receipt sink stats - forwarded receipts, buffered receipts, outgoing limits
pub receipt_sink: ReceiptSinkStats,
/// Bandwidth scheduler stats
pub bandwidth_scheduler: BandwidthSchedulerStats,
/// Balance stats - used in balance checker.
pub balance: BalanceStatsV1,
}
impl ChunkApplyStatsV1 {
pub fn new(height: BlockHeight, shard_id: ShardId) -> ChunkApplyStatsV1 {
ChunkApplyStatsV1 {
height: height,
shard_id: shard_id,
is_new_chunk: false,
transactions_num: 0,
incoming_receipts_num: 0,
bandwidth_scheduler: Default::default(),
balance: Default::default(),
receipt_sink: Default::default(),
}
}
pub fn set_new_bandwidth_requests(
&mut self,
requests: &BandwidthRequests,
params: &BandwidthSchedulerParams,
) {
self.bandwidth_scheduler.set_new_bandwidth_requests(self.shard_id, requests, params);
}
/// Dummy data for tests.
pub fn dummy() -> ChunkApplyStatsV1 {
ChunkApplyStatsV1 {
height: 0,
shard_id: ShardId::new(0),
is_new_chunk: false,
transactions_num: 0,
incoming_receipts_num: 0,
bandwidth_scheduler: Default::default(),
balance: Default::default(),
receipt_sink: Default::default(),
}
}
}
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct BandwidthSchedulerStats {
/// Scheduler params, should always be Some but there is no Default impl.
pub params: Option<BandwidthSchedulerParams>,
/// Bandwidth requests generated by previous chunks, used as input to bandwidth scheduler.
pub prev_bandwidth_requests: BTreeMap<(ShardId, ShardId), Vec<Bandwidth>>,
/// Number of previous bandwidth requests (prev_bandwidth_requests.len()).
pub prev_bandwidth_requests_num: u64,
/// How long it took to run the bandwidth scheduler (in milliseconds).
pub time_to_run_ms: u128,
/// Bandwidth granted by the scheduler.
pub granted_bandwidth: BTreeMap<(ShardId, ShardId), Bandwidth>,
/// Bandwidth requests generated at the end of chunk application.
pub new_bandwidth_requests: BTreeMap<(ShardId, ShardId), Vec<Bandwidth>>,
}
impl BandwidthSchedulerStats {
/// Set `prev_bandwidth_requests` and `prev_bandwidth_requests_num`. Automatically converts to
/// the target representation.
pub fn set_prev_bandwidth_requests(
&mut self,
requests: &BlockBandwidthRequests,
params: &BandwidthSchedulerParams,
) {
for (from_shard, shard_requests) in &requests.shards_bandwidth_requests {
Self::add_requests_to_map(
shard_requests,
&mut self.prev_bandwidth_requests,
*from_shard,
params,
);
}
self.prev_bandwidth_requests_num = self.prev_bandwidth_requests.len().try_into().unwrap();
}
/// Set `new_bandwidth_requests`. Automatically converts to the target representation.
pub fn set_new_bandwidth_requests(
&mut self,
from_shard: ShardId,
requests: &BandwidthRequests,
params: &BandwidthSchedulerParams,
) {
Self::add_requests_to_map(requests, &mut self.new_bandwidth_requests, from_shard, params);
}
/// Convert bandwidth requests to the target representation and add to the given map.
fn add_requests_to_map(
requests: &BandwidthRequests,
map: &mut BTreeMap<(ShardId, ShardId), Vec<u64>>,
from_shard: ShardId,
params: &BandwidthSchedulerParams,
) {
match requests {
BandwidthRequests::V1(requests_v1) => {
for request in &requests_v1.requests {
map.insert(
(from_shard, request.to_shard.into()),
get_requested_values(request, params),
);
}
}
}
}
}
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct ReceiptSinkStats {
/// Outgoing size and gas limits to every shard.
pub outgoing_limits: BTreeMap<ShardId, OutgoingLimitStats>,
/// New outgoing receipts generated during this chunk application.
pub forwarded_receipts: BTreeMap<ShardId, ReceiptsStats>,
/// New buffered receipts that couldn't be forwarded because of the outgoing limits.
pub buffered_receipts: BTreeMap<ShardId, ReceiptsStats>,
/// Final state of the outgoing buffers after all new receipts have been forwarded or buffered.
/// Used to generate new bandwidth requests.
pub final_outgoing_buffers: BTreeMap<ShardId, ReceiptsStats>,
/// Whether the `ReceiptGroupsQueue` is fully initialized. Can only be false during the protocol
/// upgrade that enables bandwidth scheduler.
pub is_outgoing_metadata_ready: BTreeMap<ShardId, bool>,
/// Whether `is_outgoing_metadata_ready` is true for all shards. This must be true before
/// resharding can start.
pub all_outgoing_metadatas_ready: bool,
}
impl ReceiptSinkStats {
pub fn set_outgoing_limits(&mut self, limits: impl Iterator<Item = (ShardId, (u64, Gas))>) {
for (shard_id, (size, gas)) in limits {
self.outgoing_limits.insert(shard_id, OutgoingLimitStats { size, gas });
}
}
}
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct OutgoingLimitStats {
pub size: u64,
pub gas: Gas,
}
/// Stats about a set of receipts
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct ReceiptsStats {
/// Number of receipts
pub num: u64,
/// Total size of receipts, as calculated by `congestion_control::compute_receipt_size`.
pub total_size: u64,
/// Total gas of receipts, as calculated by `compute_receipt_congestion_gas`.
pub total_gas: u128,
}
impl ReceiptsStats {
pub fn add_receipt(&mut self, size: u64, gas: Gas) {
self.num += 1;
self.total_size += size;
let gas_u128: u128 = gas.as_gas().into();
self.total_gas += gas_u128;
}
}
/// Stats about token balance, used in balance checker.
///
/// This V0 layout is only kept so rows written by older binaries still
/// deserialize. New code uses [`BalanceStatsV1`].
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct BalanceStats {
pub tx_burnt_amount: Balance,
pub slashed_burnt_amount: Balance,
pub other_burnt_amount: Balance,
/// This is a negative amount. This amount was not charged from the account that issued
/// the transaction. It's likely due to the delayed queue of the receipts.
pub gas_deficit_amount: Balance,
/// No longer used, keeping to preserve borsh deserialization of the old data in the db.
pub _deprecated_global_actions_burnt_amount: Balance,
}
/// Stats about token balance, used in balance checker.
#[derive(Debug, Clone, Default, BorshSerialize, BorshDeserialize, ProtocolSchema)]
pub struct BalanceStatsV1 {
pub tx_burnt_amount: Balance,
pub slashed_burnt_amount: Balance,
pub other_burnt_amount: Balance,
/// This is a negative amount. This amount was not charged from the account that issued
/// the transaction. It's likely due to the delayed queue of the receipts.
pub gas_deficit_amount: Balance,
/// Amount of balance subsidized (effectively minted) for zero-balance contracts
/// attaching 1 yoctoNEAR to promise function calls. This amount must be
/// subtracted from total_balance_burnt to keep total supply correct.
pub subsidized_amount: Balance,
}
/// Convert a bandwidth request from the bitmap representation to a list of requested values.
fn get_requested_values(
bandwidth_request: &BandwidthRequest,
params: &BandwidthSchedulerParams,
) -> Vec<Bandwidth> {
let values = BandwidthRequestValues::new(params);
let mut res = Vec::new();
for i in 0..bandwidth_request.requested_values_bitmap.len() {
if bandwidth_request.requested_values_bitmap.get_bit(i) {
res.push(values.values[i]);
}
}
res
}