Derek interprets Bytom source code-the Genesis block

Source: Internet
Author: User

Author: Derek

Brief introduction

GitHub Address: Https://github.com/Bytom/bytom

Gitee Address: Https://gitee.com/BytomBlockc ...

This chapter introduces Derek's interpretation of-bytom source code Analysis-the Genesis block

The author uses the MacOS operating system, and the other platforms are similar

Golang version:1.8

Introduction to the Genesis block

The first chunk in a blockchain is called the Genesis block. It is the common ancestor of all chunks within the blockchain.

In comparison to the original chain, the creation block is hardcoded into the BYTOMD, each of which begins with the same creation block, which ensures that the creation block will not be changed. Each node takes the Genesis block as the first chunk of the blockchain, thus building a secure, trusted blockchain.

Get the Genesis block

./bytomcli get-block 0{"bits": 2161727821137910500, "difficulty": "15154807", "hash": "a75483474799ea1aa6bb910a1a5025 b4372bf20bef20f246a2c2dc5e12e8a053 "," height ": 0," nonce ": 9253507043297," Previous_block_hash ":" 0000000000000000000000000000000000000000000000000000000000000000 "," size ": 546," timestamp ": 1524549600," Transaction_merkle_root ":" 58e45ceb675a0b3d7ad3ab9d4288048789de8194e9766b26d8f42fdb624d4390 "," Transaction_ Status_hash ":" C9C377E5192668BC0A367E4A4764F11E7C725ECCED1D7B6A492974FAB1B6D5BC "," Transactions ": [{" id ":" 158d 7d7c6a8d2464725d508fafca76f0838d998eacaacb42ccc58cfb0c155352 "," inputs ": [{" Amount ": 0," a Rbitrary ":" 496e666f726d6174696f6e20697320706f7765722e202d2d204a616e2f31312f323031332e20436f6d707574696e6720697320706f7765722e202d2d2 04170722f32342f323031382e "," Asset_definition ": {}," asset_id ":" 000000000000000000000000000000000000000       0000000000000000000000000 "," type ":" Coinbase " }], "outputs": [{"Address": "bm1q3jwsv0lhfmndnlag3kp6avpcq6pkd3xy8e5r88", "Amount": 140700041250000000, "Asset_definition": {}, "asset_id": "Ffffffffffffffffffffffffffffffffffffffffffffff Ffffffffffffffffff "," Control_program ":" 00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4 "," id ":" e3325bf07      c4385af4b60ad6ecc682ee0773f9b96e1cfbbae9f0f12b86b5f1093 "," position ": 0," type ":" Control "} ], "size": 151, "Status_fail": false, "Time_range": 0, "version": 1}], "Version": 1}

Use the BYTOMCLI client to query for chunk information with a height of 0. We can see the above output results.

    • BITS: target value, which is less than or equal to the value of the hash after it is mined, the new block is built successfully
    • Difficulty: Difficulty value, miners find the next effective block of difficulty. This parameter is not stored on the blockchain and is calculated by bits
    • Hash: Current Chunk hash
    • Height: current Block Heights
    • Nonce: Random number, which uses different nonce to generate different hash values when mining
    • Previous_block_hash: The hash value of the parent chunk of the current chunk
    • Size: The number of bytes in the current chunk
    • Timestamp: Out of block time
    • Transaction_merkle_root: Merkle root node of the Genesis block
    • Transactions: Utxo transactions in the current block

Since the creation block is the first block, the parent block of the Genesis block, which is the Previous_block_hash parameter, 0000000000000000000000000000000000000000000000000000000000000000 By default

The timestamp timestamp is 1524549600, and the time is 2018-04-24 14:00:00, which is the time of the primary network than the original chain.

SOURCE Analysis

Get Blockchain status

Protocol/protocol.go

