From 38ee3bdc72a5386827bafa0931a971da9aa32966 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Thu, 19 Sep 2024 15:40:34 +0200 Subject: [PATCH] chore: fix ci (#49) Description --- fixes fmt fixes ci profiles --- .config/nextest.toml | 14 +++++ .github/workflows/ci.yml | 13 +++-- .gitignore | 2 + src/cli/args.rs | 10 ++-- src/cli/commands/list_tribes.rs | 17 +++--- src/cli/commands/start.rs | 6 +- src/cli/commands/util.rs | 25 ++++---- src/cli/util.rs | 3 +- src/server/config.rs | 7 ++- src/server/grpc/base_node.rs | 96 +++++++++++++++++++++---------- src/server/grpc/p2pool.rs | 50 +++++++++------- src/server/http/server.rs | 32 ++++++----- src/server/http/stats/cache.rs | 10 ++-- src/server/http/stats/handlers.rs | 34 ++++++----- src/server/http/stats/models.rs | 3 +- src/server/p2p/error.rs | 7 ++- src/server/p2p/messages.rs | 15 +++-- src/server/p2p/network.rs | 72 ++++++++++++++--------- src/server/p2p/peer_store.rs | 12 ++-- src/server/p2p/util.rs | 3 +- src/server/server.rs | 27 ++++----- src/sharechain/block.rs | 10 ++-- src/sharechain/error.rs | 10 ++-- src/sharechain/in_memory.rs | 65 +++++++++++++-------- src/sharechain/mod.rs | 9 +-- 25 files changed, 329 insertions(+), 223 deletions(-) create mode 100644 .config/nextest.toml diff --git a/.config/nextest.toml b/.config/nextest.toml new file mode 100644 index 0000000..dac06fa --- /dev/null +++ b/.config/nextest.toml @@ -0,0 +1,14 @@ +[profile.ci] +slow-timeout = { period = "60s", terminate-after=2} + +[profile.ci.junit] # this can be some other profile, too +path = "junit.xml" + +[profile.intellij] +retries = 0 +slow-timeout = { period = "30s" } +failure-output = "immediate-final" +fail-fast = false + +[profile.intellij.junit] # this can be some other profile, too +path = "junit.xml" \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b687ff6..20c7c74 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ name: CI merge_group: env: - toolchain: nightly-2024-03-01 + toolchain: nightly-2024-09-19 CARGO_HTTP_MULTIPLEXING: false CARGO_TERM_COLOR: always CARGO_UNSTABLE_SPARSE_REGISTRY: true @@ -26,7 +26,7 @@ concurrency: jobs: clippy: name: clippy - runs-on: [ ubuntu-20.04 ] + runs-on: [ ubuntu-latest ] steps: - name: checkout uses: actions/checkout@v4 @@ -39,7 +39,11 @@ jobs: run: | sudo apt-get update sudo bash scripts/install_ubuntu_dependencies.sh + - name: Cache cargo files and outputs + if: startsWith(runner.environment,'github-hosted') + uses: Swatinem/rust-cache@v2 - name: caching (nightly) + if: startsWith(runner.environment,'self-hosted') # Don't use rust-cache. # Rust-cache disables a key feature of actions/cache: restoreKeys. # Without restore keys, we lose the ability to get partial matches on caches, and end @@ -59,11 +63,12 @@ jobs: tari-${{ runner.os }}-${{ runner.cpu-model }}-${{ env.toolchain }}-nightly-${{ hashFiles('**/Cargo.lock') }} tari-${{ runner.os }}-${{ runner.cpu-model }}-${{ env.toolchain }}-nightly - name: cargo format - run: cargo fmt --all -- --check + run: cargo +${{ env.toolchain }} fmt --all -- --check - name: Install cargo-lints run: cargo install cargo-lints - name: Clippy check (with lints) - run: cargo lints clippy --all-targets --all-features + run: cargo +${{ env.toolchain }} lints clippy --all-targets --all-features + machete: # Checks for unused dependencies. name: machete diff --git a/.gitignore b/.gitignore index 1560a6f..e8d7375 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ target/ # Ignore OS files .DS_Store + +.idea/ diff --git a/src/cli/args.rs b/src/cli/args.rs index 2573632..dd02197 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -1,15 +1,15 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::path::PathBuf; -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; use clap::{Parser, Subcommand}; use tari_shutdown::ShutdownSignal; -use crate::cli::commands; -use crate::cli::util::cli_styles; -use crate::cli::util::validate_tribe; +use crate::cli::{ + commands, + util::{cli_styles, validate_tribe}, +}; #[allow(clippy::struct_excessive_bools)] #[derive(Clone, Parser, Debug)] diff --git a/src/cli/commands/list_tribes.rs b/src/cli/commands/list_tribes.rs index b848355..6f05a0e 100644 --- a/src/cli/commands/list_tribes.rs +++ b/src/cli/commands/list_tribes.rs @@ -1,19 +1,20 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::sync::Arc; -use std::time::Duration; +use std::{sync::Arc, time::Duration}; use anyhow::anyhow; use itertools::Itertools; use tari_shutdown::{Shutdown, ShutdownSignal}; -use tokio::sync::oneshot; -use tokio::task::JoinHandle; -use tokio::{select, time}; +use tokio::{select, sync::oneshot, task::JoinHandle, time}; -use crate::cli::args::{Cli, ListTribeArgs, StartArgs}; -use crate::cli::commands::util; -use crate::server::p2p::peer_store::PeerStore; +use crate::{ + cli::{ + args::{Cli, ListTribeArgs, StartArgs}, + commands::util, + }, + server::p2p::peer_store::PeerStore, +}; pub async fn handle_list_tribes( cli: Arc, diff --git a/src/cli/commands/start.rs b/src/cli/commands/start.rs index 2e269b2..887d1eb 100644 --- a/src/cli/commands/start.rs +++ b/src/cli/commands/start.rs @@ -5,8 +5,10 @@ use std::sync::Arc; use tari_shutdown::ShutdownSignal; -use crate::cli::args::{Cli, StartArgs}; -use crate::cli::commands::util; +use crate::cli::{ + args::{Cli, StartArgs}, + commands::util, +}; pub async fn handle_start(cli: Arc, args: &StartArgs, cli_shutdown_signal: ShutdownSignal) -> anyhow::Result<()> { util::server(cli, args, cli_shutdown_signal, true) diff --git a/src/cli/commands/util.rs b/src/cli/commands/util.rs index 86ef516..2b63a56 100644 --- a/src/cli/commands/util.rs +++ b/src/cli/commands/util.rs @@ -1,23 +1,22 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::env; -use std::sync::Arc; +use std::{env, sync::Arc}; use libp2p::identity::Keypair; -use tari_common::configuration::Network; -use tari_common::initialize_logging; -use tari_core::consensus::ConsensusManager; -use tari_core::proof_of_work::randomx_factory::RandomXFactory; -use tari_core::proof_of_work::PowAlgorithm; +use tari_common::{configuration::Network, initialize_logging}; +use tari_core::{ + consensus::ConsensusManager, + proof_of_work::{randomx_factory::RandomXFactory, PowAlgorithm}, +}; use tari_shutdown::ShutdownSignal; -use crate::cli::args::{Cli, StartArgs}; -use crate::server as main_server; -use crate::server::p2p::Tribe; -use crate::server::Server; -use crate::sharechain::in_memory::InMemoryShareChain; -use crate::sharechain::{BlockValidationParams, MAX_BLOCKS_COUNT}; +use crate::{ + cli::args::{Cli, StartArgs}, + server as main_server, + server::{p2p::Tribe, Server}, + sharechain::{in_memory::InMemoryShareChain, BlockValidationParams, MAX_BLOCKS_COUNT}, +}; pub async fn server( cli: Arc, diff --git a/src/cli/util.rs b/src/cli/util.rs index 0240928..3fc6b10 100644 --- a/src/cli/util.rs +++ b/src/cli/util.rs @@ -1,8 +1,7 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use clap::builder::styling::AnsiColor; -use clap::builder::Styles; +use clap::builder::{styling::AnsiColor, Styles}; pub fn cli_styles() -> Styles { Styles::styled() diff --git a/src/server/config.rs b/src/server/config.rs index 9f138e7..b0c3254 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -5,8 +5,11 @@ use std::{path::PathBuf, time::Duration}; use libp2p::identity::Keypair; -use crate::server::p2p::Tribe; -use crate::server::{http, p2p, p2p::peer_store::PeerStoreConfig}; +use crate::server::{ + http, + p2p, + p2p::{peer_store::PeerStoreConfig, Tribe}, +}; /// Config is the server configuration struct. #[derive(Clone)] diff --git a/src/server/grpc/base_node.rs b/src/server/grpc/base_node.rs index 56049cb..07f0f72 100644 --- a/src/server/grpc/base_node.rs +++ b/src/server/grpc/base_node.rs @@ -8,16 +8,52 @@ use log::error; use minotari_app_grpc::{ tari_rpc, tari_rpc::{ - base_node_client::BaseNodeClient, Block, BlockBlobRequest, BlockGroupRequest, BlockGroupResponse, - BlockHeaderResponse, BlockHeight, BlockTimingResponse, ConsensusConstants, Empty, FetchMatchingUtxosRequest, - GetActiveValidatorNodesRequest, GetBlocksRequest, GetHeaderByHashRequest, GetMempoolTransactionsRequest, - GetNewBlockBlobResult, GetNewBlockResult, GetNewBlockTemplateWithCoinbasesRequest, - GetNewBlockWithCoinbasesRequest, GetPeersRequest, GetShardKeyRequest, GetShardKeyResponse, - GetSideChainUtxosRequest, GetTemplateRegistrationsRequest, HeightRequest, HistoricalBlock, - ListConnectedPeersResponse, ListHeadersRequest, MempoolStatsResponse, NetworkStatusResponse, NewBlockTemplate, - NewBlockTemplateRequest, NewBlockTemplateResponse, NodeIdentity, SearchKernelsRequest, SearchUtxosRequest, - SoftwareUpdate, StringValue, SubmitBlockResponse, SubmitTransactionRequest, SubmitTransactionResponse, - SyncInfoResponse, SyncProgressResponse, TipInfoResponse, TransactionStateRequest, TransactionStateResponse, + base_node_client::BaseNodeClient, + Block, + BlockBlobRequest, + BlockGroupRequest, + BlockGroupResponse, + BlockHeaderResponse, + BlockHeight, + BlockTimingResponse, + ConsensusConstants, + Empty, + FetchMatchingUtxosRequest, + GetActiveValidatorNodesRequest, + GetBlocksRequest, + GetHeaderByHashRequest, + GetMempoolTransactionsRequest, + GetNewBlockBlobResult, + GetNewBlockResult, + GetNewBlockTemplateWithCoinbasesRequest, + GetNewBlockWithCoinbasesRequest, + GetPeersRequest, + GetShardKeyRequest, + GetShardKeyResponse, + GetSideChainUtxosRequest, + GetTemplateRegistrationsRequest, + HeightRequest, + HistoricalBlock, + ListConnectedPeersResponse, + ListHeadersRequest, + MempoolStatsResponse, + NetworkStatusResponse, + NewBlockTemplate, + NewBlockTemplateRequest, + NewBlockTemplateResponse, + NodeIdentity, + SearchKernelsRequest, + SearchUtxosRequest, + SoftwareUpdate, + StringValue, + SubmitBlockResponse, + SubmitTransactionRequest, + SubmitTransactionResponse, + SyncInfoResponse, + SyncProgressResponse, + TipInfoResponse, + TransactionStateRequest, + TransactionStateResponse, ValueAtHeightResponse, }, }; @@ -112,48 +148,66 @@ impl TariBaseNodeGrpc { #[tonic::async_trait] impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { + type FetchMatchingUtxosStream = mpsc::Receiver>; + type GetActiveValidatorNodesStream = mpsc::Receiver>; + type GetBlocksStream = mpsc::Receiver>; + type GetMempoolTransactionsStream = mpsc::Receiver>; + type GetNetworkDifficultyStream = mpsc::Receiver>; + type GetPeersStream = mpsc::Receiver>; + type GetSideChainUtxosStream = mpsc::Receiver>; + type GetTemplateRegistrationsStream = mpsc::Receiver>; + type GetTokensInCirculationStream = mpsc::Receiver>; type ListHeadersStream = mpsc::Receiver>; + type SearchKernelsStream = mpsc::Receiver>; + type SearchUtxosStream = mpsc::Receiver>; + async fn list_headers( &self, request: Request, ) -> Result, Status> { proxy_stream_result!(self, list_headers, request, LIST_HEADERS_PAGE_SIZE) } + async fn get_header_by_hash( &self, request: Request, ) -> Result, Status> { proxy_simple_result!(self, get_header_by_hash, request) } - type GetBlocksStream = mpsc::Receiver>; + async fn get_blocks(&self, request: Request) -> Result, Status> { proxy_stream_result!(self, get_blocks, request, GET_BLOCKS_PAGE_SIZE) } + async fn get_block_timing(&self, request: Request) -> Result, Status> { proxy_simple_result!(self, get_block_timing, request) } + async fn get_constants(&self, request: Request) -> Result, Status> { proxy_simple_result!(self, get_constants, request) } + async fn get_block_size( &self, request: Request, ) -> Result, Status> { proxy_simple_result!(self, get_block_size, request) } + async fn get_block_fees( &self, request: Request, ) -> Result, Status> { proxy_simple_result!(self, get_block_fees, request) } + async fn get_version(&self, request: Request) -> Result, Status> { proxy_simple_result!(self, get_version, request) } + async fn check_for_updates(&self, request: Request) -> Result, Status> { proxy_simple_result!(self, check_for_updates, request) } - type GetTokensInCirculationStream = mpsc::Receiver>; async fn get_tokens_in_circulation( &self, @@ -167,8 +221,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { ) } - type GetNetworkDifficultyStream = mpsc::Receiver>; - async fn get_network_difficulty( &self, request: Request, @@ -238,8 +290,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_simple_result!(self, get_tip_info, request) } - type SearchKernelsStream = mpsc::Receiver>; - async fn search_kernels( &self, request: Request, @@ -247,8 +297,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_stream_result!(self, search_kernels, request, GET_BLOCKS_PAGE_SIZE) } - type SearchUtxosStream = mpsc::Receiver>; - async fn search_utxos( &self, request: Request, @@ -256,8 +304,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_stream_result!(self, search_utxos, request, GET_BLOCKS_PAGE_SIZE) } - type FetchMatchingUtxosStream = mpsc::Receiver>; - async fn fetch_matching_utxos( &self, request: Request, @@ -265,14 +311,10 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_stream_result!(self, fetch_matching_utxos, request, GET_BLOCKS_PAGE_SIZE) } - type GetPeersStream = mpsc::Receiver>; - async fn get_peers(&self, request: Request) -> Result, Status> { proxy_stream_result!(self, get_peers, request, GET_BLOCKS_PAGE_SIZE) } - type GetMempoolTransactionsStream = mpsc::Receiver>; - async fn get_mempool_transactions( &self, request: Request, @@ -306,8 +348,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_simple_result!(self, get_mempool_stats, request) } - type GetActiveValidatorNodesStream = mpsc::Receiver>; - async fn get_active_validator_nodes( &self, request: Request, @@ -322,8 +362,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_simple_result!(self, get_shard_key, request) } - type GetTemplateRegistrationsStream = mpsc::Receiver>; - async fn get_template_registrations( &self, request: Request, @@ -331,8 +369,6 @@ impl tari_rpc::base_node_server::BaseNode for TariBaseNodeGrpc { proxy_stream_result!(self, get_template_registrations, request, 10) } - type GetSideChainUtxosStream = mpsc::Receiver>; - async fn get_side_chain_utxos( &self, request: Request, diff --git a/src/server/grpc/p2pool.rs b/src/server/grpc/p2pool.rs index 5dc3078..76973d8 100644 --- a/src/server/grpc/p2pool.rs +++ b/src/server/grpc/p2pool.rs @@ -1,37 +1,46 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use std::{collections::HashMap, sync::Arc}; + use log::{debug, error, info, warn}; -use minotari_app_grpc::tari_rpc::pow_algo::PowAlgos; use minotari_app_grpc::tari_rpc::{ - base_node_client::BaseNodeClient, sha_p2_pool_server::ShaP2Pool, GetNewBlockRequest, GetNewBlockResponse, - GetNewBlockTemplateWithCoinbasesRequest, NewBlockTemplateRequest, SubmitBlockRequest, SubmitBlockResponse, + base_node_client::BaseNodeClient, + pow_algo::PowAlgos, + sha_p2_pool_server::ShaP2Pool, + GetNewBlockRequest, + GetNewBlockResponse, + GetNewBlockTemplateWithCoinbasesRequest, + NewBlockTemplateRequest, + SubmitBlockRequest, + SubmitBlockResponse, }; -use std::collections::HashMap; -use std::sync::Arc; use tari_common::configuration::Network; use tari_common_types::types::FixedHash; -use tari_core::consensus; -use tari_core::consensus::ConsensusManager; -use tari_core::proof_of_work::randomx_factory::RandomXFactory; -use tari_core::proof_of_work::{randomx_difficulty, sha3x_difficulty, PowAlgorithm}; +use tari_core::{ + consensus, + consensus::ConsensusManager, + proof_of_work::{randomx_difficulty, randomx_factory::RandomXFactory, sha3x_difficulty, PowAlgorithm}, +}; use tari_shutdown::ShutdownSignal; use tari_utilities::hex::Hex; use tokio::sync::Mutex; use tonic::{Request, Response, Status}; -use crate::server::http::stats::{ - algo_stat_key, MINER_STAT_ACCEPTED_BLOCKS_COUNT, MINER_STAT_REJECTED_BLOCKS_COUNT, - P2POOL_STAT_ACCEPTED_BLOCKS_COUNT, P2POOL_STAT_REJECTED_BLOCKS_COUNT, -}; -use crate::server::stats_store::StatsStore; -use crate::sharechain::BlockValidationParams; use crate::{ server::{ grpc::{error::Error, util}, + http::stats::{ + algo_stat_key, + MINER_STAT_ACCEPTED_BLOCKS_COUNT, + MINER_STAT_REJECTED_BLOCKS_COUNT, + P2POOL_STAT_ACCEPTED_BLOCKS_COUNT, + P2POOL_STAT_REJECTED_BLOCKS_COUNT, + }, p2p, + stats_store::StatsStore, }, - sharechain::{block::Block, ShareChain, SHARE_COUNT}, + sharechain::{block::Block, BlockValidationParams, ShareChain, SHARE_COUNT}, }; const LOG_TARGET: &str = "p2pool::server::grpc::p2pool"; @@ -57,8 +66,7 @@ pub fn min_difficulty(pow: PowAlgorithm) -> Result { /// P2Pool specific gRPC service to provide `get_new_block` and `submit_block` functionalities. pub struct ShaP2PoolGrpc -where - S: ShareChain, +where S: ShareChain { /// Base node client client: Arc>>, @@ -77,8 +85,7 @@ where } impl ShaP2PoolGrpc -where - S: ShareChain, +where S: ShareChain { pub async fn new( base_node_address: String, @@ -140,8 +147,7 @@ where #[tonic::async_trait] impl ShaP2Pool for ShaP2PoolGrpc -where - S: ShareChain, +where S: ShareChain { /// Returns a new block (that can be mined) which contains all the shares generated /// from the current share chain as coinbase transactions. diff --git a/src/server/http/server.rs b/src/server/http/server.rs index e4fed17..f91ec0c 100644 --- a/src/server/http/server.rs +++ b/src/server/http/server.rs @@ -1,21 +1,27 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use crate::server::http::stats::cache::StatsCache; -use crate::server::http::stats::handlers; -use crate::server::http::{health, version}; -use crate::server::p2p::peer_store::PeerStore; -use crate::server::p2p::Tribe; -use crate::server::stats_store::StatsStore; -use crate::sharechain::ShareChain; -use axum::routing::get; -use axum::Router; -use log::info; use std::sync::Arc; + +use axum::{routing::get, Router}; +use log::info; use tari_shutdown::ShutdownSignal; use thiserror::Error; use tokio::io; +use crate::{ + server::{ + http::{ + health, + stats::{cache::StatsCache, handlers}, + version, + }, + p2p::{peer_store::PeerStore, Tribe}, + stats_store::StatsStore, + }, + sharechain::ShareChain, +}; + const LOG_TARGET: &str = "p2pool::server::stats"; #[derive(Clone, Debug)] @@ -40,8 +46,7 @@ pub enum Error { } pub struct HttpServer -where - S: ShareChain, +where S: ShareChain { share_chain_sha3x: Arc, share_chain_random_x: Arc, @@ -64,8 +69,7 @@ pub struct AppState { } impl HttpServer -where - S: ShareChain, +where S: ShareChain { pub fn new( share_chain_sha3x: Arc, diff --git a/src/server/http/stats/cache.rs b/src/server/http/stats/cache.rs index 996e49a..0181692 100644 --- a/src/server/http/stats/cache.rs +++ b/src/server/http/stats/cache.rs @@ -1,12 +1,12 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use crate::server::http::stats::models::Stats; -use std::sync::Arc; -use std::time::Duration; +use std::{sync::Arc, time::Duration}; + use tari_core::proof_of_work::PowAlgorithm; -use tokio::sync::RwLock; -use tokio::time::Instant; +use tokio::{sync::RwLock, time::Instant}; + +use crate::server::http::stats::models::Stats; #[derive(Clone)] pub struct CachedStats { diff --git a/src/server/http/stats/handlers.rs b/src/server/http/stats/handlers.rs index 8352b3d..00ab41e 100644 --- a/src/server/http/stats/handlers.rs +++ b/src/server/http/stats/handlers.rs @@ -1,28 +1,32 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::collections::HashMap; -use std::sync::Arc; +use std::{collections::HashMap, sync::Arc}; -use axum::extract::State; -use axum::http::StatusCode; -use axum::Json; +use axum::{extract::State, http::StatusCode, Json}; use itertools::Itertools; use log::error; use tari_common::configuration::Network; -use tari_core::consensus::ConsensusManager; -use tari_core::proof_of_work::PowAlgorithm; -use tari_core::transactions::tari_amount::MicroMinotari; +use tari_core::{consensus::ConsensusManager, proof_of_work::PowAlgorithm, transactions::tari_amount::MicroMinotari}; use tari_utilities::epoch_time::EpochTime; -use crate::server::http::server::AppState; -use crate::server::http::stats::models::{BlockStats, EstimatedEarnings, Stats, TribeDetails}; -use crate::server::http::stats::{ - algo_stat_key, MINER_STAT_ACCEPTED_BLOCKS_COUNT, MINER_STAT_REJECTED_BLOCKS_COUNT, - P2POOL_STAT_ACCEPTED_BLOCKS_COUNT, P2POOL_STAT_REJECTED_BLOCKS_COUNT, +use crate::{ + server::{ + http::{ + server::AppState, + stats::{ + algo_stat_key, + models::{BlockStats, EstimatedEarnings, Stats, TribeDetails}, + MINER_STAT_ACCEPTED_BLOCKS_COUNT, + MINER_STAT_REJECTED_BLOCKS_COUNT, + P2POOL_STAT_ACCEPTED_BLOCKS_COUNT, + P2POOL_STAT_REJECTED_BLOCKS_COUNT, + }, + }, + stats_store::StatsStore, + }, + sharechain::SHARE_COUNT, }; -use crate::server::stats_store::StatsStore; -use crate::sharechain::SHARE_COUNT; const LOG_TARGET: &str = "p2pool::server::stats::get"; diff --git a/src/server/http/stats/models.rs b/src/server/http/stats/models.rs index 31f600f..edc7f8f 100644 --- a/src/server/http/stats/models.rs +++ b/src/server/http/stats/models.rs @@ -5,8 +5,7 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; use tari_core::transactions::tari_amount::MicroMinotari; -use tari_utilities::epoch_time::EpochTime; -use tari_utilities::hex::Hex; +use tari_utilities::{epoch_time::EpochTime, hex::Hex}; use crate::sharechain::block::Block; diff --git a/src/server/p2p/error.rs b/src/server/p2p/error.rs index f366eae..353e93d 100644 --- a/src/server/p2p/error.rs +++ b/src/server/p2p/error.rs @@ -4,7 +4,12 @@ use std::string::FromUtf8Error; use libp2p::{ - gossipsub::PublishError, identity::DecodingError, kad::NoKnownPeers, multiaddr, noise, swarm::DialError, + gossipsub::PublishError, + identity::DecodingError, + kad::NoKnownPeers, + multiaddr, + noise, + swarm::DialError, TransportError, }; use thiserror::Error; diff --git a/src/server/p2p/messages.rs b/src/server/p2p/messages.rs index 1672e82..9d0ccc3 100644 --- a/src/server/p2p/messages.rs +++ b/src/server/p2p/messages.rs @@ -3,12 +3,15 @@ use std::time::{SystemTime, UNIX_EPOCH}; -use crate::server::p2p::Tribe; -use crate::{server::p2p::Error, sharechain::block::Block}; use libp2p::PeerId; use serde::{Deserialize, Serialize}; use tari_core::proof_of_work::PowAlgorithm; +use crate::{ + server::p2p::{Error, Tribe}, + sharechain::block::Block, +}; + #[macro_export] macro_rules! impl_conversions { ($type:ty) => { @@ -30,16 +33,12 @@ macro_rules! impl_conversions { }; } pub fn deserialize_message<'a, T>(raw_message: &'a [u8]) -> Result -where - T: Deserialize<'a>, -{ +where T: Deserialize<'a> { serde_cbor::from_slice(raw_message).map_err(Error::SerializeDeserialize) } pub fn serialize_message(input: &T) -> Result, Error> -where - T: Serialize, -{ +where T: Serialize { serde_cbor::to_vec(input).map_err(Error::SerializeDeserialize) } diff --git a/src/server/p2p/network.rs b/src/server/p2p/network.rs index d731185..91394f6 100644 --- a/src/server/p2p/network.rs +++ b/src/server/p2p/network.rs @@ -1,11 +1,24 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use std::{ + collections::HashMap, + fmt::Display, + hash::{DefaultHasher, Hash, Hasher}, + path::PathBuf, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, + time::Duration, +}; + use convert_case::{Case, Casing}; -use hickory_resolver::config::{ResolverConfig, ResolverOpts}; -use hickory_resolver::TokioAsyncResolver; +use hickory_resolver::{ + config::{ResolverConfig, ResolverOpts}, + TokioAsyncResolver, +}; use itertools::Itertools; -use libp2p::swarm::behaviour::toggle::Toggle; use libp2p::{ dcutr, futures::StreamExt, @@ -18,23 +31,26 @@ use libp2p::{ mdns, mdns::tokio::Tokio, multiaddr::Protocol, - noise, relay, request_response, + noise, + relay, + request_response, request_response::{cbor, ResponseChannel}, - swarm::{NetworkBehaviour, SwarmEvent}, - tcp, yamux, Multiaddr, PeerId, StreamProtocol, Swarm, + swarm::{behaviour::toggle::Toggle, NetworkBehaviour, SwarmEvent}, + tcp, + yamux, + Multiaddr, + PeerId, + StreamProtocol, + Swarm, }; -use log::kv::{ToValue, Value}; -use log::{debug, error, info, warn}; -use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use std::fmt::Display; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::{ - hash::{DefaultHasher, Hash, Hasher}, - path::PathBuf, - sync::Arc, - time::Duration, +use log::{ + debug, + error, + info, + kv::{ToValue, Value}, + warn, }; +use serde::{Deserialize, Serialize}; use tari_common::configuration::Network; use tari_core::proof_of_work::PowAlgorithm; use tari_shutdown::ShutdownSignal; @@ -48,19 +64,22 @@ use tokio::{ time, }; -use crate::server::p2p::messages::LocalShareChainSyncRequest; -use crate::sharechain::block::CURRENT_CHAIN_ID; use crate::{ server::{ config, p2p::{ messages, - messages::{PeerInfo, ShareChainSyncRequest, ShareChainSyncResponse}, + messages::{LocalShareChainSyncRequest, PeerInfo, ShareChainSyncRequest, ShareChainSyncResponse}, peer_store::PeerStore, - Error, LibP2PError, ServiceClient, + Error, + LibP2PError, + ServiceClient, }, }, - sharechain::{block::Block, ShareChain}, + sharechain::{ + block::{Block, CURRENT_CHAIN_ID}, + ShareChain, + }, }; const PEER_INFO_TOPIC: &str = "peer_info"; @@ -144,8 +163,7 @@ pub struct ServerNetworkBehaviour { /// Service is the implementation that holds every peer-to-peer related logic /// that makes sure that all the communications, syncing, broadcasting etc... are done. pub struct Service -where - S: ShareChain, +where S: ShareChain { swarm: Swarm, port: u16, @@ -166,8 +184,7 @@ where } impl Service -where - S: ShareChain, +where S: ShareChain { /// Constructs a new Service from the provided config. /// It also instantiates libp2p swarm inside. @@ -494,7 +511,8 @@ where } }, // TODO: send a signature that proves that the actual block was coming from this peer - // TODO: (sender peer's wallet address should be included always in the conibases with a fixed percent (like 20%)) + // TODO: (sender peer's wallet address should be included always in the conibases with a fixed percent (like + // 20%)) topic if topic == Self::tribe_topic(&self.config.tribe, NEW_BLOCK_TOPIC) => { if self.sync_in_progress.load(Ordering::SeqCst) { return; diff --git a/src/server/p2p/peer_store.rs b/src/server/p2p/peer_store.rs index 3d87106..c8030a8 100644 --- a/src/server/p2p/peer_store.rs +++ b/src/server/p2p/peer_store.rs @@ -1,19 +1,19 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use itertools::Itertools; -use libp2p::PeerId; -use log::{debug, warn}; -use moka::future::{Cache, CacheBuilder}; use std::{ sync::RwLock, time::{Duration, Instant}, }; + +use itertools::Itertools; +use libp2p::PeerId; +use log::{debug, warn}; +use moka::future::{Cache, CacheBuilder}; use tari_core::proof_of_work::PowAlgorithm; use tari_utilities::epoch_time::EpochTime; -use crate::server::p2p::messages::PeerInfo; -use crate::server::p2p::Tribe; +use crate::server::p2p::{messages::PeerInfo, Tribe}; const LOG_TARGET: &str = "p2pool::server::p2p::peer_store"; const PEER_BAN_TIME: Duration = Duration::from_secs(60 * 5); diff --git a/src/server/p2p/util.rs b/src/server/p2p/util.rs index b3ec67f..e641c2f 100644 --- a/src/server/p2p/util.rs +++ b/src/server/p2p/util.rs @@ -1,8 +1,7 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use libp2p::identity::Keypair; -use libp2p::PeerId; +use libp2p::{identity::Keypair, PeerId}; use serde::{Deserialize, Serialize}; use tari_utilities::hex::Hex; diff --git a/src/server/server.rs b/src/server/server.rs index fad1bec..d41e176 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -1,29 +1,28 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use log::{error, info}; -use minotari_app_grpc::tari_rpc::{base_node_server::BaseNodeServer, sha_p2_pool_server::ShaP2PoolServer}; -use std::sync::atomic::AtomicBool; use std::{ net::{AddrParseError, SocketAddr}, str::FromStr, - sync::Arc, + sync::{atomic::AtomicBool, Arc}, }; + +use log::{error, info}; +use minotari_app_grpc::tari_rpc::{base_node_server::BaseNodeServer, sha_p2_pool_server::ShaP2PoolServer}; use tari_common::configuration::Network; -use tari_core::consensus::ConsensusManager; -use tari_core::proof_of_work::randomx_factory::RandomXFactory; +use tari_core::{consensus::ConsensusManager, proof_of_work::randomx_factory::RandomXFactory}; use tari_shutdown::ShutdownSignal; use thiserror::Error; -use crate::server::http::server::HttpServer; -use crate::server::http::stats::cache::StatsCache; -use crate::server::p2p::peer_store::PeerStore; -use crate::server::stats_store::StatsStore; use crate::{ server::{ - config, grpc, + config, + grpc, grpc::{base_node::TariBaseNodeGrpc, error::TonicError, p2pool::ShaP2PoolGrpc}, + http::{server::HttpServer, stats::cache::StatsCache}, p2p, + p2p::peer_store::PeerStore, + stats_store::StatsStore, }, sharechain::ShareChain, }; @@ -44,8 +43,7 @@ pub enum Error { /// Server represents the server running all the necessary components for sha-p2pool. pub struct Server -where - S: ShareChain, +where S: ShareChain { config: config::Config, p2p_service: p2p::Service, @@ -56,8 +54,7 @@ where } impl Server -where - S: ShareChain, +where S: ShareChain { pub async fn new( config: config::Config, diff --git a/src/sharechain/block.rs b/src/sharechain/block.rs index b10b66c..0807ae5 100644 --- a/src/sharechain/block.rs +++ b/src/sharechain/block.rs @@ -1,21 +1,19 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use crate::impl_conversions; -use crate::sharechain::CHAIN_ID; use blake2::Blake2b; use digest::consts::U32; use lazy_static::lazy_static; use serde::{Deserialize, Serialize}; use tari_common::configuration::Network; use tari_common_types::{tari_address::TariAddress, types::BlockHash}; -use tari_core::blocks::genesis_block::get_genesis_block; use tari_core::{ - blocks::{BlockHeader, BlocksHashDomain}, + blocks::{genesis_block::get_genesis_block, BlockHeader, BlocksHashDomain}, consensus::DomainSeparatedConsensusHasher, }; -use tari_utilities::epoch_time::EpochTime; -use tari_utilities::hex::Hex; +use tari_utilities::{epoch_time::EpochTime, hex::Hex}; + +use crate::{impl_conversions, sharechain::CHAIN_ID}; lazy_static! { pub static ref CURRENT_CHAIN_ID: String = { diff --git a/src/sharechain/error.rs b/src/sharechain/error.rs index 5ea2310..2352ca5 100644 --- a/src/sharechain/error.rs +++ b/src/sharechain/error.rs @@ -3,11 +3,11 @@ use std::num::TryFromIntError; -use tari_common_types::tari_address::TariAddressError; -use tari_common_types::types::FixedHashSizeError; -use tari_core::consensus::ConsensusBuilderError; -use tari_core::proof_of_work::monero_rx::MergeMineError; -use tari_core::proof_of_work::DifficultyError; +use tari_common_types::{tari_address::TariAddressError, types::FixedHashSizeError}; +use tari_core::{ + consensus::ConsensusBuilderError, + proof_of_work::{monero_rx::MergeMineError, DifficultyError}, +}; use thiserror::Error; use crate::sharechain::block::Block; diff --git a/src/sharechain/in_memory.rs b/src/sharechain/in_memory.rs index 0f674cf..31479d9 100644 --- a/src/sharechain/in_memory.rs +++ b/src/sharechain/in_memory.rs @@ -1,29 +1,44 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use std::ops::{Add, Div}; -use std::slice::Iter; -use std::str::FromStr; -use std::{collections::HashMap, sync::Arc}; - -use crate::server::grpc::p2pool::min_difficulty; -use crate::sharechain::{ - error::{BlockConvertError, Error}, - Block, BlockValidationParams, ShareChain, ShareChainResult, SubmitBlockResult, ValidateBlockResult, BLOCKS_WINDOW, - MAX_BLOCKS_COUNT, MAX_SHARES_PER_MINER, SHARE_COUNT, +use std::{ + collections::HashMap, + ops::{Add, Div}, + slice::Iter, + str::FromStr, + sync::Arc, }; + use async_trait::async_trait; use itertools::Itertools; use log::{debug, error, info, warn}; use minotari_app_grpc::tari_rpc::{NewBlockCoinbase, SubmitBlockRequest}; use num::{BigUint, FromPrimitive, Integer, Zero}; -use tari_common_types::tari_address::TariAddress; -use tari_common_types::types::BlockHash; -use tari_core::blocks; -use tari_core::proof_of_work::{randomx_difficulty, sha3x_difficulty, Difficulty, PowAlgorithm}; +use tari_common_types::{tari_address::TariAddress, types::BlockHash}; +use tari_core::{ + blocks, + proof_of_work::{randomx_difficulty, sha3x_difficulty, Difficulty, PowAlgorithm}, +}; use tari_utilities::{epoch_time::EpochTime, hex::Hex}; use tokio::sync::{RwLock, RwLockWriteGuard}; +use crate::{ + server::grpc::p2pool::min_difficulty, + sharechain::{ + error::{BlockConvertError, Error}, + Block, + BlockValidationParams, + ShareChain, + ShareChainResult, + SubmitBlockResult, + ValidateBlockResult, + BLOCKS_WINDOW, + MAX_BLOCKS_COUNT, + MAX_SHARES_PER_MINER, + SHARE_COUNT, + }, +}; + const LOG_TARGET: &str = "p2pool::sharechain::in_memory"; pub struct InMemoryShareChain { @@ -230,8 +245,8 @@ impl InMemoryShareChain { if let Some(last_block) = last_block { // check if we have outdated tip of chain - let block_height_diff = i64::try_from(block.height()).map_err(Error::FromIntConversion)? - - i64::try_from(last_block.height()).map_err(Error::FromIntConversion)?; + let block_height_diff = i64::try_from(block.height()).map_err(Error::FromIntConversion)? - + i64::try_from(last_block.height()).map_err(Error::FromIntConversion)?; if block_height_diff > 10 { // TODO: use const warn!(target: LOG_TARGET, @@ -333,8 +348,8 @@ impl InMemoryShareChain { .blocks .iter() .filter(|curr_block| curr_block.generate_hash() == block.generate_hash()) - .count() - > 0; + .count() > + 0; if !found { found_level.add_block(block.clone())?; info!(target: LOG_TARGET, "[{:?}] 🆕 New block added at height {:?}: {:?}", self.pow_algo, block.height(), block.hash().to_hex()); @@ -377,10 +392,10 @@ impl ShareChain for InMemoryShareChain { if sync { let chain = self.chain(block_levels_write_lock.iter()); if let Some(last_block) = chain.last() { - if last_block.hash() != genesis_block().hash() - && !blocks.is_empty() - && last_block.height() < blocks[0].height() - && (blocks[0].height() - last_block.height()) > 1 + if last_block.hash() != genesis_block().hash() && + !blocks.is_empty() && + last_block.height() < blocks[0].height() && + (blocks[0].height() - last_block.height()) > 1 { block_levels_write_lock.clear(); } @@ -532,11 +547,11 @@ impl ShareChain for InMemoryShareChain { #[cfg(test)] mod test { - use super::*; use tari_common::configuration::Network; use tari_common_types::tari_address::TariAddressFeatures; - use tari_crypto::keys::PublicKey; - use tari_crypto::ristretto::RistrettoPublicKey; + use tari_crypto::{keys::PublicKey, ristretto::RistrettoPublicKey}; + + use super::*; fn new_random_address() -> TariAddress { let mut rng = rand::thread_rng(); diff --git a/src/sharechain/mod.rs b/src/sharechain/mod.rs index 0061af0..eb7f7b4 100644 --- a/src/sharechain/mod.rs +++ b/src/sharechain/mod.rs @@ -1,14 +1,15 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause -use crate::sharechain::{block::Block, error::Error}; +use std::collections::HashMap; + use async_trait::async_trait; use minotari_app_grpc::tari_rpc::{NewBlockCoinbase, SubmitBlockRequest}; use num::BigUint; -use std::collections::HashMap; use tari_common_types::types::FixedHash; -use tari_core::consensus::ConsensusManager; -use tari_core::proof_of_work::randomx_factory::RandomXFactory; +use tari_core::{consensus::ConsensusManager, proof_of_work::randomx_factory::RandomXFactory}; + +use crate::sharechain::{block::Block, error::Error}; /// Chain ID is an identifier which makes sure we apply the same rules to blocks. /// Note: This must be updated when new logic applied to blocks handling.