github.com/koko1123/flow-go-1@v0.29.6/model/flow/order/identity.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package order
     4  
     5  import (
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  )
     8  
     9  // Canonical represents the canonical ordering for identity lists.
    10  func Canonical(identity1 *flow.Identity, identity2 *flow.Identity) bool {
    11  	return IdentifierCanonical(identity1.NodeID, identity2.NodeID)
    12  }
    13  
    14  // ByReferenceOrder return a function for sorting identities based on the order
    15  // of the given nodeIDs
    16  func ByReferenceOrder(nodeIDs []flow.Identifier) func(*flow.Identity, *flow.Identity) bool {
    17  	indices := make(map[flow.Identifier]uint)
    18  	for index, nodeID := range nodeIDs {
    19  		_, ok := indices[nodeID]
    20  		if ok {
    21  			panic("should never order by reference order with duplicate node IDs")
    22  		}
    23  		indices[nodeID] = uint(index)
    24  	}
    25  	return func(identity1 *flow.Identity, identity2 *flow.Identity) bool {
    26  		return indices[identity1.NodeID] < indices[identity2.NodeID]
    27  	}
    28  }
    29  
    30  // IdentityListCanonical takes a list of identities and
    31  // check if it's ordered in canonical order.
    32  func IdentityListCanonical(identities flow.IdentityList) bool {
    33  	if len(identities) == 0 {
    34  		return true
    35  	}
    36  
    37  	prev := identities[0].ID()
    38  	for i := 1; i < len(identities); i++ {
    39  		id := identities[i].ID()
    40  		if !IdentifierCanonical(prev, id) {
    41  			return false
    42  		}
    43  		prev = id
    44  	}
    45  
    46  	return true
    47  }