Skip to content

Commit

Permalink
moved Network(View)Client(Messages/Responses) to near_client (#7908)
Browse files Browse the repository at this point in the history
These actix messages are an implementation detail of near_client crate.
  • Loading branch information
pompon0 authored and nikurt committed Nov 7, 2022
1 parent 01d2fde commit 390f52b
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 178 deletions.
109 changes: 107 additions & 2 deletions chain/client/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,125 @@ use crate::client_actor::ClientActor;
use crate::view_client::ViewClientActor;
use near_network::time;
use near_network::types::{
NetworkClientMessages, NetworkClientResponses, NetworkInfo, NetworkViewClientMessages,
NetworkViewClientResponses, PartialEncodedChunkForwardMsg, PartialEncodedChunkRequestMsg,
NetworkInfo, PartialEncodedChunkForwardMsg, PartialEncodedChunkRequestMsg,
PartialEncodedChunkResponseMsg, ReasonForBan, StateResponseInfo,
};
use near_o11y::WithSpanContextExt;
use near_primitives::block::{Approval, Block, BlockHeader};
use near_primitives::challenge::Challenge;
use near_primitives::errors::InvalidTxError;
use near_primitives::hash::CryptoHash;
use near_primitives::network::{AnnounceAccount, PeerId};
use near_primitives::sharding::PartialEncodedChunk;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, EpochId, ShardId};
use near_primitives::views::FinalExecutionOutcomeView;

#[derive(actix::Message, Debug, strum::AsRefStr, strum::IntoStaticStr)]
// TODO(#1313): Use Box
#[allow(clippy::large_enum_variant)]
#[rtype(result = "NetworkClientResponses")]
pub enum NetworkClientMessages {
#[cfg(feature = "test_features")]
Adversarial(near_network::types::NetworkAdversarialMessage),

/// Received transaction.
Transaction {
transaction: SignedTransaction,
/// Whether the transaction is forwarded from other nodes.
is_forwarded: bool,
/// Whether the transaction needs to be submitted.
check_only: bool,
},
/// Received block, possibly requested.
Block(Block, PeerId, bool),
/// Received list of headers for syncing.
BlockHeaders(Vec<BlockHeader>, PeerId),
/// Block approval.
BlockApproval(Approval, PeerId),
/// State response.
StateResponse(StateResponseInfo),

/// Request chunk parts and/or receipts.
PartialEncodedChunkRequest(PartialEncodedChunkRequestMsg, CryptoHash),
/// Response to a request for chunk parts and/or receipts.
PartialEncodedChunkResponse(PartialEncodedChunkResponseMsg, std::time::Instant),
/// Information about chunk such as its header, some subset of parts and/or incoming receipts
PartialEncodedChunk(PartialEncodedChunk),
/// Forwarding parts to those tracking the shard (so they don't need to send requests)
PartialEncodedChunkForward(PartialEncodedChunkForwardMsg),

/// A challenge to invalidate the block.
Challenge(Challenge),

NetworkInfo(NetworkInfo),
}

// TODO(#1313): Use Box
#[derive(Eq, PartialEq, Debug, actix::MessageResponse)]
#[allow(clippy::large_enum_variant)]
pub enum NetworkClientResponses {
/// Adv controls.
#[cfg(feature = "test_features")]
AdvResult(u64),

/// No response.
NoResponse,
/// Valid transaction inserted into mempool as response to Transaction.
ValidTx,
/// Invalid transaction inserted into mempool as response to Transaction.
InvalidTx(InvalidTxError),
/// The request is routed to other shards
RequestRouted,
/// The node being queried does not track the shard needed and therefore cannot provide userful
/// response.
DoesNotTrackShard,
/// Ban peer for malicious behavior.
Ban { ban_reason: ReasonForBan },
}

#[derive(actix::Message, strum::IntoStaticStr)]
#[rtype(result = "NetworkViewClientResponses")]
pub enum NetworkViewClientMessages {
#[cfg(feature = "test_features")]
Adversarial(near_network::types::NetworkAdversarialMessage),

/// Transaction status query
TxStatus { tx_hash: CryptoHash, signer_account_id: AccountId },
/// Transaction status response
TxStatusResponse(Box<FinalExecutionOutcomeView>),
/// Request a block.
BlockRequest(CryptoHash),
/// Request headers.
BlockHeadersRequest(Vec<CryptoHash>),
/// State request header.
StateRequestHeader { shard_id: ShardId, sync_hash: CryptoHash },
/// State request part.
StateRequestPart { shard_id: ShardId, sync_hash: CryptoHash, part_id: u64 },
/// Account announcements that needs to be validated before being processed.
/// They are paired with last epoch id known to this announcement, in order to accept only
/// newer announcements.
AnnounceAccount(Vec<(AnnounceAccount, Option<EpochId>)>),
}

