Search code examples
node.jsrpcblockchainethereum

Sorting data into appropriate key in an object


I'm trying to loop & sort a large number of data ( the whole ethereum blockchain lol )

I'm trying to create a record of all transactions for every address.

Obviously this is a very intensive process and I'm not sure how to make it more efficient beyond what I have (which isn't that efficient)

It starts out quick but I'm thinking now it has slowed because of the lookup for the address in the txs object.

Any help opinions / help is greatly appreciated.

https://giphy.com/gifs/3o6fJ7KWqxESY9okk8

var txs = {};

var i = 0;
// Loop over blocks
(function loop () {
setTimeout(function () {
  // Get current block
  var block = web3.eth.getBlock(i, true, (error, block) => {
    // debugger;
    // Loop over transactions in block
    for(var j = 0; j < block.transactions.length; j++) {
      // debugger;
      if(txs[block.transactions[j].to]) {

        txs[block.transactions[j].to].transactions.push(block.transactions[j]);
      } else if (txs[block.transactions[j].to]) {
        txs[block.transactions[j].from].transactions.push(block.transactions[j]);
      } else {
        txs[block.transactions[j].to] = {
          transactions: [block.transactions[j]]
        }
        txs[block.transactions[j].from] = {
          transactions: [block.transactions[j]]
        }
      }
    }
  });
  i++
  if (i < highestBlock) {
    loop();
  }
 }, 50);
})();

Solution

  • I think that your code has an error in it located at "else-if", it seems that you should use the txs[block.transactions[j].from] property instead of the txs[block.transactions[j].to]. If you simply want to accomplish a recursive pattern you could use the setImmediate function or the process.nextTick method. If you use node.js v6+ you could use a Map instead of the object.