使用 go 实现 PoW(Proof-of-Work)共识机制

使用 go 实现 Proof-of-Work 共识机制

什么是 Proof-of-Work

Proof-of-Work 简称 PoW,即为工作量证明。通过计算一个数值( nonce ),使得拼揍上交易数据后内容的 Hash 值满足规定的上限。在节点成功找到满足的Hash值之后,会马上对全网进行广播打包区块,网络的节点收到广播打包区块,会立刻对其进行验证。

如果验证通过,则表明已经有节点成功解迷,自己就不再竞争当前区块打包,而是选择接受这个区块,记录到自己的账本中,然后进行下一个区块的竞争猜谜。
网络中只有最快解谜的区块,才会添加的账本中,其他的节点进行复制,这样就保证了整个账本的唯一性。

假如节点有任何的作弊行为,都会导致网络的节点验证不通过,直接丢弃其打包的区块,这个区块就无法记录到总账本中,作弊的节点耗费的成本就白费了,因此在巨大的挖矿成本下,也使得矿工自觉自愿的遵守比特币系统的共识协议,也就确保了整个系统的安全。

工作量证明的优缺点

优点:完全去中心化,节点自由进出;

缺点:目前bitcoin已经吸引全球大部分的算力,其它再用Pow共识机制的区块链应用很难获得相同的算力来保障自身的安全;挖矿造成大量的资源浪费;共识达成的周期较长,不适合商业应用

工作量证明的简单的例子

举个例子,给定的一个基本的字符串”Hello, world!”,我们给出的工作量要求是,可以在这个字符串后面添加一个叫做nonce的整数值,对变更后(添加nonce)的字符串进行SHA256哈希运算,如果得到的哈希结果(以16进制的形式表示)是以”0000”开头的,则验证通过。为了达到这个工作量证明的目标。我们需要不停的递增nonce值,对得到的新字符串进行SHA256哈希运算。按照这个规则,我们需要经过4251次计算才能找到恰好前4位为0的哈希散列。

"Hello, world!0" => 1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64
"Hello, world!1" => e9afc424b79e4f6ab42d99c81156d3a17228d6e1eef4139be78e948a9332a7d8
"Hello, world!2" => ae37343a357a8297591625e7134cbea22f5928be8ca2a32aa475cf05fd4266b7
...
"Hello, world!4248" => 6e110d98b388e77e9c6f042ac6b497cec46660deef75a55ebc7cfdf65cc0b965
"Hello, world!4249" => c004190b822f1669cac8dc37e761cb73652e7832fb814565702245cf26ebb9e6
"Hello, world!4250" => 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9

实现 Proof-of-Work 共识机制

安装依赖软件

$ go get github.com/davecgh/go-spew/spew

$ go get github.com/gorilla/mux

$ go get github.com/joho/godotenv
  • spew 在控制台中格式化输出相应的结果。

  • gorilla/mux 是编写web处理程序的流行软件包。

  • godotenv 可以从我们项目的根目录的 .env 文件中读取数据。

实现 PoW 共识机制

新建 .env ,添加 ADDR=8080
新建 main.go,引入相应的包

package main

import (
        "crypto/sha256"
        "encoding/hex"
        "encoding/json"
        "fmt"
        "io"
        "log"
        "net/http"
        "os"
        "strconv"
        "strings"
        "sync"
        "time"

        "github.com/davecgh/go-spew/spew"
        "github.com/gorilla/mux"
        "github.com/joho/godotenv"
)

定义区块

const difficulty = 1

type Block struct {
        Index      int
        Timestamp  string
        BPM        int
        Hash       string
        PrevHash   string
        Difficulty int
        Nonce      string
}

var Blockchain []Block

type Message struct {
        BPM int
}

var mutex = &sync.Mutex{}
  • difficulty 代表难度系数,如果赋值为 1,则需要判断生成区块时所产生的 Hash 前缀至少包含1个 0
  • Block 代表区块的结构体。

    • Index 是区块链中数据记录的位置

    • Timestamp 是自动确定的,并且是写入数据的时间

    • BPM 是每分钟跳动的次数,是你的脉率

    • Hash 是代表这个数据记录的SHA256标识符

    • PrevHash 是链中上一条记录的SHA256标识符

    • Difficulty 是当前区块的难度系数
    • Nonce 是 PoW 挖矿中符合条件的数字
  • Blockchain 是存放区块数据的集合
  • Message 是使用 POST 请求传递的数据
  • mutex 是为了防止同一时间产生多个区块

生成区块

func generateBlock(oldBlock Block, BPM int) Block {
        var newBlock Block

        t := time.Now()

        newBlock.Index = oldBlock.Index + 1
        newBlock.Timestamp = t.String()
        newBlock.BPM = BPM
        newBlock.PrevHash = oldBlock.Hash
        newBlock.Difficulty = difficulty

        for i := 0; ; i++ {
                hex := fmt.Sprintf("%x", i)
                newBlock.Nonce = hex
                if !isHashValid(calculateHash(newBlock), newBlock.Difficulty) {
                        fmt.Println(calculateHash(newBlock), " do more work!")
                        time.Sleep(time.Second)
                        continue
                } else {
                        fmt.Println(calculateHash(newBlock), " work done!")
                        newBlock.Hash = calculateHash(newBlock)
                        break
                }

        }
        return newBlock
}

