github.com/koko1123/flow-go-1@v0.29.6/network/p2p/translator/fixed_translator.go (about) 1 package translator 2 3 import ( 4 "fmt" 5 6 "github.com/libp2p/go-libp2p/core/peer" 7 8 "github.com/koko1123/flow-go-1/model/flow" 9 "github.com/koko1123/flow-go-1/network/p2p/keyutils" 10 ) 11 12 // FixedTableIdentityTranslator implements an IDTranslator which translates ID's for a 13 // fixed list of identities. 14 type FixedTableIdentityTranslator struct { 15 flow2p2p map[flow.Identifier]peer.ID 16 p2p2flow map[peer.ID]flow.Identifier 17 } 18 19 func (t *FixedTableIdentityTranslator) GetFlowID(p peer.ID) (flow.Identifier, error) { 20 nodeID, ok := t.p2p2flow[p] 21 if !ok { 22 return flow.ZeroID, fmt.Errorf("could not find a flow NodeID for peer %v", p) 23 } 24 return nodeID, nil 25 } 26 27 func (t *FixedTableIdentityTranslator) GetPeerID(n flow.Identifier) (peer.ID, error) { 28 peerID, ok := t.flow2p2p[n] 29 if !ok { 30 return *new(peer.ID), fmt.Errorf("could not find a libp2p PeerID for flow NodeID %v", n) 31 } 32 return peerID, nil 33 } 34 35 func NewFixedTableIdentityTranslator(identities flow.IdentityList) (*FixedTableIdentityTranslator, error) { 36 flow2p2p := make(map[flow.Identifier]peer.ID) 37 p2p2flow := make(map[peer.ID]flow.Identifier) 38 39 for _, identity := range identities { 40 nodeID := identity.NodeID 41 networkKey := identity.NetworkPubKey 42 peerPK, err := keyutils.LibP2PPublicKeyFromFlow(networkKey) 43 if err != nil { 44 return nil, err 45 } 46 47 peerID, err := peer.IDFromPublicKey(peerPK) 48 if err != nil { 49 return nil, err 50 } 51 52 flow2p2p[nodeID] = peerID 53 p2p2flow[peerID] = nodeID 54 } 55 return &FixedTableIdentityTranslator{flow2p2p, p2p2flow}, nil 56 }