contract test{

  mapping(uint8 => bytes32) public check;
  uint8 [] public array;

  function Adddata(string document, uint8 number)  returns (bytes32) {
    check[number] = sha256(document);
    return check[number]; 


  function Getdata(uint8 number) returns(bytes32){
    return check[number];

so this is a simple test contract, I suppose to call Adddata to with a number a string, it maps an uint8 to bytes32(sha256 would convert string to bytes)

So if I call Adddata function, it will return me the sha256 of the stirng, but when I call Getdata afterwards, it returns me 0x000000000 whatever what input I gave...

Anyone know what happened?

  • module.exports = function(app, contract_address) { console.log('contract running at: ', contract_address); var MyContract = new web3.eth.Contract(abi, contract_address); var word = "abc"; var num = 2; MyContract.methods.Adddata(word,num).call({from: '0x3423a830a195300863ffcb9dcadbae2834396c29'}, function(error, result){ console.log(result); }) MyContract.methods.Getdata(1).call({from: '0x3423a830a195300863ffcb9dcadbae2834396c29'}, function(error, result){ console.log(result); }); // this is my JS code, what should I do to prevent that problem? – Alan waski Dec 14 '17 at 18:21
  • Please edit your post to include the JavaScript, as that is where the problem lies. I believe the issue is MyContract.methods.Adddata(word,num).call. Adddata mutates state, so it needs to be invoked via a transaction. – smarx Dec 14 '17 at 18:38

It works as expected. I think I would focus my bug hunt on the way you are interacting with the contract. A common error is to overlook the need to wait for the sent transactions to be mined. If you thunder ahead and GetData() before the transaction is mined, then you will find the contract state isn't updated as expected. This extra step is something Remix and Truffle tests fo "for free" so it is easy to overlook.

Here is the contract in Remix to show it working as expected. I might at that the uin8 looks like an attempt to reduce cost but it won't have quite as impact as you might expect, other than limiting the range of possible keys, so I would consider going with a regular uint to maximimze capacity.

Hope it helps.

enter image description here

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.