2.1 ProofOfWork框架
我們在區(qū)塊中添加一個屬性Nonce來表示區(qū)塊的生成難度,它是區(qū)塊生成的一個重要條件,Nonce值越高,代表生成區(qū)塊的難度越大,通過這種難度從而避免區(qū)塊隨意生成,工作量證明則是要完成這一系列難度區(qū)塊生產所需要的工作量
/Users/xxx/go/src/publicChain/part5-Basic-Prototype/BLC/Block.go文章來源:http://www.zghlxwxcb.cn/news/detail-436459.html
type Block struct {
//1.block height
Height int64
//2.the last block's hash
PreBlockHash []byte
//3.transaction data
Data []byte
//4.timestamp
Timestamp int64
//5.block's hash
Hash []byte
//6.Nonce
Nonce int64
}
當然生成區(qū)塊的函數(shù)也要改一下
/Users/xxx/go/src/publicChain/part5-Basic-Prototype/BLC/Block.go
func Newblock(height int64, preBlockHash []byte, data string) *Block {
//1.crate a new block
block := &Block{height, preBlockHash, []byte(data), time.Now().Unix(), nil, 0}
//2.calling the proof-of-work method returns Hash and Nonce
pow := NewProofOfWork(block)
hash, nonce := pow.Run()
block.Hash = hash[:]
block.Nonce = nonce
return block
}
我們通過工作量證明來生成區(qū)塊的哈希和Nonce文章來源地址http://www.zghlxwxcb.cn/news/detail-436459.html
//2.calling the proof-of-work method returns Hash and Nonce
pow := NewProofOfWork(block)
hash, nonce := pow.Run()
block.Hash = hash[:]
block.Nonce = nonce
到了這里,關于區(qū)塊鏈項目 - 2 工作量證明的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!