newBlock 中的 PrevHash 存储的上一个区块的 Hash

for 循环 通过循环改变 Nonce,然后选出符合相应难度系数的 Nonce

isHashValid 判断 hash,是否满足当前的难度系数。如果难度系数是2,则当前hash的前缀有2个0。

func isHashValid(hash string, difficulty int) bool {
        prefix := strings.Repeat("0", difficulty)
        return strings.HasPrefix(hash, prefix)
}
  • strings.Repeat("0", difficulty) 复制 difficulty0,并返回新字符串,当 difficulty 为 2 ,则 prefix 为 00

  • strings.HasPrefix(hash, prefix) 判断字符串 hash 是否包含前缀 prefix

calculateHash 根据设定的规则,生成 Hash 值。

func calculateHash(block Block) string {
        record := strconv.Itoa(block.Index) + block.Timestamp + strconv.Itoa(block.BPM) + block.PrevHash + block.Nonce
        h := sha256.New()
        h.Write([]byte(record))
        hashed := h.Sum(nil)
        return hex.EncodeToString(hashed)
}

验证区块

我们通过检查 Index 来确保它们按预期递增。我们也检查以确保我们 PrevHash 的确与 Hash 前一个区块相同。最后,我们希望通过在当前块上 calculateHash 再次运行该函数来检查当前块的散列。让我们写一个 isBlockValid 函数来完成所有这些事情并返回一个 bool

func isBlockValid(newBlock, oldBlock Block) bool {
        if oldBlock.Index+1 != newBlock.Index {
                return false
        }

        if oldBlock.Hash != newBlock.PrevHash {
                return false
        }

        if calculateHash(newBlock) != newBlock.Hash {
                return false
        }

        return true
}

web 服务器

func run() error {
        mux := makeMuxRouter()
        httpAddr := os.Getenv("ADDR")
        log.Println("Listening on ", os.Getenv("ADDR"))
        s := &http.Server{
                Addr:           ":" + httpAddr,
                Handler:        mux,
                ReadTimeout:    10 * time.Second,
                WriteTimeout:   10 * time.Second,
                MaxHeaderBytes: 1 << 20,
        }

        if err := s.ListenAndServe(); err != nil {
                return err
        }

        return nil
}

func makeMuxRouter() http.Handler {
        muxRouter := mux.NewRouter()
        muxRouter.HandleFunc("/", handleGetBlockchain).Methods("GET")
        muxRouter.HandleFunc("/", handleWriteBlock).Methods("POST")
        return muxRouter
}

makeMuxRouter 主要定义路由处理,当收到 GET 请求,就会调用 handleGetBlockchain 方法。当收到 POST 请求,就会调用 handleWriteBlock 方法。

handleGetBlockchain 获取所有区块的列表信息。

func handleGetBlockchain(w http.ResponseWriter, r *http.Request) {
        bytes, err := json.MarshalIndent(Blockchain, "", "  ")
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }
        io.WriteString(w, string(bytes))
}

handleWriteBlock 主要是生成新的区块。

func handleWriteBlock(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var m Message

    decoder := json.NewDecoder(r.Body)
    if err := decoder.Decode(&m); err != nil {
            respondWithJSON(w, r, http.StatusBadRequest, r.Body)
            return
    }   
    defer r.Body.Close()

    //ensure atomicity when creating new block
    mutex.Lock()
    newBlock := generateBlock(Blockchain[len(Blockchain)-1], m.BPM)
    mutex.Unlock()

    if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
            Blockchain = append(Blockchain, newBlock)
            spew.Dump(Blockchain)
    }   

    respondWithJSON(w, r, http.StatusCreated, newBlock)

}

func respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) {
    w.Header().Set("Content-Type", "application/json")
    response, err := json.MarshalIndent(payload, "", "  ")
    if err != nil {
            w.WriteHeader(http.StatusInternalServerError)
            w.Write([]byte("HTTP 500: Internal Server Error"))
            return
    }
    w.WriteHeader(code)
    w.Write(response)
}

主函数

func main() {
    err := godotenv.Load()
    if err != nil {
            log.Fatal(err)
    }   

    go func() {
            t := time.Now()
            genesisBlock := Block{}
            genesisBlock = Block{0, t.String(), 0, calculateHash(genesisBlock), "", difficulty, ""} 
            spew.Dump(genesisBlock)

            mutex.Lock()
            Blockchain = append(Blockchain, genesisBlock)
            mutex.Unlock()
    }() 
    log.Fatal(run())

}
  • godotenv.Load() 允许我们从 根目录的文件 .env 读取相应的变量。

  • genesisBlock 创建初始区块。

  • run() 启动 web 服务

启动web服务器

$ go run main.go

可以通过 Postman软件模拟网络请求。

通过 POST 访问 http://localhost:8080 可以添加新的区块信息。

可以在终端查看挖矿过程,如图所示:

通过 GET 访问 http://localhost:8080 可以获取区块链信息。

源码下载

使用 go 实现 Proof-of-Work 共识机制

猜你喜欢

转载自blog.csdn.net/qq_37133717/article/details/80399469