200行golang 實現的區塊鏈
下面的程式碼實現了一個最小的區塊鏈。訪問使用Http介面
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
// Block represents each 'item' in the blockchain
type Block struct {
Index int
Timestamp string
BPM int
Hash string
PrevHash string
Difficulty int
Nonce string
}
// Blockchain is a series of validated Blocks
var Blockchain []Block
// Message takes incoming JSON payload for writing heart rate
type Message struct {
BPM int
}
var mutex = &sync.Mutex{}
func main() {
err := godotenv.Load("example.env")
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())
}
// web server
func run() error {
mux := makeMuxRouter()
httpPort := os.Getenv("PORT")
log.Println("HTTP Server Listening on port :", httpPort)
s := &http.Server{
Addr: ":" + httpPort,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
// create handlers
func makeMuxRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handleGetBlockchain).Methods("GET")
muxRouter.HandleFunc("/", handleWriteBlock).Methods("POST")
return muxRouter
}
// write blockchain when we receive an http request
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))
}
// takes JSON payload as an input for heart rate (BPM)
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)
}
// make sure block is valid by checking index, and comparing the hash of the previous block
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
}
// SHA256 hasing
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)
}
// create a new block using previous block's hash
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
// 挖礦:在區塊中加入一個變數i,使用sha256計算對應的雜湊值,符合條件就算挖到礦(雜湊前面有連續difficulty個0)
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
}
func isHashValid(hash string, difficulty int) bool {
prefix := strings.Repeat("0", difficulty)
return strings.HasPrefix(hash, prefix)
}
測試:
相關文章
- 區塊鏈,中心去,何曾著眼看君王?用Go語言實現區塊鏈技術,透過Golang秒懂區塊鏈區塊鏈Golang
- JavaScript實現區塊鏈JavaScript區塊鏈
- 區塊鏈-NFT 的實現原理區塊鏈
- 雲+區塊鏈 實現區塊鏈技術的普惠應用區塊鏈
- 比特幣和區塊鏈(2):比特幣中區塊鏈的實現比特幣區塊鏈
- [上海] golang/區塊鏈開發招聘Golang區塊鏈
- 使用Javascript實現小型區塊鏈JavaScript區塊鏈
- .Net Core實現區塊鏈初探區塊鏈
- NodeJS實現簡易區塊鏈NodeJS區塊鏈
- 招聘Golang區塊鏈開發工程師Golang區塊鏈工程師
- 300行Kotlin程式碼實現的區塊鏈Kotlin區塊鏈
- V神:區塊鏈上投票流程的實現區塊鏈
- 使用MVC模式實現區塊鏈開發MVC模式區塊鏈
- [譯] 用 Java 程式碼實現區塊鏈Java區塊鏈
- 用java實現一個簡單的區塊鏈Java區塊鏈
- 一個簡單的區塊鏈程式碼實現區塊鏈
- 【北京望京】Contentbox招聘golang、區塊鏈工程師Golang區塊鏈工程師
- 一個簡單的區塊鏈貨幣,python實現區塊鏈Python
- 基於區塊鏈的智慧鎖設計與實現區塊鏈
- blockstack/stacks-blockchain:Stacks 2.0 區塊鏈的Rust實現Blockchain區塊鏈Rust
- 區塊鏈安全:實現公鏈雙花攻擊的多種方法區塊鏈
- 用Java程式碼實現區塊鏈技術Java區塊鏈
- 區塊鏈100講:區塊鏈為什麼叫“區塊”“鏈”?區塊鏈
- “區塊”和“鏈”的火花,區塊鏈到底為何物區塊鏈
- 區塊鏈應用技術,區塊鏈支付系統搭建的實際作用區塊鏈
- 區塊鏈學習-Golang 與智慧合約的互動(一)區塊鏈Golang
- 使用ABAP實現一個最簡單的區塊鏈原型區塊鏈原型
- 區塊鏈系統可以實現資料的互動需求區塊鏈
- 區塊鏈全節點的JavaScript實現:Bcoin 入門 - pradyuman區塊鏈JavaScript
- 【北京】 京東總部-golang工程師-區塊鏈方向Golang工程師區塊鏈
- 如何在NEO區塊鏈上實現資訊加密區塊鏈加密
- 區塊鏈作用在哪些領域得以實現?區塊鏈
- 區塊鏈100講:區塊鏈中的隨機數區塊鏈隨機
- 區塊鏈100講: 區塊鏈共識的確定性區塊鏈
- 區塊鏈101:區塊鏈技術是如何工作的?區塊鏈
- 區塊鏈知識,區塊鏈簡史區塊鏈
- 區塊鏈系列1-區塊鏈概述區塊鏈
- 區塊鏈 2.0:房地產區塊鏈(四)區塊鏈