Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test_loop] test_loop_sync_actor_maker implementation in TestLoopV2 #11522

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions chain/client/src/test_utils/test_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod partial_witness_actor;
pub mod sync_actor;
pub mod sync_jobs_actor;

use std::borrow::Borrow;

use crate::client_actor::{ClientActorInner, ClientSenderForPartialWitnessMessage};
use near_async::messaging::{CanSend, Handler, SendAsync};
use near_async::test_loop::delay_sender::DelaySender;
Expand Down Expand Up @@ -220,22 +222,22 @@ pub trait ClientQueries {
fn tracked_shards_for_each_client(&self) -> Vec<Vec<ShardId>>;
}

impl<Data: AsRef<Client> + AsRef<AccountId>> ClientQueries for Vec<Data> {
impl<Data> ClientQueries for Vec<Data>
where
Data: Borrow<Client>,
{
fn client_index_tracking_account(&self, account_id: &AccountId) -> usize {
let client: &Client = self[0].as_ref();
let client: &Client = self[0].borrow();
let head = client.chain.head().unwrap();
let shard_id =
client.epoch_manager.account_id_to_shard_id(&account_id, &head.epoch_id).unwrap();

for i in 0..self.len() {
let client: &Client = self[i].as_ref();
let client: &Client = self[i].borrow();
let account_id = client.validator_signer.as_ref().unwrap().validator_id();
let tracks_shard = client
.epoch_manager
.cares_about_shard_from_prev_block(
&head.prev_block_hash,
&self[i].as_ref(),
shard_id,
)
.cares_about_shard_from_prev_block(&head.prev_block_hash, account_id, shard_id)
.unwrap();
if tracks_shard {
return i;
Expand All @@ -246,7 +248,7 @@ impl<Data: AsRef<Client> + AsRef<AccountId>> ClientQueries for Vec<Data> {

fn runtime_query(&self, account_id: &AccountId, query: QueryRequest) -> QueryResponse {
let client_index = self.client_index_tracking_account(account_id);
let client: &Client = self[client_index].as_ref();
let client: &Client = self[client_index].borrow();
let head = client.chain.head().unwrap();
let last_block = client.chain.get_block(&head.last_block_hash).unwrap();
let shard_id =
Expand Down Expand Up @@ -299,27 +301,24 @@ impl<Data: AsRef<Client> + AsRef<AccountId>> ClientQueries for Vec<Data> {

fn tx_outcome(&self, tx_hash: CryptoHash) -> FinalExecutionOutcomeView {
// TODO: this does not work yet with single-shard tracking.
let client: &Client = self[0].as_ref();
let client: &Client = self[0].borrow();
client.chain.get_final_transaction_result(&tx_hash).unwrap()
}

fn tracked_shards_for_each_client(&self) -> Vec<Vec<ShardId>> {
let client: &Client = self[0].as_ref();
let client: &Client = self[0].borrow();
let head = client.chain.head().unwrap();
let all_shard_ids = client.epoch_manager.shard_ids(&head.epoch_id).unwrap();

let mut ret = Vec::new();
for i in 0..self.len() {
let client: &Client = self[i].as_ref();
let client: &Client = self[i].borrow();
let account_id = client.validator_signer.as_ref().unwrap().validator_id();
let mut tracked_shards = Vec::new();
for shard_id in &all_shard_ids {
let tracks_shard = client
.epoch_manager
.cares_about_shard_from_prev_block(
&head.prev_block_hash,
&self[i].as_ref(),
*shard_id,
)
.cares_about_shard_from_prev_block(&head.prev_block_hash, account_id, *shard_id)
.unwrap();
if tracks_shard {
tracked_shards.push(*shard_id);
Expand Down
33 changes: 29 additions & 4 deletions chain/client/src/test_utils/test_loop/sync_actor.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
use crate::sync::adapter::SyncActorHandler;
use crate::sync::sync_actor::SyncActor;
use crate::SyncMessage;
use near_async::messaging::{IntoSender, Sender};
use near_async::test_loop::delay_sender::DelaySender;
use near_async::messaging::{IntoSender, LateBoundSender, Sender};
use near_async::test_loop::data::TestLoopData;
use near_async::test_loop::delay_sender::DelaySender as DelaySenderOld;
use near_async::test_loop::event_handler::LoopEventHandler;
use near_async::test_loop::DelaySender;
use near_network::state_sync::StateSyncResponse;
use near_network::types::PeerManagerMessageRequest;
use near_primitives::shard_layout::ShardUId;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

pub fn test_loop_sync_actor_maker(
sender: DelaySender,
) -> Arc<
dyn Fn(ShardUId, Sender<SyncMessage>, Sender<PeerManagerMessageRequest>) -> SyncActorHandler
+ Send
+ Sync,
> {
Arc::new(move |shard_uid, client_sender, network_sender| {
let sync_actor = SyncActor::new(shard_uid, client_sender, network_sender);
let sync_actor_adapter = LateBoundSender::new();
let sync_actor_adapter_clone = sync_actor_adapter.clone();
let callback = move |data: &mut TestLoopData| {
data.register_actor(sync_actor, Some(sync_actor_adapter));
};
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comment why we can't register actor right now?
I guess this is because we have some dependency tree of actors. In such case, drawing it somewhere could be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment

sender.send(format!("Register SyncActor {:?}", shard_uid), Box::new(callback));
SyncActorHandler {
client_sender: sync_actor_adapter_clone.as_sender(),
network_sender: sync_actor_adapter_clone.as_sender(),
shutdown: Mutex::new(Box::new(move || {})),
}
})
}

pub type TestSyncActors = Arc<Mutex<HashMap<ShardUId, SyncActor>>>;

pub fn test_loop_sync_actor_maker<E>(
sender: DelaySender<E>,
pub fn test_loop_sync_actor_maker_old<E>(
sender: DelaySenderOld<E>,
sync_actors: TestSyncActors,
) -> Arc<
dyn Fn(ShardUId, Sender<SyncMessage>, Sender<PeerManagerMessageRequest>) -> SyncActorHandler
Expand Down
2 changes: 1 addition & 1 deletion chain/client/src/test_utils/test_loop/sync_jobs_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::client_actor::SyncJobsSenderForClientMessage;
use crate::sync_jobs_actor::SyncJobsActor;
use near_async::messaging::Handler;
use near_async::test_loop::event_handler::LoopEventHandler;
use near_async::test_loop::futures::TestLoopDelayedActionRunner;
use near_async::test_loop::futures_old::TestLoopDelayedActionRunner;

pub fn forward_messages_from_client_to_sync_jobs_actor(
mut ctx: TestLoopDelayedActionRunner<SyncJobsActor>,
Expand Down
2 changes: 1 addition & 1 deletion core/async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ once_cell.workspace = true
serde.workspace = true
serde_json.workspace = true
time.workspace = true
tokio.workspace = true
tokio = {workspace = true, features = ["rt", "macros"]}
tracing.workspace = true

near-async-derive.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions core/async/src/examples/actix_component_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::actix_component::{
use crate::futures::FutureSpawnerExt;
use crate::messaging::IntoSender;
use crate::test_loop::event_handler::{capture_events, LoopEventHandler};
use crate::test_loop::futures::{drive_futures, TestLoopDelayedActionEvent, TestLoopTask};
use crate::test_loop::TestLoopBuilder;
use crate::test_loop::futures_old::{drive_futures, TestLoopDelayedActionEvent, TestLoopTask};
use crate::test_loop::test_loop_old::TestLoopBuilder;
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use std::sync::Arc;
use time::Duration;
Expand Down
19 changes: 9 additions & 10 deletions core/async/src/examples/async_component_test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::sync::Arc;

use derive_enum_from_into::{EnumFrom, EnumTryInto};

use crate::messaging::{CanSend, IntoSender, MessageWithCallback};
use crate::test_loop::event_handler::{capture_events, LoopEventHandler};
use crate::test_loop::futures_old::{drive_futures, TestLoopFutureSpawner, TestLoopTask};
use crate::test_loop::test_loop_old::TestLoopBuilder;

use super::async_component::{
InnerComponent, InnerRequest, InnerResponse, OuterComponent, OuterRequest, OuterResponse,
};
use crate::{
messaging::{CanSend, IntoSender, MessageWithCallback},
test_loop::{
event_handler::{capture_events, LoopEventHandler},
futures::{drive_futures, TestLoopFutureSpawner, TestLoopTask},
TestLoopBuilder,
},
};
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use std::sync::Arc;

#[derive(derive_more::AsMut, derive_more::AsRef)]
struct TestData {
Expand Down
13 changes: 4 additions & 9 deletions core/async/src/examples/multi_instance_test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use near_time;

use crate::examples::sum_numbers_test::forward_sum_request;
use crate::messaging::{CanSend, IntoSender};
use crate::test_loop::delay_sender::DelaySender;
use crate::{
examples::sum_numbers_test::forward_sum_request,
messaging::{CanSend, IntoSender},
test_loop::{
event_handler::{capture_events, LoopEventHandler},
TestLoopBuilder,
},
};
use crate::test_loop::event_handler::{capture_events, LoopEventHandler};
use crate::test_loop::test_loop_old::TestLoopBuilder;

use super::sum_numbers::{ReportSumMsg, SumNumbersComponent, SumRequest};

Expand Down
13 changes: 4 additions & 9 deletions core/async/src/examples/sum_numbers_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use near_time;

use crate::{
messaging::{CanSend, IntoSender},
test_loop::{
adhoc::{handle_adhoc_events, AdhocEvent, AdhocEventSender},
event_handler::{capture_events, LoopEventHandler},
TestLoopBuilder,
},
};
use crate::messaging::{CanSend, IntoSender};
use crate::test_loop::adhoc::{handle_adhoc_events, AdhocEvent, AdhocEventSender};
use crate::test_loop::event_handler::{capture_events, LoopEventHandler};
use crate::test_loop::test_loop_old::TestLoopBuilder;

use super::sum_numbers::{ReportSumMsg, SumNumbersComponent, SumRequest};

Expand Down
Loading
Loading