Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miner: upstream alignment #665

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ type Engine interface {
FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, parent *types.Header, state *state.StateDB, txs []*types.Transaction,
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)

// Seal generates a new sealing request for the given input block and pushes
// the result into the given channel.
//
// Note, the method returns immediately and will send the result async. More
// than one result may also be returned depending on the consensus algorithm.
Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error

// SealHash returns the hash of a block prior to it being sealed.
SealHash(header *types.Header) common.Hash

// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have.
CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
Expand Down
17 changes: 17 additions & 0 deletions consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)

var (
Expand Down Expand Up @@ -496,3 +497,19 @@ func (*DummyEngine) CalcDifficulty(chain consensus.ChainHeaderReader, time uint6
func (*DummyEngine) Close() error {
return nil
}

func (*DummyEngine) SealHash(header *types.Header) common.Hash {
return header.Hash()
}

func (d *DummyEngine) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
header := block.Header()
go func() {
select {
case results <- block:
default:
log.Warn("Sealing result is not read by miner", "sealhash", d.SealHash(header))
}
}()
return nil
}
3 changes: 3 additions & 0 deletions core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
// NewTxsEvent is posted when a batch of transactions enter the transaction pool.
type NewTxsEvent struct{ Txs []*types.Transaction }

// NewMinedBlockEvent is posted when a block has been imported.
type NewMinedBlockEvent struct{ Block *types.Block }

// NewTxPoolHeadEvent is posted when the pool receives a request to update
// its head to [Block].
type NewTxPoolHeadEvent struct{ Head *types.Header }
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func (s *Ethereum) Stop() error {
s.bloomIndexer.Close()
close(s.closeBloomHandler)
s.txPool.Close()
s.miner.Close()
s.blockchain.Stop()
s.engine.Close()

Expand Down
53 changes: 49 additions & 4 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
package miner

import (
"math/big"
"time"

"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/coreth/consensus"
"github.com/ava-labs/coreth/core"
Expand All @@ -36,6 +39,7 @@ import (
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/event"
)

Expand All @@ -47,17 +51,39 @@ type Backend interface {

// Config is the configuration parameters of mining.
type Config struct {
Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards
TestOnlyAllowDuplicateBlocks bool // Allow mining of duplicate blocks (used in tests only)
Etherbase common.Address `toml:",omitempty"` // Public address for block mining rewards
ExtraData hexutil.Bytes `toml:",omitempty"` // Block extra data set by the miner
GasFloor uint64 // Target gas floor for mined blocks.
GasCeil uint64 // Target gas ceiling for mined blocks.
GasPrice *big.Int // Minimum gas price for mining a transaction
Recommit time.Duration // The time interval for miner to re-create mining work.

NewPayloadTimeout time.Duration // The maximum time allowance for creating a new payload
TestOnlyAllowDuplicateBlocks bool // Allow mining of duplicate blocks (used in tests only)
}

// DefaultConfig contains default settings for miner.
var DefaultConfig = Config{
GasCeil: 30000000,
GasPrice: big.NewInt(params.GWei),

// The default recommit time is chosen as two seconds since
// consensus-layer usually will wait a half slot of time(6s)
// for payload generation. It should be enough for Geth to
// run 3 rounds.
Recommit: 2 * time.Second,
NewPayloadTimeout: 2 * time.Second,
}

type Miner struct {
worker *worker
clock *mockable.Clock
}

func New(eth Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, clock *mockable.Clock) *Miner {
return &Miner{
worker: newWorker(config, chainConfig, engine, eth, mux, clock),
worker: newWorker(config, chainConfig, engine, eth, mux, false),
clock: clock,
}
}

Expand All @@ -66,11 +92,30 @@ func (miner *Miner) SetEtherbase(addr common.Address) {
}

func (miner *Miner) GenerateBlock(predicateContext *precompileconfig.PredicateContext) (*types.Block, error) {
return miner.worker.commitNewWork(predicateContext)
result := miner.worker.generateWork(&generateParams{
predicateContext: predicateContext,
timestamp: miner.clock.Unix(),
parentHash: miner.worker.chain.CurrentBlock().Hash(),
coinbase: miner.worker.etherbase(),
beaconRoot: &common.Hash{},
})
return result.block, result.err
}

// SubscribePendingLogs starts delivering logs from pending transactions
// to the given channel.
func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {
return miner.worker.pendingLogsFeed.Subscribe(ch)
}

func (miner *Miner) Stop() {
miner.worker.stop()
}

func (miner *Miner) Start() {
miner.worker.start()
}

func (miner *Miner) Close() {
miner.worker.close()
}
Loading
Loading