blockchain

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: GPL-3.0 Imports: 29 Imported by: 3

Documentation

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 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.ChainReader

ChainReader creates a chain reader for best/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.ChainReader

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.ChainReader)

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 blockhashes 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.

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) IsKnownBlock

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

IsKnownBlock checks whether a block with the has exists in at least one of all block chains and caches (e.g orphan)

func (*Blockchain) IsMainChain

func (b *Blockchain) IsMainChain(cr types.ChainReader) 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) (types.ChainReader, 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) SelectTransactions

func (b *Blockchain) SelectTransactions(maxSize int64) (selectedTxs []types.Transaction,
	err error)

SelectTransactions collects transactions from the head of the pool up to the specified maxSize.

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)

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.ChainReader

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 this c

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) 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) PutTransactions

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

PutTransactions stores a collection of transactions in the chain

type ChainRead

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

ChainRead provides read-only access to objects belonging to a single chain.

func NewChainReader

func NewChainReader(ch *Chain) *ChainRead

NewChainReader creates a ChainReader object

func (*ChainRead) Current

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

Current gets the current block at the tip of the chain

func (*ChainRead) GetBlock

func (r *ChainRead) 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 (*ChainRead) GetBlockByHash

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

GetBlockByHash finds and returns a block associated with chainID.

func (*ChainRead) GetHeader

func (r *ChainRead) 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 (*ChainRead) GetHeaderByHash

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

GetHeaderByHash finds and returns the header of a block matching hash

func (*ChainRead) GetID

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

GetID gets the chain ID

func (*ChainRead) GetParent

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

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

func (*ChainRead) GetParentBlock

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

GetParentBlock returns the parent block

func (*ChainRead) GetRoot

func (r *ChainRead) 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"`
	SideChainID  string `json:"sideChainID" msgpack:"sideChainID"`
	SideChainLen uint64 `json:"sideChainLen" msgpack:"sideChainLen"`
	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 in the chain provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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