Skip to content

Commit

Permalink
miner: apply rjl's diff
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed May 18, 2022
1 parent 6728d02 commit 29d68c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
7 changes: 3 additions & 4 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
Expand Down Expand Up @@ -196,12 +195,12 @@ func (api *ConsensusAPI) ForkchoiceUpdatedV1(update beacon.ForkchoiceStateV1, pa
}
// Send a request to generate a full block in the background.
// The result can be obtained via the returned channel.
resultChan := make(chan *types.Block, 1)
if err := api.eth.Miner().GetSealingBlockAsync(resultChan, update.HeadBlockHash, payloadAttributes.Timestamp, payloadAttributes.SuggestedFeeRecipient, payloadAttributes.Random, false); err != nil {
resChan, err := api.eth.Miner().GetSealingBlockAsync(update.HeadBlockHash, payloadAttributes.Timestamp, payloadAttributes.SuggestedFeeRecipient, payloadAttributes.Random, false)
if err != nil {
return valid(nil), err
}
id := computePayloadId(update.HeadBlockHash, payloadAttributes)
api.localBlocks.put(id, &payload{empty: empty, result: resultChan})
api.localBlocks.put(id, &payload{empty: empty, result: resChan})
return valid(&id), nil
}
return valid(nil), nil
Expand Down
17 changes: 8 additions & 9 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,21 @@ func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscript
// there is always a result that will be returned through the result channel.
// The difference is that if the execution fails, the returned result is nil
// and the concrete error is dropped silently.
// The caller of this method needs to set up a non-blocking result channel.
func (miner *Miner) GetSealingBlockAsync(resultChan chan *types.Block, parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash, noTxs bool) error {
_, err := miner.worker.getSealingBlock(parent, timestamp, coinbase, random, noTxs, resultChan)
return err
func (miner *Miner) GetSealingBlockAsync(parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash, noTxs bool) (chan *types.Block, error) {
resChan, _, err := miner.worker.getSealingBlock(parent, timestamp, coinbase, random, noTxs)
if err != nil {
return nil, err
}
return resChan, nil
}

// GetSealingBlockSync creates a sealing block according to the given parameters.
// If the generation is failed or the underlying work is already closed, an error
// will be returned.
func (miner *Miner) GetSealingBlockSync(parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash, noTxs bool) (*types.Block, error) {
resultChan := make(chan *types.Block, 1)
errChan, err := miner.worker.getSealingBlock(parent, timestamp, coinbase, random, noTxs, resultChan)
resChan, errChan, err := miner.worker.getSealingBlock(parent, timestamp, coinbase, random, noTxs)
if err != nil {
return nil, err
}
result := <-resultChan
err = <-errChan
return result, err
return <-resChan, <-errChan
}
16 changes: 9 additions & 7 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,12 @@ func (w *worker) commit(env *environment, interval func(), update bool, start ti

// getSealingBlock generates the sealing block based on the given parameters.
// The generation result will be passed back via the given channel no matter
// the generation itself succeeds or not. The assumption is always held the
// given channel has at-least 1-size buffer for storing result.
func (w *worker) getSealingBlock(parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash, noTxs bool, result chan *types.Block) (chan error, error) {
errChan := make(chan error, 1)
// the generation itself succeeds or not.
func (w *worker) getSealingBlock(parent common.Hash, timestamp uint64, coinbase common.Address, random common.Hash, noTxs bool) (chan *types.Block, chan error, error) {
var (
resChan = make(chan *types.Block, 1)
errChan = make(chan error, 1)
)
req := &getWorkReq{
params: &generateParams{
timestamp: timestamp,
Expand All @@ -1194,14 +1196,14 @@ func (w *worker) getSealingBlock(parent common.Hash, timestamp uint64, coinbase
noExtra: true,
noTxs: noTxs,
},
result: result,
result: resChan,
err: errChan,
}
select {
case w.getWorkCh <- req:
return errChan, nil
return resChan, errChan, nil
case <-w.exitCh:
return errChan, errors.New("miner closed")
return nil, nil, errors.New("miner closed")
}
}

Expand Down
10 changes: 4 additions & 6 deletions miner/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,8 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co

// This API should work even when the automatic sealing is not enabled
for _, c := range cases {
ch := make(chan *types.Block, 1)
errChan, _ := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, false, ch)
block := <-ch
resChan, errChan, _ := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, false)
block := <-resChan
err := <-errChan
if c.expectErr {
if err == nil {
Expand All @@ -657,9 +656,8 @@ func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine co
// This API should work even when the automatic sealing is enabled
w.start()
for _, c := range cases {
ch := make(chan *types.Block, 1)
errChan, _ := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, false, ch)
block := <-ch
resChan, errChan, _ := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, false)
block := <-resChan
err := <-errChan
if c.expectErr {
if err == nil {
Expand Down

0 comments on commit 29d68c3

Please sign in to comment.