Skip to content

Commit 8597abc

Browse files
committed
sim-rs: add id to RB events
1 parent 42c5363 commit 8597abc

File tree

6 files changed

+94
-56
lines changed

6 files changed

+94
-56
lines changed

data/simulation/trace.rust.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface RustCpuEvent extends Omit<RustBaseEvent, "message"> {
4343

4444
// Block Events
4545
interface RustBaseBlockEvent {
46-
id?: string;
46+
id: string;
4747
slot: number;
4848
producer: string;
4949
sender?: string;

data/simulation/trace.rust.schema.json

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,39 @@
7474
}
7575
]
7676
},
77-
"time": {
77+
"time_s": {
7878
"type": "number"
7979
}
8080
},
8181
"type": "object"
8282
},
8383
"RustBlockMessageType": {
8484
"enum": [
85-
"EndorserBlockGenerated",
86-
"EndorserBlockLotteryWon",
87-
"EndorserBlockReceived",
88-
"EndorserBlockSent",
89-
"InputBlockGenerated",
90-
"InputBlockLotteryWon",
91-
"InputBlockReceived",
92-
"InputBlockSent",
93-
"PraosBlockGenerated",
94-
"PraosBlockLotteryWon",
95-
"PraosBlockReceived",
96-
"PraosBlockSent"
85+
"EBGenerated",
86+
"EBLotteryWon",
87+
"EBReceived",
88+
"EBSent",
89+
"IBGenerated",
90+
"IBLotteryWon",
91+
"IBReceived",
92+
"IBSent",
93+
"RBGenerated",
94+
"RBLotteryWon",
95+
"RBReceived",
96+
"RBSent"
9797
],
9898
"type": "string"
9999
},
100100
"RustCpuEvent": {
101101
"properties": {
102102
"message": {
103103
"properties": {
104+
"cpu_time_s": {
105+
"type": "number"
106+
},
107+
"duration_s": {
108+
"type": "number"
109+
},
104110
"extra": {
105111
"type": "string"
106112
},
@@ -122,7 +128,7 @@
122128
},
123129
"type": "object"
124130
},
125-
"time": {
131+
"time_s": {
126132
"type": "number"
127133
}
128134
},
@@ -141,16 +147,16 @@
141147
},
142148
"RustCpuTaskType": {
143149
"enum": [
144-
"EndorserBlockGenerated",
145-
"EndorserBlockValidated",
146-
"InputBlockGenerated",
147-
"InputBlockValidated",
148-
"PraosBlockGenerated",
149-
"PraosBlockValidated",
150+
"EBBlockGenerated",
151+
"EBBlockValidated",
152+
"IBBlockGenerated",
153+
"IBBlockValidated",
154+
"RBBlockGenerated",
155+
"RBBlockValidated",
150156
"TransactionGenerated",
151157
"TransactionValidated",
152-
"VoteBundleGenerated",
153-
"VoteBundleValidated"
158+
"VTBundleGenerated",
159+
"VTBundleValidated"
154160
],
155161
"type": "string"
156162
},
@@ -168,7 +174,7 @@
168174
},
169175
"type": "object"
170176
},
171-
"time": {
177+
"time_s": {
172178
"type": "number"
173179
}
174180
},
@@ -210,7 +216,7 @@
210216
},
211217
"type": "object"
212218
},
213-
"time": {
219+
"time_s": {
214220
"type": "number"
215221
}
216222
},
@@ -252,7 +258,7 @@
252258
},
253259
"type": "object"
254260
},
255-
"time": {
261+
"time_s": {
256262
"type": "number"
257263
}
258264
},

sim-rs/sim-cli/src/events.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sim_core::{
1212
clock::Timestamp,
1313
config::{NodeId, SimConfiguration},
1414
events::{Event, Node},
15-
model::TransactionId,
15+
model::{BlockId, TransactionId},
1616
};
1717
use tokio::{
1818
fs::{self, File},
@@ -181,8 +181,7 @@ impl EventMonitor {
181181
}
182182
Event::RBLotteryWon { .. } => {}
183183
Event::RBGenerated {
184-
slot,
185-
producer,
184+
id: BlockId { slot, producer },
186185
vrf,
187186
endorsement,
188187
transactions,

sim-rs/sim-core/src/events.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::{
88
clock::{Clock, Timestamp},
99
config::{NodeConfiguration, NodeId},
1010
model::{
11-
Block, CpuTaskId, Endorsement, EndorserBlockId, InputBlockId, NoVoteReason, Transaction,
12-
TransactionId, VoteBundle, VoteBundleId,
11+
Block, BlockId, CpuTaskId, Endorsement, EndorserBlockId, InputBlockId, NoVoteReason,
12+
Transaction, TransactionId, VoteBundle, VoteBundleId,
1313
},
1414
};
1515

@@ -93,24 +93,26 @@ pub enum Event {
9393
recipient: Node,
9494
},
9595
RBLotteryWon {
96-
slot: u64,
97-
producer: Node,
96+
#[serde(flatten)]
97+
id: BlockId<Node>,
9898
},
9999
RBGenerated {
100-
slot: u64,
101-
producer: Node,
100+
#[serde(flatten)]
101+
id: BlockId<Node>,
102102
vrf: u64,
103103
header_bytes: u64,
104104
endorsement: Option<Endorsement<Node>>,
105105
transactions: Vec<TransactionId>,
106106
},
107107
RBSent {
108-
slot: u64,
108+
#[serde(flatten)]
109+
id: BlockId<Node>,
109110
sender: Node,
110111
recipient: Node,
111112
},
112113
RBReceived {
113-
slot: u64,
114+
#[serde(flatten)]
115+
id: BlockId<Node>,
114116
sender: Node,
115117
recipient: Node,
116118
},
@@ -266,15 +268,13 @@ impl EventTracker {
266268

267269
pub fn track_praos_block_lottery_won(&self, block: &Block) {
268270
self.send(Event::RBLotteryWon {
269-
slot: block.slot,
270-
producer: self.to_node(block.producer),
271+
id: self.to_block(block.id),
271272
});
272273
}
273274

274275
pub fn track_praos_block_generated(&self, block: &Block) {
275276
self.send(Event::RBGenerated {
276-
slot: block.slot,
277-
producer: self.to_node(block.producer),
277+
id: self.to_block(block.id),
278278
vrf: block.vrf,
279279
header_bytes: block.header_bytes,
280280
endorsement: block.endorsement.as_ref().map(|e| Endorsement {
@@ -292,15 +292,15 @@ impl EventTracker {
292292

293293
pub fn track_praos_block_sent(&self, block: &Block, sender: NodeId, recipient: NodeId) {
294294
self.send(Event::RBSent {
295-
slot: block.slot,
295+
id: self.to_block(block.id),
296296
sender: self.to_node(sender),
297297
recipient: self.to_node(recipient),
298298
});
299299
}
300300

301301
pub fn track_praos_block_received(&self, block: &Block, sender: NodeId, recipient: NodeId) {
302302
self.send(Event::RBReceived {
303-
slot: block.slot,
303+
id: self.to_block(block.id),
304304
sender: self.to_node(sender),
305305
recipient: self.to_node(recipient),
306306
});
@@ -458,6 +458,13 @@ impl EventTracker {
458458
}
459459
}
460460

461+
fn to_block(&self, id: BlockId) -> BlockId<Node> {
462+
BlockId {
463+
slot: id.slot,
464+
producer: self.to_node(id.producer),
465+
}
466+
}
467+
461468
fn to_input_block(&self, id: InputBlockId) -> InputBlockId<Node> {
462469
InputBlockId {
463470
slot: id.slot,

sim-rs/sim-core/src/model.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,34 @@ impl<Node: Display> Display for CpuTaskId<Node> {
4141
}
4242
}
4343

44+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
45+
pub struct BlockId<Node = NodeId> {
46+
pub slot: u64,
47+
pub producer: Node,
48+
}
49+
50+
impl<Node: Display> Display for BlockId<Node> {
51+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52+
f.write_fmt(format_args!("{}-{}", self.slot, self.producer))
53+
}
54+
}
55+
56+
impl<Node: Display + Serialize> Serialize for BlockId<Node> {
57+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58+
where
59+
S: serde::Serializer,
60+
{
61+
let mut obj = serializer.serialize_struct("BlockId", 3)?;
62+
obj.serialize_field("id", &self.to_string())?;
63+
obj.serialize_field("slot", &self.slot)?;
64+
obj.serialize_field("producer", &self.producer)?;
65+
obj.end()
66+
}
67+
}
68+
4469
#[derive(Clone, Debug)]
4570
pub struct Block {
46-
pub slot: u64,
47-
pub producer: NodeId,
71+
pub id: BlockId,
4872
pub vrf: u64,
4973
pub header_bytes: u64,
5074
pub endorsement: Option<Endorsement>,

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
},
2323
events::EventTracker,
2424
model::{
25-
Block, CpuTaskId, Endorsement, EndorserBlock, EndorserBlockId, InputBlock,
25+
Block, BlockId, CpuTaskId, Endorsement, EndorserBlock, EndorserBlockId, InputBlock,
2626
InputBlockHeader, InputBlockId, NoVoteReason, Transaction, TransactionId, VoteBundle,
2727
VoteBundleId,
2828
},
@@ -692,8 +692,10 @@ impl Node {
692692
}
693693

694694
let block = Block {
695-
slot,
696-
producer: self.id,
695+
id: BlockId {
696+
slot,
697+
producer: self.id,
698+
},
697699
vrf,
698700
header_bytes: self.sim_config.sizes.block_header,
699701
endorsement,
@@ -773,13 +775,13 @@ impl Node {
773775
.praos
774776
.peer_heads
775777
.get(peer)
776-
.is_none_or(|&s| s < block.slot)
778+
.is_none_or(|&s| s < block.id.slot)
777779
{
778-
self.send_to(*peer, SimulationMessage::RollForward(block.slot))?;
779-
self.praos.peer_heads.insert(*peer, block.slot);
780+
self.send_to(*peer, SimulationMessage::RollForward(block.id.slot))?;
781+
self.praos.peer_heads.insert(*peer, block.id.slot);
780782
}
781783
}
782-
self.praos.blocks.insert(block.slot, block);
784+
self.praos.blocks.insert(block.id.slot, block);
783785
Ok(())
784786
}
785787

@@ -858,18 +860,18 @@ impl Node {
858860
}
859861

860862
fn finish_validating_block(&mut self, from: NodeId, block: Arc<Block>) -> Result<()> {
861-
if let Some(old_block) = self.praos.blocks.get(&block.slot) {
863+
if let Some(old_block) = self.praos.blocks.get(&block.id.slot) {
862864
// SLOT BATTLE!!! lower VRF wins
863865
if old_block.vrf <= block.vrf {
864866
// We like our block better than this new one.
865867
return Ok(());
866868
}
867869
}
868-
self.praos.blocks.insert(block.slot, block.clone());
870+
self.praos.blocks.insert(block.id.slot, block.clone());
869871

870872
let head = self.praos.peer_heads.entry(from).or_default();
871-
if *head < block.slot {
872-
*head = block.slot
873+
if *head < block.id.slot {
874+
*head = block.id.slot
873875
}
874876
self.publish_block(block)?;
875877
Ok(())

0 commit comments

Comments
 (0)