blockchain

package
v0.0.0-...-f7c9202 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2018 License: MIT Imports: 5 Imported by: 4

Documentation

Index

Constants

View Source
const (
	BCBAddState_error                = 0
	BCBAddState_addedToTop           = 1
	BCBAddState_addedToParallelTop   = 2
	BCBAddState_addedToParallel      = 3
	BCBAddState_notAddedNoPrev       = 4
	BCBAddState_notAddedExists       = 5
	BCBState_error                   = -1
	BCBState_canAdd                  = 0
	BCBState_exists                  = 1
	BCBState_notExistAndPrevNotExist = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Blockchain

type Blockchain struct {
	DB              database.DBManager
	Logger          *utils.LoggerMan
	HashCache       map[string]int
	LastHashInCache []byte
}

func NewBlockchainManager

func NewBlockchainManager(DB database.DBManager, Logger *utils.LoggerMan) (*Blockchain, error)

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block *structures.Block) (uint, error)
  • Add new block to the blockchain BCBAddState_error = 0 not added to the chain. Because of error BCBAddState_addedToTop = 1 added to the top of current chain BCBAddState_addedToParallelTop = 2 added to the top, but on other branch. Other branch becomes primary now BCBAddState_addedToParallel = 3 added but not in main branch and heigh i lower then main branch BCBAddState_notAddedNoPrev = 4 previous not found BCBAddState_notAddedExists = 5 already in blockchain

*

func (*Blockchain) CheckBlockExists

func (bc *Blockchain) CheckBlockExists(blockHash []byte) (bool, error)

func (*Blockchain) CheckBlockIsInRange

func (bc *Blockchain) CheckBlockIsInRange(blockHash []byte, bottomHash []byte, topHash []byte) (bool, error)

Receive ist of hashes and return a hash that is in the chain defined by top hash

func (*Blockchain) ChooseHashUnderTip

func (bc *Blockchain) ChooseHashUnderTip(blockHashes [][]byte, topHash []byte) ([]byte, error)

Receive ist of hashes and return a hash that is in the chain defined by top hash This function is used for transactions. to understand if a transaction exists in current chain branch or in some parallel and where a transaction is spent

func (*Blockchain) DeleteBlock

func (bc *Blockchain) DeleteBlock() (*structures.Block, error)

* DeleteBlock deletes the top block (last added) * The function extracts the last block, deletes it and sets the tip to point to * previous block. * TODO * It is needed to make some more correct logic. f a block is removed then tip could go to some other blocks branch that * is longer now. It is needed to care blockchain branches * Returns deleted block object

func (*Blockchain) GetBestHeight

func (bc *Blockchain) GetBestHeight() (int, error)

func (*Blockchain) GetBlock

func (bc *Blockchain) GetBlock(blockHash []byte) (structures.Block, error)

GetBlock finds a block by its hash and returns i

func (*Blockchain) GetBlockAtHeight

func (bc *Blockchain) GetBlockAtHeight(height int) (*structures.Block, error)

Returns a block with specified height in current blockchain TODO can be optimized using blocks index

func (*Blockchain) GetBlocksShortInfo

func (bc *Blockchain) GetBlocksShortInfo(startfrom []byte, maxcount int) []*structures.BlockShort

Returns a list of blocks short info stating from given block or from a top

func (*Blockchain) GetBranchesReplacement

func (bc *Blockchain) GetBranchesReplacement(sideBranchHash []byte, tip []byte) ([]*structures.Block, []*structures.Block, error)

* Returns a chain of blocks starting from a hash and till * end of blockchain or block from main chain found * if already in main chain then returns empty list * * The function load all hashes to the memory from "main" chain * TODO We need to use index of blocks

func (*Blockchain) GetFirstBlocks

func (bc *Blockchain) GetFirstBlocks(maxcount int) ([]*structures.Block, int, error)

Returns first blocks in block chain

func (*Blockchain) GetGenesisBlockHash

func (bc *Blockchain) GetGenesisBlockHash() ([]byte, error)

Returns genesis block hash First block is saved in sperate record in DB

func (*Blockchain) GetNextBlocks

func (bc *Blockchain) GetNextBlocks(startfrom []byte) ([]*structures.BlockShort, error)

returns a list of hashes of all the blocks in the chain

func (*Blockchain) GetSideBranch

func (bc *Blockchain) GetSideBranch(hash []byte, currentTip []byte) ([]*structures.Block, []*structures.Block, *structures.Block, error)

func (*Blockchain) GetState

func (bc *Blockchain) GetState() ([]byte, int, error)

func (*Blockchain) GetTransactionFromBlock

func (bc *Blockchain) GetTransactionFromBlock(txID []byte, blockHash []byte) (*structures.Transaction, error)

GetTransactionFromBlock finds a transaction by its ID in given block If block is known . It worsk much faster then FindTransaction

func (*Blockchain) UpdateChainOnNewBranch

func (bc *Blockchain) UpdateChainOnNewBranch(prevTopHash []byte) error

NOTE . Operation is done in memory. On practive we don't expect to have million of hashes in different branch. it can be 1-5 . Depends on consensus can be more. but not millions top hash is already update when we execute this

type BlockchainIterator

type BlockchainIterator struct {
	DB database.DBManager
	// contains filtered or unexported fields
}

BlockchainIterator is used to iterate over blockchain blocks

func NewBlockchainIterator

func NewBlockchainIterator(DB database.DBManager) (*BlockchainIterator, error)

func NewBlockchainIteratorFrom

func NewBlockchainIteratorFrom(DB database.DBManager, startHash []byte) (*BlockchainIterator, error)

Creates new Blockchain Iterator from given block hash. Can be used to do something with blockchain from outside

func (*BlockchainIterator) GetAddressHistory

func (i *BlockchainIterator) GetAddressHistory(pubKeyHash []byte, address string) ([]structures.TransactionsHistory, error)

Returns history of transactions for given address

func (*BlockchainIterator) Next

func (i *BlockchainIterator) Next() (*structures.Block, error)

Next returns next block starting from the tip

Jump to

Keyboard shortcuts

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