Skip to content

Commit da1efda

Browse files
holimankaralabe
authored andcommitted
core: repro #18977
1 parent 4f3d22f commit da1efda

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎core/blockchain_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,51 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) {
14831483

14841484
benchmarkLargeNumberOfValueToNonexisting(b, numTxs, numBlocks, recipientFn, dataFn)
14851485
}
1486+
1487+
// Tests that importing a very large side fork, which is larger than the canon chain,
1488+
// but where the difficulty per block is kept low: this means that it will not
1489+
// overtake the 'canon' chain until after it's passed canon by about 200 blocks.
1490+
func TestLargeOldSidechainWithALowTdChain(t *testing.T) {
1491+
1492+
// Generate a canonical chain to act as the main dataset
1493+
engine := ethash.NewFaker()
1494+
1495+
db := ethdb.NewMemDatabase()
1496+
genesis := new(Genesis).MustCommit(db)
1497+
// We must use a pretty long chain to ensure that the fork
1498+
// doesn't overtake us until after at least 128 blocks post tip
1499+
blocks, _ := generateChain(params.TestChainConfig, genesis, engine, db, 6*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }, makeHeaderWithLargeDifficulty)
1500+
1501+
// Import the canonical chain
1502+
diskdb := ethdb.NewMemDatabase()
1503+
new(Genesis).MustCommit(diskdb)
1504+
1505+
chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil)
1506+
if err != nil {
1507+
t.Fatalf("failed to create tester chain: %v", err)
1508+
}
1509+
for i := 0; i < len(blocks); i++ {
1510+
if _, err := chain.InsertChain(blocks[i : i+1]); err != nil {
1511+
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
1512+
}
1513+
}
1514+
// Dereference all the recent tries and ensure no past trie is left in
1515+
for i := 0; i < triesInMemory; i++ {
1516+
chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root())
1517+
}
1518+
1519+
// Generate fork chain, starting from an early block
1520+
parent := blocks[10]
1521+
fork, _ := generateChain(params.TestChainConfig, parent, engine, db, 256+6*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) }, makeHeaderWithSmallDifficulty)
1522+
1523+
// And now import the fork
1524+
if i, err := chain.InsertChain(fork); err != nil {
1525+
t.Fatalf("block %d: failed to insert into chain: %v", i, err)
1526+
}
1527+
head := chain.CurrentBlock()
1528+
if got := fork[len(fork)-1].Hash(); got != head.Hash() {
1529+
t.Fatalf("head wrong, expected %x got %x", head.Hash(), got)
1530+
}
1531+
td := chain.GetTd(head.Hash(), head.NumberU64())
1532+
fmt.Printf("td %v", td)
1533+
}

‎core/chain_makers.go‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type BlockGen struct {
4848
engine consensus.Engine
4949
}
5050

51+
type headerGenFn func(chain consensus.ChainReader, parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header
52+
5153
// SetCoinbase sets the coinbase of the generated block.
5254
// It can be called at most once.
5355
func (b *BlockGen) SetCoinbase(addr common.Address) {
@@ -170,14 +172,19 @@ func (b *BlockGen) OffsetTime(seconds int64) {
170172
// values. Inserting them into BlockChain requires use of FakePow or
171173
// a similar non-validating proof of work implementation.
172174
func GenerateChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
175+
return generateChain(config, parent, engine, db, n, gen, makeHeader)
176+
}
177+
178+
func generateChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen), headerGen headerGenFn) ([]*types.Block, []types.Receipts) {
173179
if config == nil {
174180
config = params.TestChainConfig
175181
}
176182
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
177183
chainreader := &fakeChainReader{config: config}
178184
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
179185
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
180-
b.header = makeHeader(chainreader, parent, statedb, b.engine)
186+
//b.header = makeHeader(chainreader, parent, statedb, b.engine)
187+
b.header = headerGen(chainreader, parent, statedb, b.engine)
181188

182189
// Mutate the state and block according to any hard-fork specs
183190
if daoBlock := config.DAOForkBlock; daoBlock != nil {
@@ -248,6 +255,54 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
248255
}
249256
}
250257

258+
func makeHeaderWithLargeDifficulty(chain consensus.ChainReader, parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
259+
var time *big.Int
260+
if parent.Time() == nil {
261+
time = big.NewInt(1)
262+
} else {
263+
time = new(big.Int).Add(parent.Time(), big.NewInt(1)) // block time is fixed at 10 seconds
264+
}
265+
266+
return &types.Header{
267+
Root: state.IntermediateRoot(chain.Config().IsEIP158(parent.Number())),
268+
ParentHash: parent.Hash(),
269+
Coinbase: parent.Coinbase(),
270+
Difficulty: engine.CalcDifficulty(chain, time.Uint64(), &types.Header{
271+
Number: parent.Number(),
272+
Time: new(big.Int).Sub(time, big.NewInt(1)),
273+
Difficulty: parent.Difficulty(),
274+
UncleHash: parent.UncleHash(),
275+
}),
276+
GasLimit: CalcGasLimit(parent, parent.GasLimit(), parent.GasLimit()),
277+
Number: new(big.Int).Add(parent.Number(), common.Big1),
278+
Time: time,
279+
}
280+
}
281+
282+
func makeHeaderWithSmallDifficulty(chain consensus.ChainReader, parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
283+
var time *big.Int
284+
if parent.Time() == nil {
285+
time = big.NewInt(30)
286+
} else {
287+
time = new(big.Int).Add(parent.Time(), big.NewInt(30)) // block time is fixed at 10 seconds
288+
}
289+
290+
return &types.Header{
291+
Root: state.IntermediateRoot(chain.Config().IsEIP158(parent.Number())),
292+
ParentHash: parent.Hash(),
293+
Coinbase: parent.Coinbase(),
294+
Difficulty: engine.CalcDifficulty(chain, time.Uint64(), &types.Header{
295+
Number: parent.Number(),
296+
Time: new(big.Int).Sub(time, big.NewInt(30)),
297+
Difficulty: parent.Difficulty(),
298+
UncleHash: parent.UncleHash(),
299+
}),
300+
GasLimit: CalcGasLimit(parent, parent.GasLimit(), parent.GasLimit()),
301+
Number: new(big.Int).Add(parent.Number(), common.Big1),
302+
Time: time,
303+
}
304+
}
305+
251306
// makeHeaderChain creates a deterministic chain of headers rooted at parent.
252307
func makeHeaderChain(parent *types.Header, n int, engine consensus.Engine, db ethdb.Database, seed int) []*types.Header {
253308
blocks := makeBlockChain(types.NewBlockWithHeader(parent), n, engine, db, seed)

0 commit comments

Comments
 (0)