Skip to content

Instantly share code, notes, and snippets.

@0xdewy
Created June 17, 2019 08:00
Show Gist options
  • Save 0xdewy/78f07d48e0948378c5add9b3ca663052 to your computer and use it in GitHub Desktop.
Save 0xdewy/78f07d48e0948378c5add9b3ca663052 to your computer and use it in GitHub Desktop.
Upgrading contracts
contract Example {
bytes32 public highestHash; // The current highest hash
// @notice if the sha3() hash of the string is higher than highest hash, replace highestHash with 'a'
function setHighestHash(string memory a)
public {
bytes32 newHash = keccak256(abi.encodePacked(a));
require(isHighest(a), "NOT HIGHEST HASH");
highestHash = newHash;
}
// @dev checks if 'a' higher than the current highest hash
function isHighest(string memory a)
public
view
returns (bool) {
return keccak256(abi.encodePacked(a)) > highestHash;
}
}
// This upgrade adds an array, storing the previous hashes as well
contract Example2 is Example {
// bytes32 public highestHash;
uint256 public nonce;
// @notice adds the nonce variable to the storage hierarchy
function setHighestHash(string memory a)
public {
bytes32 newHash = keccak256(abi.encodePacked(a));
require(isHighest(a), "NOT HIGHEST HASH");
nonce++;
highestHash = newHash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment