標籤:data pat 定義 ddb nil 結構 簡單的 gen var
下面簡單的描述區塊鏈的實現:block.go
package mainimport ( "time" "crypto/sha256" "bytes")//定義塊結構type Block struct{ Version int64 PrevBlockHash []byte Hash []byte TimeStamp int64 TargetBits int64 Nonce int64 MerKelRoot []byte Data []byte}//設定建立塊的方法func NewBlock(data string, prevBlockHash []byte) *Block{ block := &Block{ Version:1, PrevBlockHash:prevBlockHash, //Hash: TimeStamp:time.Now().Unix(), TargetBits:10, Nonce:5, MerKelRoot:[]byte{}, Data:[]byte(data), } block.SetHash() //設定區塊的雜湊值 return block}// 添加雜湊值func (block *Block)SetHash(){ tmp := [][]byte{ //實現int類型轉換為byte類型的工具函數 IntToByte(block.Version), block.PrevBlockHash, IntToByte(block.TimeStamp), block.MerKelRoot, IntToByte(block.Nonce), block.Data, } //將區塊的各個欄位連結成一個切片,使用【】byte{}進行連結,目的是避免汙染源區塊的資訊 data := bytes.Join(tmp,[]byte{}) //對區塊進行sha256雜湊演算法,傳回值為[32]byte數組,不是切片 hash := sha256.Sum256(data) block.Hash = hash[:]//由數群組轉換為切片}// 創世塊的建立,它的錢一個去魁岸的雜湊值為空白func NewGenesisBlock() *Block{ return NewBlock("Genesis Block!",[]byte{})}
blockchain.go
package mainimport "os"//定義區塊鏈條type BlockChain struct{ blocks []*Block}// 建立區塊鏈,並且添加創世塊func NewBlockChain() *BlockChain{ return &BlockChain{[]*Block{ NewGenesisBlock(), }}}//添加區塊func (bc *BlockChain)AddBlock(data string){ //簡單校正 if len(bc.blocks) <= 0 { os.Exit(1) } //根據上一區塊,建立新的區塊 lastblock := bc.blocks[len(bc.blocks)-1] prevBlockHash := lastblock.Hash block := NewBlock(data, prevBlockHash) //添加到區塊鏈當中 bc.blocks = append(bc.blocks, block)}
utils
package mainimport ( "bytes" "encoding/binary" "fmt" "os")func IntToByte(num int64)[]byte{ var buffer bytes.Buffer err := binary.Write(&buffer, binary.BigEndian, num) //if err != nil{ // fmt.Println("IntToByte err occur:",err) // os.Exit(1) //} CheckErr(err) return buffer.Bytes()}func CheckErr(err error){ if err != nil{ fmt.Println("err occur:",err) os.Exit(1) }}
main.go
package mainimport "fmt"func main(){ bc := NewBlockChain() bc.AddBlock("班長轉給老師一枚比特幣") bc.AddBlock("班長又轉給老師一枚比特幣") for i, block := range bc.blocks{ fmt.Println("====block num:", i) fmt.Printf("Data:%s\n", block.Data) fmt.Println("Version:",block.Version) fmt.Printf("PrevHash:%x\n",block.Version) fmt.Printf("Hash:%x\n",block.TimeStamp) fmt.Printf("TimeStamp:%d\n",block.TimeStamp) fmt.Printf("MerKel:%x\n",block.MerKelRoot) fmt.Printf("Nonce:%d\n",block.Nonce) }}
最後,在gopath路徑下,編譯運行查看效果。
建立區塊鏈之v1