diff --git a/cmd/era/main.go b/cmd/era/main.go index 972377af71..48687617ca 100644 --- a/cmd/era/main.go +++ b/cmd/era/main.go @@ -248,7 +248,6 @@ func verify(ctx *cli.Context) error { func checkAccumulator(e *era.Era) error { var ( err error - start = e.Start() want common.Hash td *big.Int tds = make([]*big.Int, 0) @@ -271,15 +270,15 @@ func checkAccumulator(e *era.Era) error { // * the accumulator is correct by recomputing it locally, // which verifies the blocks are all correct (via hash) // * the receipts root matches the value in the block - for j := 0; it.Next(); j++ { + for it.Next() { // next() walks the block index, so we're able to // implicitly verify it. if it.Error() != nil { - return fmt.Errorf("error reading block %d: %w", start+uint64(j), err) + return fmt.Errorf("error reading block %d: %w", it.Number(), err) } block, receipts, err := it.BlockAndReceipts() if it.Error() != nil { - return fmt.Errorf("error reading block %d: %w", start+uint64(j), err) + return fmt.Errorf("error reading block %d: %w", it.Number(), err) } tr := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)) if tr != block.TxHash() { diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 7f72105fd3..4b57164665 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -293,32 +293,31 @@ func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, networ if err != nil { return fmt.Errorf("error making era reader: %w", err) } - for j := 0; it.Next(); j++ { - n := i*era.MaxEra1Size + j + for it.Next() { block, err := it.Block() if err != nil { - return fmt.Errorf("error reading block %d: %w", n, err) + return fmt.Errorf("error reading block %d: %w", it.Number(), err) } if block.Number().BitLen() == 0 { continue // skip genesis } receipts, err := it.Receipts() if err != nil { - return fmt.Errorf("error reading receipts %d: %w", n, err) + return fmt.Errorf("error reading receipts %d: %w", it.Number(), err) } if status, err := chain.HeaderChain().InsertHeaderChain([]*types.Header{block.Header()}, start, forker); err != nil { - return fmt.Errorf("error inserting header %d: %w", n, err) + return fmt.Errorf("error inserting header %d: %w", it.Number(), err) } else if status != core.CanonStatTy { - return fmt.Errorf("error inserting header %d, not canon: %v", n, status) + return fmt.Errorf("error inserting header %d, not canon: %v", it.Number(), status) } if _, err := chain.InsertReceiptChain([]*types.Block{block}, []types.Receipts{receipts}, 2^64-1); err != nil { - return fmt.Errorf("error inserting body %d: %w", n, err) + return fmt.Errorf("error inserting body %d: %w", it.Number(), err) } imported += 1 // Give the user some feedback that something is happening. if time.Since(reported) >= 8*time.Second { - log.Info("Importing Era files", "head", n, "imported", imported, "elapsed", common.PrettyDuration(time.Since(start))) + log.Info("Importing Era files", "head", it.Number(), "imported", imported, "elapsed", common.PrettyDuration(time.Since(start))) imported = 0 reported = time.Now() } diff --git a/internal/era/iterator.go b/internal/era/iterator.go index 2f353e6e85..3cc57a85e1 100644 --- a/internal/era/iterator.go +++ b/internal/era/iterator.go @@ -43,6 +43,11 @@ func (it *Iterator) Next() bool { return it.inner.Next() } +// Number returns the current number block the iterator will return. +func (it *Iterator) Number() uint64 { + return it.inner.next - 1 +} + // Error returns the error status of the iterator. func (it *Iterator) Error() error { return it.inner.Error() @@ -145,6 +150,11 @@ func (it *RawIterator) Next() bool { return true } +// Number returns the current number block the iterator will return. +func (it *RawIterator) Number() uint64 { + return it.next - 1 +} + // Error returns the error status of the iterator. func (it *RawIterator) Error() error { if it.err == io.EOF {