nodebench/
main.rs

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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::cli::Cli;
use alloy::network::primitives::BlockTransactionsKind;
use alloy::primitives::{BlockHash, BlockNumber};
use alloy::transports::BoxTransport;
use alloy::{
    eips::BlockNumberOrTag,
    primitives::TxHash,
    providers::{Provider, ProviderBuilder, RootProvider},
    rpc::types::BlockTransactions,
};
use chrono::{DateTime, Duration, Local, TimeDelta};
use clap::Parser;
use eyre::{eyre, Result};
use futures::future::join_all;
use loom_core_blockchain::Blockchain;
use loom_core_blockchain_actors::BlockchainActors;
use loom_evm_db::LoomDB;
use loom_node_actor_config::NodeBlockActorConfig;
use loom_types_events::MempoolEvents;
use std::fmt::Formatter;
use std::{collections::HashMap, fmt::Display, sync::Arc};
use tokio::{select, sync::RwLock, task::JoinHandle};

mod cli;

#[allow(dead_code)]
#[derive(Clone, Debug, Default)]
pub struct StatEntry {
    first: Vec<usize>,
    total_delay: Vec<Duration>,
    avg_delay_ms: Vec<i64>,
}

impl Display for StatEntry {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "first {:?} avg delay {:?} μs", self.first, self.avg_delay_ms)
    }
}

#[derive(Clone, Debug, Default)]
pub struct TimeMap {
    time_map: HashMap<usize, DateTime<Local>>,
}

impl TimeMap {
    pub fn add_time(&mut self, id: usize, time: DateTime<Local>) {
        self.time_map.entry(id).or_insert(time);
    }
    pub fn add_now(&mut self, id: usize) -> DateTime<Local> {
        *self.time_map.entry(id).or_insert(Local::now())
    }

    pub fn get_time(&self, id: usize) -> Option<&DateTime<Local>> {
        self.time_map.get(&id)
    }

    pub fn to_relative(&self, pings: &[TimeDelta]) -> TimeMap {
        let rel_time: HashMap<usize, DateTime<Local>> =
            self.time_map.iter().map(|(k, v)| (*k, *v - pings.get(*k).cloned().unwrap())).collect();
        TimeMap { time_map: rel_time }
    }

    pub fn get_first_time(&self) -> DateTime<Local> {
        self.time_map.values().min().cloned().unwrap_or_default()
    }

    pub fn get_time_delta(&self, id: usize) -> Option<TimeDelta> {
        self.time_map.get(&id).map(|x| *x - self.get_first_time())
    }
}

fn analyze_time_maps(time_map_vec: Vec<&TimeMap>, ping: Option<&[TimeDelta]>) -> StatEntry {
    let nodes_count = time_map_vec.first();

    if nodes_count.is_none() {
        return Default::default();
    }

    let nodes_count = nodes_count.unwrap().time_map.len();
    if nodes_count == 0 {
        return Default::default();
    }

    let mut delays: Vec<Duration> = vec![Duration::default(); nodes_count];
    let mut received_first: Vec<usize> = vec![0; nodes_count];

    for time_map in time_map_vec.iter() {
        for node_id in 0..nodes_count {
            match ping {
                Some(ping) => {
                    if let Some(t) = time_map.to_relative(ping).get_time_delta(node_id) {
                        delays[node_id] += t;
                        if t.is_zero() {
                            received_first[node_id] += 1;
                        }
                    }
                }
                None => {
                    if let Some(t) = time_map.get_time_delta(node_id) {
                        delays[node_id] += t;
                        if t.is_zero() {
                            received_first[node_id] += 1;
                        }
                    }
                }
            }
        }
    }

    let total_entries: usize = received_first.iter().sum();

    let delays_avg: Vec<i64> = delays
        .iter()
        .enumerate()
        .map(|(i, x)| {
            if total_entries - received_first[i] == 0 {
                0
            } else {
                x.num_microseconds().unwrap_or_default() / ((total_entries - received_first[i]) as i64)
            }
        })
        .collect();

    StatEntry { first: received_first, total_delay: delays, avg_delay_ms: delays_avg }
}

