blockchain

package
v0.0.0-...-c2a4f5a Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 12

Difficulty is a number. The hire the number, the harder the difficulty.

Variables

This section is empty.

Functions

func ToHex

func ToHex(num int64) []byte

ToHex converts a number to base 10 binary.

Types

type Block

type Block struct {
	Hash     []byte
	Data     []byte
	PrevHash []byte
	Nonce    int
}

Block is the struct for each block in the blockchain.

func CreateBlock

func CreateBlock(data string, prevHash []byte) *Block

CreateBlock creates a new block in the blockchain.

func Deserialize

func Deserialize(data []byte) *Block

Deserialize takes in encoded data and returns an un-encoded block.

func Genesis

func Genesis() *Block

Genesis creates the blockchains first block.

func (*Block) Serialize

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

Serialize encodes a block.

type Blockchain

type Blockchain struct {
	LastHash []byte
	Database *badger.DB
}

Blockchain is a list of blocks in the blockchain.

func InitBlockchain

func InitBlockchain() *Blockchain

InitBlockchain initializes the blockchain.

func (*Blockchain) AddBlock

func (chain *Blockchain) AddBlock(data string)

AddBlock adds a block to the blockchain.

func (*Blockchain) Iterator

func (chain *Blockchain) Iterator() *Iterator

Iterator converts a blockchain to a blockchain iterator.

type Iterator

type Iterator struct {
	CurrentHash []byte
	Database    *badger.DB
}

Iterator is struct for the current iteration of the blockchain.

func (*Iterator) Next

func (iter *Iterator) Next() *Block

Next gives you the "next" block, working backwards through the blockchain.

type ProofOfWork

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

ProofOfWork is the struct fot our proof of work algorithm.

func NewProof

func NewProof(b *Block) *ProofOfWork

NewProof creates a new proof of work.

func (*ProofOfWork) InitData

func (pow *ProofOfWork) InitData(nonce int) []byte

InitData initialized the data.

func (*ProofOfWork) Run

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

Run initiates the proof of work algorithm.

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate makes sure the proof of work is valid.

Jump to

Keyboard shortcuts

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