Create blockchain V1

Source: Internet
Author: User
The following describes the implementation of blockchain: block. Go
Package mainimport ("time" "crypto/sha256" "bytes ") // define the Block Structure Type Block struct {version int64 prevblockhash [] Byte hash [] Byte timestamp int64 targetbits int64 nonce int64 merkelroot [] byte data [] Byte} // set the block to be created method func newblock (data string, prevblockhash [] Byte) * block {block: = & Block {version: 1, prevblockhash: prevblockhash, // hash: Timestamp: time. now (). unix (), targetbits: 10, Nonce: 5, merkelroot: [] BYT E {}, data: [] Byte (data),} block. sethash () // set the block's hash value return block} // Add the hash value func (Block * block) sethash () {TMP: = [] [] Byte {// inttobyte (Block. version), block. prevblockhash, inttobyte (Block. timestamp), block. merkelroot, inttobyte (Block. nonce), block. data,} // link each field of the block into a slice and use [] Byte {} For the link to avoid the information of the source block data: = bytes. join (TMP, [] Byte {}) // hash the block by sha256. The returned value is a [32] byte array instead of a shard hash: = Sha256.sum256 (data) block. hash = hash [:] // is converted from an array to a shard} // The creation of the Creation block. The hash value of func newgenesisblock () is null for one kubernetes client () * block {return newblock ("Genesis block! ", [] Byte {})}
Blockchain. Go
Package mainimport "OS" // define the blockchain type blockchain struct {blocks [] * block} // create the blockchain and add the creation block func newblockchain () * blockchain {return & blockchain {[] * block {newgenesisblock (), }}// add block func (BC * blockchain) addblock (data string) {// simple validation if Len (BC. blocks) <= 0 {OS. exit (1)} // create a new block named lastblock: = BC. blocks [Len (BC. blocks)-1] prevblockhash: = lastblock. hash block: = newblock (data, prevblockhash) // Add to blockchain BC. blocks = append (BC. blocks, block )}
Utils
package mainimport (    "bytes"    "encoding/binary"    "fmt"    "os")func IntToByte(num int64)[]byte{    var buffer  bytes.Buffer    err := binary.Write(&buffer, binary.BigEndian, num)    //if err != nil{    //  fmt.Println("IntToByte err occur:",err)    //  os.Exit(1)    //}    CheckErr(err)    return buffer.Bytes()}func CheckErr(err error){    if err != nil{        fmt.Println("err occur:",err)        os.Exit(1)    }}
Main. Go
Package mainimport "FMT" func main () {BC: = newblockchain () BC. addblock ("the shift leader transfers a bitcoin to the instructor") BC. addblock ("") for I, block: = range BC. blocks {FMT. println ("=== block num:", I) FMT. printf ("data: % s \ n", block. data) FMT. println ("version:", block. version) FMT. printf ("prevhash: % x \ n", block. version) FMT. printf ("hash: % x \ n", block. timestamp) FMT. printf ("timestamp: % d \ n", block. timestamp) FMT. printf ("Merkel: % x \ n", block. merkelroot) FMT. printf ("nonce: % d \ n", block. nonce )}}

Finally, compile and run the program in the gopath path to view the effect.

Create blockchain V1

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.