github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/follower.go (about)

     1  package consensus
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/onflow/flow-go/consensus/hotstuff"
     9  	"github.com/onflow/flow-go/consensus/recovery"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/module"
    12  	"github.com/onflow/flow-go/storage"
    13  )
    14  
    15  // TODO: this needs to be integrated with proper configuration and bootstrapping.
    16  
    17  // NewFollower instantiates the consensus follower and recovers its in-memory state of pending blocks.
    18  // It receives the list `pending` containing _all_ blocks that
    19  //   - have passed the compliance layer and stored in the protocol state
    20  //   - descend from the latest finalized block
    21  //   - are listed in ancestor-first order (i.e. for any block B ∈ pending, B's parent must
    22  //     be listed before B, unless B's parent is the latest finalized block)
    23  //
    24  // CAUTION: all pending blocks are required to be valid (guaranteed if the block passed the compliance layer)
    25  func NewFollower(log zerolog.Logger,
    26  	mempoolMetrics module.MempoolMetrics,
    27  	headers storage.Headers,
    28  	updater module.Finalizer,
    29  	notifier hotstuff.FollowerConsumer,
    30  	rootHeader *flow.Header,
    31  	rootQC *flow.QuorumCertificate,
    32  	finalized *flow.Header,
    33  	pending []*flow.Header,
    34  ) (*hotstuff.FollowerLoop, error) {
    35  	forks, err := NewForks(finalized, headers, updater, notifier, rootHeader, rootQC)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("could not initialize forks: %w", err)
    38  	}
    39  
    40  	// recover forks internal state (inserts all pending blocks)
    41  	err = recovery.Recover(log, pending, recovery.ForksState(forks))
    42  	if err != nil {
    43  		return nil, fmt.Errorf("could not recover hotstuff follower state: %w", err)
    44  	}
    45  
    46  	// initialize the follower loop
    47  	loop, err := hotstuff.NewFollowerLoop(log, mempoolMetrics, forks)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("could not create follower loop: %w", err)
    50  	}
    51  
    52  	return loop, nil
    53  }