2018-05-21服务器pow共识算法

package main

import (
    "sync"
    "time"
    //"hash"
    "strings"
    "strconv"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "net/http"
    "github.com/gorilla/mux"
    "encoding/json"
    "io"
    "github.com/davecgh/go-spew/spew"
    "os"
    "log"
    "github.com/joho/godotenv"
)

//设置难度系数
const difficulty  =4
//定义区块
type Block struct {
    Index int
    Timestamp string
    BMP int
    Hash string
    PreHash string
    Difficulty int
    Nonce int
}


//通过数组,维护区块链
var Blockchain []Block

//Message为通过POST实现数据求发送的数据类型
type Message struct {
    BPM int
}


//控制线程异步访问
var mutex = &sync.Mutex{}


//生成区块
func generateBlock(oldBlock Block,BMP int)Block{
    var newBlock Block
    t:=time.Now()

    newBlock.Index= oldBlock.Index +1
    newBlock.Timestamp=t.String()
    newBlock.BMP = BMP
    newBlock.Difficulty = difficulty

    for i:=0;;i++ {
        newBlock.Nonce++
        //打印哈希
        fmt.Println(calculateHash(newBlock))

        if isHashValid(calculateHash(newBlock),newBlock.Difficulty){
            fmt.Println("挖矿成功")
            return newBlock
        }

    }
}


//创建isHashValid ,判断哈希的0个的数是否与难度系数一直
func isHashValid(hash string ,difficulty int) bool  {
    prefix:=strings.Repeat("0",difficulty)
    return strings.HasPrefix(hash,prefix)

}


//按照规则,生成Hash值
func calculateHash(block Block) string {
    record:= strconv.Itoa(block.Index)+block.Timestamp+strconv.Itoa(block.Nonce)+
        strconv.Itoa(block.BMP)+block.PreHash
    sha:=sha256.New()
    sha.Write([]byte(record))
    hashed:=sha.Sum(nil)
    return  hex.EncodeToString(hashed)
}

//通过run函数作为http服务器的启动函数
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
}

//get,post 请求处理
func makeMuxRouter() http.Handler {
    //负责get,post的请求处理的
    muxRouter:=mux.NewRouter()
    //handGetClockchain 回调函数
    muxRouter.HandleFunc("/",handGetClockchain).Methods("GET")
    //handWriteBlock 也是回调函数
    muxRouter.HandleFunc("/",handWriteBlock).Methods("POST")
    return muxRouter

}

//处理http的GET请求
func handGetClockchain(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))
}


//处理Http的POST请求
func handWriteBlock(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var m Message
    //当服务器错误,返回相应信息500
    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)

}

//如果错误,返回服务器500错误
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)
}

//判断产生hash的合法性
func isBlockValid(newBlock, oldBlock Block) bool {
    if oldBlock.Index+1 != newBlock.Index {
        return false
    }

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

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

    return true
}



func main() {
    //var firstBlock Block
    //firstBlock.Difficulty = 4
    //firstBlock.Nonce = 0
    //firstBlock.PreHash = "0"
    //firstBlock.BMP = 1
    //firstBlock.Index = 0
    //firstBlock.Hash = "0"
    //
    //generateBlock(firstBlock,1)

    //加载.env文件
    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, 0}
        spew.Dump(genesisBlock)

        mutex.Lock()
        Blockchain = append(Blockchain, genesisBlock)
        mutex.Unlock()
    }()
    log.Fatal(run())
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,174评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,838评论 19 139
  • 谈到青春,爱情是一道绕不过的坎,虽然在类似演讲的场合,爱情的话题往往不会特意提及。但是她却实实在在在青春的岁月中占...
    欢糖阅读 3,269评论 0 0
  • 记2018年3月18日,泰嫂让我帮忙给泰哥带的东西,被落在宿舍,过了1个小时才想起来,这是何等的过失! 未能履行自...
    周晓一条鱼阅读 3,375评论 0 0

友情链接更多精彩内容