diff --git a/plugins/autopeering/instances/entrynodes/instance.go b/plugins/autopeering/instances/entrynodes/instance.go index 3d5b27a5b333912cc504440d768636e91ca9dc9b..0c557ed3a3a74761b6cb21888578100ad8be1168 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 3f080a65bf615a9baf560598bbcf01cc5d8d9314..38d615333f0c087f952cdff1582946a0d371b215 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 1cba5d81ccf1e1af9989b8721d639c574c45b772..493ba9e93bc738ee40ad8fafe42496acc82c7eba 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 151b637982736b54d9407efefecacaf68c3ae3a4..12ddb628b4d61ca93a058e383266b896b11a603a 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 06ae95b0552c45b964f29f50129dcc1115027529..0e6ed5a33db2e5acbeb7d2c712eb30d346a30d37 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 eda383ecedc138eec9cfded85ea8402be7cc086f..3b25656d021e255f241304e132dbb3741903ca9e 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 {