Context
In the near future, the city's energy management system is crucial for maintaining the balance of power distribution. Your role is an undercover systems controller for the city's central power grid. Recently, a new piece of technology, the Energy Management System (EMS), was implemented to enhance the city's energy efficiency.
A rogue AI has begun tampering with the EMS, doubling the energy levels in an attempt to overload the system, which would lead to a catastrophic city-wide blackout. Your mission is to trigger the emergency shutdown sequence before the energy level reaches a critical threshold.
Utilize your controller access to send a verified message that will double the energy level. Monitor the energyLevel variable, and once it is near the critical threshold, ensure that the emergency shutdown is initiated to save the city. Good luck, agent.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EnergyManagementSystem {
mapping(address => bool) public isController;
uint public energyLevel;
constructor() {
isController[msg.sender] = true;
energyLevel = 42;
}
modifier onlyController() {
require(isController[msg.sender], "Unauthorized");
_;
}
function verifySignature(bytes32 messageHash, uint8 v, bytes32 r, bytes32 s) public pure returns (address) {
bytes32 prefixedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", messageHash));
address signer = ecrecover(prefixedHash, v, r, s);
return signer;
}
function processMessage(bytes32 messageHash, uint8 v, bytes32 r, bytes32 s) public {
address signer = verifySignature(messageHash, v, r, s);
require(signer != address(0), "Invalid signer");
// Process the message and update the contract state
energyLevel = energyLevel * 2;
// If the energy level doubles beyond a critical threshold, an emergency shutdown is triggered
if (energyLevel >= criticalThreshold()) {
initiateEmergencyShutdown();
}
}
function changeController(address newController) public onlyController {
require(newController != address(0), "Invalid address");
isController[newController] = true;
}
function withdrawFunds(address payable recipient) public onlyController {
require(address(this).balance > 0, "No funds available");
recipient.transfer(address(this).balance);
}
function isControllerAddress(address _address) public view returns (bool) {
return isController[_address];
}
function getEnergyLevel() public view returns (uint) {
return energyLevel;
}
receive() external payable {}
// Private function to represent the emergency shutdown logic
function initiateEmergencyShutdown() private {
// Logic to safely shutdown the energy system and prevent a city-wide blackout
}
// Private function to determine the critical threshold for the emergency shutdown
function criticalThreshold() private pure returns (uint) {
return 1000; // Arbitrary value set for the threshold
}
}