标签:RKE block nes style 版本 string lock version byte
package main
import (
"time"
"crypto/sha256"
"bytes"
)
//区块体
type Block struct {
//版本信息
Version int64
//前区块链的hash值
PrevBlockHash []byte
//当前区块的hash
Hash []byte
//梅卡尔根
MerkelRoot []byte
//时间戳
TimeStamp int64
//难度值
Bits int64
//随机数
Nonce int64
//交易信息
Data []byte
}
func (block Block) setHash() {
tep := [][]byte{
IntToByte(block.Version),
block.PrevBlockHash,
block.MerkelRoot,
IntToByte(block.TimeStamp),
IntToByte(block.Bits),
IntToByte(block.Nonce),
block.Data,
}
data := bytes.Join(tep,[]byte{})
hash := sha256.Sum256(data)
block.Hash = hash[:]
}
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
}
//传世区块
func NewGenesisBlock() *Block{
return NewBlock("Genesis Block!",[]byte{})
}
标签:RKE block nes style 版本 string lock version byte
原文地址:https://www.cnblogs.com/dqh123/p/12075421.html