Yuav ua li cas kuv tsim blocks thiab muas hauv kuv mus blockchain

Txhawm rau kom xaus nrog lub blockchain thiab tsis yog cov ntaub ntawv xwb, peb yuav tsum ntxiv 3 lub ntsiab lus tseem ceeb rau peb qhov project:

  • Kev piav qhia ntawm cov ntaub ntawv thaiv cov qauv thiab cov txheej txheem
  • Kev piav qhia ntawm cov qauv ntaub ntawv thiab kev hloov pauv
  • Blockchain ua haujlwm uas txuag cov blocks hauv cov ntaub ntawv thiab nrhiav lawv nyob ntawd los ntawm lawv cov hash lossis qhov siab (lossis lwm yam).

Yuav ua li cas kuv tsim blocks thiab muas hauv kuv mus blockchain

Nov yog tsab xov xwm thib ob hais txog blockchain rau kev lag luam, thawj S, SΡ“S,.

Nco ntsoov cov lus nug uas cov neeg nyeem tau nug kuv txog tsab xov xwm dhau los hauv koob no, nws yuav tsum raug sau tseg: qhov no, LevelDB database yog siv los khaws cov ntaub ntawv blockchain, tab sis tsis muaj dab tsi tiv thaiv koj los ntawm kev siv lwm yam, hais, MySQL. Tam sim no cia saib cov qauv ntawm cov ntaub ntawv no.

Cia peb pib nrog kev hloov pauv: github.com/Rusldv/bcstartup/blob/master/transaction/builder.go

Ntawm no yog nws cov ntaub ntawv qauv:

type TX struct {
	DataType byte		
	TxHash string 
	TxType byte	
	Timestamp int64		
	INs []TxIn
	OUTs []TxOut
}

type TxIn struct {
	ThatTxHash string
	TxOutN int
	ByteCode string
}

type TxOut struct {
	Value int
	ByteCode string
}

TX khaws cov ntaub ntawv hom (rau kev hloov pauv 2), qhov hash ntawm qhov kev hloov pauv, hom kev hloov pauv nws tus kheej, lub sijhawm, thiab cov khoom siv thiab cov khoom tawm. TxIn inputs khaws cov hash ntawm kev sib pauv uas nws cov zis tau hais txog, tus lej ntawm cov zis no thiab bytecode, thiab TxOut outputs khaws qee tus nqi thiab tseem bytecode.

Tam sim no cia saib dab tsi ua kev lag luam tuaj yeem ua ntawm nws cov ntaub ntawv, i.e. Cia peb saib cov txheej txheem.

Txhawm rau tsim kev lag luam, siv qhov kev sib pauv.NewTransaction(txtype byte) *TX muaj nuj nqi.

The AddTxIn(thattxhash []byte, txoutn int, code []byte) (*TxIn, error) txoj kev ntxiv cov tswv yim rau kev pauv.

Tus AddTxOut(tus nqi int, cov ntaub ntawv []byte) (*TxOut, yuam kev) txoj kev ntxiv cov khoom tso tawm rau kev lag luam.

Tus ToBytes() []byte txoj kev hloov pauv mus rau hauv ib daim byte.

Cov kev ua haujlwm sab hauv preByteHash(bytes []byte) txoj hlua yog siv hauv Build() thiab Check() kom ua rau cov khoom lag luam hash sib xws nrog kev sib pauv hashs tsim los ntawm JavaScript daim ntawv thov.

The Build() method sets the transaction hash as below: tx.TxHash = preByteHash(tx.ToBytes()).

Txoj Kev ToJSON() txoj hlua hloov pauv kev hloov pauv mus rau JSON txoj hlua.

Lub FromJSON(cov ntaub ntawv []byte) yuam kev txoj kev thauj khoom los ntawm JSON hom dhau los ua ib daim byte.

