blockchain

package
v0.0.0-...-bb32285 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const InitDifficulty = 12

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Timestamp  int64  `json:"timestamp"`
	Data       []byte `json:"data"`
	Hash       []byte `json:"hash"`
	LastHash   []byte `json:"lasthash"`
	Nonce      int    `json:"nonce"`
	Difficulty int    `json:"difficulty"`
}

func CreateBlock

func CreateBlock(data string, lastBlock []byte, difficulty int) *Block

func Deserialize

func Deserialize(data []byte) *Block

Deserializes bytestring into block when loading from persistance storage

func Genesis

func Genesis() *Block

Generates the first hardcoded Gensis block. The data used for the hash is NYTimes headline title from August 7, 2020.

func (*Block) AdjustDiff

func (block *Block) AdjustDiff(timestamp int64)

Utility function to adjust the difficulty of the proof of work algorithm. Difficulty is adjusted based on mineRate set at top line

func (*Block) PrintBlock

func (block *Block) PrintBlock()

Prints block information for debugging purposes

func (*Block) Serialize

func (b *Block) Serialize() []byte

Serializes a block into bytestring for persistance storage

type Blockchain

type Blockchain struct {
	Database *bolt.DB
	// contains filtered or unexported fields
}

func CreateBlockchain

func CreateBlockchain() *Blockchain

Creates a new blockchain if there is no existing blockchain and store Gensis block inside. If there is an existing blockchain, set the head of the blockchain instance to the last block block hash stored in the database

func (*Blockchain) AddBlock

func (chain *Blockchain) AddBlock(data string)

func (*Blockchain) Iterator

func (chain *Blockchain) Iterator() *ChainIterator

type ChainIterator

type ChainIterator struct {
	Database *bolt.DB
	// contains filtered or unexported fields
}

func (*ChainIterator) Next

func (i *ChainIterator) Next() *Block

type ProofOfWork

type ProofOfWork struct {
	Block  *Block
	Target *big.Int
}

func CreatePow

func CreatePow(b *Block) *ProofOfWork

func (*ProofOfWork) ComputeHash

func (pow *ProofOfWork) ComputeHash() (int, []byte)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL