header_traversal.go 2.78 KB
Newer Older
1 2 3 4
package node

import (
	"errors"
Hamdi Allam's avatar
Hamdi Allam committed
5
	"fmt"
6 7
	"math/big"

Hamdi Allam's avatar
Hamdi Allam committed
8
	"github.com/ethereum-optimism/optimism/indexer/bigint"
9 10 11 12 13 14 15 16 17
	"github.com/ethereum/go-ethereum/core/types"
)

var (
	ErrHeaderTraversalAheadOfProvider            = errors.New("the HeaderTraversal's internal state is ahead of the provider")
	ErrHeaderTraversalAndProviderMismatchedState = errors.New("the HeaderTraversal and provider have diverged in state")
)

type HeaderTraversal struct {
Hamdi Allam's avatar
Hamdi Allam committed
18 19 20 21
	ethClient EthClient

	lastHeader             *types.Header
	blockConfirmationDepth *big.Int
22 23 24
}

// NewHeaderTraversal instantiates a new instance of HeaderTraversal against the supplied rpc client.
Hamdi Allam's avatar
Hamdi Allam committed
25 26 27
// The HeaderTraversal will start fetching blocks starting from the supplied header unless nil, indicating genesis.
func NewHeaderTraversal(ethClient EthClient, fromHeader *types.Header, confDepth *big.Int) *HeaderTraversal {
	return &HeaderTraversal{ethClient: ethClient, lastHeader: fromHeader, blockConfirmationDepth: confDepth}
28 29
}

30 31 32 33 34 35
// LastHeader returns the last header that was fetched by the HeaderTraversal
// This is useful for testing the state of the HeaderTraversal
func (f *HeaderTraversal) LastHeader() *types.Header {
	return f.lastHeader
}

36
// NextFinalizedHeaders retrieves the next set of headers that have been
37
// marked as finalized by the connected client, bounded by the supplied size
38
func (f *HeaderTraversal) NextFinalizedHeaders(maxSize uint64) ([]types.Header, error) {
Hamdi Allam's avatar
Hamdi Allam committed
39
	latestBlockHeader, err := f.ethClient.BlockHeaderByNumber(nil)
40
	if err != nil {
Hamdi Allam's avatar
Hamdi Allam committed
41 42 43 44 45 46 47
		return nil, fmt.Errorf("unable to query latest block: %w", err)
	}

	endHeight := new(big.Int).Sub(latestBlockHeader.Number, f.blockConfirmationDepth)
	if endHeight.Sign() < 0 {
		// No blocks with the provided confirmation depth available
		return nil, nil
48 49 50
	}

	if f.lastHeader != nil {
Hamdi Allam's avatar
Hamdi Allam committed
51
		cmp := f.lastHeader.Number.Cmp(endHeight)
52
		if cmp == 0 { // We're synced to head and there are no new headers
53 54 55 56 57 58
			return nil, nil
		} else if cmp > 0 {
			return nil, ErrHeaderTraversalAheadOfProvider
		}
	}

Hamdi Allam's avatar
Hamdi Allam committed
59
	nextHeight := bigint.Zero
60
	if f.lastHeader != nil {
Hamdi Allam's avatar
Hamdi Allam committed
61
		nextHeight = new(big.Int).Add(f.lastHeader.Number, bigint.One)
62 63
	}

64
	// endHeight = (nextHeight - endHeight) <= maxSize
Hamdi Allam's avatar
Hamdi Allam committed
65
	endHeight = bigint.Clamp(nextHeight, endHeight, maxSize)
66 67
	headers, err := f.ethClient.BlockHeadersByRange(nextHeight, endHeight)
	if err != nil {
Hamdi Allam's avatar
Hamdi Allam committed
68
		return nil, fmt.Errorf("error querying blocks by range: %w", err)
69 70 71 72 73 74 75 76 77 78 79
	}

	numHeaders := len(headers)
	if numHeaders == 0 {
		return nil, nil
	} else if f.lastHeader != nil && headers[0].ParentHash != f.lastHeader.Hash() {
		// The indexer's state is in an irrecoverable state relative to the provider. This
		// should never happen since the indexer is dealing with only finalized blocks.
		return nil, ErrHeaderTraversalAndProviderMismatchedState
	}

80
	f.lastHeader = &headers[numHeaders-1]
81 82
	return headers, nil
}