关于go:Go-区块链实现

46次阅读

共计 3208 个字符,预计需要花费 9 分钟才能阅读完成。

区块链七层架构模型

创立工程文件

mkdir BlockChain && cd BlockChain && go mod init BlockChain
mkdir block blockChain
touch main.go block/block.go blockChain/blockChain.go

数据层

hash

func CalcHash(tobeHash string) string {
    // "crypto/sha256"
    // func Sum256(data []byte) [Size]byte
    // size = 32, 32 byte = 256 bit
    tobeHash_256 := sha256.Sum256([]byte(tobeHash))

    // "encoding/hex"
    // func EncodeToString(src []byte) string
    return hex.EncodeToString(tobeHash_256[:])
}

block

  • 类定义

    // block
    type Block struct{
        Index         int64  // 区块编号
        Timestamp     int64  // 区块工夫戳
        PrevBlockHash string // 上一个区块的哈希值
        Data          string // 以后区块数据
        Hash          string // 以后区块哈希值
    }
  • 办法

    计算以后区块哈希值

    func CalcHash(b *Block) string {total_block_data := string(Index) + string(Timestamp) + b.PrevBlockHash + b.Data
        block_data_sha256 := sha256.Sum256([]byte(total_block_data))
        return hex.EncodeToString(block_data_sha256[:])
    }

    生成区块

    func GenerateBlock(prevBlock *Block, data string) *Block{new_block := Block{}
        new_block.Index = prevBlock.Index + 1
        new_block.Timestamp = time.Now().Unix()
        new_block.PrevBlockHash = prevBlock.PrevBlockHash
        new_block.Data = data
        new_block.Hash = CalcHash(&new_block)
        
        return &new_block
    }

    创世块 (第一个 block)

    func GenesisBlock() *Block{prevBlock := Block{}
        prevBlock.Index = -1
        prevBlock.PrevBlockHash = ""return GenerateBlock(&prevBlock,"Genesis Block")
    }
  • 测试

    package main
    
    import("fmt")
    
    func main(){a := GenesisBlock()
        b := GenerateBlock(a, "first block")
        fmt.Println(a)
        fmt.Println(b)
    }
    $ go run main.go  
    &{0 1666282848  Genesis Block 90d7d6d9adc8a6dd4eca1e30d8c1a8556a8e3e508da81f30a9e520c2ee7124b0}
    &{1 1666282848 90d7d6d9adc8a6dd4eca1e30d8c1a8556a8e3e508da81f30a9e520c2ee7124b0 first block 16f4d5551953062eca3e7858c28726967d6129f03ac393175efb6044e797589b}

blockchain

  • 类定义

    type BlockChain struct{Blocks []*block.Block
        Count int64
    }
  • 初始化区块链

    func NewBlockChain() *BlockChain{bc := BlockChain{}
        bc.Blocks = append(bc.Blocks, block.GenesisBlock())
        bc.Count++
        return &bc
    }
  • 区块链间接减少区块

    func (bc *BlockChain) AddBlock(newBlock *block.Block){
        if bc.Count == 0 {bc =  NewBlockChain()
        }
        // 判断新增区块是否无效
        if isValid(newBlock, bc){bc.Blocks = append(bc.Blocks, newBlock)
            bc.Count++
        }else{panic("invalid block")
        }
    }
  • 判断新增区块是否无效

    func isValid(newBlock *block.Block, bc *BlockChain) bool {oldBlock := bc.Blocks[bc.Count - 1]
        if newBlock.Index - 1 != oldBlock.Index{return false}
        if newBlock.PrevBlockHash != oldBlock.PrevBlockHash{return false}
        if block.CalcHash(newBlock) != newBlock.Hash{return false}
        return true
    }
  • 写区块链并减少区块

    func (bc *BlockChain) Write(data string){oldBlock := bc.Blocks[bc.Count - 1]
        newBlock := block.GenerateBlock(oldBlock, data)
        bc.AddBlock(newBlock)
    }
  • 打印区块链

    func (bc *BlockChain) Print(){
        for _, b := range bc.Blocks{fmt.Printf("Index: %d", b.Index)
            fmt.Printf("Timestamp: %d", b.Timestamp)
            fmt.Printf("PrevBlockHash: %s", b.PrevBlockHash)
            fmt.Printf("Data: %s", b.Data)
            fmt.Printf("Hash: %s\n", b.Hash)
        }
    }
  • 测试

    package main
    
    import("BlockChain/blockChain")
    
    func main(){bc := blockChain.NewBlockChain()
    
        bc.Write("first block")
        bc.Write("second block")
        bc.Print()}
    $ go run main.go                                                                            
    Index: 0 Timestamp: 1666286818 PrevBlockHash:  Data: Genesis Block Hash: 90d7d6d9adc8a6dd4eca1e30d8c1a8556a8e3e508da81f30a9e520c2ee7124b0
    Index: 1 Timestamp: 1666286818 PrevBlockHash: 90d7d6d9adc8a6dd4eca1e30d8c1a8556a8e3e508da81f30a9e520c2ee7124b0 Data: first block Hash: 16f4d5551953062eca3e7858c28726967d6129f03ac393175efb6044e797589b
    Index: 2 Timestamp: 1666286818 PrevBlockHash: 16f4d5551953062eca3e7858c28726967d6129f03ac393175efb6044e797589b Data: second block Hash: ad14b2a702a2a1d6765190c84128daca2fa350396b80e95aeb1aaa9e5c77aa11

正文完
 0