ether-camp / ethereum-sandbox

Ethereum Sandbox
GNU Affero General Public License v3.0
7 stars 8 forks source link

event handlers for past events with `get` receive also all other events (filter.watch) #61

Open SCBuergel opened 7 years ago

SCBuergel commented 7 years ago

This is my test contract A.sol (in folder test on sbuergel.by.ether.camp):

pragma solidity ^0.4.5;

contract A {
  event e1(uint u);
  event e2(uint u);
  event e3(uint u);
  event e4(uint u);

  function f1(uint u) {
    e1(u);
  }

  function f2(uint u) {
    e2(u);
  }

  function f3(uint u) {
    e3(u);
  }
}

I interface it via the following nodeJS code test.js:

var w3 = require('web3');

contractAbi = [{"constant": false,"inputs": [{"name": "u","type": "uint256"}],"name": "f1","outputs": [],"payable": false,"type": "function"},{"constant": false,"inputs": [{"name": "u","type": "uint256"}],"name": "f2","outputs": [],"payable": false,"type": "function"},{"constant": false,"inputs": [{"name": "u","type": "uint256"}],"name": "f3","outputs": [],"payable": false,"type": "function"},{"anonymous": false,"inputs": [{"indexed": false,"name": "u","type": "uint256"}],"name": "e1","type": "event"},{"anonymous": false,"inputs": [{"indexed": false,"name": "u","type": "uint256"}],"name": "e2","type": "event"},{"anonymous": false,"inputs": [{"indexed": false,"name": "u","type": "uint256"}],"name": "e3","type": "event"},{"anonymous": false,"inputs": [{"indexed": false,"name": "u","type": "uint256"}],"name": "e4","type": "event"}];

contractAddress = '0x86e0497e32a8e1d79fe38ab87dc80140df5470d9';
var ideUrl = 'https://sbuergel.by.ether.camp:8555/sandbox/';
var sandboxId = '7a6e63dca9';
var sandboxUrl = ideUrl + sandboxId;
var web3 = new w3(new w3.providers.HttpProvider(sandboxUrl));

var contract = web3.eth.contract(contractAbi).at(contractAddress);

contract.e1({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {
  console.log('===> GET e1: ' + JSON.stringify(results));
});

contract.e2({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {
  console.log('===> GET e2: ' + JSON.stringify(results));
});

contract.e3({}, { fromBlock: 0, toBlock: 'latest' }).get((error, results) => {
  console.log('===> GET e3: ' + JSON.stringify(results));
});

contract.e1().watch((error, result) => {
  console.log('===> WATCH e1: ' + JSON.stringify(result));
});

contract.e2().watch((error, result) => {
  console.log('===> WATCH e2: ' + JSON.stringify(result));
});

contract.e3().watch((error, result) => {
  console.log('===> WATCH e3: ' + JSON.stringify(result));
});

The following is the expected output from my nodeJS program on startup, this is fine:

===> GET e2: []
===> GET e3: []
===> GET e1: []

Now I call the function f1 from the sandbox and get in the Ethereum Console the expected output Sandbox Event (A.e1): 123. My nodeJS program prints also as expected:

===> WATCH e1: {"logIndex":0,"transactionIndex":1,"transactionHash":"0xc256a9905d90abe8cab06806fcce7a03e829c151d37d59b7ff1a0428de8a5cc9","blockHash":"0x27a087db6ec8239b0dbc4979d7064c3e53ae3ae31bab744f89a64d7be9912bb1","blockNumber":14,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","event":"e1","args":{"u":"123"}}

Now I simply stop the nodeJS program and start it again and receive the following wrong output:

===> GET e3: [{"logIndex":0,"transactionIndex":1,"transactionHash":"0xc256a9905d90abe8cab06806fcce7a03e829c151d37d59b7ff1a0428de8a5cc9","blockHash":"0x27a087db6ec8239b0dbc4979d7064c3e53ae3ae31bab744f89a64d7be9912bb1","blockNumber":14,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","event":"e3","args":{"u":"123"}}]
===> GET e1: [{"logIndex":0,"transactionIndex":1,"transactionHash":"0xc256a9905d90abe8cab06806fcce7a03e829c151d37d59b7ff1a0428de8a5cc9","blockHash":"0x27a087db6ec8239b0dbc4979d7064c3e53ae3ae31bab744f89a64d7be9912bb1","blockNumber":14,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","event":"e1","args":{"u":"123"}}]
===> GET e2: [{"logIndex":0,"transactionIndex":1,"transactionHash":"0xc256a9905d90abe8cab06806fcce7a03e829c151d37d59b7ff1a0428de8a5cc9","blockHash":"0x27a087db6ec8239b0dbc4979d7064c3e53ae3ae31bab744f89a64d7be9912bb1","blockNumber":14,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","event":"e2","args":{"u":"123"}}]

It seems that something is wrong with the filters, here it should only get the events e1 and not also e2 and e3.

SCBuergel commented 7 years ago

When going more low-level via filter it turns out that actually there is just one event being fired (as the Ethereum Console suggested already):

var filter = web3.eth.filter({fromBlock:0, toBlock: 'latest', address: contractAddress});
filter.watch(function(error, result) {
  console.log('----->>> filter watch: ' + JSON.stringify(result));
  if (error)
    console.log('error: ' + error);
})

This produces one output per event, e.g. (here I called 3 functions):

----->>> filter watch: {"logIndex":0,"transactionIndex":1,"transactionHash":"0xc256a9905d90abe8cab06806fcce7a03e829c151d37d59b7ff1a0428de8a5cc9","blockHash":"0x6e704396137cdcb3020e692fb9363fed8daa65ff3b1f5b8d7a836d12abc573f0","blockNumber":5,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","data":"0x000000000000000000000000000000000000000000000000000000000000007b","topics":["0x3680e78b6fdf571695c81f108d81181ea63f50c100e6375e765b14bd7ac0adbb"]}
----->>> filter watch: {"logIndex":0,"transactionIndex":1,"transactionHash":"0xd1d01d8da34ad1c5db6776fb9589cf4c2cbb7e398cab7209739d216de6c40d2d","blockHash":"0x47cc815f1517fd7e7fa19dcff37ca07cac562cec610f73f649f14f161a9dd6d2","blockNumber":32,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","data":"0x00000000000000000000000000000000000000000000000000000000000000de","topics":["0xf04f5fc87a72102f7c0b228f8bbaf9b9aa7a2b5dc295c86538fdde91e95866e9"]}
----->>> filter watch: {"logIndex":0,"transactionIndex":1,"transactionHash":"0x28947e0240d1cc59b184a17485a4c1ab12c82e4dcf01f9a6d8c47897d2948fa0","blockHash":"0x85d314207deae9ff53ad2ff0a5d8dfe3a082a89b6087c5237b76cd487ddcbe74","blockNumber":52,"address":"0x86e0497e32a8e1d79fe38ab87dc80140df5470d9","data":"0x000000000000000000000000000000000000000000000000000000000000014d","topics":["0x6fbec89a9bad4c7daaf5b053ac2c5ad4e0ff33c287295fe9a98cf7f3a3043f9c"]}

This suggests that we have an issue on the listener side or in the "event producer".

SCBuergel commented 7 years ago

My last statement

This produces one output per event

is not generally correct - as described in https://github.com/ether-camp/ethereum-sandbox/issues/74 but that is IMO an unrelated issue.