In this Proof of existence, integrity, and ownership contract of a file why files[fileHash].timestamp == 0 is used? why timestamp?

contract Proof

   struct FileDetails
       uint timestamp;
       string owner;

   mapping (string => FileDetails) files;
   event logFileAddedStatus(bool status, uint timestamp, string owner, string fileHash);

   //this is used to store the owner of file at the block timestamp
   function set(string owner, string fileHash)


       //There is no proper way to check if a key already exists or not therefore we are checking for default value i.e., all bits are 0
       if(files[fileHash].timestamp == 0)

           files[fileHash] = FileDetails(block.timestamp, owner);

           //we are triggering an event so that the frontend of our app knows that the file's existence and ownership details have been stored
           logFileAddedStatus(true, block.timestamp, owner, fileHash);
        {//this tells to the frontend that file's existence and ownership details couldn't be stored because the file's details had already been stored earlier
           logFileAddedStatus(false, block.timestamp, owner, fileHash);
        }           } 

   //this is used to get file information
   function get(string fileHash) returns (uint timestamp, string owner)
       return (files[fileHash].timestamp, files[fileHash].owner);


1 Answer 1


There's no way to check directly if something exists in a mapping. In solidity, everything is set to it's "default value" until it's changed.

That means every integer starts as 0, every string starts a "", every array starts as []. Solidity has no concept of "null" like other languages.

A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false. The default value for the uint or int types is 0. For statically-sized arrays and bytes1 to bytes32, each individual element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, bytes and string, the default value is an empty array or string.

So if you have a mapping to a struct, then every possible key points to the "default struct". In your case that will be a struct with a timestamp of 0 and a owner of "".

Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value.

So if you want to check if the mapping hasn't been set, you can either check if files[fileHash].timestamp == 0 or check files[fileHash].owner.length == 0.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.