blockchain

package module
v0.0.0-...-d2b1f7f Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

README

Blockchain

GoDoc Build Status

A simple blockchain implementation.

Install

Fist, use go get to install the latest version of the library:

go get -u github.com/lynn9388/blockchain

Next, include this package in your application:

import "github.com/lynn9388/blockchain"

Example

genesis := GenesisBlock()
bc := NewBlockchain(genesis)
bc.AddBlock(genesis.Header.NewBlock(StringData("lynn9388")))

Documentation

Overview

Package blockchain provides a simple blockchain implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Header *BlockHeader `json:"header"`
	Data   [][]byte     `json:"data"`
}

Block holds batches of valid data/transactions.

func NewBlock

func NewBlock(prevBlockHeader *BlockHeader, extra []byte, data [][]byte) *Block

NewBlock creates a new block. If there is no extra info then extra can be nil (will be converted to empty) or empty. The data can not be nil or empty, because a block without data is invalid.

func NewGenesisBlock

func NewGenesisBlock() *Block

NewGenesisBlock returns the genesis block.

func (*Block) IsValid

func (b *Block) IsValid(prevBlockHeader *BlockHeader, isExtraValid func([]byte) bool) bool

IsValid checks if every fields in a block is valid. isExtraValid can be nil if check extra info is unnecessary.

type BlockHeader

type BlockHeader struct {
	Index      int    `json:"index"`
	Time       int64  `json:"time"`
	PrevHash   []byte `json:"prevHash"`
	MerkleRoot []byte `json:"merkleRoot"`
	Extra      []byte `json:"extra"`
}

BlockHeader holds the metadata of a block

func (*BlockHeader) Hash

func (bh *BlockHeader) Hash() []byte

Hash returns the SHA256 hash of the block header.

func (*BlockHeader) ToByte

func (bh *BlockHeader) ToByte() []byte

ToByte converts the block header to bytes.

type Blockchain

type Blockchain struct {
	DB      *bolt.DB // DB stored the blockchain
	Tips    []*Block // last block of all branches
	BestTip *Block   // last block of longest branch
}

Blockchain implements interactions with a DB.

func NewBlockchain

func NewBlockchain(dbFile string, genesis *Block) *Blockchain

NewBlockchain creates a blockchain from DB file. If the file does not exist then it will be created and initialize a new blockchain with genesis block.

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(b *Block, isExtraValid func([]byte) bool)

AddBlock saves a block into the blockchain if it is valid. isExtraValid can be nil if check extra info is unnecessary.

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator is used to iterate over blockchain's blocks.

func NewIterator

func NewIterator(bc *Blockchain, blockHash []byte) *Iterator

NewIterator returns a iterator with a start block.

func (*Iterator) Prev

func (i *Iterator) Prev() *Block

Prev returns the block pointed by the iterator and move to the previous block. It will end with genesis block and then return nil.

Directories

Path Synopsis
Package network is a simple blockchain network implementation.
Package network is a simple blockchain network implementation.

Jump to

Keyboard shortcuts

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