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}