Kamoo ke entseng li-blocks le transactions ho Go blockchain ea ka

E le hore qetellong re qetelle re e-na le blockchain eseng feela database, re hloka ho kenyelletsa lintlha tsa bohlokoa tsa 3 morerong oa rona:

  • Tlhaloso ea block data sebopeho le mekhoa
  • Tlhaloso ea sebopeho sa data le mekhoa ea transaction
  • Mesebetsi ea Blockchain e bolokang li-blocks sebakeng sa polokelo ea litaba le ho li fumana moo ka hashe kapa bophahamo ba tsona (kapa ntho e 'ngoe).

Kamoo ke entseng li-blocks le transactions ho Go blockchain ea ka

Ena ke sengoloa sa bobeli mabapi le blockchain bakeng sa indasteri, ea pele mona.

Ho hopola lipotso tseo babali ba ileng ba mpotsa tsona ka sehlooho se fetileng letotong lena, ho lokela ho hlokomeloa: tabeng ena, database ea LevelDB e sebelisetsoa ho boloka data ea blockchain, empa ha ho letho le u thibelang ho sebelisa leha e le efe e 'ngoe, e re, MySQL. Joale a re shebeng sebopeho sa data ena.

Ha re qale ka li-transaction: github.com/Rusldv/bcstartup/blob/master/transaction/builder.go

Sebopeho sa eona ke sena:

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 e boloka mofuta oa data (bakeng sa transaction 2), hash ea transaction eo, mofuta oa transaction ka boeona, setempe sa nako, le lintho tse kenang le tse hlahisoang. Lits'oants'o tsa TxIn li boloka hash ea transaction eo tlhahiso ea eona e bonts'itsoeng, palo ea tlhahiso ena le bytecode, 'me liphetho tsa TxOut li boloka boleng bo itseng le bytecode.

Joale a re boneng hore na ke liketso life tseo transaction e ka li etsang ho data ea eona, ke hore. A re shebeng mekhoa.

Ho etsa transaction, sebelisa transaction.NewTransaction(txtype byte) *TX function.

Mokhoa oa AddTxIn(thattxhash []byte, txoutn int, code []byte) (*TxIn, error) o kenya letsoho thekeng.

Mokhoa oa AddTxOut(value int, data []byte) (*TxOut, error) o eketsa tlhahiso ho transaction.

Mokhoa oa ToBytes() []byte o fetola khoebo hore e be selae sa byte.

Ts'ebetso ea kahare ea preByteHash(byte []byte) thapo e sebelisoa ho Build() le Check() ho etsa hore transaction hash e hlahisitsoeng e tsamaisane le transaction hashes e hlahisoang ho tsoa lits'ebetsong tsa JavaScript.

Mokhoa oa Build () o beha hash ea transaction ka tsela e latelang: tx.TxHash = preByteHash(tx.ToBytes()).

Mokhoa oa likhoele oa ToJSON() o fetolela transaction hore e be khoele ea JSON.

Mokhoa oa phoso oa FromJSON(data []byte) o kenya transaction ho tsoa ho fomete ea JSON e fetisitsoeng joalo ka selae sa byte.

Mokhoa oa ho hlahloba () bool o bapisa hashe e hlahang sebakeng sa transaction hash le hashe e fumanoeng ka lebaka la ho hashing transaction ena (ho iphapanyetsa sebaka sa hash).

Litšebelisano li eketsoa ho block: github.com/Rusldv/bcstartup/blob/master/block/builder.go

Sebopeho sa data ea block se matla le ho feta:

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 e boloka mofuta oa data, node e e sebelisa mme e khetholla thibela ho tloha transaction kapa data e 'ngoe. Bakeng sa block, boleng bona ke 1.

BlockHeight e boloka bolelele ba boloko.
Setempe sa nako sa nako.
HeaderSize ke boholo ba li-block ka li-byte.
PrevBlockHash ke hashe ea boloko e fetileng, 'me SelfBlockHash ke hashe ea hona joale.
TxsHash ke hash e akaretsang ea litšebelisano.
MerkleRoot ke motso oa sefate sa Merkle.

Ho feta moo masimong ho na le senotlolo sa sechaba sa moetsi oa block, signature ea moetsi, mofuta oa block, palo ea litšebelisano ho block, le litšebelisano tsena ka botsona.

Ha re shebe mekhoa ea eona:
Ho theha block, sebelisa block.NewBlock() function: NewBlock(prevBlockHash string, height int) *Block, e nkang hashe ea boloko bo fetileng le bophahamo bo behiloeng bakeng sa boloko bo entsoeng ho blockchain. Mofuta oa block o boetse o behiloe ho tsoa ho mefuta ea sephutheloana kamehla:

b.DataType = types.BLOCK_TYPE.

Mokhoa oa AddTx(tx *transaction.TX) o eketsa transaction ho block.

Mokhoa oa Build () o kenya litekanyetso masimong a block mme o hlahisa le ho beha hash ea eona ea hajoale.

Mokhoa oa ToBytesHeader() []byte o fetola sehlooho sa block (ntle le litšebelisano) ho selae sa byte.

Mokhoa oa likhoele oa ToJSON () o fetolela block hore e be sebopeho sa JSON ka sesupo sa likhoele tsa data.

Mokhoa oa phoso oa FromJSON(data []byte) o kenya data ho tsoa ho JSON ho ea ho sebopeho sa block.

Mokhoa oa Check () bool o hlahisa block hash mme o e bapisa le e boletsoeng lebaleng la block hash.

Mokhoa oa khoele oa GetTxsHash () o khutlisa kakaretso ea hash ea litšebelisano tsohle tse block.

Mokhoa oa GetMerkleRoot() o totobatsa motso oa sefate sa Merkle bakeng sa litšebelisano ka bolokong.

Mokhoa oa Sign(privk string) o saena boloko ka senotlolo sa lekunutu sa moetsi oa block.

Mokhoa oa SetHeight (height int) o ngola bolelele ba boloko ho lebaleng la sebopeho sa block.

Mokhoa oa GetHeight () int o khutlisetsa bophahamo ba boloko joalo ka ha ho boletsoe tšimong e tsamaellanang ea sebopeho sa block.

Mokhoa oa ToGOBBytes() []byte o kopanya boloko ka sebopeho sa GOB ebe o o khutlisa joalo ka selae sa byte.

Mokhoa oa phoso oa FromGOBBytes(data []byte) o ngola data ea block ho sebopeho sa block ho tloha ho selae sa byte se fetisitsoeng ka sebopeho sa GOB.

Mokhoa oa likhoele oa GetHash () o khutlisa hash ea block e fanoeng.

Mokhoa oa likhoele oa GetPrevHash () o khutlisa hash ea block e fetileng.

Mokhoa oa SetPublicKey(pubk string) o ngola senotlolo sa sechaba sa moetsi oa block ho thibela.

Ka hona, ka ho sebelisa mekhoa ea ntho ea Block, re ka e fetola habonolo ka mokhoa oa ho fetisetsa marang-rang le ho boloka ho database ea LevelDB.

Mesebetsi ea sephutheloana sa blockchain e ikarabella ho boloka ho blockchain: github.com/Rusldv/bcstartup/tree/master/blockchain

Ho etsa sena, block e tlameha ho kenya tšebetsong sebopeho sa IBlock:

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

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

}

