Skip to content

Commit 0c8a0ab

Browse files
committed
sim-rs: implement random sampling
1 parent 983b32c commit 0c8a0ab

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

data/simulation/config.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export interface Config {
4545
* This is Δhdr from the Leios paper.
4646
* */
4747
"leios-header-diffusion-time-ms": number;
48+
/**
49+
* The strategy to use when selecting TXs from the Leios mempool.
50+
*/
51+
"leios-mempool-sampling-strategy": MempoolSamplingStrategy;
4852
/**
4953
* Praos blockchain quality parameter.
5054
* This is η from the Leios paper.
@@ -248,4 +252,11 @@ export enum LeiosVariant {
248252
Full = "full",
249253
/** Full Leios Without IBs: EBs reference TXs directly, as well as other EBs */
250254
FullWithoutIbs = "full-without-ibs",
251-
}
255+
}
256+
257+
export enum MempoolSamplingStrategy {
258+
/** Include transactions in order by ID (corresponds to generation time). */
259+
OrderedById = "ordered-by-id",
260+
/** Include transactions in random order. */
261+
Random = "random",
262+
}

data/simulation/config.default.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ leios-stage-active-voting-slots: 1
2828
leios-vote-send-recv-stages: false
2929
leios-late-ib-inclusion: true
3030
leios-header-diffusion-time-ms: 1000.0
31+
leios-mempool-sampling-strategy: ordered-by-id
3132
# TODO: revise default
3233
praos-chain-quality: 40
3334
praos-fallback-enabled: true

data/simulation/config.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
},
9292
"type": "object"
9393
},
94+
"MempoolSamplingStrategy": {
95+
"enum": ["ordered-by-id", "random"],
96+
"type": "string"
97+
},
9498
"NormalDistribution": {
9599
"properties": {
96100
"distribution": {
@@ -269,6 +273,10 @@
269273
"description": "Extends Leios so that EB producers include IBs directly from previous pipelines\nwhere no certified EB was observed.\n\nOnly supported by Rust simulation.",
270274
"type": "boolean"
271275
},
276+
"leios-mempool-sampling-strategy": {
277+
"$ref": "#/definitions/MempoolSamplingStrategy",
278+
"description": "The strategy to use when selecting TXs from the Leios mempool."
279+
},
272280
"leios-stage-active-voting-slots": {
273281
"additionalProperties": false,
274282
"properties": {},
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
leios-variant: full
22

3+
# How to choose transactions from the mempool when producing IBs.
4+
# "Random" is meant to ensure that different producers include different TXs.
5+
leios-mempool-sampling-strategy: random
6+
37
# Allow EBs to include IBs from previous slots
48
leios-late-ib-inclusion: true
59

610
# Chain quality controls how far back EB recursion can reach.
7-
praos-chain-quality: 40
11+
praos-chain-quality: 100

sim-rs/parameters/full.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
leios-variant: full
2+
leios-mempool-sampling-strategy: random
23
leios-late-ib-inclusion: true
3-
praos-chain-quality: 40
4+
praos-chain-quality: 100

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct RawParameters {
6262
pub leios_stage_active_voting_slots: u64,
6363
pub leios_late_ib_inclusion: bool,
6464
pub leios_header_diffusion_time_ms: f64,
65+
pub leios_mempool_sampling_strategy: MempoolSamplingStrategy,
6566
pub praos_chain_quality: u64,
6667
pub praos_fallback_enabled: bool,
6768

@@ -149,6 +150,13 @@ pub enum RelayStrategy {
149150
RequestFromFirst,
150151
}
151152

153+
#[derive(Debug, Copy, Clone, Deserialize, PartialEq, Eq)]
154+
#[serde(rename_all = "kebab-case")]
155+
pub enum MempoolSamplingStrategy {
156+
OrderedById,
157+
Random,
158+
}
159+
152160
#[derive(Debug, Serialize, Deserialize)]
153161
#[serde(rename_all = "kebab-case")]
154162
pub struct RawTopology {
@@ -439,6 +447,7 @@ pub struct SimConfiguration {
439447
pub(crate) praos_fallback: bool,
440448
pub(crate) header_diffusion_time: Duration,
441449
pub(crate) relay_strategy: RelayStrategy,
450+
pub(crate) mempool_strategy: MempoolSamplingStrategy,
442451
pub(crate) praos_chain_quality: u64,
443452
pub(crate) block_generation_probability: f64,
444453
pub(crate) ib_generation_probability: f64,
@@ -482,6 +491,7 @@ impl SimConfiguration {
482491
praos_fallback: params.praos_fallback_enabled,
483492
header_diffusion_time: duration_ms(params.leios_header_diffusion_time_ms),
484493
relay_strategy: params.relay_strategy,
494+
mempool_strategy: params.leios_mempool_sampling_strategy,
485495
praos_chain_quality: params.praos_chain_quality,
486496
block_generation_probability: params.rb_generation_probability,
487497
ib_generation_probability: params.ib_generation_probability,

sim-rs/sim-core/src/sim/node.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use std::{
99
use anyhow::Result;
1010
use netsim_async::HasBytesSize as _;
1111
use priority_queue::PriorityQueue;
12-
use rand::Rng as _;
12+
use rand::{seq::SliceRandom as _, Rng as _};
1313
use rand_chacha::ChaChaRng;
1414
use tokio::{select, sync::mpsc};
1515
use tracing::{info, trace};
1616

1717
use crate::{
1818
clock::{ClockBarrier, FutureEvent, Timestamp},
1919
config::{
20-
DiffusionStrategy, LeiosVariant, NodeConfiguration, NodeId, RelayStrategy,
21-
SimConfiguration, TransactionConfig,
20+
DiffusionStrategy, LeiosVariant, MempoolSamplingStrategy, NodeConfiguration, NodeId,
21+
RelayStrategy, SimConfiguration, TransactionConfig,
2222
},
2323
events::EventTracker,
2424
model::{
@@ -1392,7 +1392,7 @@ impl Node {
13921392
where
13931393
C: Fn(&SeenTransaction) -> bool,
13941394
{
1395-
let candidate_txs: Vec<_> = self
1395+
let mut candidate_txs: Vec<_> = self
13961396
.leios
13971397
.mempool
13981398
.values()
@@ -1404,6 +1404,12 @@ impl Node {
14041404
}
14051405
})
14061406
.collect();
1407+
if matches!(
1408+
self.sim_config.mempool_strategy,
1409+
MempoolSamplingStrategy::Random
1410+
) {
1411+
candidate_txs.shuffle(&mut self.rng);
1412+
}
14071413
let mut txs = vec![];
14081414
let mut size = 0;
14091415
for (id, bytes) in candidate_txs {

0 commit comments

Comments
 (0)