#[derive(Debug, actix::MessageResponse)]
pub enum NetworkViewClientResponses {
/// Transaction execution outcome
TxStatus(Box<FinalExecutionOutcomeView>),
/// Block response.
Block(Box<Block>),
/// Headers response.
BlockHeaders(Vec<BlockHeader>),
/// Response to state request.
StateResponse(Box<StateResponseInfo>),
/// Valid announce accounts.
AnnounceAccount(Vec<AnnounceAccount>),
/// Ban peer for malicious behavior.
Ban { ban_reason: ReasonForBan },
/// Response not needed
NoResponse,
}

pub struct Adapter {
/// Address of the client actor.
client_addr: actix::Addr<ClientActor>,
Expand Down
5 changes: 2 additions & 3 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use near_client_primitives::debug::ChunkProduction;
use near_primitives::time::Clock;
use tracing::{debug, error, info, trace, warn};

use crate::adapter::NetworkClientResponses;
use near_chain::chain::{
ApplyStatePartsRequest, BlockCatchUpRequest, BlockMissingChunks, BlocksCatchUpState,
OrphanMissingChunks, StateSplitRequest, TX_ROUTING_HEIGHT_HORIZON,
Expand All @@ -26,9 +27,7 @@ use near_chain::{
};
use near_chain_configs::ClientConfig;
use near_chunks::ShardsManager;
use near_network::types::{
FullPeerInfo, NetworkClientResponses, NetworkRequests, PeerManagerAdapter,
};
use near_network::types::{FullPeerInfo, NetworkRequests, PeerManagerAdapter};
use near_primitives::block::{Approval, ApprovalInner, ApprovalMessage, Block, BlockHeader, Tip};
use near_primitives::challenge::{Challenge, ChallengeBody};
use near_primitives::hash::CryptoHash;
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Client actor orchestrates Client and facilitates network connection.

use crate::adapter::{NetworkClientMessages, NetworkClientResponses};
use crate::client::{Client, EPOCH_START_INFO_BLOCKS};
use crate::info::{
display_sync_status, get_validator_epoch_stats, InfoHelper, ValidatorInfoHelper,
Expand Down Expand Up @@ -32,8 +33,7 @@ use near_client_primitives::types::{
};
use near_network::types::ReasonForBan;
use near_network::types::{
NetworkClientMessages, NetworkClientResponses, NetworkInfo, NetworkRequests,
PeerManagerAdapter, PeerManagerMessageRequest,
NetworkInfo, NetworkRequests, PeerManagerAdapter, PeerManagerMessageRequest,
};
use near_o11y::{handler_debug_span, OpenTelemetrySpanExt, WithSpanContext, WithSpanContextExt};
use near_performance_metrics;
Expand Down
16 changes: 9 additions & 7 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ use once_cell::sync::OnceCell;
use rand::{thread_rng, Rng};
use tracing::info;

use crate::adapter::{
NetworkClientMessages, NetworkClientResponses, NetworkViewClientMessages,
NetworkViewClientResponses,
};
use crate::{start_view_client, Client, ClientActor, SyncStatus, ViewClientActor};
use near_chain::chain::{do_apply_chunks, BlockCatchUpRequest, StateSplitRequest};
use near_chain::test_utils::{
wait_for_all_blocks_in_processing, wait_for_block_in_processing, KeyValueRuntime,
Expand All @@ -29,13 +34,12 @@ use near_crypto::{InMemorySigner, KeyType, PublicKey};
use near_network::test_utils::MockPeerManagerAdapter;
use near_network::types::PartialEdgeInfo;
use near_network::types::{
AccountOrPeerIdOrHash, NetworkViewClientMessages, NetworkViewClientResponses,
PartialEncodedChunkRequestMsg, PartialEncodedChunkResponseMsg, PeerChainInfoV2, PeerInfo,
PeerType,
AccountOrPeerIdOrHash, PartialEncodedChunkRequestMsg, PartialEncodedChunkResponseMsg,
PeerChainInfoV2, PeerInfo, PeerType,
};
use near_network::types::{
ConnectedPeerInfo, FullPeerInfo, NetworkClientMessages, NetworkClientResponses,
NetworkRecipient, NetworkRequests, NetworkResponses, PeerManagerAdapter,
ConnectedPeerInfo, FullPeerInfo, NetworkRecipient, NetworkRequests, NetworkResponses,
PeerManagerAdapter,
};
use near_network::types::{
NetworkInfo, PeerManagerMessageRequest, PeerManagerMessageResponse, SetChainInfo,
Expand Down Expand Up @@ -67,8 +71,6 @@ use near_store::test_utils::create_test_store;
use near_store::Store;
use near_telemetry::TelemetryActor;

use crate::{start_view_client, Client, ClientActor, SyncStatus, ViewClientActor};

pub struct PeerManagerMock {
handle: Box<
dyn FnMut(
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/tests/bug_repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use actix::{Addr, System};
use futures::FutureExt;
use rand::{thread_rng, Rng};

use crate::adapter::NetworkClientMessages;
use crate::test_utils::setup_mock_all_validators;
use crate::{ClientActor, GetBlock, ViewClientActor};
use near_actix_test_utils::run_actix;
Expand All @@ -17,8 +18,7 @@ use near_crypto::{InMemorySigner, KeyType};
use near_network::types::NetworkRequests::PartialEncodedChunkMessage;
use near_network::types::PeerInfo;
use near_network::types::{
NetworkClientMessages, NetworkRequests, NetworkResponses, PeerManagerMessageRequest,
PeerManagerMessageResponse,
NetworkRequests, NetworkResponses, PeerManagerMessageRequest, PeerManagerMessageResponse,
};
use near_o11y::testonly::init_test_logger;
use near_o11y::WithSpanContextExt;
Expand Down
5 changes: 2 additions & 3 deletions chain/client/src/tests/catching_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use actix::{Addr, System};
use borsh::{BorshDeserialize, BorshSerialize};
use futures::{future, FutureExt};

use crate::adapter::NetworkClientMessages;
use crate::test_utils::setup_mock_all_validators;
use crate::{ClientActor, Query, ViewClientActor};
use near_actix_test_utils::run_actix;
use near_chain::test_utils::{account_id_to_shard_id, ValidatorSchedule};
use near_chain_configs::TEST_STATE_SYNC_TIMEOUT;
use near_crypto::{InMemorySigner, KeyType};
use near_network::types::{AccountIdOrPeerTrackingShard, AccountOrPeerIdOrHash, PeerInfo};
use near_network::types::{
NetworkClientMessages, NetworkRequests, NetworkResponses, PeerManagerMessageRequest,
};
use near_network::types::{NetworkRequests, NetworkResponses, PeerManagerMessageRequest};
use near_o11y::testonly::init_integration_logger;
use near_o11y::WithSpanContextExt;
use near_primitives::hash::{hash as hash_func, CryptoHash};
Expand Down
5 changes: 2 additions & 3 deletions chain/client/src/tests/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use actix::{Addr, System};
use near_chain::test_utils::ValidatorSchedule;
use rand::{thread_rng, Rng};

use crate::adapter::NetworkClientMessages;
use crate::test_utils::setup_mock_all_validators;
use crate::{ClientActor, ViewClientActor};
use near_actix_test_utils::run_actix;
use near_chain::Block;
use near_network::types::PeerInfo;
use near_network::types::{
NetworkClientMessages, NetworkRequests, NetworkResponses, PeerManagerMessageRequest,
};
use near_network::types::{NetworkRequests, NetworkResponses, PeerManagerMessageRequest};
use near_o11y::testonly::init_integration_logger;
use near_o11y::WithSpanContextExt;
use near_primitives::block::{Approval, ApprovalInner};
Expand Down
4 changes: 2 additions & 2 deletions chain/client/src/tests/cross_shard_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use std::sync::{Arc, RwLock};
use actix::{Addr, MailboxError, System};
use futures::{future, FutureExt};

use crate::adapter::{NetworkClientMessages, NetworkClientResponses};
use near_actix_test_utils::run_actix;
use near_chain::test_utils::{account_id_to_shard_id, ValidatorSchedule};
use near_crypto::{InMemorySigner, KeyType};
use near_network::types::PeerInfo;
use near_network::types::{
NetworkClientMessages, NetworkClientResponses, NetworkResponses, PeerManagerMessageRequest,
PeerManagerMessageResponse,
NetworkResponses, PeerManagerMessageRequest, PeerManagerMessageResponse,
};
use near_o11y::testonly::init_integration_logger;
use near_o11y::WithSpanContextExt;
Expand Down
9 changes: 6 additions & 3 deletions chain/client/src/tests/query_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use near_primitives::merkle::PartialMerkleTree;
use std::sync::Arc;
use std::time::Duration;

use crate::adapter::{
NetworkClientMessages, NetworkClientResponses, NetworkViewClientMessages,
NetworkViewClientResponses,
};
use crate::test_utils::{setup_mock_all_validators, setup_no_network, setup_only_view};
use crate::{
GetBlock, GetBlockWithMerkleTree, GetExecutionOutcomesForBlock, Query, QueryError, Status,
Expand All @@ -14,11 +18,10 @@ use near_actix_test_utils::run_actix;
use near_chain_configs::DEFAULT_GC_NUM_EPOCHS_TO_KEEP;
use near_crypto::{InMemorySigner, KeyType};
use near_network::test_utils::MockPeerManagerAdapter;
use near_network::types::PeerInfo;
use near_network::types::{
NetworkClientMessages, NetworkClientResponses, NetworkRequests, NetworkResponses,
PeerManagerMessageRequest, PeerManagerMessageResponse,
NetworkRequests, NetworkResponses, PeerManagerMessageRequest, PeerManagerMessageResponse,
};
use near_network::types::{NetworkViewClientMessages, NetworkViewClientResponses, PeerInfo};

use near_o11y::testonly::init_test_logger;
use near_o11y::WithSpanContextExt;
Expand Down
6 changes: 3 additions & 3 deletions chain/client/src/view_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::time::{Duration, Instant};

use tracing::{debug, error, info, trace, warn};

use crate::adapter::{NetworkViewClientMessages, NetworkViewClientResponses};
use near_chain::{
get_epoch_block_producers_view, Chain, ChainGenesis, ChainStoreAccess, DoomslugThresholdMode,
RuntimeAdapter,
Expand All @@ -28,9 +29,8 @@ use near_client_primitives::types::{
#[cfg(feature = "test_features")]
use near_network::types::NetworkAdversarialMessage;
use near_network::types::{
NetworkRequests, NetworkViewClientMessages, NetworkViewClientResponses, PeerManagerAdapter,
PeerManagerMessageRequest, ReasonForBan, StateResponseInfo, StateResponseInfoV1,
StateResponseInfoV2,
NetworkRequests, PeerManagerAdapter, PeerManagerMessageRequest, ReasonForBan,
StateResponseInfo, StateResponseInfoV1, StateResponseInfoV2,
};
use near_o11y::{handler_debug_span, OpenTelemetrySpanExt, WithSpanContext, WithSpanContextExt};
use near_performance_metrics_macros::perf;
Expand Down
12 changes: 6 additions & 6 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::time::{sleep, timeout};
use tracing::info;

use near_chain_configs::GenesisConfig;
use near_client::adapter::{NetworkClientMessages, NetworkClientResponses};
use near_client::{
ClientActor, DebugStatus, GetBlock, GetBlockProof, GetChunk, GetExecutionOutcome, GetGasPrice,
GetNetworkInfo, GetNextLightClientBlock, GetProtocolConfig, GetReceipt, GetStateChanges,
Expand All @@ -27,7 +28,6 @@ pub use near_jsonrpc_client as client;
use near_jsonrpc_primitives::errors::RpcError;
use near_jsonrpc_primitives::message::{Message, Request};
use near_jsonrpc_primitives::types::config::RpcProtocolConfigResponse;
use near_network::types::{NetworkClientMessages, NetworkClientResponses};
use near_o11y::metrics::{prometheus, Encoder, TextEncoder};
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::SignedTransaction;
Expand Down Expand Up @@ -1147,7 +1147,7 @@ impl JsonRpcHandler {
actix::spawn(
self.client_addr
.send(
near_network::types::NetworkClientMessages::Adversarial(
near_client::adapter::NetworkClientMessages::Adversarial(
near_network::types::NetworkAdversarialMessage::AdvSetSyncInfo(height),
)
.with_span_context(),
Expand All @@ -1161,7 +1161,7 @@ impl JsonRpcHandler {
actix::spawn(
self.client_addr
.send(
near_network::types::NetworkClientMessages::Adversarial(
near_client::adapter::NetworkClientMessages::Adversarial(
near_network::types::NetworkAdversarialMessage::AdvDisableHeaderSync,
)
.with_span_context(),
Expand All @@ -1171,7 +1171,7 @@ impl JsonRpcHandler {
actix::spawn(
self.view_client_addr
.send(
near_network::types::NetworkViewClientMessages::Adversarial(
near_client::adapter::NetworkViewClientMessages::Adversarial(
near_network::types::NetworkAdversarialMessage::AdvDisableHeaderSync,
)
.with_span_context(),
Expand All @@ -1195,7 +1195,7 @@ impl JsonRpcHandler {
actix::spawn(
self.view_client_addr
.send(
near_network::types::NetworkViewClientMessages::Adversarial(
near_client::adapter::NetworkViewClientMessages::Adversarial(
near_network::types::NetworkAdversarialMessage::AdvDisableDoomslug,
)
.with_span_context(),
Expand Down Expand Up @@ -1237,7 +1237,7 @@ impl JsonRpcHandler {
actix::spawn(
self.view_client_addr
.send(
near_network::types::NetworkViewClientMessages::Adversarial(
near_client::adapter::NetworkViewClientMessages::Adversarial(
near_network::types::NetworkAdversarialMessage::AdvSwitchToHeight(height),
)
.with_span_context(),
Expand Down
Loading

0 comments on commit 390f52b

Please sign in to comment.