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)?

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);
});
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
    }
}
  • 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 9: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

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.