blockchain

package
v0.1.8-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2019 License: GPL-3.0 Imports: 30 Imported by: 3

Documentation

Overview

Package blockchain provides functionalities for creating, managing and accessing the blockchain state.

Index

Constants

View Source
const (
	// MaxOrphanBlocksCacheSize is the number of blocks we can keep in the orphan block cache
	MaxOrphanBlocksCacheSize = 500

	// MaxRejectedBlocksCacheSize is the number of blocks we can keep in the rejected block cache
	MaxRejectedBlocksCacheSize = 100
)

Variables

View Source
var (
	// GenesisBlockFileName is the name of the file that contains the genesis block
	GenesisBlockFileName = "genesis.json"
)
View Source
var KnownTransactionTypes = []int64{
	core.TxTypeBalance,
	core.TxTypeAlloc,
}

KnownTransactionTypes are the supported transaction types

Functions

func LoadBlockFromFile

func LoadBlockFromFile(name string) (types.Block, error)

LoadBlockFromFile loads a block from a file

Types

type BlockValidator

type BlockValidator struct {
	VContexts
	// contains filtered or unexported fields
}

BlockValidator implements a validator for checking syntactic, contextual and state correctness of a block in relation to various parts of the system.

func NewBlockValidator

func NewBlockValidator(block types.Block, txPool types.TxPool,
	bChain types.Blockchain, cfg *config.EngineConfig,
	log logger.Logger) *BlockValidator

NewBlockValidator creates and returns a BlockValidator object

func (*BlockValidator) CheckAllocs

func (v *BlockValidator) CheckAllocs() (errs []error)

CheckAllocs verifies allocation transactions such as transaction fees, mining rewards etc.

func (*BlockValidator) CheckFields

func (v *BlockValidator) CheckFields() (errs []error)

CheckFields checks the field and their values.

func (*BlockValidator) CheckPoW

func (v *BlockValidator) CheckPoW(opts ...types.CallOp) (errs []error)

CheckPoW checks the PoW and difficulty values in the header. If chain is set, the parent chain is search within the provided chain, otherwise, the best chain is searched

func (*BlockValidator) CheckSize

func (v *BlockValidator) CheckSize() (errs []error)

CheckSize checks the size of the blocks

func (*BlockValidator) CheckTransactions

func (v *BlockValidator) CheckTransactions(opts ...types.CallOp) (errs []error)

CheckTransactions validates all transactions in the block in relation to the block's destined chain.

type Blockchain

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

Blockchain represents the Ellcrys blockchain. It provides functionalities for interacting with the underlying database and primitives.

func New

func New(txPool types.TxPool, cfg *config.EngineConfig, log logger.Logger) *Blockchain

New creates a Blockchain instance.

func (*Blockchain) APIs

func (b *Blockchain) APIs() jsonrpc.APISet

APIs returns all API handlers

func (*Blockchain) ChainReader

func (b *Blockchain) ChainReader() types.ChainReaderFactory

ChainReader creates a chain reader to read the main chain

func (*Blockchain) CreateAccount

func (b *Blockchain) CreateAccount(blockNo uint64, chain types.Chainer,
	account types.Account) error

CreateAccount creates an account that is associated with the given block number and chain.

func (*Blockchain) Generate

func (b *Blockchain) Generate(params *types.GenerateBlockParams, opts ...types.CallOp) (types.Block,
	error)

Generate produces a valid block for a target chain. By default the main chain is used but a different chain can be passed in as a CallOp.

func (*Blockchain) GetAccount

func (b *Blockchain) GetAccount(address util.String,
	opts ...types.CallOp) (types.Account, error)

GetAccount gets an account by its address

func (*Blockchain) GetAccountNonce

func (b *Blockchain) GetAccountNonce(address util.String,
	opts ...types.CallOp) (uint64, error)

GetAccountNonce gets the nonce of an account

func (*Blockchain) GetBestChain

func (b *Blockchain) GetBestChain() types.Chainer

GetBestChain gets the chain that is currently considered the main chain

func (*Blockchain) GetBlock

func (b *Blockchain) GetBlock(number uint64, hash util.Hash) (types.Block, error)

GetBlock finds a block in any chain with a matching block number and hash.

func (*Blockchain) GetBlockByHash

func (b *Blockchain) GetBlockByHash(hash util.Hash, opts ...types.CallOp) (types.Block, error)

GetBlockByHash finds a block in any chain with a matching hash.

func (*Blockchain) GetChainReaderByHash

func (b *Blockchain) GetChainReaderByHash(hash util.Hash) types.ChainReaderFactory

