Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content
在本机端口进行查询 由于是第一次运行该链 生成创世区块

通过http实现RPC命令的调用  实现添加块

查询当前所有块的信息

这里只做了两个命令 分别是get和write
源代码如下
基础功能的实现

package core
import (
        "crypto/sha256"
        "encoding/hex"
        "time"
)
//定义区块
type Block struct {
        Index         int64  //区块编号
   
  1.     Timestamp     int64  //区块时间戳
  2.         PrevBlockHash string //上一个区块的hash值
  3.         Hash          string //当前区块哈希值
  4.         Data string //区块数据
  5. }
  6. //计算Hash
  7. func calculateHash(b *Block) string {
  8.         blockData := string(b.Index) + string(b.Timestamp) + b.PrevBlockHash
  9.         hashInBytes := sha256.Sum256([]byte(blockData))
  10.         return hex.EncodeToString(hashInBytes[:])
  11. }
Copy the Code


//生成新的区块
func GenerateNewBlock(preBlock *Block, data string) *Block {
        newBlock := &Block{}
        newBlock.Index = preBlock.Index + 1
        newBlock.PrevBlockHash = preBlock.Hash
        newBlock.Timestamp = time.Now().Unix()
        newBlock.Hash = calculateHash(newBlock)
        newBlock.Data = data
        return newBlock
}

//生成创始区块
func GenerateGenesisBlock() *Block {
        preBlock := &Block{}
        preBlock.Index = -1
        preBlock.Hash = ""
        return GenerateNewBlock(preBlock, "Genesis Block")
}
将基础功能实现的块进行链接 实现blockchain
package core

import (
        "fmt"
        "log"
)

//定义区块链
type BlockChain struct {
        Blocks []*Block
}

//创建一个区块链
func NewBlockChain() *BlockChain {
        genesisBlock := GenerateGenesisBlock()
        blockChain := &BlockChain{}
        blockChain.AppendBlock(genesisBlock)
        return blockChain
}

//记录区块数据
func (bc *BlockChain) SendData(data string) {
        preBlock := bc.Blocks[len(bc.Blocks)-1]
        newBlock := GenerateNewBlock(preBlock, data)
        bc.AppendBlock(newBlock)
}

//往区块链添加区块
func (bc *BlockChain) AppendBlock(newBlock *Block) {
        if len(bc.Blocks) == 0 {
                bc.Blocks = append(bc.Blocks, newBlock)
                return
        }
        if isValid(newBlock, bc.Blocks[len(bc.Blocks)-1]) {
                bc.Blocks = append(bc.Blocks, newBlock)
        } else {
                log.Fatal("invalid block")
        }
        return
}

//输出区块链信息
func (bc *BlockChain) Print() {
        for _, block := range bc.Blocks {
                fmt.Printf("Index : %d\n", block.Index)
                fmt.Printf("Prev.Hash : %s\n", block.PrevBlockHash)
                fmt.Printf("Curr.Hash : %s\n", block.Hash)
                fmt.Printf("Curr.Data : %s\n", block.Data)
                fmt.Printf("Curr.Timestamp : %d\n", block.Timestamp)
                fmt.Println("==========================================")
        }
}

//验证区块
func isValid(newBlock *Block, oldBlock *Block) bool {
        if newBlock.Index-1 != oldBlock.Index {
                return false
        }
        if newBlock.PrevBlockHash != oldBlock.Hash {
                return false
        }
        if calculateHash(newBlock) != newBlock.Hash {
                return false
        }
        return true
}
实现RPC接口的交互
  1. package main

  2. import (
  3.         "encoding/json"
  4.         "BlockChain/core"
  5.         "io"
  6.         "net/http"
  7. )

  8. var blockChain *core.BlockChain

  9. func run() {
  10.         http.HandleFunc("/block_chain/get", blockChainGetHandle)
  11.         http.HandleFunc("/block_chain/write", blockChainWriteHandle)
  12.         http.ListenAndServe(":8332", nil)
  13. }

  14. func blockChainGetHandle(w http.ResponseWriter, r *http.Request) {
  15.         bytes, err := json.Marshal(blockChain)
  16.         if err != nil {
  17.                 http.Error(w, err.Error(), http.StatusInternalServerError)
  18.                 return
  19.         }
  20.         io.WriteString(w, string(bytes))
  21. }

  22. func blockChainWriteHandle(w http.ResponseWriter, r *http.Request) {
  23.         blockData := r.URL.Query().Get("data")
  24.         blockChain.SendData(blockData)
  25.         blockChainGetHandle(w, r)
  26. }

  27. func main() {
  28.         blockChain = core.NewBlockChain()
  29.         run()
  30. }
Copy the Code

通过两部分代码实现简易区块链的RPC调用
BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
You have to log in before you can reply Login | 立即注册

Points Rules

Write the first review

朋友一起走 初中生
  • Follow

    0

  • Following

    0

  • Articles

    16

Promoted