2.1 ProofOfWork框架
我们在区块中添加一个属性Nonce来表示区块的生成难度,它是区块生成的一个重要条件,Nonce值越高,代表生成区块的难度越大,通过这种难度从而避免区块随意生成,工作量证明则是要完成这一系列难度区块生产所需要的工作量
/Users/xxx/go/src/publicChain/part5-Basic-Prototype/BLC/Block.go
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
}
当然生成区块的函数也要改一下
/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
}
我们通过工作量证明来生成区块的哈希和Nonce
//2.calling the proof-of-work method returns Hash and Nonce
pow := NewProofOfWork(block)
hash, nonce := pow.Run()
block.Hash = hash[:]
block.Nonce = nonce