2

What I'm trying to achieve with 2 clients running on my computer (same, private network):

client1 makes a request by sending a value k
client1 starts to listen for an answer to the request
client2 is listening for such a request
client2's event listener is triggered
client2 then performs an action (sends k+1 to the contract as a response)
client2 starts listening for a new request
client1's event listener is triggered - she got an answer to her request
client1 sends a new request (k+1)

I have tried a number of variants of the code below, with varying degrees of (un)success - either one client keeps pumping out requests/responses (like in the example below) or one of them sends its data, and the other reads it multiple times (?) and acts accordingly (i.e. starts sending out as many requests/responses as it has received events). I of course want each client to wait until its event is triggered and then just perform its action and go back to waiting. Would really appreciate some help with this!

This is the JavaScript web3js code (process1 is called with a 0 in the first run).

function process1(k){
  myContract.query(k, 2000000, {from: web3.eth.accounts[0]});
  myContract.responding({}, {fromBlock: 'latest', toBlock: 'latest'}).watch((error, eventResult) => {
    if (error)
    console.log('Error: ' + error);
    else{
      console.log('myEvent: ' + JSON.stringify(eventResult));
      k=eventResult.args.r;
      k++;
      myContract.responding({}, {}).stopWatching();
      process1(k);
    }
  });
}

function process2(){
  myContract.asking({}, {fromBlock: 'latest', toBlock: 'latest'}).watch((error, eventResult) => {
    if (error)
    console.log('Error: ' + error);
    else{
      console.log('myEvent: ' + JSON.stringify(eventResult));
      k=eventResult.args.q;
      k++;
      myContract.response(k, 2000000, {from: web3.eth.accounts[0]});
      myContract.asking({},{}).stopWatching();
      process2();
    }
  });
}

This is the Solidity contract:

pragma solidity ^0.4.9;

contract Testing {


  function Testing() payable {
  }

  event asking (address sender, uint q);
  event responding (address sender, uint r);


  function query(uint q){
    asking(msg.sender, q);
  }

  function response(uint r){
    responding(msg.sender, r);
  }
}

Output:

myEvent: {
"address":"0x7dae29167d71e027a56672b6cba6d6f32b68456f",
"blockNumber":6985,
"transactionHash":"0xdef57d92d37166d54673d128bd56f8e170a11ba84695395f5b06fac723450d90",
"transactionIndex":0,
"blockHash":"0xd9e58ebdce3d3803771ea6f9c6d8d71d58ab94920a781be2fdae6ac8de7e1db5",
"logIndex":0,
"removed":false,
"event":"asking",
"args":{"sender":"0x197d8fc7da84f7ee0a763db8d759255d1341e906","q":"0"}
}
1

Something like this should work

function process1(k){
  myContract.query(k, 2000000, {from: web3.eth.accounts[0]});
  var responding = myContract.responding({}, {fromBlock: 'latest', toBlock: 'latest'});
  responding.watch((error, eventResult) => {
    if (error)
    console.log('Error: ' + error);
    else{
      console.log('myEvent: ' + JSON.stringify(eventResult));
      k = eventResult.args.r;
      k++;
      myContract.query(k, 2000000, {from: web3.eth.accounts[0]});
    }
  });
}


function process2(){
  var asking = myContract.asking({}, {fromBlock: 'latest', toBlock: 'latest'});
  asking.watch((error, eventResult) => {
    if (error)
    console.log('Error: ' + error);
    else{
      console.log('myEvent: ' + JSON.stringify(eventResult));
      k = eventResult.args.q;
      k++;
      myContract.response(k, 2000000, {from: web3.eth.accounts[0]});
    }
  });
}

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.