js-libs icon indicating copy to clipboard operation
js-libs copied to clipboard

Use a dependency graph for light.js

Open amaury1093 opened this issue 6 years ago • 0 comments
trafficstars

light.js is used when pubsub on state is not available (e.g. on a light client). In that case, we need to find the minimal viable calls to track the relevant portion of the state we're interested in. (track == be notified when it changes).

Some examples:

  • To track the balance of an ETH account, we subscribe to new heads, and on each new head, we make a JSONRPC call api.eth.getBalance(...)
  • To track the balance of an account in a ERC20 contract, we subscribe to the events, and make a balanceOf call on events that are relevant to the tracked account.

An immediate remark that comes up is that there's a lot of "on each ..., do ...". And the best data structure to express "on each ..., do ..." is a dependency graph. Luckily, it also fits perfectly well with reactive programming, see for example reactive-graph.

We could refactor light.js into a graph structure for RxJS, similar to reactive-graph, where:

  • nodes are Observables
  • edges are pipes

So:

  • We start by manually defining some source nodes (STARTUP, NEW_HEAD, EVERY_SECOND), they don't have incoming edges.
  • Define all other nodes (balanceOf$, syncing$...) by combing source nodes (or other nodes), with the correct pipes.

amaury1093 avatar Dec 06 '18 15:12 amaury1093