美文网首页
2.创建区块链并创建创世区块

2.创建区块链并创建创世区块

作者: J_zhe | 来源:发表于2018-07-05 18:52 被阅读0次

上篇教程我们讲到了如何去创建一个区块,当然。真正项目中的区块属性就不像我们上篇文章讲到的那么简单了。但是依然不妨碍我们学习。

我们一直说的是区块链,区块链就是由若干个区块去构成的一个链条,并且当前区块和前一个区块都有一定的联系。这样就做到了,如果修改了其中的一个区块。那么在这个区块之后的区块就都不会验证通过了。

1. 构造区块链的结构体

//创建区块链
type BlockChain struct {
    Block []*Block
}

不难发现,这个区块链就是保存由若干个区块组成的数组


2. 创建区块链并创建创世区块

func CreateGenesisBlockWithChain(data string) *BlockChain {
    blockChain := &BlockChain{[]*Block{ CreateGenesisBlock(data) }}
    return blockChain
}

此函数创建一个区块链,并将创世区块添加到这个链中。返回一个区块链对象


3. 创建创世区块的方法

func CreateGenesisBlock(data string) *Block {
    height := 1
    currentData := data
    prevhash := []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 := NewBlock(
        int64(height),
        []byte(currentData),
        prevhash,
    )

    return block
}

通过main函数构造区块链对象

//创建区块链并创建创世区块
blockChain := BlockChain.CreateGenesisBlockWithChain("Genesis block")

运行代码:

区块的高度:1
交易数据:Genesis block
时间戳:2018-07-05 06:29:00 PM
上一个区块的哈希:0000000000000000000000000000000000000000000000000000000000000000
Hash:7e98d58b3d620da554f5787c6fce48655b047653cf609fb1ba6ce8b0fc7212ec

为了更加好玩一点,我们继续创建一个往区块链中添加区块的方法

func (blc *BlockChain) AddBlockToBlockChain(height int64, data string, prev []byte) *BlockChain {
    newBlock := NewBlock(height, []byte(data), prev)
    blc.Block = append(blc.Block, newBlock)
    return blc
}

通过main方法往链中添加区块

//添加区块1
    blockChain.AddBlockToBlockChain(
        //区块高度
        int64(len(blockChain.Block)) + 1,
        //交易数据
        "Second block",
        //上一个区块的哈希
        blockChain.Block[len(blockChain.Block) - 1].Hash,
    )

    //添加区块2
    blockChain.AddBlockToBlockChain(
        //区块高度
        int64(len(blockChain.Block)) + 1,
        //交易数据
        "Thred block",
        //上一个区块的哈希
        blockChain.Block[len(blockChain.Block) - 1].Hash,
    )

    //遍历输出区块信息
    for _,block := range blockChain.Block {
        fmt.Printf("区块的高度:%d\n", block.Height)
        fmt.Printf("交易数据:%s\n", block.Data)
        fmt.Printf("时间戳:%s\n", time.Unix(block.Timestamp, 0).Format("2006-01-02 03:04:05 PM"))
        fmt.Printf("上一个区块的哈希:%x\n", block.PrevHash)
        fmt.Printf("Hash:%x\n", block.Hash)
        fmt.Println("-------------------------------")
    }

我们在创世块后追加了两个区块,那么现在我们运行看下输出信息

区块的高度:1
交易数据:Genesis block
时间戳:2018-07-05 06:29:00 PM
上一个区块的哈希:0000000000000000000000000000000000000000000000000000000000000000
Hash:7e98d58b3d620da554f5787c6fce48655b047653cf609fb1ba6ce8b0fc7212ec
-------------------------------
区块的高度:2
交易数据:Second block
时间戳:2018-07-05 06:29:00 PM
上一个区块的哈希:7e98d58b3d620da554f5787c6fce48655b047653cf609fb1ba6ce8b0fc7212ec
Hash:cb9b4e766cae61b5c190517c878e0579619da864e8ee6649b9fbd1b0851f24f0
-------------------------------
区块的高度:3
交易数据:Thred block
时间戳:2018-07-05 06:29:00 PM
上一个区块的哈希:cb9b4e766cae61b5c190517c878e0579619da864e8ee6649b9fbd1b0851f24f0
Hash:8de86e78b0099b8ffb96b6fbe801125588f70515d8bc0a47b095e57ff848ffba
-------------------------------

好了,到此为止。你学会了吗?如果不懂你可以查看下方的源码,对照着源码去理解。下节我们将会学习鼎鼎大名的POW(工作量证明)算法,并应用到我们的案例中。

源码:https://gitee.com/itgjz/blockchain_learn/tree/master/block_chain_learn2

相关文章

网友评论

      本文标题:2.创建区块链并创建创世区块

      本文链接:https://www.haomeiwen.com/subject/urksuftx.html