From b67c7d9e0c85bd87e06af1b9ddea5e540310f1e9 Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Fri, 12 Apr 2019 15:33:41 +0200 Subject: [PATCH] Refactor: refactored autopeering plugin --- .../instances/entrynodes/instance.go | 10 ++++- .../protocol/incoming_ping_processor.go | 15 ++++---- .../protocol/incoming_request_processor.go | 8 +--- .../protocol/outgoing_ping_processor.go | 38 +++++++++---------- plugins/autopeering/protocol/plugin.go | 12 ++---- plugins/autopeering/types/ping/ping.go | 22 +++++++++-- 6 files changed, 58 insertions(+), 47 deletions(-) diff --git a/plugins/autopeering/instances/entrynodes/instance.go b/plugins/autopeering/instances/entrynodes/instance.go index 3d5b27a5..0c557ed3 100644 --- a/plugins/autopeering/instances/entrynodes/instance.go +++ b/plugins/autopeering/instances/entrynodes/instance.go @@ -1,6 +1,7 @@ package entrynodes import ( + "encoding/hex" "github.com/iotaledger/goshimmer/packages/identity" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" @@ -28,8 +29,13 @@ func parseEntryNodes() peerlist.PeerList { if len(identityBits) != 2 { panic("error while parsing identity of entry node: " + entryNodeDefinition) } - entryNode.Identity = &identity.Identity{ - StringIdentifier: identityBits[0], + if decodedIdentifier, err := hex.DecodeString(identityBits[0]); err != nil { + panic("error while parsing identity of entry node: " + entryNodeDefinition) + } else { + entryNode.Identity = &identity.Identity{ + Identifier: decodedIdentifier, + StringIdentifier: identityBits[0], + } } addressBits := strings.Split(identityBits[1], ":") diff --git a/plugins/autopeering/protocol/incoming_ping_processor.go b/plugins/autopeering/protocol/incoming_ping_processor.go index 3f080a65..38d61533 100644 --- a/plugins/autopeering/protocol/incoming_ping_processor.go +++ b/plugins/autopeering/protocol/incoming_ping_processor.go @@ -5,14 +5,13 @@ import ( "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" ) -func createIncomingPingProcessor(plugin *node.Plugin) func(p *ping.Ping) { - return func(p *ping.Ping) { - if p.Issuer.Conn != nil { - plugin.LogDebug("received TCP ping from " + p.Issuer.String()) - } else { - plugin.LogDebug("received UDP ping from " + p.Issuer.String()) - } +func createIncomingPingProcessor(plugin *node.Plugin) func(ping *ping.Ping) { + return func(ping *ping.Ping) { + plugin.LogDebug("received ping from " + ping.Issuer.String()) - Events.DiscoverPeer.Trigger(p.Issuer) + Events.DiscoverPeer.Trigger(ping.Issuer) + for _, neighbor := range ping.Neighbors { + Events.DiscoverPeer.Trigger(neighbor) + } } } diff --git a/plugins/autopeering/protocol/incoming_request_processor.go b/plugins/autopeering/protocol/incoming_request_processor.go index 1cba5d81..493ba9e9 100644 --- a/plugins/autopeering/protocol/incoming_request_processor.go +++ b/plugins/autopeering/protocol/incoming_request_processor.go @@ -13,13 +13,9 @@ import ( func createIncomingRequestProcessor(plugin *node.Plugin) func(req *request.Request) { return func(req *request.Request) { - Events.DiscoverPeer.Trigger(req.Issuer) + plugin.LogDebug("received peering request from " + req.Issuer.String()) - if req.Issuer.Conn != nil { - plugin.LogDebug("received TCP peering request from " + req.Issuer.String()) - } else { - plugin.LogDebug("received UDP peering request from " + req.Issuer.String()) - } + Events.DiscoverPeer.Trigger(req.Issuer) if len(neighbormanager.ACCEPTED_NEIGHBORS) <= constants.NEIGHBOR_COUNT / 2 { if err := req.Accept(proposedPeeringCandidates(req)); err != nil { diff --git a/plugins/autopeering/protocol/outgoing_ping_processor.go b/plugins/autopeering/protocol/outgoing_ping_processor.go index 151b6379..12ddb628 100644 --- a/plugins/autopeering/protocol/outgoing_ping_processor.go +++ b/plugins/autopeering/protocol/outgoing_ping_processor.go @@ -12,7 +12,6 @@ import ( "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" - "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "math/rand" "net" "time" @@ -62,30 +61,31 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() { } func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) { - pingDelay := constants.PING_CYCLE_LENGTH / time.Duration(len(neighborhood.LIST_INSTANCE)) + if len(neighborhood.LIST_INSTANCE) >= 1 { + pingDelay := constants.PING_CYCLE_LENGTH / time.Duration(len(neighborhood.LIST_INSTANCE)) - if lastPing.Add(pingDelay).Before(time.Now()) { - chosenPeers := make(map[string]*peer.Peer) + if lastPing.Add(pingDelay).Before(time.Now()) { + chosenPeers := make(map[string]*peer.Peer) - for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ { - randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))] + for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ { + randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))] - nodeId := randomNeighborHoodPeer.Identity.StringIdentifier - - if !neighbormanager.ACCEPTED_NEIGHBORS.Contains(nodeId) && !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) && - nodeId != accountability.OWN_ID.StringIdentifier { - chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer + if randomNeighborHoodPeer.Identity.StringIdentifier != accountability.OWN_ID.StringIdentifier { + chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer + } } - } - for _, chosenPeer := range chosenPeers { - go func(chosenPeer *peer.Peer) { - chosenPeer.Send(outgoingPing.Marshal(), types.PROTOCOL_TYPE_UDP, false) + for _, chosenPeer := range chosenPeers { + go func(chosenPeer *peer.Peer) { + if _, err := chosenPeer.Send(outgoingPing.Marshal(), types.PROTOCOL_TYPE_UDP, false); err != nil { + plugin.LogDebug("error when sending ping to " + chosenPeer.String() + ": " + err.Error()) + } else { + plugin.LogDebug("sent ping to " + chosenPeer.String()) + } + }(chosenPeer) + } - plugin.LogDebug("sent ping to " + chosenPeer.String()) - }(chosenPeer) + lastPing = time.Now() } - - lastPing = time.Now() } } \ No newline at end of file diff --git a/plugins/autopeering/protocol/plugin.go b/plugins/autopeering/protocol/plugin.go index 06ae95b0..0e6ed5a3 100644 --- a/plugins/autopeering/protocol/plugin.go +++ b/plugins/autopeering/protocol/plugin.go @@ -8,19 +8,13 @@ import ( ) func Configure(plugin *node.Plugin) { - incomingPingProcessor := createIncomingPingProcessor(plugin) - incomingRequestProcessor := createIncomingRequestProcessor(plugin) - incomingResponseProcessor := createIncomingResponseProcessor(plugin) errorHandler := createErrorHandler(plugin) - udp.Events.ReceivePing.Attach(incomingPingProcessor) - udp.Events.ReceiveRequest.Attach(incomingRequestProcessor) - udp.Events.ReceiveResponse.Attach(incomingResponseProcessor) + udp.Events.ReceivePing.Attach(createIncomingPingProcessor(plugin)) udp.Events.Error.Attach(errorHandler) - tcp.Events.ReceivePing.Attach(incomingPingProcessor) - tcp.Events.ReceiveRequest.Attach(incomingRequestProcessor) - tcp.Events.ReceiveResponse.Attach(incomingResponseProcessor) + tcp.Events.ReceiveRequest.Attach(createIncomingRequestProcessor(plugin)) + tcp.Events.ReceiveResponse.Attach(createIncomingResponseProcessor(plugin)) tcp.Events.Error.Attach(errorHandler) } diff --git a/plugins/autopeering/types/ping/ping.go b/plugins/autopeering/types/ping/ping.go index eda383ec..3b25656d 100644 --- a/plugins/autopeering/types/ping/ping.go +++ b/plugins/autopeering/types/ping/ping.go @@ -3,13 +3,15 @@ package ping import ( "bytes" "github.com/iotaledger/goshimmer/packages/identity" + "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" ) type Ping struct { Issuer *peer.Peer - Neighbors []*peer.Peer + Neighbors peerlist.PeerList Signature [MARSHALLED_SIGNATURE_SIZE]byte } @@ -18,18 +20,32 @@ func Unmarshal(data []byte) (*Ping, error) { return nil, ErrMalformedPing } - ping := &Ping{} + ping := &Ping{ + Neighbors: make(peerlist.PeerList, 0), + } if unmarshalledPeer, err := peer.Unmarshal(data[MARSHALLED_ISSUER_START:MARSHALLED_ISSUER_END]); err != nil { return nil, err } else { ping.Issuer = unmarshalledPeer } - if err := saltmanager.CheckSalt(ping.Issuer.Salt); err != nil { return nil, err } + offset := MARSHALLED_PEERS_START + for i:= 0; i < constants.NEIGHBOR_COUNT; i++ { + if data[offset] == 1 { + if unmarshalledPing, err := peer.Unmarshal(data[offset + 1:offset + MARSHALLED_PEER_ENTRY_SIZE]); err != nil { + return nil, err + } else { + ping.Neighbors = append(ping.Neighbors, unmarshalledPing) + } + } + + offset += MARSHALLED_PEER_ENTRY_SIZE + } + if issuer, err := identity.FromSignedData(data[:MARSHALLED_SIGNATURE_START], data[MARSHALLED_SIGNATURE_START:]); err != nil { return nil, err } else { -- GitLab