core

package
v0.0.0-...-eef6c14 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2018 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const BlockBucket = "blocks"

BlockBucket is the name of the bucket for the blockchain database

View Source
const BlockFile = "%s.blk"

BlockFile is the format of block filenames

View Source
const IndexDB = "bc_%s.db" // node id

IndexDB is the database file for the node

Variables

View Source
var (
	//ErrUnableToExport occurs when unable to write blockfile to disk
	ErrUnableToExport = errors.New("Unable to export block")
	//ErrHashModification occurs when hash of block is attempted to be modified
	ErrHashModification = errors.New("Attempt to modify hash value of block")
)
View Source
var (
	//ErrUnverifiedBlock when unable to verify block
	ErrUnverifiedBlock = errors.New("Unverified block cannot be added to the blockchain")
	//ErrJobNotFound when unable to find job in bc
	ErrJobNotFound = errors.New("Job not found")
	//ErrBlockNotFound when unable to finc block in the bc
	ErrBlockNotFound = errors.New("Blockinfo not found")
)
View Source
var BlockPathDev = path.Join(os.Getenv("HOME"), ".gizo-dev", "blocks")

BlockPathDev is the path block files are saved on the disk for development

View Source
var BlockPathProd = path.Join(os.Getenv("HOME"), ".gizo", "blocks")

BlockPathProd is the path block files are saved on the disk for production

View Source
var IndexPathDev = path.Join(os.Getenv("HOME"), ".gizo-dev")

IndexPathDev is the path database files are saved on the disk for development

View Source
var IndexPathProd = path.Join(os.Getenv("HOME"), ".gizo")

IndexPathProd is the path database files are saved on the disk for production

Functions

func InitializeDataPath

func InitializeDataPath()

InitializeDataPath creates .gizo folder and block subfolder

func RemoveDataPath

func RemoveDataPath()

RemoveDataPath delete's .gizo / .gizo-dev folder

Types

type Block

type Block struct {
	Header     BlockHeader
	Jobs       []*merkletree.MerkleNode
	Height     uint64
	ReceivedAt int64  //time it was received
	By         string // id of node that generated block
}

Block - the foundation of blockchain

func GenesisBlock

func GenesisBlock(by string) *Block

GenesisBlock returns genesis block

func NewBlock

func NewBlock(tree merkletree.MerkleTree, pHash string, height uint64, difficulty uint8, by string) (*Block, error)

NewBlock returns a new block

func (Block) DeleteFile

func (b Block) DeleteFile() error

DeleteFile deletes block file on disk

func (Block) Export

func (b Block) Export() error

Export writes block on disk

func (Block) GetBy

func (b Block) GetBy() string

GetBy returns id of node that mined block

func (Block) GetHeader

func (b Block) GetHeader() BlockHeader

GetHeader returns the block header

func (Block) GetHeight

func (b Block) GetHeight() uint64

GetHeight returns the block height

func (Block) GetNodes

func (b Block) GetNodes() []*merkletree.MerkleNode

GetNodes retuns the block's merklenodes

func (*Block) Import

func (b *Block) Import(hash string) error

Import reads block file into memory

func (*Block) IsEmpty

func (b *Block) IsEmpty() bool

IsEmpty returns true is block is empty

func (*Block) VerifyBlock

func (b *Block) VerifyBlock() (bool, error)

VerifyBlock verifies a block

type BlockChain

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

BlockChain - singly linked list of blocks

func CreateBlockChain

func CreateBlockChain(nodeID string) *BlockChain

CreateBlockChain initializes a db, set's the tip to GenesisBlock and returns the blockchain

func (*BlockChain) AddBlock

func (bc *BlockChain) AddBlock(block *Block) error

AddBlock adds block to the blockchain

func (*BlockChain) FindExec

func (bc *BlockChain) FindExec(id string, hash string) (*job.Exec, error)

FindExec finds exec in the bc

func (*BlockChain) FindJob

func (bc *BlockChain) FindJob(id string) (*job.Job, error)

FindJob returns the job from the blockchain

func (*BlockChain) FindMerkleNode

func (bc *BlockChain) FindMerkleNode(h string) (*merkletree.MerkleNode, error)

FindMerkleNode returns the merklenode from the blockchain

func (*BlockChain) GetBlockByHeight

func (bc *BlockChain) GetBlockByHeight(height int) (*Block, error)

GetBlockByHeight return block by height

func (*BlockChain) GetBlockHashes

func (bc *BlockChain) GetBlockHashes() ([]string, error)

GetBlockHashes returns all the hashes of all the blocks in the current bc

func (*BlockChain) GetBlockHashesHex

func (bc *BlockChain) GetBlockHashesHex() ([]string, error)

GetBlockHashesHex returns hashes (hex) of all the blocks in the bc

