Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Getting The Pixels and Ethereum Events

Our contract has a millon pixels. At some point, we’re going to want to draw the image that all the pixels create.

Our current pixels method lets us retrieve one pixel at a time, that would take a million calls – I’m sure we can do better than that.

A first try might be to create a function getPixels which just returns them all:

  function getPixels() public constant returns (bytes3[1000][1000]) {
      return pixels;
  }

If you try this, it may or may not work. It will be slow either way, because it’s a lot of data.

Another idea we might try is to just get each row:

  function getRow(uint x) public constant returns (bytes3[1000]) {
    return pixels[x];
  }

This will work in a pinch, but it still requires a thousand calls, one for each row.

When we build the UI we probably want to cache the image so we don’t have to keep reading from the blockchain. If we had a script to periodically recreate the image, making 1,000 local calls isn’t that big of a deal.

However, it would be great to have a real time component to this page. We want any visitor to this page to automatically see any new pixels the instant their purchased.

And thankfully, Ethereum has some mechanisms for this so that we don’t have to read every pixel to find out what changed.

The mechanism is Ethereum Events. The idea with events is that our contracts can emit events at certain points in our code. These events can be indexed, which means we can find specific events very quickly.

 

Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.