internal/era,cmd: add number method to era iterator to get the current block number

This commit is contained in:
lightclient@protonmail.com 2023-06-05 09:38:18 +02:00 committed by lightclient
parent 02b3f1983e
commit b98b6d6524
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E
3 changed files with 20 additions and 12 deletions

View file

@ -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() {

View file

@ -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()
}

View file

@ -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 {