8

Does ethereum generate any event whenever a new block is mined on the chain which can trigger a daemon process(lets say java code)?

Abhiram mishra
  • 1,922
  • 1
  • 13
  • 23
Aman C
  • 983
  • 2
  • 11
  • 13
  • you can see this : https://media.consensys.net/technical-introduction-to-events-and-logs-in-ethereum-a074d65dd61e – iman Sep 09 '18 at 06:27

2 Answers2

12

Using e.g. the web3 JS API, you could setup a filter and watch block changes:

var filter = web3.eth.filter('latest');

filter.watch(function(error, result){
  var block = web3.eth.getBlock(result, true);
  console.log('current block #' + block.number);
});
Raphayol
  • 147
  • 7
tomconte
  • 221
  • 1
  • 3
1

There is an org.ethereum.core.EthereumListener.java interface,which do provide a set of function which would be invoked on such event , for example onBlock(), onTransactionExecuted() etc. You can implement it or extend the EthereumListenerAdapter.java and override the method you want,and the method executes whenever the event occurs.

For example:

public class MineAndYoursListener extends EthereumListenerAdapter {
    @Override
    public void onBlock(Block block, List<TransactionReceipt> receipts) {
    // do what you want to do with my body
    // or daemon process
    }
}
Abhiram mishra
  • 1,922
  • 1
  • 13
  • 23
  • How will this be triggered from a geth client? To be more elaborate I have a private setup of 2 nodes(both running geth client), now whenever a new block is mined on this private chain, how will it trigger the above java code? – Aman C Jun 17 '16 at 09:54
  • I think you did not understand, this is the java code , so am expecting your client to be Java ( ethereumj) , as another node. – Abhiram mishra Jun 17 '16 at 11:01
  • Ok, Is there a way to do it in a geth client as well as I am not using ethereumj? – Aman C Jun 17 '16 at 11:17
  • My question was more related to how to capture a new block mining event on a daemon process(which can be java,nodejs or any other language). Can somehow ethereum client notify the daemon about the new block? – Aman C Jun 17 '16 at 11:19
  • You can create event logs , watch them and and on every successful block with trasaction is successful the call back will be invoked on the web3,json rpc. However I am sure you can not call a java process from there – Abhiram mishra Jun 17 '16 at 11:36