Skip to content

Commit

Permalink
fix(miner): fix panic on payload building (ethereum#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored and davidtaikocha committed Jun 10, 2023
1 parent d706c13 commit 3650f57
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
29 changes: 18 additions & 11 deletions miner/taiko_payload_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ func (payload *Payload) SetFullBlock(block *types.Block, fees *big.Int) {
payload.lock.Lock()
defer payload.lock.Unlock()

go func() {
payload.lock.Lock()
defer payload.lock.Unlock()

select {
case <-payload.done:
log.Info("SetFullBlock payload done received", "id", payload.id)
case payload.stop <- struct{}{}:
default:
}
}()
go payload.afterSetFullBlock()

payload.full = block
payload.fullFees = fees
Expand All @@ -35,3 +25,20 @@ func (payload *Payload) SetFullBlock(block *types.Block, fees *big.Int) {

payload.cond.Broadcast() // fire signal for notifying full block
}

func (payload *Payload) afterSetFullBlock() {
payload.lock.Lock()
defer payload.lock.Unlock()

select {
case <-payload.done:
log.Info("SetFullBlock payload done received", "id", payload.id)
return
default:
}

select {
case payload.stop <- struct{}{}:
default:
}
}
57 changes: 57 additions & 0 deletions miner/taiko_payload_building_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,63 @@ func Test_SetFullBlock_AvoidPanic(t *testing.T) {
payload.SetFullBlock(block, fees)
}

func Test_AfterSetFullBlock_Panic_DoneChannelNotSent(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
recipient = common.HexToAddress("0xdeadbeef")
)
w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
defer w.close()

timestamp := uint64(time.Now().Unix())
args := &BuildPayloadArgs{
Parent: b.chain.CurrentBlock().Hash(),
Timestamp: timestamp,
Random: common.Hash{},
FeeRecipient: recipient,
}
payload, err := w.buildPayload(args)
if err != nil {
t.Fatalf("Failed to build payload %v", err)
}

// dont send on done channel, but close stop channel.
// should panic when sent on.
close(payload.stop)

assert.Panics(t, func() {
payload.afterSetFullBlock()
})
}

func Test_AfterSetFullBlock_AvoidPanic_DoneChannelSent(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
recipient = common.HexToAddress("0xdeadbeef")
)
w, b := newTestWorker(t, params.TestChainConfig, ethash.NewFaker(), db, 0)
defer w.close()

timestamp := uint64(time.Now().Unix())
args := &BuildPayloadArgs{
Parent: b.chain.CurrentBlock().Hash(),
Timestamp: timestamp,
Random: common.Hash{},
FeeRecipient: recipient,
}
payload, err := w.buildPayload(args)
if err != nil {
t.Fatalf("Failed to build payload %v", err)
}

payload.done <- struct{}{}
close(payload.stop)

assert.NotPanics(t, func() {
payload.afterSetFullBlock()
})
}

func Test_SetFullBlock(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
Expand Down

0 comments on commit 3650f57

Please sign in to comment.