miner: track uncles more aggressively

This commit is contained in:
Péter Szilágyi 2018-08-28 16:48:20 +03:00
parent c1c003e4ff
commit f751c6ed47
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
2 changed files with 63 additions and 51 deletions

View File

@ -18,7 +18,7 @@ package miner
import ( import (
"bytes" "bytes"
"fmt" "errors"
"math/big" "math/big"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -615,13 +615,16 @@ func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
func (w *worker) commitUncle(env *environment, uncle *types.Header) error { func (w *worker) commitUncle(env *environment, uncle *types.Header) error {
hash := uncle.Hash() hash := uncle.Hash()
if env.uncles.Contains(hash) { if env.uncles.Contains(hash) {
return fmt.Errorf("uncle not unique") return errors.New("uncle not unique")
}
if env.header.ParentHash == uncle.ParentHash {
return errors.New("uncle is sibling")
} }
if !env.ancestors.Contains(uncle.ParentHash) { if !env.ancestors.Contains(uncle.ParentHash) {
return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4]) return errors.New("uncle's parent unknown")
} }
if env.family.Contains(hash) { if env.family.Contains(hash) {
return fmt.Errorf("uncle already in family (%x)", hash) return errors.New("uncle already included")
} }
env.uncles.Add(uncle.Hash()) env.uncles.Add(uncle.Hash())
return nil return nil
@ -847,29 +850,24 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
if w.config.DAOForkSupport && w.config.DAOForkBlock != nil && w.config.DAOForkBlock.Cmp(header.Number) == 0 { if w.config.DAOForkSupport && w.config.DAOForkBlock != nil && w.config.DAOForkBlock.Cmp(header.Number) == 0 {
misc.ApplyDAOHardFork(env.state) misc.ApplyDAOHardFork(env.state)
} }
// Accumulate the uncles for the current block
// compute uncles for the new block. for hash, uncle := range w.possibleUncles {
var ( if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() {
uncles []*types.Header delete(w.possibleUncles, hash)
badUncles []common.Hash }
) }
uncles := make([]*types.Header, 0, 2)
for hash, uncle := range w.possibleUncles { for hash, uncle := range w.possibleUncles {
if len(uncles) == 2 { if len(uncles) == 2 {
break break
} }
if err := w.commitUncle(env, uncle.Header()); err != nil { if err := w.commitUncle(env, uncle.Header()); err != nil {
log.Trace("Bad uncle found and will be removed", "hash", hash) log.Trace("Possible uncle rejected", "hash", hash, "reason", err)
log.Trace(fmt.Sprint(uncle))
badUncles = append(badUncles, hash)
} else { } else {
log.Debug("Committing new uncle to block", "hash", hash) log.Debug("Committing new uncle to block", "hash", hash)
uncles = append(uncles, uncle.Header()) uncles = append(uncles, uncle.Header())
} }
} }
for _, hash := range badUncles {
delete(w.possibleUncles, hash)
}
if !noempty { if !noempty {
// Create an empty block based on temporary copied state for sealing in advance without waiting block // Create an empty block based on temporary copied state for sealing in advance without waiting block

View File

@ -45,8 +45,8 @@ var (
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds = big.NewInt(1000000000000000000) testBankFunds = big.NewInt(1000000000000000000)
acc1Key, _ = crypto.GenerateKey() testUserKey, _ = crypto.GenerateKey()
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)
// Test transactions // Test transactions
pendingTxs []*types.Transaction pendingTxs []*types.Transaction
@ -62,9 +62,9 @@ func init() {
Period: 10, Period: 10,
Epoch: 30000, Epoch: 30000,
} }
tx1, _ := types.SignTx(types.NewTransaction(0, acc1Addr, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) tx1, _ := types.SignTx(types.NewTransaction(0, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
pendingTxs = append(pendingTxs, tx1) pendingTxs = append(pendingTxs, tx1)
tx2, _ := types.SignTx(types.NewTransaction(1, acc1Addr, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) tx2, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
newTxs = append(newTxs, tx2) newTxs = append(newTxs, tx2)
} }
@ -77,7 +77,7 @@ type testWorkerBackend struct {
uncleBlock *types.Block uncleBlock *types.Block
} }
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) *testWorkerBackend { func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, n int) *testWorkerBackend {
var ( var (
db = ethdb.NewMemDatabase() db = ethdb.NewMemDatabase()
gspec = core.Genesis{ gspec = core.Genesis{
@ -92,14 +92,28 @@ func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine
copy(gspec.ExtraData[32:], testBankAddress[:]) copy(gspec.ExtraData[32:], testBankAddress[:])
case *ethash.Ethash: case *ethash.Ethash:
default: default:
t.Fatal("unexpect consensus engine type") t.Fatalf("unexpected consensus engine type: %T", engine)
} }
genesis := gspec.MustCommit(db) genesis := gspec.MustCommit(db)
chain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{}) chain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{})
txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain) txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain)
blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(acc1Addr) // Generate a small n-block chain and an uncle block for it
if n > 0 {
blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, n, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testBankAddress)
})
if _, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("failed to insert origin chain: %v", err)
}
}
parent := genesis
if n > 0 {
parent = chain.GetBlockByHash(chain.CurrentBlock().ParentHash())
}
blocks, _ := core.GenerateChain(chainConfig, parent, engine, db, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
}) })
return &testWorkerBackend{ return &testWorkerBackend{
@ -116,8 +130,8 @@ func (b *testWorkerBackend) PostChainEvents(events []interface{}) {
b.chain.PostChainEvents(events, nil) b.chain.PostChainEvents(events, nil)
} }
func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) (*worker, *testWorkerBackend) { func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, blocks int) (*worker, *testWorkerBackend) {
backend := newTestWorkerBackend(t, chainConfig, engine) backend := newTestWorkerBackend(t, chainConfig, engine, blocks)
backend.txPool.AddLocals(pendingTxs) backend.txPool.AddLocals(pendingTxs)
w := newWorker(chainConfig, engine, backend, new(event.TypeMux), time.Second) w := newWorker(chainConfig, engine, backend, new(event.TypeMux), time.Second)
w.setEtherbase(testBankAddress) w.setEtherbase(testBankAddress)
@ -134,24 +148,24 @@ func TestPendingStateAndBlockClique(t *testing.T) {
func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, b := newTestWorker(t, chainConfig, engine) w, b := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
// Ensure snapshot has been updated. // Ensure snapshot has been updated.
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
block, state := w.pending() block, state := w.pending()
if block.NumberU64() != 1 { if block.NumberU64() != 1 {
t.Errorf("block number mismatch, has %d, want %d", block.NumberU64(), 1) t.Errorf("block number mismatch: have %d, want %d", block.NumberU64(), 1)
} }
if balance := state.GetBalance(acc1Addr); balance.Cmp(big.NewInt(1000)) != 0 { if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(1000)) != 0 {
t.Errorf("account balance mismatch, has %d, want %d", balance, 1000) t.Errorf("account balance mismatch: have %d, want %d", balance, 1000)
} }
b.txPool.AddLocals(newTxs) b.txPool.AddLocals(newTxs)
// Ensure the new tx events has been processed // Ensure the new tx events has been processed
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
block, state = w.pending() block, state = w.pending()
if balance := state.GetBalance(acc1Addr); balance.Cmp(big.NewInt(2000)) != 0 { if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(2000)) != 0 {
t.Errorf("account balance mismatch, has %d, want %d", balance, 2000) t.Errorf("account balance mismatch: have %d, want %d", balance, 2000)
} }
} }
@ -165,7 +179,7 @@ func TestEmptyWorkClique(t *testing.T) {
func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, _ := newTestWorker(t, chainConfig, engine) w, _ := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
var ( var (
@ -179,10 +193,10 @@ func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consens
receiptLen, balance = 1, big.NewInt(1000) receiptLen, balance = 1, big.NewInt(1000)
} }
if len(task.receipts) != receiptLen { if len(task.receipts) != receiptLen {
t.Errorf("receipt number mismatch has %d, want %d", len(task.receipts), receiptLen) t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
} }
if task.state.GetBalance(acc1Addr).Cmp(balance) != 0 { if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
t.Errorf("account balance mismatch has %d, want %d", task.state.GetBalance(acc1Addr), balance) t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
} }
} }
@ -219,19 +233,19 @@ func TestStreamUncleBlock(t *testing.T) {
ethash := ethash.NewFaker() ethash := ethash.NewFaker()
defer ethash.Close() defer ethash.Close()
w, b := newTestWorker(t, ethashChainConfig, ethash) w, b := newTestWorker(t, ethashChainConfig, ethash, 1)
defer w.close() defer w.close()
var taskCh = make(chan struct{}) var taskCh = make(chan struct{})
taskIndex := 0 taskIndex := 0
w.newTaskHook = func(task *task) { w.newTaskHook = func(task *task) {
if task.block.NumberU64() == 1 { if task.block.NumberU64() == 2 {
if taskIndex == 2 { if taskIndex == 2 {
has := task.block.Header().UncleHash have := task.block.Header().UncleHash
want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()}) want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()})
if has != want { if have != want {
t.Errorf("uncle hash mismatch, has %s, want %s", has.Hex(), want.Hex()) t.Errorf("uncle hash mismatch: have %s, want %s", have.Hex(), want.Hex())
} }
} }
taskCh <- struct{}{} taskCh <- struct{}{}
@ -248,12 +262,12 @@ func TestStreamUncleBlock(t *testing.T) {
// Ensure worker has finished initialization // Ensure worker has finished initialization
for { for {
b := w.pendingBlock() b := w.pendingBlock()
if b != nil && b.NumberU64() == 1 { if b != nil && b.NumberU64() == 2 {
break break
} }
} }
w.start() w.start()
// Ignore the first two works // Ignore the first two works
for i := 0; i < 2; i += 1 { for i := 0; i < 2; i += 1 {
select { select {
@ -282,7 +296,7 @@ func TestRegenerateMiningBlockClique(t *testing.T) {
func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, b := newTestWorker(t, chainConfig, engine) w, b := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
var taskCh = make(chan struct{}) var taskCh = make(chan struct{})
@ -293,10 +307,10 @@ func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, en
if taskIndex == 2 { if taskIndex == 2 {
receiptLen, balance := 2, big.NewInt(2000) receiptLen, balance := 2, big.NewInt(2000)
if len(task.receipts) != receiptLen { if len(task.receipts) != receiptLen {
t.Errorf("receipt number mismatch has %d, want %d", len(task.receipts), receiptLen) t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
} }
if task.state.GetBalance(acc1Addr).Cmp(balance) != 0 { if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
t.Errorf("account balance mismatch has %d, want %d", task.state.GetBalance(acc1Addr), balance) t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
} }
} }
taskCh <- struct{}{} taskCh <- struct{}{}
@ -347,7 +361,7 @@ func TestAdjustIntervalClique(t *testing.T) {
func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, _ := newTestWorker(t, chainConfig, engine) w, _ := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
w.skipSealHook = func(task *task) bool { w.skipSealHook = func(task *task) bool {
@ -387,10 +401,10 @@ func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine co
// Check interval // Check interval
if minInterval != wantMinInterval { if minInterval != wantMinInterval {
t.Errorf("resubmit min interval mismatch want %s has %s", wantMinInterval, minInterval) t.Errorf("resubmit min interval mismatch: have %v, want %v ", minInterval, wantMinInterval)
} }
if recommitInterval != wantRecommitInterval { if recommitInterval != wantRecommitInterval {
t.Errorf("resubmit interval mismatch want %s has %s", wantRecommitInterval, recommitInterval) t.Errorf("resubmit interval mismatch: have %v, want %v", recommitInterval, wantRecommitInterval)
} }
result = append(result, float64(recommitInterval.Nanoseconds())) result = append(result, float64(recommitInterval.Nanoseconds()))
index += 1 index += 1