Skip to content

Commit 64bf350

Browse files
committed
chore: add support for instantiating owned RPCHandlerArgs in TestPeer that don't need to cross thread boundaries
1 parent da2ab6b commit 64bf350

File tree

1 file changed

+90
-7
lines changed

1 file changed

+90
-7
lines changed

stackslib/src/net/mod.rs

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ impl PeerHostExtensions for PeerHost {
619619
}
620620

621621
/// Runtime arguments to an RPC handler
622-
#[derive(Default)]
622+
#[derive(Default, Clone)]
623623
pub struct RPCHandlerArgs<'a> {
624624
/// What height at which this node will terminate (testnet only)
625625
pub exit_at_block_height: Option<u64>,
@@ -2222,7 +2222,7 @@ pub mod test {
22222222
use std::net::*;
22232223
use std::ops::{Deref, DerefMut};
22242224
use std::sync::mpsc::sync_channel;
2225-
use std::sync::Mutex;
2225+
use std::sync::{Arc, Mutex};
22262226
use std::{fs, io, thread};
22272227

22282228
use clarity::boot_util::boot_code_id;
@@ -2277,6 +2277,9 @@ pub mod test {
22772277
use crate::chainstate::*;
22782278
use crate::clarity::vm::clarity::TransactionConnection;
22792279
use crate::core::{EpochList, StacksEpoch, StacksEpochExtension, NETWORK_P2P_PORT};
2280+
use crate::cost_estimates::metrics::UnitMetric;
2281+
use crate::cost_estimates::tests::fee_rate_fuzzer::ConstantFeeEstimator;
2282+
use crate::cost_estimates::UnitEstimator;
22802283
use crate::net::asn::*;
22812284
use crate::net::atlas::*;
22822285
use crate::net::chat::*;
@@ -2287,7 +2290,7 @@ pub mod test {
22872290
use crate::net::p2p::*;
22882291
use crate::net::poll::*;
22892292
use crate::net::relay::*;
2290-
use crate::net::Error as net_error;
2293+
use crate::net::{Error as net_error, ProtocolFamily, TipRequest};
22912294
use crate::util_lib::boot::boot_code_test_addr;
22922295
use crate::util_lib::strings::*;
22932296

@@ -2552,6 +2555,75 @@ pub mod test {
25522555
}
25532556
}
25542557

2558+
const DEFAULT_RPC_HANDLER_ARGS: RPCHandlerArgs<'static> = RPCHandlerArgs {
2559+
exit_at_block_height: None,
2560+
genesis_chainstate_hash: Sha256Sum([0x00; 32]),
2561+
event_observer: None,
2562+
cost_estimator: None,
2563+
fee_estimator: None,
2564+
cost_metric: None,
2565+
coord_comms: None,
2566+
};
2567+
2568+
const NULL_COST_ESTIMATOR: () = ();
2569+
const NULL_FEE_ESTIMATOR: () = ();
2570+
const NULL_COST_METRIC: UnitMetric = UnitMetric {};
2571+
const NULL_RPC_HANDLER_ARGS: RPCHandlerArgs<'static> = RPCHandlerArgs {
2572+
exit_at_block_height: None,
2573+
genesis_chainstate_hash: Sha256Sum([0x00; 32]),
2574+
event_observer: None,
2575+
cost_estimator: Some(&NULL_COST_ESTIMATOR),
2576+
fee_estimator: Some(&NULL_FEE_ESTIMATOR),
2577+
cost_metric: Some(&NULL_COST_METRIC),
2578+
coord_comms: None,
2579+
};
2580+
2581+
const UNIT_COST_ESTIMATOR: UnitEstimator = UnitEstimator {};
2582+
const CONSTANT_FEE_ESTIMATOR: ConstantFeeEstimator = ConstantFeeEstimator {};
2583+
const UNIT_COST_METRIC: UnitMetric = UnitMetric {};
2584+
const UNIT_RPC_HANDLER_ARGS: RPCHandlerArgs<'static> = RPCHandlerArgs {
2585+
exit_at_block_height: None,
2586+
genesis_chainstate_hash: Sha256Sum([0x00; 32]),
2587+
event_observer: None,
2588+
cost_estimator: Some(&UNIT_COST_ESTIMATOR),
2589+
fee_estimator: Some(&CONSTANT_FEE_ESTIMATOR),
2590+
cost_metric: Some(&UNIT_COST_METRIC),
2591+
coord_comms: None,
2592+
};
2593+
2594+
/// Templates for RPC Handler Args (which must be owned by the TestPeer, and cannot be a bare
2595+
/// RPCHandlerArgs since references to the inner members cannot be made thread-safe).
2596+
#[derive(Clone, Debug, PartialEq)]
2597+
pub enum RPCHandlerArgsType {
2598+
Default,
2599+
Null,
2600+
Unit,
2601+
}
2602+
2603+
impl RPCHandlerArgsType {
2604+
pub fn instantiate(&self) -> RPCHandlerArgs<'static> {
2605+
match self {
2606+
Self::Default => {
2607+
debug!("Default RPC Handler Args");
2608+
DEFAULT_RPC_HANDLER_ARGS.clone()
2609+
}
2610+
Self::Null => {
2611+
debug!("Null RPC Handler Args");
2612+
NULL_RPC_HANDLER_ARGS.clone()
2613+
}
2614+
Self::Unit => {
2615+
debug!("Unit RPC Handler Args");
2616+
UNIT_RPC_HANDLER_ARGS.clone()
2617+
}
2618+
}
2619+
}
2620+
2621+
pub fn make_default() -> RPCHandlerArgs<'static> {
2622+
debug!("Default RPC Handler Args");
2623+
DEFAULT_RPC_HANDLER_ARGS.clone()
2624+
}
2625+
}
2626+
25552627
// describes a peer's initial configuration
25562628
#[derive(Debug, Clone)]
25572629
pub struct TestPeerConfig {
@@ -2771,6 +2843,8 @@ pub mod test {
27712843
/// tenure-start block of tenure to mine on.
27722844
/// gets consumed on the call to begin_nakamoto_tenure
27732845
pub nakamoto_parent_tenure_opt: Option<Vec<NakamotoBlock>>,
2846+
/// RPC handler args to use
2847+
pub rpc_handler_args: Option<RPCHandlerArgsType>,
27742848
}
27752849

27762850
impl<'a> TestPeer<'a> {
@@ -3190,6 +3264,7 @@ pub mod test {
31903264
malleablized_blocks: vec![],
31913265
mine_malleablized_blocks: true,
31923266
nakamoto_parent_tenure_opt: None,
3267+
rpc_handler_args: None,
31933268
}
31943269
}
31953270

@@ -3301,6 +3376,11 @@ pub mod test {
33013376
let mut stacks_node = self.stacks_node.take().unwrap();
33023377
let mut mempool = self.mempool.take().unwrap();
33033378
let indexer = self.indexer.take().unwrap();
3379+
let rpc_handler_args = self
3380+
.rpc_handler_args
3381+
.as_ref()
3382+
.map(|args_type| args_type.instantiate())
3383+
.unwrap_or(RPCHandlerArgsType::make_default());
33043384

33053385
let old_tip = self.network.stacks_tip.clone();
33063386

@@ -3313,14 +3393,13 @@ pub mod test {
33133393
false,
33143394
ibd,
33153395
100,
3316-
&RPCHandlerArgs::default(),
3396+
&rpc_handler_args,
33173397
);
33183398

33193399
self.sortdb = Some(sortdb);
33203400
self.stacks_node = Some(stacks_node);
33213401
self.mempool = Some(mempool);
33223402
self.indexer = Some(indexer);
3323-
33243403
ret
33253404
}
33263405

@@ -3381,7 +3460,11 @@ pub mod test {
33813460
burn_tip_height,
33823461
);
33833462
let indexer = BitcoinIndexer::new_unit_test(&self.config.burnchain.working_dir);
3384-
3463+
let rpc_handler_args = self
3464+
.rpc_handler_args
3465+
.as_ref()
3466+
.map(|args_type| args_type.instantiate())
3467+
.unwrap_or(RPCHandlerArgsType::make_default());
33853468
let old_tip = self.network.stacks_tip.clone();
33863469

33873470
let ret = self.network.run(
@@ -3393,7 +3476,7 @@ pub mod test {
33933476
false,
33943477
ibd,
33953478
100,
3396-
&RPCHandlerArgs::default(),
3479+
&rpc_handler_args,
33973480
);
33983481

33993482
self.sortdb = Some(sortdb);

0 commit comments

Comments
 (0)