Khokahano ea database e entsoe hang ha sephutheloana se qalisoa mosebetsing oa init():

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

CloseDB () ke wrapper bakeng sa db.Cloce () - e bitsoa ka mor'a ho sebetsa le mesebetsi ea sephutheloana ho koala khokahanyo ho database.

Mosebetsi oa phoso oa SetTargetBlockHash(hash string) o ngola hash ea block ea hona joale ka senotlolo se boletsoeng ke BLOCK_HASH kamehla ho database.

Mosebetsi oa GetTargetBlockHash () (khoele, phoso) o khutlisa hash ea boloko ba hajoale bo bolokiloeng polokelong ea litaba.

Mosebetsi oa phoso oa SetTargetBlockHeight(height int) o ngolla database boleng ba bophahamo ba blockchain bakeng sa node ka senotlolo se boletsoeng ke BLOCK_HEIGHT kamehla.

Mosebetsi oa GetTargetBlockHeight () (int, error) o khutlisetsa bophahamo ba blockchain bakeng sa node e fanoeng, e bolokiloeng polokelong ea litaba.

Ts'ebetso ea bool ea CheckBlock(block IBlock) e lekola block bakeng sa ho nepahala pele o eketsa boloko bona ho blockchain.

Mosebetsi oa phoso oa AddBlock(block IBlock) o eketsa thibelo ho blockchain.

Mesebetsi ea ho khutlisa le ho shebella li-block e ho faele ea explore.go ea sephutheloana sa blockchain:

Mosebetsi oa GetBlockByHash(hash string) (*block.Block, error) o etsa ntho e se nang letho, e jara thibela ho eona ho tloha ho database, eo hash e fetiselitsoeng ho eona, ebe e khutlisetsa pointer ho eona.

Ho bōptjoa ha genesis block ho etsoa ke Genese () mosebetsi oa phoso ho tloha faeleng ea genesis.go ea sephutheloana sa blockchain.

Sengoloa se latelang se tla bua ka ho hokahanya bareki ho node ho sebelisa mochini oa WebSocket.

Source: www.habr.com

Eketsa ka tlhaloso