forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(miner): payload building writing to closed channel (ethereum#91)
- Loading branch information
1 parent
9da8845
commit d706c13
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package miner | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/consensus/ethash" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/params" | ||
"github.com/ethereum/go-ethereum/trie" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func newTestBlock() *types.Block { | ||
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) | ||
txs := []*types.Transaction{tx1} | ||
|
||
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, trie.NewStackTrie(nil)) | ||
return block | ||
} | ||
|
||
func Test_SetFullBlock_AvoidPanic(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) | ||
} | ||
|
||
fees := big.NewInt(1) | ||
|
||
payload.done <- struct{}{} | ||
close(payload.stop) | ||
|
||
block := newTestBlock() | ||
// expect not to panic sending to payload.stop | ||
// now that done is closed | ||
payload.SetFullBlock(block, fees) | ||
} | ||
|
||
func Test_SetFullBlock(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) | ||
} | ||
|
||
fees := big.NewInt(1) | ||
|
||
block := newTestBlock() | ||
payload.SetFullBlock(block, fees) | ||
|
||
assert.Equal(t, block, payload.full) | ||
assert.Equal(t, fees, payload.fullFees) | ||
} |