work

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: GPL-3.0 Imports: 22 Imported by: 18

Documentation

Overview

Package work implements Klaytn block creation and mining.

work package is in charge of generating blocks and it contains `miner`, `worker` and `agents` which performs building blocks with transactions. `miner` contains `worker` and a consensus engine, so it controls the worker and passes the result to the consensus engine. `worker` contains `agent`s and each `agent` performs mining activity.

Source Files

  • agent.go : Provides CpuAgent and accompanying functions which works as an agent of a miner. Agent is in charge of creating a block
  • remote_agent.go : Provides RemoteAgent working as an another agent for a miner and can be controlled by RPC calls
  • work.go : Provides Miner struct and interfaces through which the miner communicate with other objects
  • worker.go : Provides Worker and performs the main part of block creation

Index

Constants

This section is empty.

Variables

View Source
var (
	ResultChGauge = metrics.NewRegisteredGauge("miner/resultch", nil)
)

Functions

This section is empty.

Types

type Agent

type Agent interface {
	Work() chan<- *Task
	SetReturnCh(chan<- *Result)
	Stop()
	Start()
	GetHashRate() int64
}

Agent can register themself with the worker

type Backend

type Backend interface {
	AccountManager() accounts.AccountManager
	BlockChain() BlockChain
	TxPool() TxPool
	ChainDB() database.DBManager
	ReBroadcastTxs(transactions types.Transactions)
}

Backend wraps all methods required for mining.

type BlockChain added in v1.3.0

type BlockChain interface {
	Genesis() *types.Block

	CurrentBlock() *types.Block
	CurrentFastBlock() *types.Block
	HasBlock(hash common.Hash, number uint64) bool
	GetBlock(hash common.Hash, number uint64) *types.Block
	GetBlockByHash(hash common.Hash) *types.Block
	GetBlockByNumber(number uint64) *types.Block
	GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash

	CurrentHeader() *types.Header
	HasHeader(hash common.Hash, number uint64) bool
	GetHeader(hash common.Hash, number uint64) *types.Header
	GetHeaderByHash(hash common.Hash) *types.Header
	GetHeaderByNumber(number uint64) *types.Header

	GetTd(hash common.Hash, number uint64) *big.Int
	GetTdByHash(hash common.Hash) *big.Int

	GetBodyRLP(hash common.Hash) rlp.RawValue

	GetReceiptsByBlockHash(blockHash common.Hash) types.Receipts

	InsertChain(chain types.Blocks) (int, error)
	TrieNode(hash common.Hash) ([]byte, error)
	Config() *params.ChainConfig
	State() (*state.StateDB, error)
	Rollback(chain []common.Hash)
	InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error)
	InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)
	FastSyncCommitHead(hash common.Hash) error
	StateCache() state.Database

	SubscribeChainEvent(ch chan<- blockchain.ChainEvent) event.Subscription
	SetHead(head uint64) error
	Stop()

	SubscribeRemovedLogsEvent(ch chan<- blockchain.RemovedLogsEvent) event.Subscription
	SubscribeChainHeadEvent(ch chan<- blockchain.ChainHeadEvent) event.Subscription
	SubscribeChainSideEvent(ch chan<- blockchain.ChainSideEvent) event.Subscription
	SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription
	IsParallelDBWrite() bool
	IsSenderTxHashIndexingEnabled() bool

	// Used in governance pkg
	SetProposerPolicy(val uint64)
	SetUseGiniCoeff(val bool)

	Processor() blockchain.Processor
	BadBlocks() ([]blockchain.BadBlockArgs, error)
	StateAt(root common.Hash) (*state.StateDB, error)
	StateAtWithPersistent(root common.Hash) (*state.StateDB, error)
	StateAtWithGCLock(root common.Hash) (*state.StateDB, error)
	Export(w io.Writer) error
	Engine() consensus.Engine
	GetNonceInCache(addr common.Address) (uint64, bool)
	GetTxLookupInfoAndReceipt(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
	GetTxAndLookupInfoInCache(hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
	GetBlockReceiptsInCache(blockHash common.Hash) types.Receipts
	GetTxLookupInfoAndReceiptInCache(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, *types.Receipt)
	GetTxAndLookupInfo(txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64)
	GetLogsByHash(hash common.Hash) [][]*types.Log
	ResetWithGenesisBlock(gb *types.Block) error
	Validator() blockchain.Validator
	HasBadBlock(hash common.Hash) bool
	WriteBlockWithState(block *types.Block, receipts []*types.Receipt, stateDB *state.StateDB) (blockchain.WriteStatus, error)
	PostChainEvents(events []interface{}, logs []*types.Log)
	TryGetCachedStateDB(rootHash common.Hash) (*state.StateDB, error)
	ApplyTransaction(config *params.ChainConfig, author *common.Address, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg *vm.Config) (*types.Receipt, uint64, *vm.InternalTxTrace, error)

	// State Migration
	PrepareStateMigration() error
	StartStateMigration(uint64, common.Hash) error
	StopStateMigration() error
	StateMigrationStatus() (bool, uint64, int, int, int, float64, error)

	// Warm up
	StartWarmUp() error
	StopWarmUp() error

	// Save trie node cache to this
	SaveTrieNodeCacheToDisk() error

	// KES
	BlockSubscriptionLoop(pool *blockchain.TxPool)
	CloseBlockSubscriptionLoop()
}

BlockChain is an interface of blockchain.BlockChain used by ProtocolManager.

type CpuAgent

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

func NewCpuAgent

func NewCpuAgent(chain consensus.ChainReader, engine consensus.Engine, nodetype common.ConnType) *CpuAgent

func (*CpuAgent) GetHashRate

func (self *CpuAgent) GetHashRate() int64

func (*CpuAgent) SetReturnCh

func (self *CpuAgent) SetReturnCh(ch chan<- *Result)

func (*CpuAgent) Start

func (self *CpuAgent) Start()

func (*CpuAgent) Stop

func (self *CpuAgent) Stop()

func (*CpuAgent) Work

func (self *CpuAgent) Work() chan<- *Task

type FakeWorker added in v1.5.3

type FakeWorker struct{}

func NewFakeWorker added in v1.5.3

func NewFakeWorker() *FakeWorker

NewFakeWorker disables mining and block processing worker and istanbul engine will not be started.

func (*FakeWorker) HashRate added in v1.5.3

func (*FakeWorker) HashRate() (tot int64)

func (*FakeWorker) Mining added in v1.5.3

func (*FakeWorker) Mining() bool

func (*FakeWorker) Pending added in v1.5.3

func (*FakeWorker) Pending() (*types.Block, *state.StateDB)

func (*FakeWorker) PendingBlock added in v1.5.3

func (*FakeWorker) PendingBlock() *types.Block

func (*FakeWorker) Register added in v1.5.3

func (*FakeWorker) Register(Agent)

func (*FakeWorker) SetExtra added in v1.5.3

func (*FakeWorker) SetExtra([]byte) error

func (*FakeWorker) Start added in v1.5.3

func (*FakeWorker) Start()

func (*FakeWorker) Stop added in v1.5.3

func (*FakeWorker) Stop()

type Miner

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

Miner creates blocks and searches for proof-of-work values.

func New

func New(backend Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, nodetype common.ConnType, rewardbase common.Address, TxResendUseLegacy bool, restartTimeOut time.Duration, restartFn func()) *Miner

func (*Miner) HashRate

func (self *Miner) HashRate() (tot int64)

func (*Miner) Mining

func (self *Miner) Mining() bool

func (*Miner) Pending

func (self *Miner) Pending() (*types.Block, *state.StateDB)

Pending returns the currently pending block and associated state.

func (*Miner) PendingBlock

func (self *Miner) PendingBlock() *types.Block

PendingBlock returns the currently pending block.

Note, to access both the pending block and the pending state simultaneously, please use Pending(), as the pending state can change between multiple method calls

func (*Miner) Register

func (self *Miner) Register(agent Agent)

func (*Miner) SetExtra

func (self *Miner) SetExtra(extra []byte) error

func (*Miner) Start

func (self *Miner) Start()

func (*Miner) Stop

func (self *Miner) Stop()

func (*Miner) Unregister

func (self *Miner) Unregister(agent Agent)

type RemoteAgent

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

func NewRemoteAgent

func NewRemoteAgent(chain consensus.ChainReader, engine consensus.Engine) *RemoteAgent

func (*RemoteAgent) GetHashRate

func (a *RemoteAgent) GetHashRate() (tot int64)

GetHashRate returns the accumulated hashrate of all identifier combined

func (*RemoteAgent) GetWork

func (a *RemoteAgent) GetWork() ([3]string, error)

func (*RemoteAgent) SetReturnCh

func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result)

