How CodeChain Indexer works

JinGyeong Jeong
CodeChain
Published in
5 min readJun 20, 2019

In addition to developing the CodeChain Core, the CodeChain team is engaged in various CodeChain application development. For instance, there is an Explorer that shows CodeChain blocks’ information, a Web Wallet that allows users to personally manage their keys, a mobile wallet app (iOS, Android) that one can use without having to store his/her key directly, and a console that can publish and manage assets via CodeChain. What these applications have in common is that they use the CodeChain Indexer API internally. In this post we will introduce what the CodeChain Indexer is and how it works.

A database is required to develop services such as Explorer. The CodeChain Indexer acts as this database, and the actual implementation relies on Postgres. In other words, the CodeChain Indexer indexes the block and transaction information into the database. Blockchains are frequently compared with distributed databases; however, blockchain nodes themselves do not support generic database functions. For example, it does not support a flexible query language such as SQL. Rather, it provides blockchain data with a very limited interface. So whichever service you develop, it is likely that you will need to build a separate database while simultaneously running the CodeChain node, and the CodeChain Indexer can be an alternative. This is because it provides the APIs that CodeChain applications generally need.

CodeChain Indexer Worker

The implementation of the CodeChain Indexer is divided into two major parts. One is the Worker that reads the block information and stores it in the database, and the other is the API that reads the information stored in the DB and provides it in API form.

The Worker periodically requests blocks from the CodeChain nodes, processes them, and stores them in the database. One of the main processed information is the address. For instance, blocks and transactions have ECDSA signatures, but not the address of the signer. The ECDSA signature enables recovery of the public key from the signature. Since it is possible to compute the address via the public key, recording the address is a waste of space for CodeChain nodes. The CodeChain Indexer APIs must support queries that search by address, and thus, all addresses are calculated in advance and stored in the database.

Another major processed information is UTXO. Currently, there is no way to know all the UTXOs that an address has with just JSON-RPC in CodeChain. You must either figure out every single UTXO via the transaction tracker that sent assets to your address. If you do not know the transaction tracker, every transaction must be read and calculated to figure out whether assets were sent to your address or not. With the Indexer, you can see all the UTXO information held by a particular address with a single call to the UTXO API. This is possible because the Worker pre-stores UTXO information for all addresses.

CodeChain Indexer API

Providing information stored in a database in API form is straightforward. Let me give you some examples. The code below is a part of the UTXO model implementation of the Indexer:

interface UTXOAttribute {
address: string;
assetType: string;
quantity: string;
blockNumber: number;
usedBlockNumber?: number | null;
/* ... */
}

usedBlockNumber is a field that records the block number of the transaction that used UTXO. If the UTXO has not been used yet, the field value will be NULL. All UTXOs with an address “X” are easily found in the SQL below.

SELECT * FROM UTXOs WHERE address = “X” AND usedBlockNumber IS NULL

Calling the Indexer’s “GET /api/utxo” API will internally use SQL similar to above, and searching the address in the CodeChain Explorer will be used to show the assets that you have. This API can also be called by an Explorer URL.

https://explorer.codechain.io/api/utxo?address=ccaqyqnchjfu5q7caphhtdenvg3fha2y6zj8dus20gng0

Let’s look at a slightly more complicated case this time. The CodeChain Console is a service that allows you to issue assets and recruit and manage investors. The person who issued the asset has the function of voting for the investor, paying interest or dividend. The dividend is proportional to the stake of the asset, and the point at which the stake is calculated may not be the dividend date, but may be a certain point in time before the dividend date. From the standpoint of implementing the Console service, it is a matter of calling all UTXOs for a particular asset at a particular point in time and calculating the equity percentage for each address. The SQL below is an example of SQL that gets all the UTXOs of the asset “A” in block 1234567:

SELECT * FROM UTXOs WHERE
assetType = “A”
AND blockNumber <= 1234567
AND (usedBlockNumber IS NULL OR usedBlockNumber > 1234567)

You can use the above functions by calling the Indexer’s “GET /api/snapshot” API. Likewise, you can call it through the Explorer URL. If you access the URL below, you will know the address and the quantity of wCCC at 9 pm UTC June 18, 2019:

https://explorer.codechain.io/api/snapshot?assetType=0x0000000000000000000000000000000000000000&date=2019-06-18T09:00:00.000Z

Costs

An Indexer provides convenient features, but not all of this is free. Since CodeChain data is stored redundantly in the database, it requires an additional 1.5 times the storage space of existing CodeChain data. Furthermore, it takes time to sync all the blocks when starting the Indexer for the first time. The CodeChain team is currently looking for ways to minimize these costs.

Conclusion

When developing CodeChain applications, you can save time and effort by leveraging the CodeChain Indexer API. The CodeChain team has also used Indexers to develop applications, such as Explorers and Wallets, and plans to use them in the future. If you are interested in being part of the CodeChain Indexer development process, please pay a visit to the GitHub repository or Gitter. The CodeChain Indexer is a 100% open source project.

--

--