#[derive(Clone, Debug, Default)]
pub struct StatCollector {
    ping: Vec<TimeDelta>,
    blocks: HashMap<BlockHash, BlockNumber>,
    block_headers: HashMap<BlockNumber, TimeMap>,
    block_with_tx: HashMap<BlockNumber, TimeMap>,
    block_logs: HashMap<BlockNumber, TimeMap>,
    block_state: HashMap<BlockNumber, TimeMap>,
    txs: HashMap<TxHash, TimeMap>,
}

impl Display for StatCollector {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "headers abs {}", analyze_time_maps(self.block_headers.values().collect(), None))?;
        writeln!(f, "headers rel {}", analyze_time_maps(self.block_headers.values().collect(), Some(&self.ping)))?;
        writeln!(f, "blocks abs {}", analyze_time_maps(self.block_with_tx.values().collect(), None))?;
        writeln!(f, "blocks rel {}", analyze_time_maps(self.block_with_tx.values().collect(), Some(&self.ping)))?;
        writeln!(f, "logs abs {}", analyze_time_maps(self.block_logs.values().collect(), None))?;
        writeln!(f, "logs rel {}", analyze_time_maps(self.block_logs.values().collect(), Some(&self.ping)))?;
        writeln!(f, "state abs {}", analyze_time_maps(self.block_state.values().collect(), None))?;
        writeln!(f, "state rel {}", analyze_time_maps(self.block_state.values().collect(), Some(&self.ping)))?;
        writeln!(f, "-----")
    }
}

#[derive(Clone, Debug, Default)]
pub struct TxStatCollector {
    pub(crate) total_received_tx: usize,
    pub(crate) total_txs: usize,
    pub(crate) txs_received: Vec<usize>,
    pub(crate) txs_received_first: Vec<usize>,
    pub(crate) txs_received_first_relative: Vec<usize>,
    pub(crate) txs_delays: Vec<Duration>,
    pub(crate) txs_delays_relative: Vec<Duration>,

    pub(crate) txs_received_outdated: Vec<usize>,
}

impl Display for TxStatCollector {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let total_txs: usize = self.txs_received_first.iter().sum();

        let tx_delays_avg: Vec<i64> = self
            .txs_delays
            .iter()
            .enumerate()
            .map(|(i, x)| {
                if total_txs - self.txs_received_first[i] == 0 {
                    0
                } else {
                    x.num_microseconds().unwrap_or_default() / ((total_txs - self.txs_received_first[i]) as i64)
                }
            })
            .collect();

        let total_txs_rel: usize = self.txs_received_first_relative.iter().sum();

        let tx_delays_relative_avg: Vec<i64> = self
            .txs_delays
            .iter()
            .enumerate()
            .map(|(i, x)| {
                if total_txs_rel - self.txs_received_first_relative[i] == 0 {
                    0
                } else {
                    x.num_microseconds().unwrap_or_default() / ((total_txs_rel - self.txs_received_first_relative[i]) as i64)
                }
            })
            .collect();
        writeln!(
            f,
            "txs total in blocks: {} received by nodes: {} per node {:?}  outdated {:?}",
            self.total_txs, self.total_received_tx, self.txs_received, self.txs_received_outdated,
        )?;
        writeln!(f, "txs abs first {:?} delays avg {:?} μs", self.txs_received_first, tx_delays_avg)?;
        writeln!(f, "txs rel first {:?} delays avg {:?} μs", self.txs_received_first_relative, tx_delays_relative_avg)?;

        Ok(())
    }
}

impl TxStatCollector {
    pub fn new(nodes_count: usize) -> TxStatCollector {
        TxStatCollector {
            txs_received: vec![0; nodes_count],
            txs_received_first: vec![0; nodes_count],
            txs_received_first_relative: vec![0; nodes_count],
            txs_delays: vec![Duration::default(); nodes_count],
            txs_delays_relative: vec![Duration::default(); nodes_count],

            txs_received_outdated: vec![0; nodes_count],
            ..TxStatCollector::default()
        }
    }
}

