github.com/amazechain/amc@v0.1.3/internal/network/protocol.go (about) 1 // Copyright 2022 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package network 18 19 import ( 20 "context" 21 "github.com/amazechain/amc/log" 22 23 "github.com/amazechain/amc/common/types" 24 "github.com/libp2p/go-libp2p/core/host" 25 "github.com/libp2p/go-libp2p/core/network" 26 "github.com/libp2p/go-libp2p/core/peer" 27 "github.com/libp2p/go-libp2p/core/protocol" 28 ) 29 30 const ( 31 DefaultP2PListenAddress = "/ip4/0.0.0.0/tcp/21324" 32 MSGProtocol = protocol.ID("/amc/msg/1.0.0") 33 DiscoverProtocol = "/amc/discover/1.0.0" 34 AppProtocol = "/amc/app/1.0.0" 35 P2ProtocolVersion = "0.0.1" 36 ) 37 38 type discoveryNotifee struct { 39 h host.Host 40 ctx context.Context 41 peerCh chan peerInfo 42 } 43 44 func (m *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { 45 select { 46 case <-m.ctx.Done(): 47 return 48 default: 49 50 if pi.ID == m.h.ID() { 51 log.Warnf("is self peer remote=%s == self=%s", pi.ID.ShortString(), m.h.ID().ShortString()) 52 return 53 } 54 55 log.Debugf("Found %s", pi.ID.String()) 56 m.peerCh <- peerInfo{ 57 peer: pi, 58 Connectedness: m.h.Network().Connectedness(pi.ID), 59 } 60 } 61 } 62 63 type peerInfo struct { 64 peer peer.AddrInfo 65 network.Connectedness 66 } 67 68 type Handshake func(genesisHash types.Hash, currentHeight uint64) error