1

In my QueryResultHandler.sol file, I have three functions: one for storing data (requesting two IDs as integers and two string values) and two for validation (either by providing the integers or the string values). The QueryResultHandler.js:

pragma solidity ^0.4.4;

contract QueryResultHandler {

   struct QueryResultStruct {
       bytes32 finalHash;
       bytes32 queryHash;
       bytes32 resultHash;
       uint256 queryId;
       uint256 resultId;
   }

   QueryResultStruct[] queryResultStructArray;

   function storeQueryResults(uint256 _queryId, uint256 _resultId, string _queryString, string _resultString) external returns (bool success) {
      bytes32 queryHash = keccak256(_queryString);
      bytes32 resultHash = keccak256(_resultString);
      bytes32 hashThem = keccak256(queryHash, resultHash);
      queryResultStructArray.push(QueryResultStruct(hashThem, queryHash, resultHash, _queryId, _resultId));
      return true;
    }

   function validateQueryResultsById(uint256 _queryId, uint256 _resultId) external view returns (bool success) {
      for (uint i = 0; i < queryResultStructArray.length; i++){
          if (queryResultStructArray[i].queryId == _queryId && queryResultStructArray[i].resultId == _resultId) {
              return true;
          }
       }
    return false;
    }

   function validateQueryResultsByString(string _queryString, string _resultString) external view returns (bool success) {
      bytes32 queryHash = keccak256(_queryString);
      bytes32 resultHash = keccak256(_resultString);

      bytes32 hashThem = keccak256(queryHash, resultHash);

      for (uint i = 0; i < queryResultStructArray.length; i++) {
          if (queryResultStructArray[i].finalHash == hashThem) {
              return true;
          }
      }
      return false;
   }
}

I am using Truffle and I wanted to write a very simple unit test, which stores some data and then validates it. The store function returns true, however, the validation fails afterward. Can somebody please help me, what can be the issue?

'use strict';

var QueryResultHandler = artifacts.require("./QueryResultHandler.sol");
contract('QueryResultHandler', function() {

    it("should store 1, 21, a, b and then verify by the ID/string", async () => {
        let query = await QueryResultHandler.deployed();    
        query.storeQueryResults.call(1,21,"a","b").then(function(stored){
            console.log(stored);
        });
       query.validateQueryResultsByString.call("a","b").then(function(valid){
           console.log(valid);
       });   
    });
});
0

The problem is that when you are using query.storeQueryResults.call(1,21,"a","b") to call the function. You're not really changing the contract state; this is how you call read (pure/constant/view) functions

What you want to do is change your truffle test from

query.storeQueryResults.call(1,21,"a","b").then(function(stored){
    console.log(stored);
});

To

query.storeQueryResults(1,21,"a","b").then(function(stored){
    console.log(stored);
});

Notice there's no .call() part now.

The second part of your test is calling a view function so that doesn't need to change.

  • Thanks a lot, it really does work now and I've learnt something new :) and I couldn't have figured it out by myself in a million years. – Antona Mar 28 '18 at 19:48

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.