兄弟連區塊鏈教程分享區塊鏈POW證明程式碼實現demo
兄弟連區塊鏈教程分享區塊鏈POW證明程式碼實現demo,2018年下半年,區塊鏈行業正逐漸褪去發展之初的浮躁、迴歸理性,表面上看相關人才需求與身價似乎正在回落。但事實上,正是初期泡沫的漸退,讓人們更多的關注點放在了區塊鏈真正的技術之上。
這裡強調一下區塊鏈的協議分層
· 應用層
· 合約層
· 激勵機制
· 共識層
· 網路層
· 資料層
上 一篇主要實現了區塊鏈的 資料層,資料層主要使用的技術就是對資料的校驗,求hash。
這裡介紹工作量證明POW, POW是屬於共識機制的內容。
PoW機制中根據礦工的工作量來執行貨幣的分配和記賬權的確定。算力競爭的勝者將獲得相應區塊記賬權和比特幣獎勵。因此,礦機晶片的算力越高,挖礦的時間更長,就可以獲得更多的數字貨幣。
優點:
演算法簡單,容易實現;節點間無需交換額外的資訊即可達成共識;破壞系統需要投入極大的成本。
缺點:
浪費能源;區塊的確認時間難以縮短;新的區塊鏈必須找到一種不同的雜湊演算法,否則就會面臨比特幣的算力攻擊;容易產生分叉,需要等待多個確認;永遠沒有最終性,需要檢查點機制來彌補最終性。
目前基於PoW共識機制的數字貨幣有很多,比特幣、萊特幣、狗狗幣、達士幣、門羅幣等初期的數字貨幣大多都是PoW共識機制。
其他的共識機制還有
PoS(Proof of Stake)
DPOS(Delegated Proof-of-Stake)
DAG(Directed acyclic graph)
PBFT(Practical Byzantine Fault Tolerance)
Pool驗證池
dBFT(delegated BFT)
PoA(Proof-of-Authority)
RPCA(Ripple Protocol consensus algorithm)
Hcash——PoW+PoS共識機制
這些共識機制,後面有時間會補充上的,今天主要介紹POW
pow很簡單,原理就是 利用計算力,在選擇一個nonce的值結合區塊的資料算出hash,使得hash的前面多少位都是0.
nonce是一個用來找到滿足條件的hash值的數字,nonce值一直迭代,直到hash值有效為止。在我們案例中一個有效的hash值是最少有4個前導0。找到nonce值以滿足合適條件的hash值的過程就叫做挖礦。
下面給出程式碼:
golang版
package main
import (
"bytes"
"crypto/sha256"
"fmt"
"math"
"math/big"
)
// 前導0,難度
const targetBits = 8
type ProofOfWork struct {
block *Block
targetBit *big.Int
}
func NewProofOfWork(block *Block) *ProofOfWork {
// 設定64位全1
var IntTarget = big.NewInt(1)
//00000000000000000000000000001
//10000000000000000000000000000
//00000000000100000000000000000
//0000001
// 右移 targetBits位
IntTarget.Lsh(IntTarget, uint(256 - targetBits))
return &ProofOfWork{block:block, targetBit:IntTarget}
}
func (pow *ProofOfWork)PrepareRawData(nonce int64)[]byte {
block := pow.block
tmp := [][]byte{
Int2Byte(block.Version),
block.PrevBlockHash,
Int2Byte(block.TimeStamp),
block.MerkeRoot,
Int2Byte(nonce),
Int2Byte(targetBits),
block.Data}
data := bytes.Join(tmp, []byte{})
return data
}
func (pow *ProofOfWork)Run() (int64, []byte) {
var nonce int64
var hash [32]byte
var HashInt big.Int
fmt.Printf("target hash:", pow.targetBit.Bytes())
for nonce < math.MaxInt64 {
data := pow.PrepareRawData(nonce)
hash = sha256.Sum256(data)
HashInt.SetBytes(hash[:])
//fmt.Println(nonce)
// 這裡用於 判斷算出的hash值(int)只要比最大的IntTarget小就是正確的。
if HashInt.Cmp(pow.targetBit) == -1 {
fmt.Printf("Found Hash: %x\n", hash)
break
} else {
nonce++
}
}
return nonce, hash[:]
}
// 對block的資料校驗
func (pow *ProofOfWork)IsVaild() bool {
data := pow.PrepareRawData(pow.block.Nonce)
hash := sha256.Sum256(data)
var IntHash big.Int
IntHash.SetBytes(hash[:])
return IntHash.Cmp(pow.targetBit) == -1
}
python版
function isValidHashDifficulty(hash, difficulty) {
for (var i = 0, b = hash.length; i < b; i ++) {
if (hash[i] !== '0') {
break;
}
}
return i >= difficulty;
}
import hashlib
"""
工作量證明
"""
class ProofofWork():
"""
pow
"""
def __init__(self, block):
self.block = block
def mine(self):
"""
挖礦函式
:return:
"""
i = 0
prefix = '0000'
while True:
nonce = str(i)
message = hashlib.sha256()
message.update(str(self.block.data).encode('utf-8'))
message.update(nonce.encode("utf-8"))
digest = message.hexdigest()
if digest.startswith(prefix):
return nonce, digest