I want to write solidity contract which stores hash table as mapping(uint => string) documents data structure in ethereum blockchain.

Now, each time I run a function "storeDocument" in this contract, I store a students' details in mapping as ID => Hash_of_stu_data and run the contract. Likewise, I have to store many students' details in same mapping data structure by creating several transactions. So, it is stored permanantly in blockchain.

So, now there is another function "verifyDocument" in same contract that returns boolean value if given hash is matched on providing ID as key and hash anytime by anyone on a different transaction. So, it has to access same documents mapping data structure, compare against the value and return bool value.

How do I verify a particular student's hash based on key value in table in another transaction ? Since it is permanently stored, what should I do ? This happens only if store all records and verify in the same transaction. This is possible if accessing data from Blockchain that is stored in mapping data structure in same transaction. But, I want this table to be updated with new entries everytime I create a transaction to store a key value pair as we do in database.

I am able to access mapping table data in the same transaction. But I want to store it permanently as we do so in database to verify my hash at any time. You can assume this contract is for checking the integrity of any document.

This is sample code:

pragma solidity  >=0.4.0 <0.6.0;

contract Example {

mapping(uint => string) documents;

function storeDocument(uint id, string docHash)  public
    documents[id] = docHash;

function verifyDocument(uint id, string hashToVerify) view public returns (bool)
    if(keccak256(abi.encodePacked(documents[id])) == keccak256(abi.encodePacked(hashToVerify )) )
        return true;
        return false;

Please help me. Please mention if question isn't asked properly. Thank you.


I think you're getting something wrong in understanding transactions and smart contracts. Below is my explanation.

When you deploy the above smart contract on ethereum, this will create a transaction(say TXN 1) and this "TXN 1" gets added to a block(say BLK 1). You'll also get the address for this smart contract deployed(say 0xabcd)

  1. Next, now you enter your student data i.e., your ID and HASH -> then submit it. This will create a new transaction(say TXN 2), which gets added to a new block(BLK 2)
  2. Next, you enter more student data i.e., your ID and HASH -> then submit it. Every new data creates a new transaction(say TXN 3, TXN 4 ...) and which gets added to new blocks(BLK 3, BLK 4 ...)
  3. So by doing above, you're already creating new transactions. your old transactions TXN 1 and so on will stay on blockchain forever with their data.

So, now when you intend to verify the student by submitting your ID and hashToVerify, you'll have to do this in scope of your smart contract i.e. 0xabcd. Because each smart contract you deploy is kind of new database in itself. It will assign memory for your student data and store there.

But now say, you deploy a new smart contract with the same code. This will create a new TXN 100, which gets added to BLK 100, and you get smart contract address of 0xpqrs. This is kind of new fresh database. You'll not be able to verify the student data that you entered in the scope of smart contract 0xabcd.

Try it:

I deployed your smart contract on testnet ethereum and my smart contract address is 0x1AF3cDb351E65470Cd4FFBF35E5fb8F614d01a75. Goto https://remix.ethereum.org -> In the "deploy and run transations" tab, choose the environment Injected Web 3 and select your account from Metamask -> and load my smart contract in At Address section i.e. paste 0x1AF3cDb351E65470Cd4FFBF35E5fb8F614d01a75 in that field, and press enter.

I created a student data of ID 1 and hash abcd

Then in verifyDocument, type 1, "abcd", you'll be able to see true. Here you'll not be able to verify students whatever you've added in your smart contract address.

  • thank you Sir for your response. One more doubt after reading your answer is when every smart contract is run at some address , all the data stored at that address in different transactions can be loaded later in another transactions as you said. – manivarma indukuri Apr 11 '20 at 15:26
  • thank you Sir for your response. What I understood after reading your answer is when a smart contract is run at some address , all the data stored at that address in different transactions can be loaded later in another transactions. Now, as you said, when I deploy the contract at your address and give (1,'abcd') as input to verifyDocument method, it's returning false Sir. So, as you said, I can store many student details in one smart contract address and use that address later to verify in later transactions anytime. Am I correct ? Thank you Sir. – manivarma indukuri Apr 11 '20 at 15:35
  • Yes you are right. Also, my verification of 1, "abcd" is working properly, see this - ibb.co/nkDyqsF. What steps did you do to verify? – cdadityang Apr 13 '20 at 6:00
  • Sir, I compiled the code and selected Injected Web3 environment, selected one of my acct address, pasted your address 0x1AF3cDb351E65470Cd4FFBF35E5fb8F614d01a75 and run it on clicking it. It showed me to give input , I gave 1,"abcd" for verifyDocument method but it returns false. Please see this Screenshot if I have done anything wrong. please go through the code also Sir . I added memory keyword for string parameters in methods drive.google.com/open?id=1oEE3anvhZr3LRMvZmGBsx0KV9_666gav – manivarma indukuri Apr 13 '20 at 7:08
  • Sir, working fine. Actually, code should be same if we want to load a contract from a address. I was corrected. Thank you so much Sir. – manivarma indukuri Apr 13 '20 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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