Skip to content

BlockChain HQ.go

DanielHauge edited this page Dec 3, 2017 · 2 revisions

This file contains everything to do with blockchain as a whole, and the whole controlling of the blockchain (This has BoltDB implementations aswell, for storing the blockchain)

This file has 2 structs which are:

type Blockchain struct {
	top []byte              /// This represent the most current block hash in the chain
	db *bolt.DB             /// This represent whole blockchain which are stored in BoltDB
}

type BlockChainReader struct {
	cur_hash []byte        /// This represent the current read blocks hash in the chain
	db     *bolt.DB        /// This represent whole blockchain which are stored in BoltDB
}

This file also contains the global static variable which is the blockchain

var BlockChain *Blockchain

Other than storing a global variable and 2 data structs, it has 4 functions to mainly interact with these. Which are

func (chain *Blockchain) AddBlock(data string)

This function will be called from a specific blockchain (The global variable). It will add a block with the data passed in as argument.

func NewBlockChain() *Blockchain

This function will generate a new blockchain, it will be mainly called when the program starts up. (Initializing blockchain from database or inserting new chain into database)

func (chain *Blockchain) GetReader() *BlockChainReader

This function will generate a new blockchain reader which itterates through the blockchain and reads the blocks one by one. From newest to oldest. The function needs to be called from a specific blockchain, mainly the global variable blockchain.

func (reader *BlockChainReader) Next() *Block

This function will be called from the blockchainreader, and it reads the next block in the chain.

Clone this wiki locally