Skip to content
Snippets Groups Projects
Commit b67c7d9e authored by Hans Moog's avatar Hans Moog
Browse files

Refactor: refactored autopeering plugin

parent 36285a67
No related branches found
No related tags found
No related merge requests found
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], ":")
......
......@@ -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)
}
}
}
......@@ -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 {
......
......@@ -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
......@@ -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)
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment