Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Add basic tracing of core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly committed Dec 5, 2024
1 parent 49b3948 commit 4a4ccda
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 176 deletions.
2 changes: 2 additions & 0 deletions ouroboros-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ rand = "0.8.5"
netsim = { git = "https://github.com/input-output-hk/ce-netsim", branch = "main" }
# depends on ouroboros-simulation
ouroboros-simulation = { path = "../ouroboros-simulation" }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["std", "json"] }

[build-dependencies]
cbindgen = "0.26.0"
Expand Down
4 changes: 2 additions & 2 deletions ouroboros-consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void broadcast(struct NetworkHandle *network,
* If the buffer is too small, the function returns the required buffer size.
*/
uintptr_t get_preferred_chain(struct NetworkHandle *network,
const char *node_id,
uint64_t node_id,
uint8_t *buf,
uintptr_t len);

Expand All @@ -66,7 +66,7 @@ struct NetworkHandle *start_network(const char *topology,
* Creates and starts a new Consensus node
*
*/
struct ConsensusNode *start_node(const char *node_id,
struct ConsensusNode *start_node(uint64_t node_id,
uint64_t node_stake,
uint64_t total_stake);

Expand Down
16 changes: 13 additions & 3 deletions ouroboros-consensus/src/bin/ouroboros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ use std::{thread, time::Duration};

use ouroboros_consensus::{message::Message, network::Network};
use ouroboros_simulation::{network::random_topology, parameters::Parameters};
use tracing_subscriber::{fmt::format::FmtSpan, prelude::*, Registry};

pub fn main() {
let subscriber = Registry::default();
let json_log = tracing_subscriber::fmt::layer()
.with_span_events(FmtSpan::CLOSE)
.with_target(false)
.with_level(false)
.json();
let subscriber = subscriber.with(json_log);

tracing::subscriber::set_global_default(subscriber).expect("Unable to set global subscriber");

let parameters = Parameters::default();
let topology = random_topology(&mut rand::thread_rng(), &parameters);
let network = Network::new(&topology, &parameters);
Expand All @@ -15,9 +26,8 @@ pub fn main() {

handle.stop();

for i in 1..10 {
let node_id = format!("N{}", i);
let chain = handle.get_preferred_chain(&node_id);
for node_id in 1..10 {
let chain = handle.get_preferred_chain(node_id);
println!(
"{{\"{}\": {}}}",
node_id,
Expand Down
3 changes: 1 addition & 2 deletions ouroboros-consensus/src/ffi/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ pub unsafe extern "C" fn broadcast(network: &mut NetworkHandle, buf: *const u8,
#[no_mangle]
pub unsafe extern "C" fn get_preferred_chain(
network: &mut NetworkHandle,
node_id: *const c_char,
node_id: u64,
buf: *mut u8,
len: usize,
) -> usize {
let node_id = CStr::from_ptr(node_id).to_str().unwrap().into();
let chain = network.get_preferred_chain(node_id);
let chain_bytes = serde_json::to_vec(&chain).unwrap();
let size = chain_bytes.len();
Expand Down
9 changes: 2 additions & 7 deletions ouroboros-consensus/src/ffi/node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
cmp,
ffi::{c_char, CStr},
slice,
};
use std::{cmp, slice};

use crate::ouroboros_node::{Node, NodeHandle, NodeParameters};

Expand All @@ -15,11 +11,10 @@ pub struct ConsensusNode {
///
#[no_mangle]
pub unsafe extern "C" fn start_node(
node_id: *const c_char,
node_id: u64,
node_stake: u64,
total_stake: u64,
) -> Box<ConsensusNode> {
let node_id = CStr::from_ptr(node_id).to_str().unwrap().into();
let node: Node = Node::new(
node_id,
NodeParameters {
Expand Down
2 changes: 1 addition & 1 deletion ouroboros-consensus/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::block::{Block, BlockBody, Slot};
use crate::chain::{Chain, Vote};
use crate::crypto::Hash;

pub type NodeId = String;
pub type NodeId = u64;

#[derive(Debug, Clone, Deserialize, Serialize, Hash)]
#[serde(tag = "tag", content = "contents")]
Expand Down
29 changes: 8 additions & 21 deletions ouroboros-consensus/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,14 @@ impl Network {
.connections
.iter()
.map(|(nodeId, _)| {
let iface = make_node(
&nodeId.nodeId,
&mut context,
parameters,
&protocol,
&mut seed,
);
(nodeId.clone(), iface)
let iface = make_node(*nodeId, &mut context, parameters, &protocol, &mut seed);
(*nodeId, iface)
})
.collect();

let chains = nodes
.keys()
.map(|node_id| (node_id.clone(), empty_chain()))
.map(|node_id| (*node_id, empty_chain()))
.collect();

Network {
Expand Down Expand Up @@ -195,15 +189,9 @@ impl NetworkHandle {
}
}

pub fn get_preferred_chain(&self, node_id: &str) -> Chain {
pub fn get_preferred_chain(&self, node_id: u64) -> Chain {
let network = self.network.lock().unwrap();
network
.chains
.get(&NodeId {
nodeId: node_id.to_string(),
})
.unwrap()
.clone()
network.chains.get(&node_id).unwrap().clone()
}
}

Expand Down Expand Up @@ -268,16 +256,15 @@ fn receive_and_forward_loop(
.expect("send failed");
}
OutEnvelope::Stopped(node_id) => {
tx.send(Control::Stopped(NodeId { nodeId: node_id }))
.expect("send failed");
tx.send(Control::Stopped(node_id)).expect("send failed");
}
}
}
}
}

fn make_node(
node_id: &String,
node_id: NodeId,
context: &mut Ctx,
parameters: &Parameters,
protocol: &ProtocolParameters,
Expand Down Expand Up @@ -305,7 +292,7 @@ fn make_node(
protocol_parameters: protocol.clone(),
};

let node = Node::new(node_id.to_string(), node_params);
let node = Node::new(node_id, node_params);

NodeInterface {
node,
Expand Down
Loading

0 comments on commit 4a4ccda

Please sign in to comment.