GetChainReaderByHash returns a chain reader to a chain where a block with the given hash exists

func (*Blockchain) GetChainsReader

func (b *Blockchain) GetChainsReader() (readers []types.ChainReaderFactory)

GetChainsReader gets chain reader for all known chains

func (*Blockchain) GetDB

func (b *Blockchain) GetDB() elldb.DB

GetDB gets the database

func (*Blockchain) GetEventEmitter

func (b *Blockchain) GetEventEmitter() *emitter.Emitter

GetEventEmitter gets the event emitter

func (*Blockchain) GetLocators

func (b *Blockchain) GetLocators() ([]util.Hash, error)

GetLocators fetches a list of block hashes used to compare and sync the local chain with a remote chain. We collect the most recent 10 block hashes and then exponentially fetch more hashes until there are no more blocks. The genesis block must be added as the last hash if not already included.

func (*Blockchain) GetTransaction

func (b *Blockchain) GetTransaction(hash util.Hash,
	opts ...types.CallOp) (types.Transaction, error)

GetTransaction finds a transaction in the main chain and returns it

func (*Blockchain) GetTxPool

func (b *Blockchain) GetTxPool() types.TxPool

GetTxPool gets the transaction pool

func (*Blockchain) HaveBlock

func (b *Blockchain) HaveBlock(hash util.Hash) (bool, error)

HaveBlock checks whether we have a block matching the hash in any of the known chains

func (*Blockchain) IsMainChain

func (b *Blockchain) IsMainChain(cr types.ChainReaderFactory) bool

IsMainChain checks whether cr is the main chain

func (*Blockchain) ListAccounts

func (b *Blockchain) ListAccounts(opts ...types.CallOp) ([]types.Account, error)

ListAccounts list all accounts

func (*Blockchain) ListTopAccounts

func (b *Blockchain) ListTopAccounts(n int, opts ...types.CallOp) ([]types.Account, error)

ListTopAccounts lists top n accounts

func (*Blockchain) NewChainFromChainInfo

func (b *Blockchain) NewChainFromChainInfo(ci types.ChainInfo) *Chain

NewChainFromChainInfo creates an instance of a Chain given a NewChainFromChainInfo

func (*Blockchain) NewChainTraverser

func (b *Blockchain) NewChainTraverser() *ChainTraverser

NewChainTraverser creates a new ChainTransverser instance

func (*Blockchain) NewWorldReader

func (b *Blockchain) NewWorldReader() *WorldReader

NewWorldReader creates a new WorldReader

func (*Blockchain) OrphanBlocks

func (b *Blockchain) OrphanBlocks() types.CacheReader

OrphanBlocks returns a cache reader for orphan blocks

func (*Blockchain) ProcessBlock

func (b *Blockchain) ProcessBlock(block types.Block,
	opts ...types.CallOp) (types.ChainReaderFactory, error)

ProcessBlock takes a block, performs initial validations and attempts to add it to the tip of one of the known chains (main chain or forked chain). It returns a chain reader pointing to the chain in which the block was added to.

func (*Blockchain) ProcessTransactions

func (b *Blockchain) ProcessTransactions(txs []types.Transaction, chain types.Chainer,
	opts ...types.CallOp) ([]common.Transition, error)

ProcessTransactions computes the state transition operations for each transactions that must be applied to the state tree and world state

func (*Blockchain) SetBestChain

func (b *Blockchain) SetBestChain(c *Chain)

SetBestChain sets the current main chain

func (*Blockchain) SetCoinbase

func (b *Blockchain) SetCoinbase(coinbase *crypto.Key)

SetCoinbase sets the coinbase key that is used to identify the current blockchain instance

func (*Blockchain) SetDB

func (b *Blockchain) SetDB(db elldb.DB)

SetDB sets the database to use

func (*Blockchain) SetEventEmitter

func (b *Blockchain) SetEventEmitter(ee *emitter.Emitter)

getBlockByHash finds and returns a block by hash only SetEventEmitter sets the event emitter

func (*Blockchain) SetGenesisBlock

func (b *Blockchain) SetGenesisBlock(block types.Block)

SetGenesisBlock sets the genesis block

func (*Blockchain) Up

func (b *Blockchain) Up() error

Up opens the database, initializes the store and creates the genesis block (if required)

type Chain

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

Chain represents a chain of blocks Implements types.Chainer

func NewChain

func NewChain(id util.String, db elldb.DB,
	cfg *config.EngineConfig, log logger.Logger) *Chain

NewChain creates an instance of a chain. It will create metadata object for the chain if not exists. It will return error if it is unable to do so.

