Hyperledger Fabric Genesis block Source code Analysis

Source: Internet
Author: User
Tags hyperledger fabric


1.serve function in Genesis block start, call makegenesis function

Makegenesiserror: = Genesis. Makegenesis ()
    if makegenesiserror! = nil {
        return makegenesiserror
    }
2.MakeGenesis Create a Genesis block and add it to the blockchain
Func makegenesis () error {
    once. Do (func () {
        ledger, err: = Ledger. Getledger ()
        if err! = Nil {
            Makegenesiserror = Err
            return
        }

        if ledger. Getblockchainsize () = = 0 {
            genesislogger.info ("Creating Genesis block.")
            If Makegenesiserror = ledger. Begintxbatch (0); Makegenesiserror = = Nil {
                makegenesiserror = ledger. Committxbatch (0, Nil, nil, nil)}}
    )
    return Makegenesiserror
}
3.GetLedger gives a reference to a "single-column mode" Ledger
Func Getledger () (*ledger, error) {
    once. Do (func () {
        ledger, ledgererror = Getnewledger ()
    })
    return ledger, Ledgererror
}
3.1. Getnewledger give a reference to the new ledger
Func Getnewledger () (*ledger, error) {
    Blockchain, err: = Newblockchain ()
    if err! = Nil {
        return nil, err
  
    state

    : = state. NewState ()
    return &ledger{blockchain, state, nil}, nil
}

  
A. Starting with Newblockchain, a new blockchain is created, the entire process involves getting a database handle, fetching from the database, getting the hash of the chunk, and so on. B. Starting with newstate, construct a logical structure of a state tree to store things behind this logical structure 4.GetBlockchainSize Returns the number of blocks on the block chain
Func (Ledger *ledger) getblockchainsize () UInt64 {
    return ledger.blockchain.getSize ()
}
5. Gets the call when the current Transaction-batch needs to be committed. If the transaction details and status changes (which may occur during the execution of the transaction) are committed to the permanent storage, this function returns success, in this process involves writing blocks, writing blocks to the database, sending events and other mechanisms
Func (Ledger *ledger) Committxbatch (ID interface{}, transactions []*protos. Transaction, Transactionresults []*protos.
        Transactionresult, metadata []byte) error {err: = Ledger.checkvalididcommitorrollback (ID) if err! = Nil { Return err} Statehash, err: = Ledger.state.GetHash () if err! = Nil {Ledger.resetfornexttxgroup (FAL SE) Ledger.blockchain.blockPersistenceStatus (false) return err} writebatch: = Gorocksdb. Newwritebatch () defer Writebatch.destroy () Block: = Protos. Newblock (transactions, metadata) Ccevents: = []*protos. chaincodeevent{} if Transactionresults! = Nil {ccevents = make ([]*protos. Chaincodeevent, Len (transactionresults)) for I: = 0; I < Len (transactionresults); i++ {if transactionresults[i]. Chaincodeevent! = Nil {Ccevents[i] = Transactionresults[i]. Chaincodeevent} else {ccevents[i] = &protos.
    chaincodeevent{}        }}}//The Chaincode event is stored directly in the Nonhashdata. This may change in the new consensus, and we can move it to the trading block. Nonhashdata = &protos. Nonhashdata{chaincodeevents:ccevents} newblocknumber, err: = Ledger.blockchain.addPersistenceChangesForNewBlock ( Context. TODO (), block, Statehash, writebatch) if err! = Nil {Ledger.resetfornexttxgroup (false) Ledger.blockch Ain.blockpersistencestatus (FALSE) return err} ledger.state.AddChangesForPersistence (Newblocknumber, write Batch) Opt: = Gorocksdb. Newdefaultwriteoptions () defer opt. Destroy () Dberr: = db. Getdbhandle (). Db. Write (OPT, writebatch) if dberr! = Nil {Ledger.resetfornexttxgroup (false) Ledger.blockchain.blockPers Istencestatus (False) return Dberr} ledger.resetfornexttxgroup (True) Ledger.blockchain.blockPersisten Cestatus (True) sendproducerblockevent (block)//send Chaincode events from transaction results//Send chain code from trading results Event Sendchaincodeevents (TransacTionresults) If Len (transactionresults)! = 0 {Ledgerlogger.debug ("there were some erroneous transactions.
    We need to send a ' TX rejected ' message here. ')}
 Return nil}

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.