[Ethereum] Web3 Function Calls are very slow !

contract-invocationweb3js

I am running a local geth node (–fast –rpc) and executing the following code in a local Nodejs program:

https://gist.github.com/admazzola/79daee3b41e47596e36286464b1d98dd

Strangely, it works but that loop runs extremely slow and each function call takes about one whole second to resolve. Once it gets up in the 2 or 3 thousands it usually locks up or freezes up and I don't know why that is. Is there a better way to pull all of this data and store it in a dictionary or is this the best way? Why is it so slow if I am just gathering local data?

Best Answer

Is there a better way to pull all of this data and store it in a dictionary or is this the best way?

It would almost certainly be faster to parse the data directly from the .ldb files in the chaindata directory, and then cache it. To do that would require you to parse it, which itself requires knowledge of how the data is stored.

If you're using Node.js, you could take a look at "Exploring Ethereum's state trie with Node.js". (Node.js is probably not the best language to use for this.) Note that the code in the tutorial is slightly out of date. I've included a tweaked working version below.

var levelup = require('levelup');
var rlp = require('rlp');
var trie = require('merkle-patricia-tree');
var db = levelup('chaindata');

// The genesis state root.
var stateRoot = 
  'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544';

// ERROR: AssertionError: Invalid root length. Roots are 32 bytes
var stateTrie = new trie(db, stateRoot);

// Note: We're doing everything using binary encoding.
db.get(new Buffer(stateRoot, 'hex'), {
  encoding: 'binary'
}, function (err, value) {
  console.log("Printing the entry for the State Root")
  var decoded = rlp.decode(value);
  console.log(decoded);
});

// Gav's address.
var gav = new Buffer('8a40bfaa73256b60764c1bf40675a99083efb075', 'hex');

trie.get(gav, function (err, val) {
  console.log("Printing the contents of Gav's address")
  var decoded = rlp.decode(val);
  console.log(decoded);
});
Related Topic