Add logging to peer connection and synchronization processes
This commit is contained in:
@@ -2,6 +2,14 @@
|
|||||||
[](https://github.com/seemueller-io/gsio-net/actions/workflows/main.yml)
|
[](https://github.com/seemueller-io/gsio-net/actions/workflows/main.yml)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
Warning: This API is unstable.
|
||||||
|
|
||||||
|
## run a network
|
||||||
|
```yaml
|
||||||
|
docker compose build
|
||||||
|
docker compose up
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under the MIT License - see the LICENSE file for details.
|
This project is licensed under the MIT License - see the LICENSE file for details.
|
||||||
|
@@ -195,6 +195,7 @@ async fn on_peer_message(
|
|||||||
/// ---- Individual peer-message helpers ----
|
/// ---- Individual peer-message helpers ----
|
||||||
async fn handle_peer_discovered(socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
async fn handle_peer_discovered(socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
||||||
if let Some(peer_id) = data.get("peer_id").and_then(|id| id.as_str()) {
|
if let Some(peer_id) = data.get("peer_id").and_then(|id| id.as_str()) {
|
||||||
|
info!(peer_id = peer_id, "Peer discovered, initiating peering");
|
||||||
p2p.ledger.add_known_node(peer_id.to_owned());
|
p2p.ledger.add_known_node(peer_id.to_owned());
|
||||||
socket
|
socket
|
||||||
.emit(
|
.emit(
|
||||||
@@ -207,10 +208,12 @@ async fn handle_peer_discovered(socket: SocketRef, p2p: Arc<P2PManager>, data: &
|
|||||||
|
|
||||||
async fn handle_advertise(socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
async fn handle_advertise(socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
||||||
if let Some(peer_id) = data.get("peer_id").and_then(|id| id.as_str()) {
|
if let Some(peer_id) = data.get("peer_id").and_then(|id| id.as_str()) {
|
||||||
|
info!(peer_id = peer_id, "Received peer advertisement, establishing connection");
|
||||||
p2p.ledger.add_known_node(peer_id.to_owned());
|
p2p.ledger.add_known_node(peer_id.to_owned());
|
||||||
socket
|
socket
|
||||||
.emit("peer_ack", &json!({ "type": "ack", "peer_id": p2p.node_id() }))
|
.emit("peer_ack", &json!({ "type": "ack", "peer_id": p2p.node_id() }))
|
||||||
.ok();
|
.ok();
|
||||||
|
info!(peer_id = peer_id, "Sent acknowledgment to peer, connection established");
|
||||||
socket
|
socket
|
||||||
.emit(
|
.emit(
|
||||||
"peer_sync_request",
|
"peer_sync_request",
|
||||||
@@ -235,6 +238,10 @@ async fn handle_sync_request(socket: SocketRef, p2p: Arc<P2PManager>, _data: &Js
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_sync_response(_socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
async fn handle_sync_response(_socket: SocketRef, p2p: Arc<P2PManager>, data: &JsonValue) {
|
||||||
|
if let Some(peer_id) = data.get("peer_id").and_then(|id| id.as_str()) {
|
||||||
|
info!(peer_id = peer_id, "Received sync response from peer, peering active");
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(entries_val) = data.get("entries") {
|
if let Some(entries_val) = data.get("entries") {
|
||||||
if let Ok(entries) = serde_json::from_value::<Vec<LedgerEntry>>(entries_val.clone()) {
|
if let Ok(entries) = serde_json::from_value::<Vec<LedgerEntry>>(entries_val.clone()) {
|
||||||
for e in entries {
|
for e in entries {
|
||||||
|
@@ -123,18 +123,19 @@ impl P2PManager {
|
|||||||
|
|
||||||
/// Handle a new connection from another node
|
/// Handle a new connection from another node
|
||||||
pub fn handle_connection(&self, socket: SocketRef, data: JsonValue) {
|
pub fn handle_connection(&self, socket: SocketRef, data: JsonValue) {
|
||||||
info!(ns = socket.ns(), ?socket.id, "P2P node connected");
|
|
||||||
|
|
||||||
// Extract the node ID from the connection data
|
// Extract the node ID from the connection data
|
||||||
let node_id = match data.get("node_id") {
|
let node_id = match data.get("node_id") {
|
||||||
Some(id) => id.as_str().unwrap_or("unknown").to_string(),
|
Some(id) => id.as_str().unwrap_or("unknown").to_string(),
|
||||||
None => "unknown".to_string(),
|
None => "unknown".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
info!(ns = socket.ns(), ?socket.id, node_id = node_id, "P2P node connected, establishing peering");
|
||||||
|
|
||||||
// Add the node to the connected nodes
|
// Add the node to the connected nodes
|
||||||
{
|
{
|
||||||
let mut connected_nodes = self.connected_nodes.lock().unwrap();
|
let mut connected_nodes = self.connected_nodes.lock().unwrap();
|
||||||
connected_nodes.insert(node_id.clone(), socket.clone());
|
connected_nodes.insert(node_id.clone(), socket.clone());
|
||||||
|
info!(peer_id = node_id, "Successfully peered with node");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the node to the known nodes in the ledger
|
// Add the node to the known nodes in the ledger
|
||||||
|
Reference in New Issue
Block a user