0

I am using testrpc to test my contract While executing some functions in my contract I have observed some inconstancy in responses from testrpc, Same function call give me 3 different results at different times.

1.Function executed properly and returned tx.Hash and received event callback successfully. 2.Function executed properly and returned tx.Hash But dont received event callback. 3.Transaction failed to execute with error showing invalid JSON rpc. NOTE:- I haven't restarted or stopped testrpc during these three calls and it was happening pretty often.

Here is the contract code

pragma solidity ^ 0.4.8;

contract AssetManagement {
    struct Asset {
    uint assetId;
    string assetCode;
    string assetName;
    string serviceTag;
}

    mapping (uint=>Asset) assetList;
    event onAddNewAsset(uint assetId,string assetCode,string assetName,string serviceTag);
    event onAssetAssignment(uint assetId,string assetName,uint userId,string userName);
    event onAssetReturn(uint assetId,string assetName,uint userId);
    event onGetAssignemnt(uint[] assetIds);

    // User User Id to array of Asset Id
    mapping(uint => uint[]) userAssets;
    //array of uint for assetIds
    uint[] assetsArray;
    //object of struct
    Asset asset;
    string assetNameToLog;


    function createNewAsset(uint assetId,string assetCode,string assetName,string serviceTag) public{
       asset.assetId=assetId;
       asset.assetCode=assetCode;
       asset.assetName=assetName;
       asset.serviceTag=serviceTag;
       assetList[assetId]=asset;
       onAddNewAsset(assetId,assetCode,assetName,serviceTag);
    }

    function assignAsset(uint userId, uint assetId, string userName)public {
        //add reference to userAssetRefeneces array
        assetsArray=userAssets[userId];//get existing array

        assetsArray.push(assetId);//push new refernce to aaray
        userAssets[userId]=assetsArray;//add new array to mapping
        assetNameToLog=assetList[assetId].assetName;
        onAssetAssignment(assetId,assetNameToLog,userId,userName);
    }

    function returnAsset(uint userId, uint assetId)public {
        //add reference to userAssetRefeneces array
        assetsArray=userAssets[userId];//get existing array

        uint index=IndexOf(assetsArray,assetId);//get index of assetId to be removed 
        if(index>0){
        delete assetsArray[index-1];//remove the asset Id from array
        userAssets[userId]=assetsArray;//add new array to mapping
        assetNameToLog=assetList[assetId].assetName;
        onAssetReturn(assetId,assetNameToLog,userId);
        }
    }

    function getAssetAssignment(uint userId)public returns (uint[] assetIds) {
        assetIds=userAssets[userId];
        onGetAssignemnt(assetIds);
    }

    function IndexOf(uint[] values, uint value)public returns(uint) {
        uint length=values.length;
        uint i = 0;
            while (values[i] != value) {
              i++;
              if(i==length){
                  return 0;
              }
            }
        return i+1;
    }
}

Well when I call the function assignAsset() or returnAsset() some times it return the event immediately but some times it refuse to return the events after a long wait also I am not getting events callback.

Here is the JavaScript code I am using to call the function and events.

var contract = web3.eth.contract(abi).at(contractAddress);
var assetReturnEvent = contract.onAssetReturn();
var assetAssignmentEvent = contract.onAssetAssignment();

contract.assignAsset.sendTransaction(userId,assetId,userName,{from:web3.eth.accounts[0],gas:1000000},function (error, result){ 
        if(!error){
            assetAssignmentEvent.watch(function(err, result) {
                  if (err) {
                    console.log(err);
                    return;
                  }
                  console.log("saved"+result);
                  assetAssignmentEvent.stopWatching();
                });
        } else{
            console.log(error);
        }
    });

contract.returnAsset.sendTransaction(userId,assetId,{from:web3.eth.accounts[0],gas:1000000},function (error, result){ 
        if(!error){
            assetReturnEvent.watch(function(err, result) {
                  if (err) {
                    console.log(err);
                    return;
                  }
                  console.log("Removed"+result);
                  assetReturnEvent.stopWatching();
                });
        } else{
            console.log(error);
        }
    });

Is there any thing I am doing wrong?? Or its just a testrpc issue??

  • It could be a timing issue? You should start watching for the event before you make the transaction. – smarx Jan 8 '18 at 15:47
  • @smarx I will try doing this, But I dont think that should be an issue as I am doing so many transaction is this same way and all of them are doing fine, I am facing issues with this particular function call – SwapnilKumbhar Jan 9 '18 at 9:00

Your Answer

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

Browse other questions tagged or ask your own question.