-
Notifications
You must be signed in to change notification settings - Fork 20.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: fix import errors on clique crashes + empty blocks #19544
Merged
+160
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
feb8029
core: fix import errors on clique crashes + empty blocks
karalabe 72e41e6
cosensus/clique, core: add test for the mirrored state issue
karalabe 69149ca
core: address todo question wrt log count
karalabe 2d0b6e7
core: raise a louder warning for non-clique known blocks
karalabe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2019 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package clique | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
// This test case is a repro of an annoying bug that took us forever to catch. | ||
// In Clique PoA networks (Rinkeby, Görli, etc), consecutive blocks might have | ||
// the same state root (no block subsidy, empty block). If a node crashes, the | ||
// chain ends up losing the recent state and needs to regenerate it from blocks | ||
// already in the database. The bug was that processing the block *prior* to an | ||
// empty one **also completes** the empty one, ending up in a known-block error. | ||
func TestReimportMirroredState(t *testing.T) { | ||
// Initialize a Clique chain with a single signer | ||
var ( | ||
db = rawdb.NewMemoryDatabase() | ||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") | ||
addr = crypto.PubkeyToAddress(key.PublicKey) | ||
engine = New(params.AllCliqueProtocolChanges.Clique, db) | ||
signer = new(types.HomesteadSigner) | ||
) | ||
genspec := &core.Genesis{ | ||
ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal), | ||
Alloc: map[common.Address]core.GenesisAccount{ | ||
addr: {Balance: big.NewInt(1)}, | ||
}, | ||
} | ||
copy(genspec.ExtraData[extraVanity:], addr[:]) | ||
genesis := genspec.MustCommit(db) | ||
|
||
// Generate a batch of blocks, each properly signed | ||
chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil) | ||
defer chain.Stop() | ||
|
||
blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, 3, func(i int, block *core.BlockGen) { | ||
// The chain maker doesn't have access to a chain, so the difficulty will be | ||
// lets unset (nil). Set it here to the correct value. | ||
block.SetDifficulty(diffInTurn) | ||
|
||
// We want to simulate an empty middle block, having the same state as the | ||
// first one. The last is needs a state change again to force a reorg. | ||
if i != 1 { | ||
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, nil, nil), signer, key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
block.AddTxWithChain(chain, tx) | ||
} | ||
}) | ||
for i, block := range blocks { | ||
header := block.Header() | ||
if i > 0 { | ||
header.ParentHash = blocks[i-1].Hash() | ||
} | ||
header.Extra = make([]byte, extraVanity+extraSeal) | ||
header.Difficulty = diffInTurn | ||
|
||
sig, _ := crypto.Sign(SealHash(header).Bytes(), key) | ||
copy(header.Extra[len(header.Extra)-extraSeal:], sig) | ||
blocks[i] = block.WithSeal(header) | ||
} | ||
// Insert the first two blocks and make sure the chain is valid | ||
db = rawdb.NewMemoryDatabase() | ||
genspec.MustCommit(db) | ||
|
||
chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil) | ||
defer chain.Stop() | ||
|
||
if _, err := chain.InsertChain(blocks[:2]); err != nil { | ||
t.Fatalf("failed to insert initial blocks: %v", err) | ||
} | ||
if head := chain.CurrentBlock().NumberU64(); head != 2 { | ||
t.Fatalf("chain head mismatch: have %d, want %d", head, 2) | ||
} | ||
|
||
// Simulate a crash by creating a new chain on top of the database, without | ||
// flushing the dirty states out. Insert the last block, trigerring a sidechain | ||
// reimport. | ||
chain, _ = core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil) | ||
defer chain.Stop() | ||
|
||
if _, err := chain.InsertChain(blocks[2:]); err != nil { | ||
t.Fatalf("failed to insert final block: %v", err) | ||
} | ||
if head := chain.CurrentBlock().NumberU64(); head != 3 { | ||
t.Fatalf("chain head mismatch: have %d, want %d", head, 3) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also do
events = append(events, ChainEvent{block, block.Hash(), nil})
for prefix known blocks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, my reluctance around these is that I'm not sure if we're supposed to fire canon or side things? Are we sure it's always canon? If so we can definitely do.
But how do we fire logs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, known blocks are canonical since the
externTd > localTd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure we should announce: if we get 10 new blocks from the network, and we in the mean time imported the first 2, we should not double announce the chain events.