From 250f60a4b6ccc8ce519adba977216de5f76d52d5 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sun, 25 Apr 2021 05:34:00 +0200 Subject: [PATCH] New flag: IsRootPeer. Rearranged initialization priority (initNetwork should always be last). --- Bootstrap.go | 1 + Peer ID.go | 8 ++++++-- Peernet.go | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Bootstrap.go b/Bootstrap.go index 0abd4a6..fd88990 100644 --- a/Bootstrap.go +++ b/Bootstrap.go @@ -33,6 +33,7 @@ type rootPeer struct { var rootPeers map[[btcec.PubKeyBytesLenCompressed]byte]*rootPeer // initSeedList loads the seed list from the config +// Note: This should be called before any network listening function so that incoming root peers are properly recognized. func initSeedList() { rootPeers = make(map[[btcec.PubKeyBytesLenCompressed]byte]*rootPeer) diff --git a/Peer ID.go b/Peer ID.go index 85bcb23..3530983 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -88,6 +88,7 @@ type PeerInfo struct { connectionLatest *Connection // Latest valid connection. sync.RWMutex // Mutex for access to list of connections. messageSequence uint32 // Sequence number. Increased with every message. + IsRootPeer bool // Whether the peer is a trusted root peer. // statistics StatsPacketSent uint64 // Count of packets sent @@ -102,17 +103,20 @@ func PeerlistAdd(PublicKey *btcec.PublicKey, connections ...*Connection) (peer * if len(connections) == 0 { return nil, false } + publicKeyCompressed := publicKey2Compressed(PublicKey) peerlistMutex.Lock() defer peerlistMutex.Unlock() - peer, ok := peerList[publicKey2Compressed(PublicKey)] + peer, ok := peerList[publicKeyCompressed] if ok { return peer, false } peer = &PeerInfo{PublicKey: PublicKey, connectionActive: connections, connectionLatest: connections[0], NodeID: publicKey2NodeID(PublicKey), messageSequence: rand.Uint32()} - peerList[publicKey2Compressed(peer.PublicKey)] = peer + _, peer.IsRootPeer = rootPeers[publicKeyCompressed] + + peerList[publicKeyCompressed] = peer // add to Kademlia nodesDHT.AddNode(&dht.Node{ID: peer.NodeID, Info: peer}) diff --git a/Peernet.go b/Peernet.go index 2154057..86be704 100644 --- a/Peernet.go +++ b/Peernet.go @@ -11,11 +11,11 @@ func Init() { initPeerID() initKademlia() initMessageSequence() + initSeedList() initMulticastIPv6() initBroadcastIPv4() - initNetwork() - initSeedList() initStore() + initNetwork() } // Connect starts bootstrapping and local peer discovery.