func NewChain(store Store, txPool *TxPool) (*Chain, error) {    // ...    storeStatus := store.GetStoreStatus()    if storeStatus == nil {        if err := c.initChainStatus(); err != nil {            return nil, err        }        storeStatus = store.GetStoreStatus()    }    // ...}

When we first start than the original chain node, the store. Getstorestatus will get the storage state from the DB, the process of obtaining the stored state is to query the data of key blockstore from LEVELDB, if the query error is considered to be the first run than the original chain node, then it needs to initialize the parent chain.

Initializing the main chain

Protocol/protocol.go

func (c *Chain) initChainStatus() error {    genesisBlock := config.GenesisBlock()    txStatus := bc.NewTransactionStatus()    for i := range genesisBlock.Transactions {        txStatus.SetStatus(i, false)    }    if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {        return err    }    utxoView := state.NewUtxoViewpoint()    bcBlock := types.MapBlock(genesisBlock)    if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {        return err    }    node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)    if err != nil {        return err    }    return c.store.SaveChainStatus(node, utxoView)}

There are several steps to initializing the main chain:

    • Config. Genesisblock () Get the Genesis block
    • Set all trading statuses in the creation block
    • Storage of Genesis blocks to Leveldb
    • State. Newutxoviewpoint () for temporary small portions of Utxo State storage Collection
    • Instantiate Blocknode,blocknode for choosing the best chain as the main chain
    • Save the latest main chain status

Hard-coded Genesis block

Config/genesis.go

Func genesistx () *types. Tx {contract, err: = Hex. Decodestring ("00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4") if err! = Nil {log. PANICF ("Fail on decode Genesis TX Output Control Program")} TxData: = types. txdata{Version:1, Inputs: []*types. txinput{types. Newcoinbaseinput ([]byte ("Information is power. --jan/11/2013. Computing is power. -apr/24/2018. ")),}, Outputs: []*types. txoutput{types. Newtxoutput (*consensus. Btmassetid, consensus. Initialblocksubsidy, contract),},} return types. NEWTX (TxData)}func mainnetgenesisblock () *types. Block {tx: = GENESISTX () txstatus: = BC. Newtransactionstatus () txstatus.setstatus (0, false) Txstatushash, err: = BC. Txstatusmerkleroot (txstatus.verifystatus) if err! = Nil {log. PANICF ("Fail on Calc Genesis TX Status Merkle Root")} Merkleroot, err: = BC. Txmerkleroot ([]*BC. Tx{tx. TX}) If err! = Nil {log. PANICF ("Fail on Calc Genesis TX Merkel root ")} BLOCK: = &types. block{Blockheader:types. blockheader{Version:1, height:0, nonce:9253507043297, Timestamp: 1524549600, bits:2161727821137910632, Blockcommitment:types.            blockcommitment{Transactionsmerkleroot:merkleroot, Transactionstatushash:txstatushash, },}, transactions: []*types. TX{TX},} return block}

Mainnetgenesisblock mainly has the following operations:

    • Create a transaction in the Genesis block by default
    • Set the transaction status in the block to False
    • Set the Genesis block as the root node of the Merkle tree
    • Instantiate block blocks and return

The GENESISTX function generates transactions in the creation block by default on a single trade, with input inputs and output outputs in a single transaction.

Input inputs:
The input has a word "information is power." --jan/11/2013. Computing is power. --apr/24/2018. " This is to commemorate the spirit of Aaron Swartz.

Output outputs:
In the output we see consensus. Initialblocksubsidy for the creation of the World Block Award. Total 140700041250000000/1e8 = 1407000412. That is, 1.4 billion BTM coins.

Calculation is Power

Quote than the original chain founder long clip words:

April 24, our main network on-line, the information is power, 2013 Jaruary11; calculation is power, 2018 April24. This sentence is to commemorate the spirit of Aaron Swartz, the message that power can be regarded as the Internet manifesto, committed to the free dissemination of information, so that citizens privacy protection. Computing is power, committed to letting assets free of trade, free flow, and allowing the wealth of citizens to be protected, I think it is a very good remembrance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.