Skip to content

Commit 6273aec

Browse files
core: fix optimistic sync by reinserting blocks one by one
1 parent 3251c40 commit 6273aec

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

core/blockchain.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/common/mclock"
3232
"github.com/ethereum/go-ethereum/common/prque"
3333
"github.com/ethereum/go-ethereum/consensus"
34+
"github.com/ethereum/go-ethereum/consensus/beacon"
3435
"github.com/ethereum/go-ethereum/core/rawdb"
3536
"github.com/ethereum/go-ethereum/core/state"
3637
"github.com/ethereum/go-ethereum/core/state/snapshot"
@@ -1358,6 +1359,29 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
13581359
prev.Hash().Bytes()[:4], i, block.NumberU64(), block.Hash().Bytes()[:4], block.ParentHash().Bytes()[:4])
13591360
}
13601361
}
1362+
if chain[len(chain)-1].Difficulty().Cmp(common.Big0) == 0 {
1363+
start := 0
1364+
for i := 0; i < len(chain); i++ {
1365+
if chain[i].Difficulty().Cmp(common.Big0) != 0 {
1366+
start++
1367+
}
1368+
}
1369+
if start != 0 {
1370+
newChain := types.Blocks(chain[0:start])
1371+
if in, err := bc.insertChain(newChain, true, true); err != nil {
1372+
return in, err
1373+
}
1374+
}
1375+
for i := start; i < len(chain); i++ {
1376+
conf := bc.GetVMConfig()
1377+
conf.RandomOpcode = true
1378+
bc.SetVMConfig(*conf)
1379+
if err := bc.InsertBlockWithoutSetHead(chain[i]); err != nil {
1380+
return len(chain), err
1381+
}
1382+
}
1383+
return len(chain), nil
1384+
}
13611385
// Pre-checks passed, start the full block imports
13621386
if !bc.chainmu.TryLock() {
13631387
return 0, errChainStopped
@@ -2258,6 +2282,16 @@ func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, e
22582282
i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(),
22592283
receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState)
22602284
}
2285+
if eng, ok := bc.engine.(*beacon.Beacon); ok {
2286+
if eng.IsPoSHeader(block.Header()) {
2287+
fmt.Println("PoSHeader")
2288+
}
2289+
if reached, err := beacon.IsTTDReached(bc, block.ParentHash(), block.NumberU64()-1); reached {
2290+
fmt.Println("TTDD reached")
2291+
} else if err != nil {
2292+
fmt.Printf("TTDReached error: %v\n", err)
2293+
}
2294+
}
22612295
log.Error(fmt.Sprintf(`
22622296
########## BAD BLOCK #########
22632297
Chain config: %v

0 commit comments

Comments
 (0)