Commit 61014db
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
1 file changed
+78
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
0 commit comments