Preface: The following is a source-code reading note I wrote before, which is transferred from yanyiwu.com and is based on asch@1.3.0 version, so there is a little gap with the latest version, for reference only. I hope this note can help you when you just start reading the source code. Thank you.

Asch is the abbreviation for Application of Sidechain. It is an application development platform based on blockchain cross-chain technology. Currently, all core code is open source on GitHub.

The blockchain is the underlying technology of Bitcoin, but it is less well-known than Bitcoin. But I think its potential is far greater than Bitcoin. It is one of the technologies that are most worthy of the attention of the technical staffs.

Generally, I prefer to study the code that is updated earlier when I study new technology. Because very sophisticated code is often over-designed, it may not be a good choice for reading code. It is easier to understand the core principles of the code which is in the early stage of the project. Asch is currently a very good open source project for blockchain learning.

Overall Structure

The entire Asch chain is currently composed of the following source-code libraries.

1. asch: core server-side code of Asch and mainly developed by node.js.

2. asch-frontend: source code of Asch web wallet

3. asch-js&asch-cli: the client-side code of Asch

Source reading is currently based on asch@1.3.0 version.

Directory Structure

proto/index.proto

genesisBlock-mainnet.json

config-mainnet.json

public/

src/core

src/base

『proto/index.proto』

The block core data structure of Asch is serialized and deserialized through protobuf. So proto/index.proto is the data structure of the core block. In my understanding, the name index.proto should be called block.proto.

『public/』

It is the root directory of the web wallet code, which contains code generated by packaging the asch-frontend code.

『src/』

src / init.js is responsible for initialization, which uses async to deal with a variety of priorities to avoid callback. It’s a little bit convoluted at first, but it will be pretty clear when you understand it. Many modules in the code have common module variables, the most typical is moduels, library. Modules corresponds to the module under src /core/ and library.base corresponds to the module under src / base /.

『The rotating forging mechanism of the trustees』

Asch does not use the mining mechanism used in Bitcoin. Instead, it uses the rotating forging mechanism of the trustees, mainly in the following process.

onBlockchainReady(core/delegates.js): When the blockchain database loading is completed, the forging initialization begins.

loadMyDelegates: Get the secret from the configuration file (read several in turn) and use secret to generate the key pair (public key and private key).

accounts.getAccount: Obtain an account from the blockchain database with a public key, and check whether the account belongs to a trustee. If so, then continue forging.

loop: Start polling with 100ms per polling interval.

slots.getSlotNumber (utils/slots.js): Get epochTime first. EpochTime records how many seconds have elapsed from May 27, 2016 T20: 00: 00 to a specific time. Then getSlotNumber = epochTime / interval, interval = 10. That is, a new slot appears every 10s.

blocks.getLastBlock: As the name implies, get the latest block.

getBlockSlotData->generateDelegateList (core/delegates.js): Get the current list of trustees, sort it by number of votes and get the public keys of the 101 trustees with the highest number of votes. Sort the list of trustees randomly. The randomness here should be random for each node, not completely random.

getBlockSlotData: Gets the list of the trustees of the current slot, and if the trustee’s key is correct in the configuration, then it goes to the “generation process of the block”.

『generation process of the block』

loop(core/delegates.js): For nodes registered as trustees, there will be a timing trigger called generateBlock inside blocks.js.

generateBlock(core/blocks.js): First, get the unconfirmed transaction lists from transactions.

generateBlock: Get the lists of transactions that have been filtered out of the unconfirmed transaction lists.

base.block.create: Create a new block containing these transaction lists and verify that the block is valid.

hasEnoughVotes: Check whether the trustee has enough votes, and if the votes are not sufficient, no block can be generated. (Should this step be advanced?)

processBlock: Query the local database through block.id and continue the following process if it already exists.

applyBlock: Make some transfer operation to the accounts involved in the transaction to validate the transaction lists that have just been verified. This part of the logic is implemented in function apply in the src/base/transaction.js. There are many types of transactions, which are detailed later.

base.block.dbSave: store block

base.transaction.dbSave: store transaction lists

accounts.mergeAccountAndGet: Update the data of the creator of the new block. The creator is actually the node that is registered as the trustee.

setLastBlock: Set this new block to the current block

bus.message(‘newBlock’): Broadcast the new block.

『ToDo』

The transaction process of Asch chain

The HTTP interface implementation principle of Asch chain

DAPP principle and development process

…