async fn collect_stat_task(
    id: usize,
    provider: RootProvider<BoxTransport>,
    grps: bool,
    stat: Arc<RwLock<StatCollector>>,
    warn_up_blocks: usize,
    blocks_needed: usize,
    ping_time: TimeDelta,
) -> Result<()> {
    let bc = Blockchain::<LoomDB>::new(1);

    let mut bc_actors = BlockchainActors::new(provider, bc.clone(), vec![]);
    if grps {
        bc_actors.with_exex_events()?;
    } else {
        bc_actors.with_block_events(NodeBlockActorConfig::all_enabled())?.with_local_mempool_events()?;
    }

    let mut blocks_counter: usize = 0;

    let mut block_header_subscription = bc.new_block_headers_channel().subscribe().await;
    let mut block_with_tx_subscription = bc.new_block_with_tx_channel().subscribe().await;
    let mut block_logs_subscription = bc.new_block_logs_channel().subscribe().await;
    let mut block_state_subscription = bc.new_block_state_update_channel().subscribe().await;

    let mut pending_tx_subscription = bc.mempool_events_channel().subscribe().await;

    loop {
        select! {
            header = block_header_subscription.recv() => {
                match header {
                    Ok(header)=>{
                        let block_number = header.inner.header.number;
                        let block_hash = header.inner.header.hash;
                        stat.write().await.blocks.insert(block_hash, block_number);

                        blocks_counter += 1;
                        if blocks_counter >= warn_up_blocks {
                            let recv_time = stat.write().await.block_headers.entry(block_number).or_default().add_now(id);
                            println!("{id} : {} block header received {} {}", block_number, block_hash, recv_time - ping_time);
                        }else{
                            println!("Warming up {id} : {} block header received {}", block_number, block_hash);
                        }

                        if blocks_counter >= blocks_needed + warn_up_blocks {
                            break;
                        }
                    }
                    Err(e)=>{
                        println!("Error receiving block header {id} {e}");
                    }
                }

            }
            block = block_with_tx_subscription.recv() => {
                match block {
                    Ok(block)=>{
                        let block_number = block.header.number;
                        let block_hash = block.header.hash;
                        if blocks_counter >= warn_up_blocks {
                            let recv_time = stat.write().await.block_with_tx.entry(block_number).or_default().add_now(id);
                            println!("{id} : {} block with tx received {} {}", block_number, block_hash, recv_time - ping_time);
                        }else{
                            println!("Warming up {id} : {} block with tx received {}", block_number, block_hash);
                        }
                    }
                    Err(e)=>{
                        println!("Error receiving block with tx {id} {e}");
                    }
                }
            }
            logs = block_logs_subscription.recv() => {
                match logs {
                    Ok(logs)=>{
                        let block_number = stat.read().await.blocks.get(&logs.block_header.hash).cloned().unwrap_or_default();

                        if blocks_counter >= warn_up_blocks {
                            let recv_time = stat.write().await.block_logs.entry(block_number).or_default().add_now(id);
                            println!("{id} : {} block logs received {} {}", block_number, logs.block_header.hash, recv_time - ping_time);
                        }else{
                            println!("Warming up {id} : {} block logs received {}", block_number, logs.block_header.hash);
                        }
                    }
                    Err(e)=>{
                        println!("Error receiving block logs {id} {e}");
                    }
                }
            }


            state_update = block_state_subscription.recv() => {
                match state_update  {
                    Ok(state_update)=>{
                        let block_number = stat.read().await.blocks.get(&state_update.block_header.hash).cloned().unwrap_or_default();
                        let block_hash = state_update.block_header.hash;

                        if blocks_counter >= warn_up_blocks {
                            let recv_time = stat.write().await.block_state.entry(block_number).or_default().add_now(id);
                            println!("{id} : {} block state received {} {}", block_number, block_hash, recv_time - ping_time);
                        }else{
                            println!("Warming up {id} : {} block state tx received {}", block_number, block_hash);
                        }
                    }
                    Err(e)=>{
                        println!("Error receiving block state {id} {e}");
                    }
                }
            }

            mempool_event = pending_tx_subscription.recv() =>{
                match mempool_event {
                    Ok(mempool_event) =>{
                        if let MempoolEvents::MempoolTxUpdate{ tx_hash} = mempool_event {
                            stat.write().await.txs.entry(tx_hash).or_default().add_now(id);
                        }
                    }
                    Err(e)=>{
                        println!("Error receiving tx {id} {e}");
                    }
                }

            }

        }
    }
    println!("{id} finished");

    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug,alloy_rpc_client=info,h2=info"));
    let cli = Cli::parse();

    if cli.endpoint.is_empty() {
        return Err(eyre!("NO_NODES_SELECTED"));
    }

    let nodes_count = cli.endpoint.len();

    let stat = Arc::new(RwLock::new(StatCollector::default()));

    println!("Hello, nodebench!");

    let mut tasks: Vec<JoinHandle<_>> = vec![];

    let mut first_provider: Option<RootProvider<BoxTransport>> = None;
    let mut prev_provider: Option<RootProvider<BoxTransport>> = None;

    for (idx, endpoint) in cli.endpoint.iter().enumerate() {
        //let conn = WsConnect::new(endpoint.clone());
        let (provider, is_grpc) = if endpoint == "grpc" {
            (prev_provider.clone().unwrap(), true)
        } else {
            (ProviderBuilder::new().on_builtin(endpoint.clone().as_str()).await?, false)
        };

        prev_provider = Some(provider.clone());

        if first_provider.is_none() {
            first_provider = Some(provider.clone());
        }

        let start_time = Local::now();
        for _i in 0u64..10 {
            let block_number = provider.get_block_number().await?;
            let _ = provider.get_block_by_number(BlockNumberOrTag::Number(block_number), BlockTransactionsKind::Hashes).await?;
        }
        let ping_time = (Local::now() - start_time) / (10 * 2);
        println!("Ping time {idx} : {ping_time}");
        stat.write().await.ping.push(ping_time);

        let join_handler = tokio::spawn(collect_stat_task(idx, provider, is_grpc, stat.clone(), 3, 10, ping_time));
        tasks.push(join_handler);
    }

    join_all(tasks).await;

    let stat = stat.read().await;
    let first_provider = first_provider.unwrap();

    let mut calc = TxStatCollector::new(cli.endpoint.len());

    println!("{}", stat);

    for (block_number, _) in stat.block_headers.iter() {
        println!("Getting block {block_number}");
        let block =
            first_provider.get_block_by_number(BlockNumberOrTag::Number(*block_number), BlockTransactionsKind::Hashes).await?.unwrap();

        calc.total_txs += block.transactions.len();

        let block_time_map = stat.block_headers.get(block_number).unwrap();

        if let BlockTransactions::Hashes(tx_hash_vec) = block.transactions {
            for tx_hash in tx_hash_vec {
                if let Some(tx_time) = stat.txs.get(&tx_hash) {
                    calc.total_received_tx += 1;
                    for node_id in 0..nodes_count {
                        let block_time_node = block_time_map.get_time(node_id).unwrap();

                        if let Some(tx_local_time) = tx_time.get_time(node_id) {
                            calc.txs_received[node_id] += 1;

                            // check if tx received after block
                            if tx_local_time > block_time_node
                                || tx_time.get_time_delta(node_id).unwrap_or_default() > TimeDelta::seconds(2)
                            {
                                calc.txs_received_outdated[node_id] += 1;
                            } else {
                                // calc absolute delay
                                if let Some(t) = tx_time.get_time_delta(node_id) {
                                    calc.txs_delays[node_id] += t;
                                    if t.is_zero() {
                                        calc.txs_received_first[node_id] += 1;
                                    }
                                }
                                //calc relative delay
                                if let Some(t) = tx_time.to_relative(&stat.ping).get_time_delta(node_id) {
                                    calc.txs_delays_relative[node_id] += t;
                                    if t.is_zero() {
                                        calc.txs_received_first_relative[node_id] += 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    println!("{calc}");

    Ok(())
}