Txoj kev Check() bool piv cov txiaj ntsig hash los ntawm kev sib pauv hash teb nrog cov hash tau los ntawm kev hashing qhov kev pauv no (tsis quav ntsej hash teb).

Kev ua lag luam raug ntxiv rau qhov thaiv: github.com/Rusldv/bcstartup/blob/master/block/builder.go

Cov ntaub ntawv thaiv cov qauv yog ntau voluminous:

type Block struct {
	DataType byte				
	BlockHeight int					
        Timestamp int64				 
        HeaderSize int					
        PrevBlockHash string				 
        SelfBlockHash string			
	TxsHash string			
	MerkleRoot string
	CreatorPublicKey string			
	CreatorSig string
	Version int
	TxsN int
	Txs []transaction.TX
}

DataType khaws cov ntaub ntawv hom, cov node siv nws thiab txawv qhov thaiv los ntawm kev sib pauv lossis lwm yam ntaub ntawv. Rau ib qho thaiv no tus nqi yog 1.

BlockHeight khaws qhov siab ntawm qhov thaiv.
Timestamp sij hawm.
HeaderSize yog qhov block loj hauv bytes.
PrevBlockHash yog qhov hash ntawm qhov thaiv dhau los, thiab SelfBlockHash yog qhov hash ntawm tam sim no.
TxsHash yog ib qho kev lag luam dav dav.
MerkleRoot yog lub hauv paus ntawm tsob ntoo Merkle.

Tsis tas li ntawd nyob rau hauv lub teb muaj cov pej xeem tseem ceeb ntawm tus creator ntawm lub block, kos npe ntawm tus creator, lub version ntawm lub block, tus naj npawb ntawm muas nyob rau hauv lub block, thiab cov muas lawv tus kheej.

Cia peb saib nws cov txheej txheem:
Txhawm rau tsim ib qho thaiv, siv qhov block.NewBlock() muaj nuj nqi: NewBlock(prevBlockHash hlua, qhov siab int) * Thaiv, uas yuav siv lub hash ntawm lub block dhau los thiab qhov siab teem rau cov tsim thaiv hauv blockchain. Hom thaiv kuj tau teeb tsa los ntawm hom pob tas li:

b.DataType = types.BLOCK_TYPE.

Txoj kev AddTx(tx *transaction.TX) ntxiv kev sib pauv rau ib qho thaiv.

Build() method loads values ​​into the block's fields and generates and sets its current hash.

Tus ToBytesHeader() []byte txoj kev hloov cov block header (tsis muaj kev hloov pauv) rau hauv ib daim byte.

ToJSON() txoj hlua txoj kev hloov lub thaiv rau JSON hom ntawv hauv ib txoj hlua sawv cev ntawm cov ntaub ntawv.

Lub FromJSON(cov ntaub ntawv []byte) yuam kev txoj kev thauj cov ntaub ntawv los ntawm JSON rau hauv cov qauv thaiv.

Txoj kev Check() bool tsim ib qho thaiv thaiv thiab muab piv nrog ib qho uas tau teev tseg hauv block hash teb.

Txoj kev GetTxsHash() txoj hlua xa rov qab tag nrho hash ntawm tag nrho cov kev lag luam hauv qhov thaiv.

Txoj kev GetMerkleRoot() qhia txog lub hauv paus ntawm tsob ntoo Merkle rau kev lag luam hauv ib qho thaiv.

Txoj Kev Kos Npe (privk hlua) txoj kev kos npe rau thaiv nrog tus yuam sij ntiag tug ntawm tus tsim thaiv.

Txoj kev SetHeight(height int) sau qhov siab ntawm qhov thaiv mus rau qhov thaiv cov qauv teb.

Txoj kev GetHeight() int rov qab qhov siab ntawm qhov thaiv raws li tau teev tseg hauv qhov sib thooj ntawm cov qauv thaiv.

ToGOBBytes() []byte txoj kev encodes ib qho thaiv hauv GOB hom ntawv thiab xa rov qab los ua ib daim byte.

