Documentation
¶
Index ¶
- Constants
- Variables
- func InitializeDataPath()
- func RemoveDataPath()
- type Block
- func (b Block) DeleteFile() error
- func (b Block) Export() error
- func (b Block) GetBy() string
- func (b Block) GetHeader() BlockHeader
- func (b Block) GetHeight() uint64
- func (b Block) GetNodes() []*merkletree.MerkleNode
- func (b *Block) Import(hash string) error
- func (b *Block) IsEmpty() bool
- func (b *Block) VerifyBlock() (bool, error)
- type BlockChain
- func (bc *BlockChain) AddBlock(block *Block) error
- func (bc *BlockChain) FindExec(id string, hash string) (*job.Exec, error)
- func (bc *BlockChain) FindJob(id string) (*job.Job, error)
- func (bc *BlockChain) FindMerkleNode(h string) (*merkletree.MerkleNode, error)
- func (bc *BlockChain) GetBlockByHeight(height int) (*Block, error)
- func (bc *BlockChain) GetBlockHashes() ([]string, error)
- func (bc *BlockChain) GetBlockHashesHex() ([]string, error)
- func (bc *BlockChain) GetBlockInfo(hash string) (*BlockInfo, error)
- func (bc *BlockChain) GetBlocksWithinMinute() ([]Block, error)
- func (bc *BlockChain) GetJobExecs(id string) ([]job.Exec, error)
- func (bc *BlockChain) GetLatest15() ([]Block, error)
- func (bc *BlockChain) GetLatestBlock() (*Block, error)
- func (bc *BlockChain) GetLatestHeight() (int, error)
- func (bc BlockChain) GetNextHeight() (uint64, error)
- func (bc BlockChain) GetPrevHash() (string, error)
- func (bc *BlockChain) InitGenesisBlock(nodeID string)
- func (bc *BlockChain) Verify() (bool, error)
- 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 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") )
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") )
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 NewBlock ¶
func NewBlock(tree merkletree.MerkleTree, pHash string, height uint64, difficulty uint8, by string) (*Block, error)
NewBlock returns a new block
func (Block) GetNodes ¶
func (b Block) GetNodes() []*merkletree.MerkleNode
GetNodes retuns the block's merklenodes
func (*Block) VerifyBlock ¶
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) 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) GetMerkleRoot ¶
func (bh BlockHeader) GetMerkleRoot() string
GetMerkleRoot returns merkleroot
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) 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