golang-Blockchain Learning 01

Source: Internet
Author: User

Objective

Blockchain technology is now also the IT sector of the network red, do a bit of technical learning, do not engage in a block chain with all the nerve to go out and greet others. Now learning the go language is just as hot as a wave of blockchain.

Profile

Golang Knowledge Points:
1. Use of Golang structural body
2, common API use, such as bytes. Join, sha256.sum256, StrConv. Formatint and so on, learn the application of arrays and slices
Blockchain Simplification Instructions:
1. The simplified block object block contains four members, TimeStamp (timestamp), Preblockhash (hash value of the previous chunk), data (stored content), hash (hash value of its own chunk)
2. A simplified Blockchain object containing an array of chunks to blocks

Construction of coded analog block chain creation

1, create a Golang project, the directory structure is as follows: directory structure

Description
The bin directory holds the executable file that compiles the build
Src-coin Storage Main.go
Src-core Storage Block.go and Blockchian.go

2, Block.go

package coreimport (    "time"    "strconv"    "bytes"    "crypto/sha256")type Block struct {    TimeStamp     int64//时间戳    Data          []byte//存储的数据信息    PrevBlockHash []byte// 上一个区块的hash    Hash          []byte//本区块的hash}// 创建一个新的区块func NewBlock(data string, prevBlockHash []byte) *Block {        // 新建一个区块,自己hash值为空    block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}}      // 计算自己区块的hash值    block.SetHash()    return block}func (b *Block) SetHash() {    strTimeStamp := []byte(strconv.FormatInt(b.TimeStamp, 10))    headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, strTimeStamp}, []byte{})    hash := sha256.Sum256(headers)    b.Hash = hash[:]}// 模拟创世区块func NewGenesisBlock() *Block {    return NewBlock("Genesis Block", []byte{})}

3, Blockchain.go

package coretype BlockChain struct {    Blocks []*Block}// 添加区块到链上func (bc *BlockChain) AddBlock(data string) {    preBlock := bc.Blocks[len(bc.Blocks)-1]// 获取前一个区块    newBlock := NewBlock(data, preBlock.Hash)//创建新区块    bc.Blocks = append(bc.Blocks, newBlock)// 链接新区块到链上}// 创建区块链func NewBlockChain() *BlockChain {    return &BlockChain{[]*Block{NewGenesisBlock()}}}

4. Main.go Test

package mainimport (    "core"    "fmt")func main() {    bc := core.NewBlockChain()//创建区块链    bc.AddBlock("send 1 btc to Ivan")// 添加一个区块到连上    bc.AddBlock("send 2 btc to Ivan")// 测试打印区块链上的信息    for _, block := range bc.Blocks {        fmt.Printf("PrevBlockHash:%x\n", block.PrevBlockHash)        fmt.Printf("Data:%s\n", block.Data)        fmt.Printf("Hash:%x\n", block.Hash)        fmt.Printf("TimeStamp:%d\n", block.TimeStamp)        println()    }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.