Go public chain combat 0x00 blockchain infrastructure

Source: Internet
Author: User

Fallen for a period of time, and finally found the motivation to study, full of blood back ...

We know that in the full swing of the blockchain application of the Red Sea, Golang more and more play an irreplaceable role. On the one hand, it is based on the simplicity of its syntax, which has the characteristics of C + + efficient processing. Today, we start with the go language to build a simple public-chain project with full blockchain functionality.

Since the simple blockchain structure has already been built with Python, the basic structure of the block is no longer detailed.

Cut the crap and dry up a little.

Block block

Block

type Block struct {    //1.区块高度    Height int64    //2.上一个区块HAsh    PrevBlockHash []byte    //3.交易数据    Data []byte    //4.时间戳    Timestamp int64    //5.Hash    Hash []byte}

Set the current chunk hash

func (block *Block) SetHash() {    //1.将高度,时间戳转换为字节数组    //base:2  二进制形式    heightBytes := IntToHex(block.Height)    timeStampStr := strconv.FormatInt(block.Timestamp, 2)    timeStamp := []byte(timeStampStr)    //fmt.Println(heightBytes)    //fmt.Println(timeStampStr)    //fmt.Println(timeStamp)    //2.拼接所有属性    blockBytes := bytes.Join([][]byte{        heightBytes,        block.PrevBlockHash,        block.Data,        timeStamp,        block.Hash}, []byte{})    //fmt.Println(blockBytes)    //3.将拼接后的字节数组转换为Hash值    hash := sha256.Sum256(blockBytes)    fmt.Println(hash)    block.Hash = hash[:]    //fmt.Println(block.Hash)}

Create new District Fast

func NewBlock(data string, height int64, prevBlockHash []byte) *Block {    //创建区块    block := &Block{        Height:        height,        PrevBlockHash: prevBlockHash,        Data:          []byte(data),        Timestamp:     time.Now().Unix(),        Hash:          nil}    //设置HAsh值    block.SetHash()    return block}

Genesis Block Creation

func CreateGenesisBlock(data string) *Block {    return NewBlock(data, 1, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})}

Block chain Blockchain

Block chain

type Blockchain struct {    //有序区块的数组    Blocks [] *Block}

Create a blockchain with a Genesis block

func CreateBlockchainWithGensisBlock() *Blockchain  {    gensisBlock := CreateGenesisBlock("Gensis Block...")    return &Blockchain{[] *Block{gensisBlock}}}

Add a chunk to the blockchain

func (blc *Blockchain) AddBlockToBlockchain(data string, height int64, prevHash []byte)  {    //新建区块    newBlock := NewBlock(data, height, prevHash)    //上链    blc.Blocks = append(blc.Blocks, newBlock)}

Utils Auxiliary tool (int64 converted to byte array)

import (    "bytes"    "encoding/binary"    "log")//将int64转换为bytesfunc IntToHex(num int64) []byte  {    buff := new(bytes.Buffer)    err := binary.Write(buff, binary.BigEndian, num)    if err != nil {        log.Panic(err)    }    return buff.Bytes()}

Test Demo

/**@author: chaors@file:   main.go@time:   2018/06/21 22:01@desc:   区块信息的示例*/package mainimport (    "chaors.com/LearnGo/publicChaorsChain/part1-Basic-Prototype/BLC"    "fmt")func main() {    //genesisBlock := BLC.CreateGenesisBlock("Genenis Block")    //创建带有创世区块的区块链    blockchain := BLC.CreateBlockchainWithGensisBlock()    //添加一个新区快    blockchain.AddBlockToBlockchain("first Block",        blockchain.Blocks[len(blockchain.Blocks)-1].Height,        blockchain.Blocks[len(blockchain.Blocks)-1].Hash)    blockchain.AddBlockToBlockchain("second Block",        blockchain.Blocks[len(blockchain.Blocks)-1].Height,        blockchain.Blocks[len(blockchain.Blocks)-1].Hash)    blockchain.AddBlockToBlockchain("third Block",        blockchain.Blocks[len(blockchain.Blocks)-1].Height,        blockchain.Blocks[len(blockchain.Blocks)-1].Hash)    fmt.Println(blockchain)}

Run results

We see the results of the operation and the printed content is a blockchain of four blocks, including the Genesis block.

Image.png

.
.
.
.

The internet is disrupting the world and the blockchain is disrupting the internet!

-------------------------------------------------20180622 20:35

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.