func NewChainFromChainInfo

func NewChainFromChainInfo(ci *core.ChainInfo, db elldb.DB,
	cfg *config.EngineConfig, log logger.Logger) *Chain

NewChainFromChainInfo creates a chain with a given chain info

func (*Chain) ChainReader

func (c *Chain) ChainReader() types.ChainReaderFactory

ChainReader gets a chain reader for this chain

func (*Chain) CreateAccount

func (c *Chain) CreateAccount(targetBlockNum uint64, account types.Account, opts ...types.CallOp) error

CreateAccount creates an account on a target block

func (*Chain) Current

func (c *Chain) Current(opts ...types.CallOp) (types.Header, error)

Current returns the header of the highest block on the chain

func (*Chain) GetAccount

func (c *Chain) GetAccount(address util.String, opts ...types.CallOp) (types.Account, error)

GetAccount gets an account

func (*Chain) GetAccounts

func (c *Chain) GetAccounts(opts ...types.CallOp) ([]types.Account, error)

GetAccounts gets all accounts

func (*Chain) GetBlock

func (c *Chain) GetBlock(number uint64, opts ...types.CallOp) (types.Block, error)

GetBlock fetches a block by its number

func (*Chain) GetID

func (c *Chain) GetID() util.String

GetID returns the id of the chain

func (*Chain) GetInfo

func (c *Chain) GetInfo() types.ChainInfo

GetInfo gets the chain information

func (*Chain) GetMinedBlocks

func (c *Chain) GetMinedBlocks(args *core.ArgGetMinedBlock, opts ...types.CallOp) ([]*core.MinedBlock, bool, error)

GetMinedBlocks fetches mined blocks. It allows the query to be adjusted using values in args.

  • args.Limit forces only a limited number of results to be returned.
  • args.CreatorPubKey filters out results that do not match a given public key.
  • args.LastHash allows only records after the given hash to be returned. Useful for pagination.

It returns a slice of mined blocks and also a boolean that indicates whether there are more records.

func (*Chain) GetParent

func (c *Chain) GetParent(opts ...types.CallOp) *Chain

GetParent gets an instance of this chain's parent

func (*Chain) GetParentBlock

func (c *Chain) GetParentBlock() types.Block

GetParentBlock gets the chain's parent block if it has one

func (*Chain) GetRoot

func (c *Chain) GetRoot() types.Block

GetRoot finds the block on the main chain from which this chain or its parents/ancestors originate from.

Example: [1]-[2]-[3]-[4]-[5] Main

|__[3]-[4]		Chain B
    |__[4]		Chain C

In the example above, the Chain B is the first generation to Chain C. The root parent block of Chain C is [2].

func (*Chain) GetStore

func (c *Chain) GetStore() types.ChainStorer

GetStore gets the store

func (*Chain) GetTransaction

func (c *Chain) GetTransaction(hash util.Hash, opts ...types.CallOp) (types.Transaction, error)

GetTransaction gets a transaction by hash

func (*Chain) HasParent

func (c *Chain) HasParent(opts ...types.CallOp) bool

HasParent checks whether the chain has a parent

func (*Chain) NewStateTree

func (c *Chain) NewStateTree(opts ...types.CallOp) (types.Tree, error)

NewStateTree creates a new tree seeded with the state root of the chain's tip block. For chains with no block (new chains), the state root of their parent block is used.

func (*Chain) PutMinedBlock

func (c *Chain) PutMinedBlock(block types.Block, opts ...types.CallOp) error

PutMinedBlock records a block mined by the block creator

func (*Chain) PutTransactions

func (c *Chain) PutTransactions(txs []types.Transaction, blockNumber uint64, opts ...types.CallOp) error

PutTransactions stores a collection of transactions in the chain

func (Chain) String

func (c Chain) String() string

type ChainReader

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

ChainReader provides read-only access to objects belonging to a single chain. It implements ChainReaderFactory interface

func NewChainReader

func NewChainReader(ch *Chain) *ChainReader

NewChainReader creates a ChainReader object

func (*ChainReader) Current

func (r *ChainReader) Current(opts ...types.CallOp) (types.Block, error)

Current gets the current block at the tip of the chain

func (*ChainReader) GetAccount

func (r *ChainReader) GetAccount(address util.String, opts ...types.CallOp) (types.Account, error)

GetAccount gets an account

func (*ChainReader) GetBlock

func (r *ChainReader) GetBlock(number uint64, opts ...types.CallOp) (types.Block, error)

GetBlock finds and returns a block associated with chainID. When 0 is passed, it should return the block with the highest number

