go to create your own blocks

main Package 

Import ( 
    " Time " 
    " Crypto / SHA256 " 
    " bytes " 

) 

// block body 
type Block struct {
     // Version information 
    Version Int64
     // the hash value before the block chain 
    PrevBlockHash [] byte 
    // current block hash 
    hash [] byte 
    // Mei Kaer root 
    MerkelRoot [] byte 
    // timestamp 
    timeStamp int64
     // difficulty value 
    Bits int64
     // random number 
    nonce int64
     // transaction information
    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{})
}

 

Guess you like

Origin www.cnblogs.com/dqh123/p/12075421.html