Verified contractExactMIT
ServerRegistry
Address0xd2e7cd9bc0222bfb8908de5675c1634c3b0306a2
- Balance
0 ETH
- Transactions
10
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- No
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,689,201
- Creation transaction
- 0xccdae85b42…ee0d1f5754
ServerRegistry(3).sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/**
* @title ServerRegistry
* @notice Public registry mapping a unique server title to an IP address and
* the wallet authorized to manage that record.
*
* @dev Titles are case-sensitive and whitespace-sensitive. Client software
* should normalize titles before submitting them, for example by using
* lowercase text and rejecting leading or trailing spaces.
*/
contract ServerRegistry {
struct ServerRecord {
string serverTitle;
string serverIP;
address serverOwner;
uint64 version;
uint64 updatedAt;
bool exists;
}
enum ChangeType {
RECORD_ADDED,
IP_UPDATED,
OWNER_CHANGED
}
// Records are keyed internally by keccak256(bytes(serverTitle)).
mapping(bytes32 => ServerRecord) private serverRecords;
event RecordAdded(
bytes32 indexed recordKey,
string serverTitle,
string serverIP,
address indexed serverOwner,
uint64 version,
uint64 updatedAt
);
event RecordUpdated(
bytes32 indexed recordKey,
string serverTitle,
string previousIP,
string newIP,
address indexed serverOwner,
uint64 version,
uint64 updatedAt
);
event RecordOwnershipTransferred(
bytes32 indexed recordKey,
string serverTitle,
address indexed previousOwner,
address indexed newOwner,
uint64 version,
uint64 updatedAt
);
/**
* @notice General event emitted whenever any record changes.
*/
event RegistryChanged(
ChangeType indexed changeType,
bytes32 indexed recordKey,
string serverTitle,
string previousIP,
string newIP,
address indexed changedBy,
address previousOwner,
address newOwner,
uint64 version,
uint64 updatedAt
);
modifier onlyServerOwner(string calldata serverTitle) {
bytes32 recordKey = _recordKey(serverTitle);
ServerRecord storage record = serverRecords[recordKey];
require(record.exists, "Record does not exist");
require(
record.serverOwner == msg.sender,
"Caller is not the server owner"
);
_;
}
/**
* @notice Add a new record. Anyone may create an unused title.
* @dev The caller becomes the initial serverOwner.
*/
function addRecord(
string calldata serverTitle,
string calldata serverIP
) external {
_requireNonEmpty(serverTitle, "Server title cannot be empty");
_requireNonEmpty(serverIP, "Server IP cannot be empty");
bytes32 recordKey = _recordKey(serverTitle);
require(!serverRecords[recordKey].exists, "Record already exists");
uint64 changedAt = uint64(block.timestamp);
serverRecords[recordKey] = ServerRecord({
serverTitle: serverTitle,
serverIP: serverIP,
serverOwner: msg.sender,
version: 1,
updatedAt: changedAt,
exists: true
});
emit RecordAdded(
recordKey,
serverTitle,
serverIP,
msg.sender,
1,
changedAt
);
emit RegistryChanged(
ChangeType.RECORD_ADDED,
recordKey,
serverTitle,
"",
serverIP,
msg.sender,
address(0),
msg.sender,
1,
changedAt
);
}
/**
* @notice Change the IP address of an existing record.
* @dev Only the record's serverOwner may call this function.
*/
function updateRecord(
string calldata serverTitle,
string calldata newServerIP
) external onlyServerOwner(serverTitle) {
_requireNonEmpty(newServerIP, "Server IP cannot be empty");
bytes32 recordKey = _recordKey(serverTitle);
ServerRecord storage record = serverRecords[recordKey];
string memory previousIP = record.serverIP;
require(
keccak256(bytes(previousIP)) != keccak256(bytes(newServerIP)),
"New IP matches current IP"
);
record.serverIP = newServerIP;
record.version += 1;
record.updatedAt = uint64(block.timestamp);
emit RecordUpdated(
recordKey,
record.serverTitle,
previousIP,
newServerIP,
record.serverOwner,
record.version,
record.updatedAt
);
emit RegistryChanged(
ChangeType.IP_UPDATED,
recordKey,
record.serverTitle,
previousIP,
newServerIP,
msg.sender,
record.serverOwner,
record.serverOwner,
record.version,
record.updatedAt
);
}
/**
* @notice Transfer control of a record to another wallet.
* @dev Only the current serverOwner may transfer ownership.
*/
function transferRecordOwnership(
string calldata serverTitle,
address newServerOwner
) external onlyServerOwner(serverTitle) {
require(
newServerOwner != address(0),
"New owner cannot be zero address"
);
require(
newServerOwner != msg.sender,
"New owner is already the server owner"
);
bytes32 recordKey = _recordKey(serverTitle);
ServerRecord storage record = serverRecords[recordKey];
address previousOwner = record.serverOwner;
record.serverOwner = newServerOwner;
record.version += 1;
record.updatedAt = uint64(block.timestamp);
emit RecordOwnershipTransferred(
recordKey,
record.serverTitle,
previousOwner,
newServerOwner,
record.version,
record.updatedAt
);
emit RegistryChanged(
ChangeType.OWNER_CHANGED,
recordKey,
record.serverTitle,
record.serverIP,
record.serverIP,
msg.sender,
previousOwner,
newServerOwner,
record.version,
record.updatedAt
);
}
/**
* @notice Retrieve the complete public data for a record.
*/
function getRecord(
string calldata serverTitle
) external view returns (
string memory serverIP,
address serverOwner,
uint64 version,
uint64 updatedAt
) {
ServerRecord storage record = serverRecords[_recordKey(serverTitle)];
require(record.exists, "Record does not exist");
return (
record.serverIP,
record.serverOwner,
record.version,
record.updatedAt
);
}
/**
* @notice Check whether an exact server title has already been registered.
*/
function hasRecord(
string calldata serverTitle
) external view returns (bool) {
return serverRecords[_recordKey(serverTitle)].exists;
}
/**
* @notice Return the bytes32 key used internally for a server title.
*/
function getRecordKey(
string calldata serverTitle
) external pure returns (bytes32) {
return _recordKey(serverTitle);
}
function _recordKey(
string calldata serverTitle
) internal pure returns (bytes32) {
return keccak256(bytes(serverTitle));
}
function _requireNonEmpty(
string calldata value,
string memory errorMessage
) internal pure {
require(bytes(value).length > 0, errorMessage);
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": false
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.