Skip to content

Commit 6eb3238

Browse files
committed
sim-rs: respect tx-start-time and tx-stop-time in attacks
1 parent eb2d394 commit 6eb3238

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

sim-rs/sim-core/src/config.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ pub struct RawLateTXAttackConfig {
206206
pub attackers: NodeSelection,
207207
pub attack_probability: f64,
208208
pub tx_generation_distribution: DistributionConfig,
209+
pub tx_start_time: Option<f64>,
210+
pub tx_stop_time: Option<f64>,
209211
}
210212

211213
#[derive(Deserialize)]
@@ -617,7 +619,7 @@ impl AttackConfig {
617619
late_tx: params
618620
.late_tx_attack
619621
.as_ref()
620-
.map(|raw| LateTXAttackConfig::build(raw, topology)),
622+
.map(|raw| LateTXAttackConfig::build(raw, topology, params)),
621623
}
622624
}
623625
}
@@ -645,16 +647,26 @@ impl LateEBAttackConfig {
645647
pub(crate) struct LateTXAttackConfig {
646648
pub(crate) probability: f64,
647649
pub(crate) txs_to_generate: FloatDistribution,
650+
pub(crate) start_time: Option<Timestamp>,
651+
pub(crate) stop_time: Option<Timestamp>,
648652
}
649653

650654
impl LateTXAttackConfig {
651-
fn build(raw: &RawLateTXAttackConfig, topology: &mut Topology) -> Self {
655+
fn build(raw: &RawLateTXAttackConfig, topology: &mut Topology, params: &RawParameters) -> Self {
652656
for attacker in topology.select(&raw.attackers) {
653657
attacker.behaviours.withhold_txs = true;
654658
}
655659
Self {
656660
probability: raw.attack_probability,
657661
txs_to_generate: raw.tx_generation_distribution.into(),
662+
start_time: raw
663+
.tx_start_time
664+
.or(params.tx_start_time)
665+
.map(|t| Timestamp::zero() + Duration::from_secs_f64(t)),
666+
stop_time: raw
667+
.tx_stop_time
668+
.or(params.tx_stop_time)
669+
.map(|t| Timestamp::zero() + Duration::from_secs_f64(t)),
658670
}
659671
}
660672
}

sim-rs/sim-core/src/sim/linear_leios.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl LinearLeiosNode {
571571
if !produce_empty_block {
572572
// If we are performing a "withheld TX" attack, we will include a bunch of brand-new TXs in this EB.
573573
// They will get disseminated through the network at the same time as the EB.
574-
withheld_txs = self.generate_withheld_txs();
574+
withheld_txs = self.generate_withheld_txs(slot);
575575
eb_transactions.extend(withheld_txs.iter().cloned());
576576

577577
if let TransactionConfig::Mock(config) = &self.sim_config.transactions {
@@ -1127,11 +1127,18 @@ impl LinearLeiosNode {
11271127
// transactions, so that they cannot propagate in advance.
11281128
// We implement this by generating the transactions at the same time as the EB itself.
11291129
impl LinearLeiosNode {
1130-
fn generate_withheld_txs(&mut self) -> Vec<Arc<Transaction>> {
1130+
fn generate_withheld_txs(&mut self, slot: u64) -> Vec<Arc<Transaction>> {
11311131
if !self.behaviours.withhold_txs {
11321132
return vec![];
11331133
}
11341134
let withhold_tx_config = self.sim_config.attacks.late_tx.as_ref().unwrap();
1135+
let slot_ts = Timestamp::from_secs(slot);
1136+
if withhold_tx_config.start_time.is_some_and(|s| slot_ts < s) {
1137+
return vec![];
1138+
}
1139+
if withhold_tx_config.stop_time.is_some_and(|s| slot_ts > s) {
1140+
return vec![];
1141+
}
11351142
if !self.rng.random_bool(withhold_tx_config.probability) {
11361143
return vec![];
11371144
}

0 commit comments

Comments
 (0)