lemooljiang avatar

区块的结构 / go实现简易区块链 #1

lemooljiang

Published: 14 Aug 2022 › Updated: 14 Aug 2022

区块的结构 / go实现简易区块链 #1

利用假期学习些底层的知识,比如用go语言实现下区块链的逻辑。重新学习下区块的结构,区块链的结构,哈希函数这些。前期进展较慢,慢慢地,渐入佳境。

区块结构.jpg

区块结构示意图

type Block struct {
    Version       int64
    PrevBlockHash []byte
    Hash          []byte
    MerKelRoot    []byte
    TimeStamp     int64
    Bits          int64
    Nonce         int64
    Data          []byte
}

func NewBlock(data string, prevBlockHash []byte) *Block {
    var block Block
    block = Block{
        Version:       1,
        PrevBlockHash: prevBlockHash,
        MerKelRoot:    []byte{},
        TimeStamp:     time.Now().Unix(),
        Bits:          1,
        Nonce:         1,
        Data:          []byte(data)}
    block.SetHash()
    return &block
}

type BlockChain struct {
    blocks []*Block
}

func NewBlockChain() *BlockChain {
    block := NewGenesisBlock()
    return &BlockChain{blocks: []*Block{block}}
}

先用struct来实现区块和区块链,对比下真实的结构,感觉还是蛮有味的。

Leave 区块的结构 / go实现简易区块链 #1 to:

Written by

Designer , Poet , Technology enthusiasts

Read more #cn posts


Best Posts From lemooljiang

We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From lemooljiang