func (*BlockChain) GetBlockInfo

func (bc *BlockChain) GetBlockInfo(hash string) (*BlockInfo, error)

GetBlockInfo returns the blockinfo of a particular block from the db

func (*BlockChain) GetBlocksWithinMinute

func (bc *BlockChain) GetBlocksWithinMinute() ([]Block, error)

GetBlocksWithinMinute returns all blocks in the db within the last minute

func (*BlockChain) GetJobExecs

func (bc *BlockChain) GetJobExecs(id string) ([]job.Exec, error)

GetJobExecs returns all execs of a job

func (*BlockChain) GetLatest15

func (bc *BlockChain) GetLatest15() ([]Block, error)

GetLatest15 retuns the latest 15 blocks

func (*BlockChain) GetLatestBlock

func (bc *BlockChain) GetLatestBlock() (*Block, error)

GetLatestBlock returns the tip as a block

func (*BlockChain) GetLatestHeight

func (bc *BlockChain) GetLatestHeight() (int, error)

GetLatestHeight returns the height of the latest block to the blockchain

func (BlockChain) GetNextHeight

func (bc BlockChain) GetNextHeight() (uint64, error)

GetNextHeight returns the next height in the blockchain

func (BlockChain) GetPrevHash

func (bc BlockChain) GetPrevHash() (string, error)

GetPrevHash returns the hash of the last block in the bc

func (*BlockChain) InitGenesisBlock

func (bc *BlockChain) InitGenesisBlock(nodeID string)

InitGenesisBlock creates genesis block

func (*BlockChain) Verify

func (bc *BlockChain) Verify() (bool, error)

Verify verifies the blockchain

type BlockChainIterator

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

BlockChainIterator - a way to loop through the blockchain (from newest block to oldest block)

func (BlockChainIterator) GetCurrent

func (i BlockChainIterator) GetCurrent() []byte

GetCurrent returns current block

func (*BlockChainIterator) Next

func (i *BlockChainIterator) Next() (*Block, error)

Next returns the next block in the blockchain

func (*BlockChainIterator) NextBlockinfo

func (i *BlockChainIterator) NextBlockinfo() (*BlockInfo, error)

NextBlockinfo returns the next blockinfo in the blockchain - more lightweight

type BlockHeader

type BlockHeader struct {
	Timestamp     int64
	PrevBlockHash string
	MerkleRoot    string // hash of merkle root
	Nonce         uint64
	Difficulty    *big.Int
	Hash          string
}

BlockHeader holds the header of the block

func (BlockHeader) GetDifficulty

func (bh BlockHeader) GetDifficulty() *big.Int

GetDifficulty returns difficulty

func (BlockHeader) GetHash

func (bh BlockHeader) GetHash() string

GetHash returns hash

func (BlockHeader) GetMerkleRoot

func (bh BlockHeader) GetMerkleRoot() string

GetMerkleRoot returns merkleroot

func (BlockHeader) GetNonce

func (bh BlockHeader) GetNonce() uint64

GetNonce returns the nonce

func (BlockHeader) GetPrevBlockHash

func (bh BlockHeader) GetPrevBlockHash() string

GetPrevBlockHash returns previous block hash

func (BlockHeader) GetTimestamp

func (bh BlockHeader) GetTimestamp() int64

GetTimestamp returns timestamp

type BlockInfo

type BlockInfo struct {
	Header    BlockHeader
	Height    uint64
	TotalJobs uint
	FileName  string
	FileSize  int64
}

BlockInfo - model of data written to embedded database

func (BlockInfo) GetBlock

func (bi BlockInfo) GetBlock() *Block

GetBlock - imports block from file into memory

func (BlockInfo) GetFileName

func (bi BlockInfo) GetFileName() string

GetFileName returns filename

func (BlockInfo) GetFileSize

func (bi BlockInfo) GetFileSize() int64

GetFileSize returns the blocks filesize

func (BlockInfo) GetHeader

func (bi BlockInfo) GetHeader() BlockHeader

GetHeader returns block header

func (BlockInfo) GetHeight

func (bi BlockInfo) GetHeight() uint64

GetHeight return height

func (BlockInfo) GetTotalJobs

func (bi BlockInfo) GetTotalJobs() uint

GetTotalJobs returns total jobs

type POW

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

POW - consensus algorithm

func NewPOW

func NewPOW(b *Block) *POW

NewPOW returns POW

func (POW) GetBlock

func (p POW) GetBlock() *Block

GetBlock returns block

func (POW) GetDifficulty

func (p POW) GetDifficulty() uint8

GetDifficulty returns difficulty

func (POW) GetTarget

func (p POW) GetTarget() *big.Int

GetTarget returns target

func (*POW) Validate

func (p *POW) Validate() (bool, error)

Validate - validates POW

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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