core

package
v0.0.0-...-db26c95 Latest Latest
Warning

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

Go to latest
Published: May 26, 2018 License: MIT Imports: 22 Imported by: 27

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   = errors.New("Unable to export block")
	ErrHashModification = errors.New("Attempt to modify hash value of block")
)
View Source
var (
	ErrUnverifiedBlock = errors.New("Unverified block cannot be added to the blockchain")
	ErrJobNotFound     = errors.New("Job not found")
	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 DeserializeBlock

func DeserializeBlock(b []byte) (*Block, error)

DeserializeBlock returns block from bytes

func GenesisBlock

func GenesisBlock(by string) *Block

GenesisBlock returns genesis block

func NewBlock

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

NewBlock returns a new block

func (Block) DeleteFile

func (b Block) DeleteFile()

DeleteFile deletes block file on disk

func (Block) Export

func (b Block) Export() error

writes block on disk

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 []byte)

Import reads block file into memory

func (*Block) IsEmpty

func (b *Block) IsEmpty() bool

IsEmpty returns true is block is empty

func (*Block) Serialize

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

Serialize returns bytes of block

func (*Block) VerifyBlock

func (b *Block) VerifyBlock() bool

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 []byte) (*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 []byte) (*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() [][]byte

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

func (*BlockChain) GetBlockHashesHex

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

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

func (*BlockChain) GetBlockInfo

func (bc *BlockChain) GetBlockInfo(hash []byte) (*BlockInfo, error)

GetBlockInfo returns the blockinfo of a particular block from the db

func (*BlockChain) GetBlocksWithinMinute

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

GetBlocksWithinMinute returns all blocks in the db within the last minute

func (*BlockChain) GetJobExecs

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

GetJobExecs returns all execs of a job

func (*BlockChain) GetLatest15

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

GetLatest15 retuns the latest 15 blocks

func (*BlockChain) GetLatestBlock

func (bc *BlockChain) GetLatestBlock() *Block

GetLatestBlock returns the tip as a block

func (*BlockChain) GetLatestHeight

func (bc *BlockChain) GetLatestHeight() uint64

GetLatestHeight returns the height of the latest block to the blockchain

func (BlockChain) GetNextHeight

func (bc BlockChain) GetNextHeight() uint64

GetNextHeight returns the next height in the blockchain

func (BlockChain) GetPrevHash

func (bc BlockChain) GetPrevHash() []byte

GetPrevHash returns the hash of the last block in the bc

func (*BlockChain) Verify

func (bc *BlockChain) Verify() bool

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

Next returns the next block in the blockchain

func (*BlockChainIterator) NextBlockinfo

func (i *BlockChainIterator) NextBlockinfo() *BlockInfo

Next returns the next blockinfo in the blockchain - more lightweight

type BlockHeader

type BlockHeader struct {
	Timestamp     int64
	PrevBlockHash []byte
	MerkleRoot    []byte
	Nonce         uint64
	Difficulty    *big.Int
	Hash          []byte
}

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() []byte

GetHash returns hash

func (BlockHeader) GetMerkleRoot

func (bh BlockHeader) GetMerkleRoot() []byte

GetMerkleRoot returns merkleroot

func (BlockHeader) GetNonce

func (bh BlockHeader) GetNonce() uint64

GetNonce returns the nonce

func (BlockHeader) GetPrevBlockHash

func (bh BlockHeader) GetPrevBlockHash() []byte

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 DeserializeBlockInfo

func DeserializeBlockInfo(bi []byte) *BlockInfo

DeserializeBlockInfo return blockinfo

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

func (*BlockInfo) Serialize

func (bi *BlockInfo) Serialize() []byte

Serialize returns the blockinfo in json bytes

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

Validate - validates POW

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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