2

This question is related to this one or this one. There is a tutorial here that shows some filtering examples but it doesn't get into transaction level granularity. Can we query the block chain by transaction and process specific transactions asynchronously?

For example, this code goes through blocks starting and ending with a given block number and displays each transaction to or from a given address:

function getTransactionsByAccounts(myaccount, startBlockNumber, endBlockNumber) {

  console.log("Searching for transactions to/from account \"" + myaccount + "\" within blocks "  + startBlockNumber + " and " + endBlockNumber + "\"");

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {

    var block = eth.getBlock(i, true);
    if (block != null && block.transactions != null) {
      block.transactions.forEach( function(e) {
        if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
          console.log("  tx hash          : " + e.hash + "\n"
            + "   nonce           : " + e.nonce + "\n"
            + "   blockHash       : " + e.blockHash + "\n"
            + "   blockNumber     : " + e.blockNumber + "\n"
            + "   transactionIndex: " + e.transactionIndex + "\n"
            + "   from            : " + e.from + "\n" 
            + "   to              : " + e.to + "\n"
            + "   value           : " + e.value + "\n"
            + "   gasPrice        : " + e.gasPrice + "\n"
            + "   gas             : " + e.gas + "\n"
            + "   input           : " + e.input);

            // Do something asynchronously with the transaction:
            call_async_method(e); // <== HOW CAN WE ADD THIS AS A PROMISE?
        }
      })
    }
  }
}

How can we process a transaction asynchronously (as shown in the code)? possibly using promises and the filters defined in web3.js.

  • 1
    what exactly do you mean 'as a promise'? If your method is i.e. saving the transaction to a db or calling a web service with transaction data, i don't see the need to promisify it, as far as I understand promises well -but I doubt that like 10 times a day :-) -. – Sergeon Jul 15 '16 at 16:07
  • Calling a function confirming that a transaction was found by the filter (I plan on iterating through the block chain and process a set of transactions concurrently. The above code works serially but I need a form of parallelism as each transaction may take a possibly lengthy amount of time). – Sebi Jul 15 '16 at 17:47
4

Unfortunately, there is no filter provided to parse the confirmed transactions without going through a block first i.e., you can trigger a filter (and execute call_async_method()) whenever there is a latest block: eth.getBlock("latest"), but there is no such function for a transaction.

That said, if you're operating on a private blockchain (you trust all the nodes) and believe that all the initiated transactions will successfully be added to the blockchain, you can create filters to react upon eth.pendingTransactions so that the actions like call_async_method() will be performed immediately without waiting for the transactions to get confirmed and added to the blockchain. However, at a later time, you should always check if these transactions are indeed confirmed.

This is not an ideal or recommended solution and contradicts with the fundamental principles of blockchain, but you can test and check if it's suitable for your use case.

Edit: Fast way to scan blocks for transactions on an account. This is an asynchronous scanner with 200 threads: https://ethereum.stackexchange.com/a/7184/2460

Source code here.

  • I haven't used node js. From what I understand it's a back end module (pretty much like php) that runs javascript. This is great for event based and asynchronous programming as it abstracts from dealing with multithreading. I don't know how easily it can be put in a library and distributed in a software project. And also, what happens when a certain call (call_async_method()) takes "too" long (from my understanding node is single threaded: stackoverflow.com/questions/1884724/what-is-node-js) – Sebi Jul 20 '16 at 8:48
  • I'm not sure, but are all functions running in node js asynchronous? It appears so from gist.github.com/ross-p/bd5d4258ac23319f363dc75c2b722dd9 – Sebi Jul 20 '16 at 10:33
  • Even I'm not an expert in node.js. Maybe you can post these as separate questions on stackoverflow? You can try using the Date().getTime() function to determine how long it's taking for the call. – galahad Jul 21 '16 at 20:57
  • The thing is that Javascript, to my knowledge, executes sequentially in the browser, I need a server-like functionality from a node that checks the block for transactions parallelly using Javascript. I'm reading about nodejs as it seems to run everything in pseudo parallel manner (using deterministic scheduling). – Sebi Jul 22 '16 at 7:09
  • Please post your findings/thoughts. – galahad Jul 22 '16 at 14:20

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.