0

I am running my program to deploy thousand of contracts and millions of transaction to interact with them, which definitely need a lot of hours. But, when i let my program running during night (without using my laptop at all), then Ganache GUI become disconnected after some hours, which cause my program to stop. and my program shows this error Error: Invalid JSON RPC response: ""

It should be noted that i turned off all option of auto sleep and choose Never/Do nothing options. Why Ganache become unresponsive after some hours ? Is there any way to keep Ganache running all the time ?

UDDATE: my code is here

const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("HTTP://127.0.0.1:8545");
const web3 = new Web3(provider);
const account1 = "0x2cAe18ac37d85CccdE2e...." // Ganache GUI
const privateKey1 ="0x19c30ab1a928ab11f18..........." // Ganache GUI

//////////////////// Final Deploy //////////////////////////////
async function finalDeploy(r){

    const options = {
      data: '0x' + myBin[0]+constructorParams[r],
      gas: 5000000,
  };
  const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
  const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
  console.log("Contract: is deployed at  " +receipt.contractAddress);
  var address = receipt.contractAddress;
  return address;
  // contractAddressess.push(receipt.contractAddress)


}
//////////////////// Final Setter //////////////////////////////
async function finalSettersFunctions(address, r, j){
  var myFunc; 
      myContAddr = address;

          myFunc = finalSetters[r][j];

            try {
              await web3.eth.sendTransaction(
                // web3.eth.sendTransaction(
                {from:account1,
                // nonce: myNonce,
                to:myContAddr,
                data: myFunc
                    }, function(err, receipt) {
                      console.log("Normal Setters: ",receipt)
                    //   myNonce++;
                    //   console.log("Current nonce Normal is : ", myNonce)
                    });
            } catch (error) {
              console.log(" Normal Setters: ERROR !", error); 
            //   myNonce--;
            }
}

//////////// Getting Status ////////////////////////
async function finalGetStatus(statusArr, address) {
  var myFunc; 
  var myFuncParams; 
  var res;
  var myContractAbiDefenition = myAbi[0]

    var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, address);
    for(j=0; j<selectorObjects.length; j++) {
      // console.log("function name: ", selectorObjects[j].func);
      myFunc = selectorObjects[j].func;

      try {
        res = await myContractInstance.methods[myFunc](...[...Array(finalGettersParams[j].__length__).keys()].map(key => finalGettersParams[j][key])).call({from: account1})  // without input
        console.log("Getter output", res);
        statusArr.push(res)
      } catch (error) {
        console.log("Getter output: ERROR !", error);
        noOfErrors++;
        statusArr.push(noOfErrors)
      }

    }       

  // return statusArr;
}

async function main() {
  myData =  fs.readFileSync('./param/param0.txt', 'utf8').split('\n');

  for(r=0; r<myData.length; r++) {

    for(let i = 0; i < finalGetters[r].length; i++){
      for(let j =0; j < finalSetters[r].length; j++){
        console.log("============TestCase No. "+r+"  Getter  g"+i+"  Setter   s"+j+"=================")
      var myReceipt = await finalDeploy(r);
      console.log("============  Setter Function of Contract No. ", r);
      var myget = await finalSettersFunctions(myReceipt, r,j);
    console.log("============ NORMAL ORDER Status Functions of Contract No. ", r);
        var myset =await finalGetStatus(prvResult, myReceipt);
      }
    }
  }
}
main().then(() => {
    console.log("ALL has been DONE !")
  })

and myData is all my testcases/parameters like this [c1(constructor), g1, g2, s1, s2, s3, g3, g4, s4, g5, g6.........]... it should be noted that these values are encoded pareamters like this [0x000000, 0x123fs000, ........, 0x23abc220000]and also noted that myData contain millions of such testcase.. in finalGetters = [g1, g2, g3,.. ], finalSetter =[s1, s2, s3...]

Ganche GUI version: v2.1.2 Web3 version: [email protected] Node version: v10.17.0 Windows 10

13
  • How do you interact with Ganache? Truffle 4? Truffle 5? A standalone web3 script? Something else? Jan 5, 2020 at 9:42
  • standalone web3 script..
    – Amir Ali
    Jan 6, 2020 at 3:32
  • Please share it, and also indicate what Ganache and Web3 versions you are using. Jan 6, 2020 at 4:39
  • Ganace GUI version is : v2.1.2 and web3 : [email protected]
    – Amir Ali
    Jan 6, 2020 at 5:00
  • Why don't you use ganache-cli? And where is your script? Jan 6, 2020 at 5:02

1 Answer 1

0

Try to wrap each one of your getter function calls with this:

async function rpc(func) {
    while (true) {
        try {
            return await func;
        }
        catch (error) {
            console.log(error.message);
            if (!error.message.startsWith("Invalid JSON RPC response"))
                throw error;
        }
    }
}

For example, change this:

res = await myContractInstance.methods[myFunc](xxx).call(yyy);

To this:

res = await rpc(myContractInstance.methods[myFunc](xxx).call(yyy));
3
  • @AmirAli: "due to my laptop is in use"??? Doesn't that imply anything to you? You have not disabled all the "sleep" features in your OS! This issue has got nothing to do with your code, so changing the code won't fix it!!! I'll delete this answer in a few minutes. Jan 8, 2020 at 5:06
  • i have disabled all sleep option , but could not figure it out why this happened...
    – Amir Ali
    Jan 8, 2020 at 5:35
  • @AmirAli: In any case, that should keep your program pending until you get back to work and start using your PC again (i.e., it will keep retrying the same method as long as the Invalid JSON RPC response exception is thrown). If you wanna retain your printouts prior to this error, then remove the console.log(error.message); statement (or extend the rpc function with a mechanism which ensures that it this error message not printed more than once per function-call). Jan 8, 2020 at 7:21

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.