How can I know the whole entityData of the struct and add a new entity only if it didn't exist before (ensure no replicated data)?

struct EntityStruct {
   uint entityData; 
   bool isEntity; 
   bool iscontent; 
   uint vote; 
   uint predata; 

mapping(address => EntityStruct) public entityStructs; address[] public entityList;

function newEntity(address entityAddress, uint entityDataa) public payable returns() {

entityStructs[entityAddress].entityData = entityDataa; 
entityStructs[entityAddress].isEntity = true; 
entityList.push(entityAddress) - 1;


1 Answer 1


One way you could do it is to hash the contents of the struct and use that for the uniqueness check. Then you have a choice. It is conceivable that the hash is the index to the structs but that may be unworkable if the data is subject to change and belongs to an address. In that case, it's a separate mapping.

mapping(bytes32 => bool) public exists;


function generateHash(
   uint entityData,
   bool isEntity, 
   bool iscontent, 
   uint vote,
   uint predata
   returns(bytes32 hash)
   return keccak256(abi.encodePacked(entityData, isEntity, iscontent, vote, predata);

It's up to you to maintain the structure and enforce the rules, so your insert function could do something like:

bytes32 index = generateHash( args ...);
require(!exists[index], "Duplicate");
exists[index] = true;

// carry on

You could roll that up into modifiers or simple functions if it's repetitive:

Hope it helps.


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.