建立區塊鏈之v1

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.