github.com/MetalBlockchain/metalgo@v1.11.9/snow/networking/handler/engine.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package handler
     5  
     6  import (
     7  	"github.com/MetalBlockchain/metalgo/proto/pb/p2p"
     8  	"github.com/MetalBlockchain/metalgo/snow"
     9  	"github.com/MetalBlockchain/metalgo/snow/engine/common"
    10  )
    11  
    12  // Engine is a wrapper around a consensus engine's components.
    13  type Engine struct {
    14  	StateSyncer  common.StateSyncer
    15  	Bootstrapper common.BootstrapableEngine
    16  	Consensus    common.Engine
    17  }
    18  
    19  // Get returns the engine corresponding to the provided state,
    20  // and whether its corresponding engine is initialized (not nil).
    21  func (e *Engine) Get(state snow.State) (common.Engine, bool) {
    22  	if e == nil {
    23  		return nil, false
    24  	}
    25  	switch state {
    26  	case snow.StateSyncing:
    27  		return e.StateSyncer, e.StateSyncer != nil
    28  	case snow.Bootstrapping:
    29  		return e.Bootstrapper, e.Bootstrapper != nil
    30  	case snow.NormalOp:
    31  		return e.Consensus, e.Consensus != nil
    32  	default:
    33  		return nil, false
    34  	}
    35  }
    36  
    37  // EngineManager resolves the engine that should be used given the current
    38  // execution context of the chain.
    39  type EngineManager struct {
    40  	Avalanche *Engine
    41  	Snowman   *Engine
    42  }
    43  
    44  // Get returns the engine corresponding to the provided type if possible.
    45  // If an engine type is not specified, the initial engine type is returned.
    46  func (e *EngineManager) Get(engineType p2p.EngineType) *Engine {
    47  	switch engineType {
    48  	case p2p.EngineType_ENGINE_TYPE_AVALANCHE:
    49  		return e.Avalanche
    50  	case p2p.EngineType_ENGINE_TYPE_SNOWMAN:
    51  		return e.Snowman
    52  	default:
    53  		return nil
    54  	}
    55  }