func (*RemoteAgent) Start

func (a *RemoteAgent) Start()

func (*RemoteAgent) Stop

func (a *RemoteAgent) Stop()

func (*RemoteAgent) SubmitHashrate

func (a *RemoteAgent) SubmitHashrate(id common.Hash, rate uint64)

func (*RemoteAgent) SubmitWork

func (a *RemoteAgent) SubmitWork(hash common.Hash) bool

SubmitWork tries to inject a pow solution into the remote agent, returning whether the solution was accepted or not (not can be both a bad pow as well as any other error, like no work pending).

func (*RemoteAgent) Work

func (a *RemoteAgent) Work() chan<- *Task

type Result

type Result struct {
	Task  *Task
	Block *types.Block
}

type Task

type Task struct {
	Block *types.Block // the new block
	// contains filtered or unexported fields
}

Task is the workers current environment and holds all of the current state information

func NewTask

func NewTask(config *params.ChainConfig, signer types.Signer, statedb *state.StateDB, header *types.Header) *Task

func (*Task) ApplyTransactions

func (env *Task) ApplyTransactions(txs *types.TransactionsByPriceAndNonce, bc BlockChain, rewardbase common.Address) []*types.Log

func (*Task) Receipts

func (env *Task) Receipts() []*types.Receipt

func (*Task) Transactions

func (env *Task) Transactions() []*types.Transaction

type TxPool added in v1.3.0

type TxPool interface {
	// HandleTxMsg should add the given transactions to the pool.
	HandleTxMsg(types.Transactions)

	// Pending should return pending transactions.
	// The slice should be modifiable by the caller.
	Pending() (map[common.Address]types.Transactions, error)

	CachedPendingTxsByCount(count int) types.Transactions

	// SubscribeNewTxsEvent should return an event subscription of
	// NewTxsEvent and send events to the given channel.
	SubscribeNewTxsEvent(chan<- blockchain.NewTxsEvent) event.Subscription

	GetPendingNonce(addr common.Address) uint64
	AddLocal(tx *types.Transaction) error
	GasPrice() *big.Int
	SetGasPrice(price *big.Int)
	Stop()
	Get(hash common.Hash) *types.Transaction
	Stats() (int, int)
	Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
}

TxPool is an interface of blockchain.TxPool used by ProtocolManager and Backend.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
work
mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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