I wrote this piece of code with the goal to store unique documents hash. Used mapping to accomplished that.
pragma solidity ^0.5.16;
contract DocumentHash{
string public documentHash;
mapping (string => string) hashAlreadyRegistered;
function setDocumentHash(string memory hash) public {
require(keccak256( abi.encodePacked((""))) != keccak256(abi.encodePacked((hash))), "Empty hash!");
require(keccak256( abi.encodePacked((hashAlreadyRegistered[documentHash]))) != keccak256(abi.encodePacked((hash))), "Hash already registered");
documentHash = hash;
hashAlreadyRegistered[documentHash] = hash;
}
function getDocumentHash() public view returns(string memory) {
return documentHash;
}
}
When i start a local server (npm start) with geth connection and send a hash to this contract, it works fine. When i try to send same hash, it active the expected behavior (require condition). But if i for example, restart the local server and geth connection, i'm able to store the same hash that i stored already. Wont the hash's be unique? or my code fails somewhere?