Documentation ¶
Index ¶
- Constants
- Variables
- func InitializeDataPath()
- func RemoveDataPath()
- type Block
- func (b Block) DeleteFile()
- func (b Block) Export() error
- func (b Block) GetHeader() BlockHeader
- func (b Block) GetHeight() uint64
- func (b Block) GetNodes() []*merkletree.MerkleNode
- func (b *Block) Import(hash []byte)
- func (b *Block) IsEmpty() bool
- func (b *Block) Serialize() []byte
- func (b *Block) VerifyBlock() bool
- type BlockChain
- func (bc *BlockChain) AddBlock(block *Block) error
- func (bc *BlockChain) FindExec(id string, hash []byte) (*job.Exec, error)
- func (bc *BlockChain) FindJob(id string) (*job.Job, error)
- func (bc *BlockChain) FindMerkleNode(h []byte) (*merkletree.MerkleNode, error)
- func (bc *BlockChain) GetBlockByHeight(height int) (*Block, error)
- func (bc *BlockChain) GetBlockHashes() [][]byte
- func (bc *BlockChain) GetBlockHashesHex() []string
- func (bc *BlockChain) GetBlockInfo(hash []byte) (*BlockInfo, error)
- func (bc *BlockChain) GetBlocksWithinMinute() []Block
- func (bc *BlockChain) GetJobExecs(id string) []job.Exec
- func (bc *BlockChain) GetLatest15() []Block
- func (bc *BlockChain) GetLatestBlock() *Block
- func (bc *BlockChain) GetLatestHeight() uint64
- func (bc BlockChain) GetNextHeight() uint64
- func (bc BlockChain) GetPrevHash() []byte
- func (bc *BlockChain) Verify() bool
- type BlockChainIterator
- type BlockHeader
- type BlockInfo
- type POW
Constants ¶
const BlockBucket = "blocks"
BlockBucket is the name of the bucket for the blockchain database
const BlockFile = "%s.blk"
BlockFile is the format of block filenames
const IndexDB = "bc_%s.db" // node id
IndexDB is the database file for the node
Variables ¶
var ( ErrUnableToExport = errors.New("Unable to export block") ErrHashModification = errors.New("Attempt to modify hash value of block") )
var ( ErrUnverifiedBlock = errors.New("Unverified block cannot be added to the blockchain") ErrJobNotFound = errors.New("Job not found") ErrBlockNotFound = errors.New("Blockinfo not found") )
var BlockPathDev = path.Join(os.Getenv("HOME"), ".gizo-dev", "blocks")
BlockPathDev is the path block files are saved on the disk for development
var BlockPathProd = path.Join(os.Getenv("HOME"), ".gizo", "blocks")
BlockPathProd is the path block files are saved on the disk for production
var IndexPathDev = path.Join(os.Getenv("HOME"), ".gizo-dev")
IndexPathDev is the path database files are saved on the disk for development
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
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 ¶
DeserializeBlock returns block from bytes
func NewBlock ¶
func NewBlock(tree merkletree.MerkleTree, pHash []byte, height uint64, difficulty uint8, by string) *Block
NewBlock returns a new block
func (Block) GetNodes ¶
func (b Block) GetNodes() []*merkletree.MerkleNode
GetNodes retuns the block's merklenodes
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) 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
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) GetMerkleRoot ¶
func (bh BlockHeader) GetMerkleRoot() []byte
GetMerkleRoot returns merkleroot
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 ¶
DeserializeBlockInfo return blockinfo
func (BlockInfo) GetFileName ¶
GetFileName returns filename
func (BlockInfo) GetFileSize ¶
GetFileSize returns the blocks filesize
func (BlockInfo) GetHeader ¶
func (bi BlockInfo) GetHeader() BlockHeader
GetHeader returns block header
func (BlockInfo) GetTotalJobs ¶
GetTotalJobs returns total jobs