Skip to content
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

cmd/utils: report the correct invalid block number in block import #27213

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
blocks := make(types.Blocks, importBatchSize)
n := 0
for batch := 0; ; batch++ {
var firstblockindex int
// Load a batch of RLP blocks.
if checkInterrupt() {
return fmt.Errorf("interrupted")
Expand All @@ -196,6 +197,9 @@ func ImportChain(chain *core.BlockChain, fn string) error {
i--
continue
}
if i == 0 {
firstblockindex = int(b.NumberU64())
}
blocks[i] = &b
n++
}
Expand All @@ -211,8 +215,11 @@ func ImportChain(chain *core.BlockChain, fn string) error {
log.Info("Skipping batch as all blocks present", "batch", batch, "first", blocks[0].Hash(), "last", blocks[i-1].Hash())
continue
}
if _, err := chain.InsertChain(missing); err != nil {
return fmt.Errorf("invalid block %d: %v", n, err)
if failindex, err := chain.InsertChain(missing); err != nil {
Copy link
Member

@rjl493456442 rjl493456442 May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify it a bit.

diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
index e1bafc53c3..37b6d222a5 100644
--- a/cmd/utils/cmd.go
+++ b/cmd/utils/cmd.go
@@ -211,8 +211,14 @@ func ImportChain(chain *core.BlockChain, fn string) error {
                        log.Info("Skipping batch as all blocks present", "batch", batch, "first", blocks[0].Hash(), "last", blocks[i-1].Hash())
                        continue
                }
-               if _, err := chain.InsertChain(missing); err != nil {
-                       return fmt.Errorf("invalid block %d: %v", n, err)
+               if failIndex, err := chain.InsertChain(missing); err != nil {
+                       var failNumber uint64
+                       if failIndex == -1 {
+                               failNumber = missing[0].NumberU64()
+                       } else {
+                               failNumber = missing[failIndex].NumberU64()
+                       }
+                       return fmt.Errorf("invalid block %d: %v", failNumber, err)
                }
        }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change @rjl493456442 's condition-check into

+                       if failIndex > 0 && failIndex < len(missing) {
+                               failNumber = missing[failIndex].NumberU64()
+                       } else {
+                               failNumber = missing[0].NumberU64()
+                       }

if failindex < 0 {
failindex = 0
}
return fmt.Errorf("invalid block %d: %v", failindex+firstblockindex, err)
}
}
return nil
Expand Down