Skip to content

Instantly share code, notes, and snippets.

View ITExpert0228's full-sized avatar

Soft Dev ITExpert0228

View GitHub Profile
package main //Import the main package
// Create the Block data structure
// A block contains this info:
type Block struct {
Timestamp int64 // the time when the block was created
PreviousBlockHash []byte // the hash of the previous block
MyBlockHash []byte // the hash of the current block
AllData []byte // the data or transactions (body info)
}
package main
import (
// We will need these libraries:
"bytes" // need to convert data into byte in order to be sent on the network, computer understands better the byte(8bits)language
"crypto/sha256" //crypto library to hash the data
"strconv" // for conversion
"time" // the time for our timestamp
)
package main
// create the method that adds a new block to a blockchain
func (blockchain *Blockchain) AddBlock(data string) {
PreviousBlock := blockchain.Blocks[len(blockchain.Blocks)-1] // the previous block is needed, so let's get it
newBlock := NewBlock(data, PreviousBlock.MyBlockHash) // create a new block containing the data and the hash of the previous block
blockchain.Blocks = append(blockchain.Blocks, newBlock) // add that block to the chain to create a chain of blocks
}
/* Create the function that returns the whole blockchain and add the genesis to it first. the genesis block is the first ever mined block, so let's create a function that will return it since it does not exist yet */