Txoj kev yuam kev FromGOBBytes (cov ntaub ntawv []byte) sau cov ntaub ntawv thaiv rau cov qauv thaiv los ntawm cov hla byte hlais hauv GOB hom.

Txoj kev GetHash() txoj hlua xa rov qab hash ntawm qhov muab thaiv.

Txoj kev GetPrevHash() txoj hlua rov qab hash ntawm qhov thaiv dhau los.

Txoj kev SetPublicKey(pubk hlua) txoj kev sau tus yuam sij pej xeem ntawm tus tsim thaiv rau lub thaiv.

Yog li, siv cov txheej txheem ntawm Block object, peb tuaj yeem hloov pauv tau yooj yim rau hauv hom kev sib kis hauv lub network thiab txuag rau LevelDB database.

Lub luag haujlwm ntawm lub pob blockchain yog lub luag haujlwm rau txuag rau blockchain: github.com/Rusldv/bcstartup/tree/master/blockchain

Txhawm rau ua qhov no, qhov thaiv yuav tsum siv IBlock interface:

type IGOBBytes interface {
	ToGOBBytes() []byte
	FromGOBBytes(data []byte) error
}

type IBlock interface {
	IGOBBytes
	GetHash() string
	GetPrevHash() string
	GetHeight() int
	Check() bool

}

Qhov kev sib txuas database yog tsim ib zaug thaum lub pob pib pib hauv init() muaj nuj nqi:

db, err = leveldb.OpenFile(BLOCKCHAIN_DB_DEBUG, nil).

CloseDB() yog wrapper rau db.Cloce() - hu ua tom qab ua hauj lwm nrog lub pob functions kaw qhov kev twb kev txuas mus rau lub database.

Lub SetTargetBlockHash(hash txoj hlua) yuam kev muaj nuj nqi sau lub hash ntawm tam sim no thaiv nrog tus yuam sij teev los ntawm BLOCK_HASH tas li mus rau hauv cov ntaub ntawv.

Lub GetTargetBlockHash() (txoj hlua, yuam kev) muaj nuj nqi rov qab cov hash ntawm tam sim no thaiv khaws cia hauv cov ntaub ntawv.

Lub SetTargetBlockHeight(qhov siab int) yuam kev ua haujlwm sau rau hauv cov ntaub ntawv tus nqi ntawm blockchain qhov siab rau ntawm lub pob nrog tus yuam sij teev los ntawm BLOCK_HEIGHT tas li.

Lub GetTargetBlockHeight() (int, yuam kev) muaj nuj nqi rov qab qhov siab ntawm blockchain rau ib qho muab, khaws cia hauv cov ntaub ntawv.

Lub CheckBlock (block IBlock) bool muaj nuj nqi kuaj xyuas qhov thaiv kom raug ua ntej ntxiv qhov thaiv no rau blockchain.

AddBlock (block IBlock) yuam kev muaj nuj nqi ntxiv ib qho thaiv rau blockchain.

Cov haujlwm rau kev khaws thiab saib cov blocks yog nyob rau hauv cov ntaub ntawv explore.go ntawm pob blockchain:

Lub GetBlockByHash(hash hlua) (*block.Block, yuam kev) muaj nuj nqi tsim ib qho khoom thaiv khoob, thauj khoom thaiv rau hauv nws los ntawm cov ntaub ntawv, cov hash uas tau dhau mus rau nws, thiab xa rov qab tus taw tes rau nws.

Lub creation ntawm ib tug genesis block yog nqa tawm los ntawm lub Genesis() yuam kev muaj nuj nqi los ntawm lub genesis.go ntaub ntawv ntawm lub blockchain pob.

Cov tshooj tom ntej no yuav tham txog kev sib txuas cov neeg siv khoom mus rau qhov node siv WebSocket mechanism.

Tau qhov twg los: www.hab.com

Ntxiv ib saib