github.com/koko1123/flow-go-1@v0.29.6/module/validation/common.go (about)

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/engine"
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  	"github.com/koko1123/flow-go-1/state/protocol"
     9  )
    10  
    11  // identityForNode ensures that `nodeID` is an authorized member of the network
    12  // at the given block and returns the corresponding node's full identity.
    13  // Error returns:
    14  //   - sentinel engine.InvalidInputError is nodeID is NOT an authorized member of the network
    15  //   - generic error indicating a fatal internal problem
    16  func identityForNode(state protocol.State, blockID flow.Identifier, nodeID flow.Identifier) (*flow.Identity, error) {
    17  	// get the identity of the origin node
    18  	identity, err := state.AtBlockID(blockID).Identity(nodeID)
    19  	if err != nil {
    20  		if protocol.IsIdentityNotFound(err) {
    21  			return nil, engine.NewInvalidInputErrorf("unknown node identity: %w", err)
    22  		}
    23  		// unexpected exception
    24  		return nil, fmt.Errorf("failed to retrieve node identity: %w", err)
    25  	}
    26  
    27  	return identity, nil
    28  }
    29  
    30  // ensureNodeHasWeightAndRole checks whether, at the given block, `nodeID`
    31  //   - has _positive_ weight
    32  //   - and has the expected role
    33  //   - and is not ejected
    34  //
    35  // Returns the following errors:
    36  //   - sentinel engine.InvalidInputError if any of the above-listed conditions are violated.
    37  //
    38  // Note: the method receives the identity as proof of its existence.
    39  // Therefore, we consider the case where the respective identity is unknown to the
    40  // protocol state as a symptom of a fatal implementation bug.
    41  func ensureNodeHasWeightAndRole(identity *flow.Identity, expectedRole flow.Role) error {
    42  	// check that the role is expected
    43  	if identity.Role != expectedRole {
    44  		return engine.NewInvalidInputErrorf("expected node %x to have role %s but got %s", identity.NodeID, expectedRole, identity.Role)
    45  	}
    46  
    47  	// check if the identity has non-zero weight
    48  	if identity.Weight == 0 {
    49  		return engine.NewInvalidInputErrorf("node has zero weight (%x)", identity.NodeID)
    50  	}
    51  
    52  	// check that node was not ejected
    53  	if identity.Ejected {
    54  		return engine.NewInvalidInputErrorf("node was ejected from network (%x)", identity.NodeID)
    55  	}
    56  
    57  	return nil
    58  }