diff options
author | Determinant <[email protected]> | 2019-08-30 09:00:43 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2019-08-30 09:00:43 -0400 |
commit | 0424afc658dae706b017aeb3d6208026c1f6f547 (patch) | |
tree | cae1127d880fbe8ea5454821d330dc9acc4ed112 | |
parent | bf3776a5d5475f9ee35820099debfc49d71ea10c (diff) |
add callbacks for the miner
-rw-r--r-- | consensus/dummy/consensus.go | 2 | ||||
-rw-r--r-- | coreth.go | 11 | ||||
-rw-r--r-- | eth/backend.go | 4 | ||||
-rw-r--r-- | miner/miner.go | 4 | ||||
-rw-r--r-- | miner/worker.go | 25 |
5 files changed, 31 insertions, 15 deletions
diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index 791719e..0fbc674 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -8,7 +8,6 @@ import ( "runtime" "time" - mapset "github.com/deckarep/golang-set" "github.com/ava-labs/go-ethereum/common" "github.com/ava-labs/go-ethereum/consensus" "github.com/ava-labs/go-ethereum/core/state" @@ -16,6 +15,7 @@ import ( "github.com/ava-labs/go-ethereum/params" "github.com/ava-labs/go-ethereum/rlp" "github.com/ava-labs/go-ethereum/rpc" + mapset "github.com/deckarep/golang-set" ) type OnFinalizeCallbackType = func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) @@ -7,6 +7,7 @@ import ( "github.com/ava-labs/coreth/consensus/dummy" "github.com/ava-labs/coreth/eth" + "github.com/ava-labs/coreth/miner" "github.com/ava-labs/coreth/node" "github.com/ava-labs/go-ethereum/common" "github.com/ava-labs/go-ethereum/core/state" @@ -24,6 +25,7 @@ type Hash = common.Hash type ETHChain struct { backend *eth.Ethereum cb *dummy.ConsensusCallbacks + mcb *miner.MinerCallbacks } func isLocalBlock(block *types.Block) bool { @@ -37,8 +39,9 @@ func NewETHChain(config *eth.Config, etherBase *common.Address) *ETHChain { mux := new(event.TypeMux) ctx := node.NewServiceContext(mux) cb := new(dummy.ConsensusCallbacks) - backend, _ := eth.New(&ctx, config, cb) - chain := ÐChain{backend: backend, cb: cb} + mcb := new(miner.MinerCallbacks) + backend, _ := eth.New(&ctx, config, cb, mcb) + chain := ÐChain{backend: backend, cb: cb, mcb: mcb} if etherBase == nil { etherBase = &common.Address{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -73,6 +76,10 @@ func (self *ETHChain) SetOnSeal(cb func(*types.Block) error) { self.cb.OnSeal = cb } +func (self *ETHChain) SetOnSealMiner(cb func(*types.Block) error) { + self.mcb.OnSeal = cb +} + func (self *ETHChain) SetOnAPIs(cb dummy.OnAPIsCallbackType) { self.cb.OnAPIs = cb } diff --git a/eth/backend.go b/eth/backend.go index 5fb5728..666f0fb 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -116,7 +116,7 @@ func (s *Ethereum) SetContractBackend(backend bind.ContractBackend) { // New creates a new Ethereum object (including the // initialisation of the common Ethereum object) -func New(ctx *node.ServiceContext, config *Config, cb *dummy.ConsensusCallbacks) (*Ethereum, error) { +func New(ctx *node.ServiceContext, config *Config, cb *dummy.ConsensusCallbacks, mcb *miner.MinerCallbacks) (*Ethereum, error) { // Ensure configuration values are compatible and sane if config.SyncMode == downloader.LightSync { return nil, errors.New("can't run eth.Ethereum in light sync mode, use les.LightEthereum") @@ -214,7 +214,7 @@ func New(ctx *node.ServiceContext, config *Config, cb *dummy.ConsensusCallbacks) if eth.protocolManager, err = NewProtocolManager(chainConfig, checkpoint, config.SyncMode, config.NetworkId, eth.eventMux, eth.txPool, eth.engine, eth.blockchain, chainDb, cacheLimit, config.Whitelist); err != nil { return nil, err } - eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock) + eth.miner = miner.New(eth, &config.Miner, chainConfig, eth.EventMux(), eth.engine, eth.isLocalBlock, mcb) eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) eth.APIBackend = &EthAPIBackend{ctx.ExtRPCEnabled(), eth, nil} diff --git a/miner/miner.go b/miner/miner.go index 51cded2..1f1baa8 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -56,9 +56,9 @@ type Miner struct { w *worker } -func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool) *Miner { +func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool, mcb *MinerCallbacks) *Miner { return &Miner{ - w: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock), + w: newWorker(config, chainConfig, engine, eth, mux, isLocalBlock, mcb), } } diff --git a/miner/worker.go b/miner/worker.go index ec34bdf..a7254e1 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -28,7 +28,6 @@ import ( "time" //"fmt" - mapset "github.com/deckarep/golang-set" "github.com/ava-labs/go-ethereum/common" "github.com/ava-labs/go-ethereum/consensus" "github.com/ava-labs/go-ethereum/consensus/misc" @@ -38,6 +37,7 @@ import ( "github.com/ava-labs/go-ethereum/event" "github.com/ava-labs/go-ethereum/log" "github.com/ava-labs/go-ethereum/params" + mapset "github.com/deckarep/golang-set" ) const ( @@ -123,6 +123,10 @@ type intervalAdjust struct { inc bool } +type MinerCallbacks struct { + OnSeal func(*types.Block) error +} + // worker is the main object which takes care of submitting new work to consensus engine // and gathering the sealing result. type worker struct { @@ -174,15 +178,16 @@ type worker struct { isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner. // Test hooks - newTaskHook func(*task) // Method to call upon receiving a new sealing task. - skipSealHook func(*task) bool // Method to decide whether skipping the sealing. - fullTaskHook func() // Method to call before pushing the full sealing task. - resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. - manualMining bool - manualUncle bool + newTaskHook func(*task) // Method to call upon receiving a new sealing task. + skipSealHook func(*task) bool // Method to decide whether skipping the sealing. + fullTaskHook func() // Method to call before pushing the full sealing task. + resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. + manualMining bool + manualUncle bool + minerCallbacks *MinerCallbacks } -func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, isLocalBlock func(*types.Block) bool) *worker { +func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, isLocalBlock func(*types.Block) bool, mcb *MinerCallbacks) *worker { worker := &worker{ config: config, chainConfig: chainConfig, @@ -207,6 +212,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus resubmitAdjustCh: make(chan *intervalAdjust, resubmitAdjustChanSize), manualMining: config.ManualMining, manualUncle: config.ManualUncle, + minerCallbacks: mcb, } // Subscribe NewTxsEvent for tx pool worker.txsSub = eth.TxPool().SubscribeNewTxsEvent(worker.txsCh) @@ -623,6 +629,9 @@ func (w *worker) resultLoop() { } log.Info("Successfully sealed new block", "number", block.Number(), "sealhash", sealhash, "hash", hash, "elapsed", common.PrettyDuration(time.Since(task.createdAt))) + if w.minerCallbacks.OnSeal != nil { + w.minerCallbacks.OnSeal(block) + } // Broadcast the block and announce chain insertion event w.mux.Post(core.NewMinedBlockEvent{Block: block}) |