1. 程式人生 > >兄弟連區塊鏈入門教程分享區塊鏈POW證明代碼實現demo

兄弟連區塊鏈入門教程分享區塊鏈POW證明代碼實現demo

fault dem val == 否則 缺點 ESS lang pos

這裏強調一下區塊鏈的協議分層
?應用層
?合約層
?激勵機制
?共識層
?網絡層
?數據層
上 一篇主要實現了區塊鏈的 數據層,數據層主要使用的技術就是對數據的校驗,求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
            i += 1

兄弟連區塊鏈入門教程分享區塊鏈POW證明代碼實現demo