Skip to content

Commit 8820949

Browse files
committed
correctly handle override by setting the head of the chain to the parent's height so that created blocks will always become part of canonical chain
1 parent e9154ca commit 8820949

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

rollup/da_syncer/da_syncer.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,38 @@ func (s *DASyncer) SyncOneBlock(block *da.PartialBlock, override bool, sign bool
4242

4343
parentBlockNumber := currentBlock.Number().Uint64()
4444
if override {
45-
// TODO: does this work when we do a reorg and actually overwrite the existing chain?
4645
parentBlockNumber = block.PartialHeader.Number - 1
46+
// reset the chain head to the parent block so that the new block can be inserted as part of the new canonical chain.
47+
err := s.blockchain.SetHead(parentBlockNumber)
48+
if err != nil {
49+
return fmt.Errorf("failed setting head, number: %d, error: %v", parentBlockNumber, err)
50+
}
4751
}
4852

4953
parentBlock := s.blockchain.GetBlockByNumber(parentBlockNumber)
5054
if parentBlock == nil {
5155
return fmt.Errorf("failed getting parent block, number: %d", parentBlockNumber)
5256
}
5357

54-
_, _, err := s.blockchain.BuildAndWriteBlock(parentBlock, block.PartialHeader.ToHeader(), block.Transactions, sign)
58+
fullBlock, writeStatus, err := s.blockchain.BuildAndWriteBlock(parentBlock, block.PartialHeader.ToHeader(), block.Transactions, sign)
5559
if err != nil {
5660
return fmt.Errorf("failed building and writing block, number: %d, error: %v", block.PartialHeader.Number, err)
5761
}
62+
if writeStatus != core.CanonStatTy {
63+
return fmt.Errorf("failed writing block as part of canonical chain, number: %d, status: %d", block.PartialHeader.Number, writeStatus)
64+
}
5865

5966
currentBlock = s.blockchain.CurrentBlock()
60-
if override && block.PartialHeader.Number != currentBlock.Number().Uint64() && block.PartialHeader.Number%100 == 0 {
61-
newBlock := s.blockchain.GetHeaderByNumber(block.PartialHeader.Number)
62-
log.Info("L1 sync progress", "processed block ", newBlock.Number.Uint64(), "block hash", newBlock.Hash(), "root", newBlock.Root)
63-
log.Info("L1 sync progress", "blockchain height", currentBlock.Number().Uint64(), "block hash", currentBlock.Hash(), "root", currentBlock.Root())
64-
} else if currentBlock.Number().Uint64()%100 == 0 {
65-
log.Info("L1 sync progress", "blockchain height", currentBlock.Number().Uint64(), "block hash", currentBlock.Hash(), "root", currentBlock.Root())
67+
if currentBlock.Number().Uint64() != fullBlock.NumberU64() || currentBlock.Hash() != fullBlock.Hash() {
68+
return fmt.Errorf("failed to insert block: not part of canonical chain, number: %d, hash: %s - canonical: number: %d, hash: %s", fullBlock.NumberU64(), fullBlock.Hash(), currentBlock.Number().Uint64(), currentBlock.Hash())
69+
}
70+
71+
if fullBlock.Number().Uint64()%100 == 0 {
72+
log.Info("L1 sync progress", "blockchain height", fullBlock.Number().Uint64(), "block hash", fullBlock.Hash(), "root", fullBlock.Root())
6673
}
6774

6875
if s.l2EndBlock > 0 && s.l2EndBlock == block.PartialHeader.Number {
69-
newBlock := s.blockchain.GetHeaderByNumber(block.PartialHeader.Number)
70-
log.Warn("L1 sync reached L2EndBlock: you can terminate recovery mode now", "L2EndBlock", newBlock.Number.Uint64(), "block hash", newBlock.Hash(), "root", newBlock.Root)
76+
log.Warn("L1 sync reached L2EndBlock: you can terminate recovery mode now", "L2EndBlock", fullBlock.NumberU64(), "block hash", fullBlock.Hash(), "root", fullBlock.Root())
7177
return serrors.Terminated
7278
}
7379

0 commit comments

Comments
 (0)