Skip to content

Commit

Permalink
attempt to use upstream worker
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush committed Sep 27, 2024
1 parent 6a07289 commit 559c20b
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 49 deletions.
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: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
Expand Down
41 changes: 37 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, true),
clock: clock,
}
}

Expand All @@ -66,7 +92,14 @@ 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
Expand Down
Loading

0 comments on commit 559c20b

Please sign in to comment.