cool hit counter Derek Explains Bytom Source Code - Genesis Block_Intefrankly

Derek Explains Bytom Source Code - Genesis Block


By Derek

brief introduction

Github address.https://github.com/Bytom/bytom

Gitee address.https://gitee.com/BytomBlockchain/bytom

This chapter introduces Derek Interpretation - Bytom Source Code Analysis - Genesis Block

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

Golang Version: 1.8

Introduction to the Genesis Block

The first block in the blockchain, Tron, is known as the Genesis block. It is the common ancestor of all blocks inside the blockchain.

In the bytomd chain the creation blocks are hard-coded into the bytomd, and every bytomd node starts with the same creation block, which ensures that the creation blocks are not changed. Each node uses the Genesis block as the first block of the blockchain, thus building a secure and trusted blockchain.

Get the Genesis block

./bytomcli get-block 0
{
  "bits": 2161727821137910500,
  "difficulty": "15154807",
  "hash": "a75483474799ea1aa6bb910a1a5025b4372bf20bef20f246a2c2dc5e12e8a053",
  "height": 0,
  "nonce": 9253507043297,
  "previous_block_hash": "0000000000000000000000000000000000000000000000000000000000000000",
  "size": 546,
  "timestamp": 1524549600,
  "transaction_merkle_root": "58e45ceb675a0b3d7ad3ab9d4288048789de8194e9766b26d8f42fdb624d4390",
  "transaction_status_hash": "c9c377e5192668bc0a367e4a4764f11e7c725ecced1d7b6a492974fab1b6d5bc",
  "transactions": [
    {
      "id": "158d7d7c6a8d2464725d508fafca76f0838d998eacaacb42ccc58cfb0c155352",
      "inputs": [
        {
          "amount": 0,
          "arbitrary": "496e666f726d6174696f6e20697320706f7765722e202d2d204a616e2f31312f323031332e20436f6d707574696e6720697320706f7765722e202d2d204170722f32342f323031382e",
          "asset_definition": {},
          "asset_id": "0000000000000000000000000000000000000000000000000000000000000000",
          "type": "coinbase"
        }
      ],
      "outputs": [
        {
          "address": "bm1q3jwsv0lhfmndnlag3kp6avpcq6pkd3xy8e5r88",
          "amount": 140700041250000000,
          "asset_definition": {},
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "control_program": "00148c9d063ff74ee6d9ffa88d83aeb038068366c4c4",
          "id": "e3325bf07c4385af4b60ad6ecc682ee0773f9b96e1cfbbae9f0f12b86b5f1093",
          "position": 0,
          "type": "control"
        }
      ],
      "size": 151,
      "status_fail": false,
      "time_range": 0,
      "version": 1
    }
  ],
  "version": 1
}

Use the bytomcli client to query for information about blocks with height 0. We can see the output above.

  • bits: target value, the new block is built successfully if the hash calculated during mining is less than or equal to the target value
  • difficulty: Difficulty value, how hard it is for miners to find the next valid block. This parameter is not stored on the blockchain and is calculated from thebits
  • hash: current block hash
  • height: the height of the current block
  • nonce: random number, mining repeatedly uses different nonce to generate different hash values
  • previous_block_hash: The hash value of the parent of the current block
  • size: the number of bytes in the current block
  • timestamp: time of block out
  • transaction_merkle_root: root node of the merkle tree for the Genesis block
  • transactions: utxo transactions in the current block

The timestamp timestamp is 1524549600 at 2018-04-24 14:00:00 which is the time than the original chain was on the main network.

Source Code 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 the Bebop chain node, store. GetStoreStatus will get the store status from the db, the process of getting the store status is to query the data with key blockStore from LevelDB, if the query is wrong then it is considered to be the first time to run than the original chain node, then it needs to initialize than the original master chain.

Initializing the master 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)
}

Initializing the master chain has several operations.

  • config. GenesisBlock() to get the Genesis block
  • Set the status of all transactions in the Genesis block
  • Storing Genesis Blocks to LevelDB
  • state. NewUtxoViewpoint() is used to temporarily store a small portion of the utxo state collection
  • Instantiate BlockNode, which is used to select the best chain as the master chain
  • Save the latest master link status

The 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
}

The mainNetGenesisBlock has the following main operations.

  • Generate a transaction in the Genesis block, default on one transaction
  • Set the status of the transaction in the block to false
  • Set the genesis block as the root node of the merekle tree
  • Instantiate the Block and return

The genesisTx function generates the transactions in the genesis block, by default just one transaction, one transaction containing input input and output output.

input input.

There is a phrase in the input"Information is power. -- Jan/11/2013. Computing is power. -- Apr/24/2018." This is in honor ofAaron Swartz spirit of

output output.

In the output we see the consensus. InitialBlockSubsidy Genesis block rewards. Total 140700041250000000/1e8 = 1407000412. That's 1.4 billion BTM coins.

Calculation as power

To quote the founder of the Biogenesis chain, Chang Tongs.

On April 24, we went online on our main website, Information as Power, 2013 Jaruary11; Computing as Power, 2018 April24. The quote honors the spirit of Aaron Swartz, and Information as Power can be seen as an Internet manifesto dedicated to the free dissemination of information and allowing citizens' privacy to be protected. Computing is power, working to keep assets freely traded, freely flowing, and citizens' wealth protected, which I think is a very good meme.


Recommended>>
1、An 80page PowerPoint look at Blockchain from theory to practice
2、5294挖地雷
3、盘点五大趋势了解中国制造2025智能工厂
4、clown number
5、ofoTwo tricks in three days After the last honey sale This time the faintest move is not1 Hours were called off urgently

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号