func (*ChainReader) GetBlockByHash

func (r *ChainReader) GetBlockByHash(hash util.Hash, opts ...types.CallOp) (types.Block, error)

GetBlockByHash finds and returns a block associated with chainID.

func (*ChainReader) GetHeader

func (r *ChainReader) GetHeader(number uint64, opts ...types.CallOp) (types.Header, error)

GetHeader gets the header of a block. When 0 is passed, it should return the header of the block with the highest number

func (*ChainReader) GetHeaderByHash

func (r *ChainReader) GetHeaderByHash(hash util.Hash, opts ...types.CallOp) (types.Header, error)

GetHeaderByHash finds and returns the header of a block matching hash

func (*ChainReader) GetID

func (r *ChainReader) GetID() util.String

GetID gets the chain ID

func (*ChainReader) GetParent

func (r *ChainReader) GetParent() types.ChainReaderFactory

GetParent returns a chain reader to the parent chain. Returns nil if chain has no parent.

func (*ChainReader) GetParentBlock

func (r *ChainReader) GetParentBlock() types.Block

GetParentBlock returns the parent block

func (*ChainReader) GetRoot

func (r *ChainReader) GetRoot() types.Block

GetRoot fetches the root block of this chain. If the chain has more than one parents/ancestors, it will traverse the parents to return the root parent block.

type ChainTraverseFunc

type ChainTraverseFunc func(chain types.Chainer) (bool, error)

ChainTraverseFunc is the function type that runs a query against a given chain

type ChainTraverser

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

ChainTraverser allows a user to run a query function against a chain of chains. If no result is found in the start or initial chain, the parent chain is passed to the query function till we reach a chain with no parent.

func (*ChainTraverser) Query

func (t *ChainTraverser) Query(qf ChainTraverseFunc) error

Query begins a chain traversal session. If false is returned, the chain's parent is searched next and so on, until a chain with no parent is encountered. If true is returned, the query ends. If error is returned, the query ends with the error from qf returned.

func (*ChainTraverser) Start

func (t *ChainTraverser) Start(chain types.Chainer) *ChainTraverser

Start sets the start chain

type DocType

type DocType int

DocType represents a document type

const (
	// TypeBlock represents a block document type
	TypeBlock DocType = 0x1

	// TypeTx represents a transaction document type
	TypeTx DocType = 0x2
)

type ReOrgInfo

type ReOrgInfo struct {
	MainChainID string `json:"mainChainID" msgpack:"mainChainID"`
	BranchID    string `json:"branchID" msgpack:"branchID"`
	BranchLen   uint64 `json:"branchLen" msgpack:"branchLen"`
	ReOrgLen    uint64 `json:"reOrgLen" msgpack:"reOrgLen"`
	Timestamp   int64  `json:"timestamp" msgpack:"timestamp"`
}

ReOrgInfo describes a re-organization event

type TxsValidator

type TxsValidator struct {
	VContexts
	// contains filtered or unexported fields
}

TxsValidator implements a validator for checking syntactic, contextual and state correctness of transactions in relation to various parts of the system.

func NewTxValidator

func NewTxValidator(tx types.Transaction, txPool types.TxPool,
	bChain types.Blockchain) *TxsValidator

NewTxValidator is like NewTxsValidator except it accepts a single transaction

func NewTxsValidator

func NewTxsValidator(txs []types.Transaction, txPool types.TxPool,
	bChain types.Blockchain) *TxsValidator

NewTxsValidator creates an instance of TxsValidator

func (*TxsValidator) CheckFields

func (v *TxsValidator) CheckFields(tx types.Transaction) (errs []error)

CheckFields checks validates the transaction fields and values.

func (*TxsValidator) Validate

func (v *TxsValidator) Validate(opts ...types.CallOp) (errs []error)

Validate execute validation checks against each transactions

func (*TxsValidator) ValidateTx

func (v *TxsValidator) ValidateTx(tx types.Transaction, opts ...types.CallOp) []error

ValidateTx validates a single transaction coming received by the gossip handler..

type VContexts

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

VContexts manages validation contexts

type WorldReader

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

WorldReader provides functionalities for reading the the main and side chains. Using a ChainTraverser, it can search for and object from a chain and the chain's grand-parents.

func (*WorldReader) GetAccount

func (r *WorldReader) GetAccount(chain types.Chainer, address util.String,
	opts ...types.CallOp) (types.Account, error)

GetAccount gets an account by the given address. If chain is nil, it will check in the main chain. If chain is provided, it will check within the chain or traverse the chain's parent and ancestors to find the account.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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