Skip to content

Commit 61014db

Browse files
authored
Create HeartbeatSystem.sol
```solidty // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract HeartbeatSystem { struct NodeStatus { uint256 lastHeartbeat; bool isActive; } // Mapping of node address to their status mapping(address => NodeStatus) public nodeStatus; // List of all registered nodes address[] public nodes; // Heartbeat timeout (in seconds) uint256 public heartbeatTimeout = 5 minutes; // Events event HeartbeatReceived(address indexed node, uint256 timestamp); event NodeRegistered(address indexed node, uint256 timestamp); event NodeBecameInactive(address indexed node, uint256 timestamp); // Register as a node function register() public { if (nodeStatus[msg.sender].lastHeartbeat == 0) { nodes.push(msg.sender); emit NodeRegistered(msg.sender, block.timestamp); } // Register and send first heartbeat _recordHeartbeat(msg.sender); } // Send a heartbeat function heartbeat() public { require(nodeStatus[msg.sender].lastHeartbeat > 0, "Node not registered"); _recordHeartbeat(msg.sender); } // Internal function to record a heartbeat function _recordHeartbeat(address node) private { nodeStatus[node] = NodeStatus({ lastHeartbeat: block.timestamp, isActive: true }); emit HeartbeatReceived(node, block.timestamp); } // Check if nodes are still active function checkNodes() public { for (uint256 i = 0; i < nodes.length; i++) { address node = nodes[i]; NodeStatus storage status = nodeStatus[node]; // If the node was active but hasn't sent a heartbeat recently if (status.isActive && block.timestamp - status.lastHeartbeat > heartbeatTimeout) { status.isActive = false; emit NodeBecameInactive(node, block.timestamp); } } } // Get active node count function getActiveNodeCount() public view returns (uint256 count) { for (uint256 i = 0; i < nodes.length; i++) { if (nodeStatus[nodes[i]].isActive) { count++; } } } // Set the heartbeat timeout function setHeartbeatTimeout(uint256 newTimeout) public { heartbeatTimeout = newTimeout; } } ```
1 parent c040295 commit 61014db

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

App/Claude/HeartbeatSystem.sol

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract HeartbeatSystem {
5+
struct NodeStatus {
6+
uint256 lastHeartbeat;
7+
bool isActive;
8+
}
9+
10+
// Mapping of node address to their status
11+
mapping(address => NodeStatus) public nodeStatus;
12+
13+
// List of all registered nodes
14+
address[] public nodes;
15+
16+
// Heartbeat timeout (in seconds)
17+
uint256 public heartbeatTimeout = 5 minutes;
18+
19+
// Events
20+
event HeartbeatReceived(address indexed node, uint256 timestamp);
21+
event NodeRegistered(address indexed node, uint256 timestamp);
22+
event NodeBecameInactive(address indexed node, uint256 timestamp);
23+
24+
// Register as a node
25+
function register() public {
26+
if (nodeStatus[msg.sender].lastHeartbeat == 0) {
27+
nodes.push(msg.sender);
28+
emit NodeRegistered(msg.sender, block.timestamp);
29+
}
30+
31+
// Register and send first heartbeat
32+
_recordHeartbeat(msg.sender);
33+
}
34+
35+
// Send a heartbeat
36+
function heartbeat() public {
37+
require(nodeStatus[msg.sender].lastHeartbeat > 0, "Node not registered");
38+
_recordHeartbeat(msg.sender);
39+
}
40+
41+
// Internal function to record a heartbeat
42+
function _recordHeartbeat(address node) private {
43+
nodeStatus[node] = NodeStatus({
44+
lastHeartbeat: block.timestamp,
45+
isActive: true
46+
});
47+
48+
emit HeartbeatReceived(node, block.timestamp);
49+
}
50+
51+
// Check if nodes are still active
52+
function checkNodes() public {
53+
for (uint256 i = 0; i < nodes.length; i++) {
54+
address node = nodes[i];
55+
NodeStatus storage status = nodeStatus[node];
56+
57+
// If the node was active but hasn't sent a heartbeat recently
58+
if (status.isActive && block.timestamp - status.lastHeartbeat > heartbeatTimeout) {
59+
status.isActive = false;
60+
emit NodeBecameInactive(node, block.timestamp);
61+
}
62+
}
63+
}
64+
65+
// Get active node count
66+
function getActiveNodeCount() public view returns (uint256 count) {
67+
for (uint256 i = 0; i < nodes.length; i++) {
68+
if (nodeStatus[nodes[i]].isActive) {
69+
count++;
70+
}
71+
}
72+
}
73+
74+
// Set the heartbeat timeout
75+
function setHeartbeatTimeout(uint256 newTimeout) public {
76+
heartbeatTimeout = newTimeout;
77+
}
78+
}

0 commit comments

Comments
 (0)