diff --git a/plugins/autopeering/instances/chosenneighborcandidates/instance.go b/plugins/autopeering/instances/chosenneighborcandidates/instance.go new file mode 100644 index 0000000000000000000000000000000000000000..c4697d4fffc9fdc94c76a083c250bb134c121eec --- /dev/null +++ b/plugins/autopeering/instances/chosenneighborcandidates/instance.go @@ -0,0 +1,28 @@ +package chosenneighborcandidates + +import ( + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" +) + +var INSTANCE peerlist.PeerList + +var DISTANCE = func(req *request.Request) func(p *peer.Peer) float64 { + return func(p *peer.Peer) float64 { + return 1 + } +} + +func init() { + updateNeighborCandidates() + + neighborhood.Events.Update.Attach(updateNeighborCandidates) +} + +func updateNeighborCandidates() { + INSTANCE = neighborhood.LIST_INSTANCE.Sort(DISTANCE(outgoingrequest.INSTANCE)) +} + diff --git a/plugins/autopeering/peermanager/entry_nodes.go b/plugins/autopeering/instances/entrynodes/instance.go similarity index 68% rename from plugins/autopeering/peermanager/entry_nodes.go rename to plugins/autopeering/instances/entrynodes/instance.go index f7f7b22c01ce73e96a4acc8f365ad0176e8a4322..3d5b27a5b333912cc504440d768636e91ca9dc9b 100644 --- a/plugins/autopeering/peermanager/entry_nodes.go +++ b/plugins/autopeering/instances/entrynodes/instance.go @@ -1,20 +1,19 @@ -package peermanager +package entrynodes import ( "github.com/iotaledger/goshimmer/packages/identity" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" - peermanagerTypes "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" "net" "strconv" "strings" ) -var ENTRY_NODES = parseEntryNodes() +var INSTANCE = parseEntryNodes() -func parseEntryNodes() peermanagerTypes.PeerList { - result := make(peermanagerTypes.PeerList, 0) +func parseEntryNodes() peerlist.PeerList { + result := make(peerlist.PeerList, 0) for _, entryNodeDefinition := range strings.Fields(*parameters.ENTRY_NODES.Value) { if entryNodeDefinition == "" { @@ -25,18 +24,7 @@ func parseEntryNodes() peermanagerTypes.PeerList { Identity: nil, } - protocolBits := strings.Split(entryNodeDefinition, "://") - if len(protocolBits) != 2 { - panic("invalid entry in list of trusted entry nodes: " + entryNodeDefinition) - } - switch protocolBits[0] { - case "tcp": - entryNode.PeeringProtocolType = types.PROTOCOL_TYPE_TCP - case "udp": - entryNode.PeeringProtocolType = types.PROTOCOL_TYPE_UDP - } - - identityBits := strings.Split(protocolBits[1], "@") + identityBits := strings.Split(entryNodeDefinition, "@") if len(identityBits) != 2 { panic("error while parsing identity of entry node: " + entryNodeDefinition) } diff --git a/plugins/autopeering/instances/knownpeers/instance.go b/plugins/autopeering/instances/knownpeers/instance.go new file mode 100644 index 0000000000000000000000000000000000000000..3b5af569574180ecb053700765c9fea9009ec70c --- /dev/null +++ b/plugins/autopeering/instances/knownpeers/instance.go @@ -0,0 +1,17 @@ +package knownpeers + +import ( + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/entrynodes" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister" +) + +var INSTANCE = initKnownPeers() + +func initKnownPeers() peerregister.PeerRegister { + knownPeers := make(peerregister.PeerRegister) + for _, entryNode := range entrynodes.INSTANCE { + knownPeers.AddOrUpdate(entryNode) + } + + return knownPeers +} diff --git a/plugins/autopeering/peermanager/events.go b/plugins/autopeering/instances/neighborhood/events.go similarity index 77% rename from plugins/autopeering/peermanager/events.go rename to plugins/autopeering/instances/neighborhood/events.go index db39e88834ac1efbd67ad70c11ceb4231d7bba0e..3796ec2d60ec93d9bcd213c0e54fa54e0a63facd 100644 --- a/plugins/autopeering/peermanager/events.go +++ b/plugins/autopeering/instances/neighborhood/events.go @@ -1,13 +1,13 @@ -package peermanager +package neighborhood import "reflect" var Events = moduleEvents{ - UpdateNeighborhood: &callbackEvent{make(map[uintptr]Callback)}, + Update: &callbackEvent{make(map[uintptr]Callback)}, } type moduleEvents struct { - UpdateNeighborhood *callbackEvent + Update *callbackEvent } type callbackEvent struct { @@ -28,4 +28,4 @@ func (this *callbackEvent) Trigger() { } } -type Callback = func() \ No newline at end of file +type Callback = func() diff --git a/plugins/autopeering/instances/neighborhood/instance.go b/plugins/autopeering/instances/neighborhood/instance.go new file mode 100644 index 0000000000000000000000000000000000000000..7ce955001f5a924278c53eedbd30f57cafa50bdb --- /dev/null +++ b/plugins/autopeering/instances/neighborhood/instance.go @@ -0,0 +1,45 @@ +package neighborhood + +import ( + "github.com/iotaledger/goshimmer/packages/timeutil" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister" + "time" +) + +var INSTANCE peerregister.PeerRegister + +var LIST_INSTANCE peerlist.PeerList + +// Selects a fixed neighborhood from all known peers - this allows nodes to "stay in the same circles" that share their +// view on the ledger an is a preparation for economic clustering +var NEIGHBORHOOD_SELECTOR = func(this peerregister.PeerRegister, req *request.Request) peerregister.PeerRegister { + filteredPeers := make(peerregister.PeerRegister) + for id, peer := range this { + filteredPeers[id] = peer + } + + return filteredPeers +} + +var lastUpdate = time.Now() + +func init() { + updateNeighborHood() + + go timeutil.Ticker(updateNeighborHood, 1 * time.Second) +} + +func updateNeighborHood() { + if float64(len(INSTANCE)) * 1.2 <= float64(len(knownpeers.INSTANCE)) || lastUpdate.Before(time.Now().Add(-300 * time.Second)) { + INSTANCE = knownpeers.INSTANCE.Filter(NEIGHBORHOOD_SELECTOR, outgoingrequest.INSTANCE) + LIST_INSTANCE = INSTANCE.List() + + lastUpdate = time.Now() + + Events.Update.Trigger() + } +} diff --git a/plugins/autopeering/instances/outgoingrequest/instance.go b/plugins/autopeering/instances/outgoingrequest/instance.go new file mode 100644 index 0000000000000000000000000000000000000000..e4310e727c860072253ed541304da73a461da63a --- /dev/null +++ b/plugins/autopeering/instances/outgoingrequest/instance.go @@ -0,0 +1,21 @@ +package outgoingrequest + +import ( + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" +) + +var INSTANCE *request.Request + +func init() { + INSTANCE = &request.Request{ + Issuer: ownpeer.INSTANCE, + } + INSTANCE.Sign() + + saltmanager.Events.UpdatePublicSalt.Attach(func(salt *salt.Salt) { + INSTANCE.Sign() + }) +} diff --git a/plugins/autopeering/instances/ownpeer/instance.go b/plugins/autopeering/instances/ownpeer/instance.go new file mode 100644 index 0000000000000000000000000000000000000000..7daff310f300a20549dfc7bcc99f1e64085f16ad --- /dev/null +++ b/plugins/autopeering/instances/ownpeer/instance.go @@ -0,0 +1,17 @@ +package ownpeer + +import ( + "github.com/iotaledger/goshimmer/packages/accountability" + "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" + "net" +) + +var INSTANCE = &peer.Peer{ + Identity: accountability.OWN_ID, + PeeringPort: uint16(*parameters.PORT.Value), + GossipPort: uint16(*parameters.PORT.Value), + Address: net.IPv4(0, 0, 0, 0), + Salt: saltmanager.PUBLIC_SALT, +} diff --git a/plugins/autopeering/parameters/parameters.go b/plugins/autopeering/parameters/parameters.go index 8d5b29f79857a4493e5d46e215a4ef25edc60d6c..092ff6f452489a50617af06abfb2188057889be4 100644 --- a/plugins/autopeering/parameters/parameters.go +++ b/plugins/autopeering/parameters/parameters.go @@ -4,7 +4,6 @@ import "github.com/iotaledger/goshimmer/packages/parameter" var ( ADDRESS = parameter.AddString("AUTOPEERING/ADDRESS", "0.0.0.0", "address to bind for incoming peering requests") - ENTRY_NODES = parameter.AddString("AUTOPEERING/ENTRY_NODES", "tcp://0d828930890386f036eb77982cc067c5429f7b8f@82.165.29.179:14626", "list of trusted entry nodes for auto peering") - TCP_PORT = parameter.AddInt("AUTOPEERING/TCP_PORT", 14626, "tcp port for incoming peering requests") - UDP_PORT = parameter.AddInt("AUTOPEERING/UDP_PORT", 14626, "udp port for incoming peering requests") + ENTRY_NODES = parameter.AddString("AUTOPEERING/ENTRY_NODES", "0d828930890386f036eb77982cc067c5429f7b8f@82.165.29.179:14626", "list of trusted entry nodes for auto peering") + PORT = parameter.AddInt("AUTOPEERING/PORT", 14626, "tcp port for incoming peering requests") ) diff --git a/plugins/autopeering/peermanager/chosen_neighbor_candidates.go b/plugins/autopeering/peermanager/chosen_neighbor_candidates.go deleted file mode 100644 index 52f41864e244996b9e21c177e7bea61308767980..0000000000000000000000000000000000000000 --- a/plugins/autopeering/peermanager/chosen_neighbor_candidates.go +++ /dev/null @@ -1,25 +0,0 @@ -package peermanager - -import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" -) - -var CHOSEN_NEIGHBOR_CANDIDATES types.PeerList - -var CHOSEN_NEIGHBOR_DISTANCE = func(req *request.Request) func(p *peer.Peer) float64 { - return func(p *peer.Peer) float64 { - return 1 - } -} - -func init() { - updateNeighborCandidates() - - Events.UpdateNeighborhood.Attach(updateNeighborCandidates) -} - -func updateNeighborCandidates() { - CHOSEN_NEIGHBOR_CANDIDATES = NEIGHBORHOOD.List().Sort(CHOSEN_NEIGHBOR_DISTANCE(request.OUTGOING_REQUEST)) -} diff --git a/plugins/autopeering/peermanager/known_peers.go b/plugins/autopeering/peermanager/known_peers.go deleted file mode 100644 index 1b23fb82e7821e65b576f953fb60ce0e371e5efb..0000000000000000000000000000000000000000 --- a/plugins/autopeering/peermanager/known_peers.go +++ /dev/null @@ -1,16 +0,0 @@ -package peermanager - -import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" -) - -var KNOWN_PEERS = initKnownPeers() - -func initKnownPeers() types.PeerRegister { - knownPeers := make(types.PeerRegister) - for _, entryNode := range ENTRY_NODES { - knownPeers.AddOrUpdate(entryNode) - } - - return knownPeers -} \ No newline at end of file diff --git a/plugins/autopeering/peermanager/neighborhood.go b/plugins/autopeering/peermanager/neighborhood.go deleted file mode 100644 index e6d95d89ccaa06fdf5c7f3d882a0a86e8448809c..0000000000000000000000000000000000000000 --- a/plugins/autopeering/peermanager/neighborhood.go +++ /dev/null @@ -1,42 +0,0 @@ -package peermanager - -import ( - "github.com/iotaledger/goshimmer/packages/timeutil" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "time" -) - -var NEIGHBORHOOD types.PeerRegister - -var NEIGHBORHOOD_LIST types.PeerList - -// Selects a fixed neighborhood from all known peers - this allows nodes to "stay in the same circles" that share their -// view on the ledger an is a preparation for economic clustering -var NEIGHBORHOOD_SELECTOR = func(this types.PeerRegister, req *request.Request) types.PeerRegister { - filteredPeers := make(types.PeerRegister) - for id, peer := range this { - filteredPeers[id] = peer - } - - return filteredPeers -} - -var lastUpdate = time.Now() - -func init() { - updateNeighborHood() - - go timeutil.Ticker(updateNeighborHood, 1 * time.Second) -} - -func updateNeighborHood() { - if float64(len(NEIGHBORHOOD)) * 1.2 <= float64(len(KNOWN_PEERS)) || lastUpdate.Before(time.Now().Add(-300 * time.Second)) { - NEIGHBORHOOD = KNOWN_PEERS.Filter(NEIGHBORHOOD_SELECTOR, request.OUTGOING_REQUEST) - NEIGHBORHOOD_LIST = NEIGHBORHOOD.List() - - lastUpdate = time.Now() - - Events.UpdateNeighborhood.Trigger() - } -} \ No newline at end of file diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go index 4f8590d6304b29825fd63422bc9d210a7c0eee33..19e4e5ae8c2ab01e07d61a46552542258bbae4ff 100644 --- a/plugins/autopeering/plugin.go +++ b/plugins/autopeering/plugin.go @@ -3,11 +3,11 @@ package autopeering import ( "github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/node" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "github.com/iotaledger/goshimmer/plugins/autopeering/server" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" ) @@ -33,7 +33,7 @@ func configure(plugin *node.Plugin) { }) protocol.Events.DiscoverPeer.Attach(func(p *peer.Peer) { - if peermanager.KNOWN_PEERS.AddOrUpdate(p) { + if knownpeers.INSTANCE.AddOrUpdate(p) { plugin.LogInfo("new peer detected: " + p.Address.String() + " / " + p.Identity.StringIdentifier) } }) diff --git a/plugins/autopeering/protocol/constants/constants.go b/plugins/autopeering/protocol/constants/constants.go index 4e26ea74b9fce29ea226f23f1a1a5357b8161de5..e181bf150c4deb04eba98dae6d197cafdd836d9c 100644 --- a/plugins/autopeering/protocol/constants/constants.go +++ b/plugins/autopeering/protocol/constants/constants.go @@ -5,7 +5,14 @@ import "time" const ( NEIGHBOR_COUNT = 8 - FIND_NEIGHBOR_INTERVAL = 5 * time.Second - PING_RANDOM_PEERS_INTERVAL = 1 * time.Second - PING_RANDOM_PEERS_COUNT = 3 + FIND_NEIGHBOR_INTERVAL = 5 * time.Second + + // How often does the outgoing ping processor check if new pings should be sent. + PING_PROCESS_INTERVAL = 1 * time.Second + + // The amount of times each neighbor should be contacted in this cycle. + PING_CONTACT_COUNT_PER_CYCLE = 2 + + // The length of a ping cycle (after this time we have sent randomized pings to all of our neighbors). + PING_CYCLE_LENGTH = 900 * time.Second ) diff --git a/plugins/autopeering/protocol/events.go b/plugins/autopeering/protocol/events.go index 6d4d78bca27f20e033d62263aa0dd2028f609a92..31b3c1f10b5a9db57fb9da3025d239848333434e 100644 --- a/plugins/autopeering/protocol/events.go +++ b/plugins/autopeering/protocol/events.go @@ -1,9 +1,9 @@ package protocol import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "reflect" ) diff --git a/plugins/autopeering/protocol/incoming_ping_processor.go b/plugins/autopeering/protocol/incoming_ping_processor.go index f2b30f9ad90ea593aa8f212b99c6061ef1fc2cca..3f080a65bf615a9baf560598bbcf01cc5d8d9314 100644 --- a/plugins/autopeering/protocol/incoming_ping_processor.go +++ b/plugins/autopeering/protocol/incoming_ping_processor.go @@ -2,7 +2,7 @@ package protocol import ( "github.com/iotaledger/goshimmer/packages/node" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" ) func createIncomingPingProcessor(plugin *node.Plugin) func(p *ping.Ping) { diff --git a/plugins/autopeering/protocol/incoming_request_processor.go b/plugins/autopeering/protocol/incoming_request_processor.go index 2f94ff19ef1f8fd35a0cd405b2433292d76332dc..1cba5d81ccf1e1af9989b8721d639c574c45b772 100644 --- a/plugins/autopeering/protocol/incoming_request_processor.go +++ b/plugins/autopeering/protocol/incoming_request_processor.go @@ -2,11 +2,12 @@ package protocol import ( "github.com/iotaledger/goshimmer/packages/node" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighborcandidates" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" ) @@ -40,8 +41,8 @@ func createIncomingRequestProcessor(plugin *node.Plugin) func(req *request.Reque } } -func proposedPeeringCandidates(req *request.Request) types.PeerList { - return peermanager.NEIGHBORHOOD.List().Filter(func(p *peer.Peer) bool { +func proposedPeeringCandidates(req *request.Request) peerlist.PeerList { + return neighborhood.LIST_INSTANCE.Filter(func(p *peer.Peer) bool { return p.Identity.PublicKey != nil - }).Sort(peermanager.CHOSEN_NEIGHBOR_DISTANCE(req)) + }).Sort(chosenneighborcandidates.DISTANCE(req)) } \ No newline at end of file diff --git a/plugins/autopeering/protocol/incoming_response_processor.go b/plugins/autopeering/protocol/incoming_response_processor.go index d99cf1cd0bb245963a46303fb755ef8ddbf58434..9325c655547b844f6803c0f87e0ae2409a92d644 100644 --- a/plugins/autopeering/protocol/incoming_response_processor.go +++ b/plugins/autopeering/protocol/incoming_response_processor.go @@ -2,7 +2,7 @@ package protocol import ( "github.com/iotaledger/goshimmer/packages/node" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "strconv" ) diff --git a/plugins/autopeering/protocol/outgoing_ping_processor.go b/plugins/autopeering/protocol/outgoing_ping_processor.go index 808cb1aefb2e80564101612192621c768a06bfa0..151b637982736b54d9407efefecacaf68c3ae3a4 100644 --- a/plugins/autopeering/protocol/outgoing_ping_processor.go +++ b/plugins/autopeering/protocol/outgoing_ping_processor.go @@ -4,33 +4,35 @@ import ( "github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/node" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" + "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" ) +var lastPing time.Time + func createOutgoingPingProcessor(plugin *node.Plugin) func() { return func() { plugin.LogInfo("Starting Ping Processor ...") plugin.LogSuccess("Starting Ping Processor ... done") + + lastPing = time.Now().Add(-constants.PING_CYCLE_LENGTH) outgoingPing := &ping.Ping{ Issuer: &peer.Peer{ Identity: accountability.OWN_ID, - PeeringProtocolType: types.PROTOCOL_TYPE_TCP, - PeeringPort: uint16(*parameters.UDP_PORT.Value), - GossipProtocolType: types.PROTOCOL_TYPE_TCP, - GossipPort: uint16(*parameters.UDP_PORT.Value), Address: net.IPv4(0, 0, 0, 0), + PeeringPort: uint16(*parameters.PORT.Value), + GossipPort: uint16(*parameters.PORT.Value), Salt: saltmanager.PUBLIC_SALT, }, } @@ -42,7 +44,7 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() { pingPeers(plugin, outgoingPing) - ticker := time.NewTicker(constants.PING_RANDOM_PEERS_INTERVAL) + ticker := time.NewTicker(constants.PING_PROCESS_INTERVAL) ticker: for { select { @@ -60,24 +62,30 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() { } func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) { - chosenPeers := make(map[string]*peer.Peer) + pingDelay := constants.PING_CYCLE_LENGTH / time.Duration(len(neighborhood.LIST_INSTANCE)) - for i := 0; i < constants.PING_RANDOM_PEERS_COUNT; i++ { - randomNeighborHoodPeer := peermanager.NEIGHBORHOOD_LIST[rand.Intn(len(peermanager.NEIGHBORHOOD_LIST))] + if lastPing.Add(pingDelay).Before(time.Now()) { + chosenPeers := make(map[string]*peer.Peer) - nodeId := randomNeighborHoodPeer.Identity.StringIdentifier + for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ { + randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))] - if !neighbormanager.ACCEPTED_NEIGHBORS.Contains(nodeId) && !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) && + 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 + chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer + } } - } - for _, chosenPeer := range chosenPeers { - go func(chosenPeer *peer.Peer) { - chosenPeer.Send(outgoingPing.Marshal(), false) + for _, chosenPeer := range chosenPeers { + go func(chosenPeer *peer.Peer) { + chosenPeer.Send(outgoingPing.Marshal(), types.PROTOCOL_TYPE_UDP, false) + + plugin.LogDebug("sent ping to " + chosenPeer.String()) + }(chosenPeer) + } - plugin.LogDebug("sent ping to " + chosenPeer.String()) - }(chosenPeer) + lastPing = time.Now() } } \ No newline at end of file diff --git a/plugins/autopeering/protocol/chosen_neighbor_processor.go b/plugins/autopeering/protocol/outgoing_request_processor.go similarity index 63% rename from plugins/autopeering/protocol/chosen_neighbor_processor.go rename to plugins/autopeering/protocol/outgoing_request_processor.go index d76a4a190f53592e18ba7f48cf4d76557007c512..4edccbdda6117c0cbbe14eb3bac10fc1b2b8af3f 100644 --- a/plugins/autopeering/protocol/chosen_neighbor_processor.go +++ b/plugins/autopeering/protocol/outgoing_request_processor.go @@ -4,21 +4,22 @@ import ( "github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/node" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighborcandidates" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" "github.com/iotaledger/goshimmer/plugins/autopeering/server/tcp" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "time" ) -func createChosenNeighborProcessor(plugin *node.Plugin) func() { +func createOutgoingRequestProcessor(plugin *node.Plugin) func() { return func() { plugin.LogInfo("Starting Chosen Neighbor Processor ...") plugin.LogSuccess("Starting Chosen Neighbor Processor ... done") - chooseNeighbors(plugin) + sendOutgoingRequests(plugin) ticker := time.NewTicker(constants.FIND_NEIGHBOR_INTERVAL) ticker: @@ -29,7 +30,7 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() { break ticker case <- ticker.C: - chooseNeighbors(plugin) + sendOutgoingRequests(plugin) } } @@ -37,8 +38,8 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() { } } -func chooseNeighbors(plugin *node.Plugin) { - for _, chosenNeighborCandidate := range peermanager.CHOSEN_NEIGHBOR_CANDIDATES { +func sendOutgoingRequests(plugin *node.Plugin) { + for _, chosenNeighborCandidate := range chosenneighborcandidates.INSTANCE { go func(peer *peer.Peer) { nodeId := peer.Identity.StringIdentifier @@ -46,14 +47,14 @@ func chooseNeighbors(plugin *node.Plugin) { !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) && accountability.OWN_ID.StringIdentifier != nodeId { - if connectionAlive, err := request.Send(peer); err != nil { + if dialed, err := peer.Send(outgoingrequest.INSTANCE.Marshal(), types.PROTOCOL_TYPE_TCP, true); err != nil { plugin.LogDebug(err.Error()) - } else if connectionAlive { - plugin.LogDebug("sent TCP peering request to " + peer.String()) - - tcp.HandleConnection(peer.Conn) } else { - plugin.LogDebug("sent UDP peering request to " + peer.String()) + plugin.LogDebug("sent peering request to " + peer.String()) + + if dialed { + tcp.HandleConnection(peer.Conn) + } } } }(chosenNeighborCandidate) diff --git a/plugins/autopeering/protocol/plugin.go b/plugins/autopeering/protocol/plugin.go index e7e9820a2c8c1536a330ce34ef0c3760cd1a303b..06ae95b0552c45b964f29f50129dcc1115027529 100644 --- a/plugins/autopeering/protocol/plugin.go +++ b/plugins/autopeering/protocol/plugin.go @@ -25,6 +25,6 @@ func Configure(plugin *node.Plugin) { } func Run(plugin *node.Plugin) { - daemon.BackgroundWorker(createChosenNeighborProcessor(plugin)) + daemon.BackgroundWorker(createOutgoingRequestProcessor(plugin)) daemon.BackgroundWorker(createOutgoingPingProcessor(plugin)) } diff --git a/plugins/autopeering/protocol/request/send.go b/plugins/autopeering/protocol/request/send.go deleted file mode 100644 index 739eba9dcf77cd3af58834a8360b68c575a77cc6..0000000000000000000000000000000000000000 --- a/plugins/autopeering/protocol/request/send.go +++ /dev/null @@ -1,51 +0,0 @@ -package request - -import ( - "github.com/iotaledger/goshimmer/packages/accountability" - "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" - "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" - "github.com/pkg/errors" - "net" -) - -var OUTGOING_REQUEST *Request - -func Send(peer *peer.Peer) (bool, error) { - var keepAlive bool - switch peer.PeeringProtocolType { - case types.PROTOCOL_TYPE_TCP: - keepAlive = true - case types.PROTOCOL_TYPE_UDP: - keepAlive = false - default: - return false, errors.New("peer uses invalid protocol") - } - - if err := peer.Send(OUTGOING_REQUEST.Marshal(), keepAlive); err != nil { - return false, err - } - - return keepAlive, nil -} - -func init() { - OUTGOING_REQUEST = &Request{ - Issuer: &peer.Peer{ - Identity: accountability.OWN_ID, - PeeringProtocolType: types.PROTOCOL_TYPE_TCP, - PeeringPort: uint16(*parameters.UDP_PORT.Value), - GossipProtocolType: types.PROTOCOL_TYPE_TCP, - GossipPort: uint16(*parameters.UDP_PORT.Value), - Address: net.IPv4(0, 0, 0, 0), - Salt: saltmanager.PUBLIC_SALT, - }, - } - OUTGOING_REQUEST.Sign() - - saltmanager.Events.UpdatePublicSalt.Attach(func(salt *salt.Salt) { - OUTGOING_REQUEST.Sign() - }) -} diff --git a/plugins/autopeering/saltmanager/events.go b/plugins/autopeering/saltmanager/events.go index 5fb00471ad39355ba633679b842df205323d6d86..b9493017916e911dae12da1d1c60bc7adc9bad89 100644 --- a/plugins/autopeering/saltmanager/events.go +++ b/plugins/autopeering/saltmanager/events.go @@ -1,7 +1,7 @@ package saltmanager import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" "reflect" ) diff --git a/plugins/autopeering/saltmanager/saltmanager.go b/plugins/autopeering/saltmanager/saltmanager.go index 2c3d62399f378cbfe3ec6c4d3ca9cd6a6d721570..acb88f066b8303be093d2072d358cafe1c2036c9 100644 --- a/plugins/autopeering/saltmanager/saltmanager.go +++ b/plugins/autopeering/saltmanager/saltmanager.go @@ -4,7 +4,7 @@ import ( "github.com/dgraph-io/badger" "github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/settings" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" "time" ) diff --git a/plugins/autopeering/saltmanager/types.go b/plugins/autopeering/saltmanager/types.go index 1a0438c005e7b072acb1fa2e9d8225eaf742305c..4a273e65685b26ada412b29272d8052ed88c6459 100644 --- a/plugins/autopeering/saltmanager/types.go +++ b/plugins/autopeering/saltmanager/types.go @@ -1,5 +1,5 @@ package saltmanager -import "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" +import "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" type SaltConsumer = func(salt *salt.Salt) diff --git a/plugins/autopeering/saltmanager/utils.go b/plugins/autopeering/saltmanager/utils.go index 5142c84beeb8b2786a7493a83d9f3f3a8ebd887f..755b07db4f20d7b00fbdefcd89e64681308be393 100644 --- a/plugins/autopeering/saltmanager/utils.go +++ b/plugins/autopeering/saltmanager/utils.go @@ -1,7 +1,7 @@ package saltmanager import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" "time" ) diff --git a/plugins/autopeering/server/tcp/events.go b/plugins/autopeering/server/tcp/events.go index dedffa4f111b6c614e0db62a5d5347c7e5573d46..d1d4376f1b746b804ee2e2d899344e8e79dbddd9 100644 --- a/plugins/autopeering/server/tcp/events.go +++ b/plugins/autopeering/server/tcp/events.go @@ -1,9 +1,9 @@ package tcp import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "net" "reflect" ) diff --git a/plugins/autopeering/server/tcp/server.go b/plugins/autopeering/server/tcp/server.go index 81127fa8daa6fa8227798fe057adf4a9eb7a268f..bfe9e987f887fcf28bd1e123792b5ee7eed37350 100644 --- a/plugins/autopeering/server/tcp/server.go +++ b/plugins/autopeering/server/tcp/server.go @@ -6,9 +6,9 @@ import ( "github.com/iotaledger/goshimmer/packages/network/tcp" "github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "github.com/pkg/errors" "math" "net" @@ -24,9 +24,9 @@ func ConfigureServer(plugin *node.Plugin) { }) server.Events.Start.Attach(func() { if *parameters.ADDRESS.Value == "0.0.0.0" { - plugin.LogSuccess("Starting TCP Server (port " + strconv.Itoa(*parameters.TCP_PORT.Value) + ") ... done") + plugin.LogSuccess("Starting TCP Server (port " + strconv.Itoa(*parameters.PORT.Value) + ") ... done") } else { - plugin.LogSuccess("Starting TCP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.TCP_PORT.Value) + ") ... done") + plugin.LogSuccess("Starting TCP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.PORT.Value) + ") ... done") } }) server.Events.Shutdown.Attach(func() { @@ -37,12 +37,12 @@ func ConfigureServer(plugin *node.Plugin) { func RunServer(plugin *node.Plugin) { daemon.BackgroundWorker(func() { if *parameters.ADDRESS.Value == "0.0.0.0" { - plugin.LogInfo("Starting TCP Server (port " + strconv.Itoa(*parameters.TCP_PORT.Value) + ") ...") + plugin.LogInfo("Starting TCP Server (port " + strconv.Itoa(*parameters.PORT.Value) + ") ...") } else { - plugin.LogInfo("Starting TCP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.TCP_PORT.Value) + ") ...") + plugin.LogInfo("Starting TCP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.PORT.Value) + ") ...") } - server.Listen(*parameters.TCP_PORT.Value) + server.Listen(*parameters.PORT.Value) }) } diff --git a/plugins/autopeering/server/tcp/types.go b/plugins/autopeering/server/tcp/types.go index b8dca9d4e01aa82c3e09fac8b3bfcf76ba930e47..871d557230537829e3e3a1a5afb63b74bb9d05e7 100644 --- a/plugins/autopeering/server/tcp/types.go +++ b/plugins/autopeering/server/tcp/types.go @@ -1,9 +1,9 @@ package tcp import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "net" ) diff --git a/plugins/autopeering/server/udp/events.go b/plugins/autopeering/server/udp/events.go index 588e85b898a46a7dbe028f77c0d55a88033efb23..68f8d4131190fc9cba28662e3bf92275f26a0827 100644 --- a/plugins/autopeering/server/udp/events.go +++ b/plugins/autopeering/server/udp/events.go @@ -1,9 +1,9 @@ package udp import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "net" "reflect" ) diff --git a/plugins/autopeering/server/udp/server.go b/plugins/autopeering/server/udp/server.go index 46fc6e8f892af7f15ef97e6dfcbfe1c0a4e4d3c8..704b1150eeb93c58a15ae22fc0a533a8d578d62d 100644 --- a/plugins/autopeering/server/udp/server.go +++ b/plugins/autopeering/server/udp/server.go @@ -5,9 +5,9 @@ import ( "github.com/iotaledger/goshimmer/packages/network/udp" "github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "github.com/pkg/errors" "math" "net" @@ -18,7 +18,7 @@ var udpServer = udp.NewServer(int(math.Max(float64(request.MARSHALLED_TOTAL_SIZE func ConfigureServer(plugin *node.Plugin) { Events.Error.Attach(func(ip net.IP, err error) { - plugin.LogFailure("u" + err.Error()) + plugin.LogFailure(err.Error()) }) udpServer.Events.ReceiveData.Attach(processReceivedData) @@ -27,9 +27,9 @@ func ConfigureServer(plugin *node.Plugin) { }) udpServer.Events.Start.Attach(func() { if *parameters.ADDRESS.Value == "0.0.0.0" { - plugin.LogSuccess("Starting UDP Server (port " + strconv.Itoa(*parameters.UDP_PORT.Value) + ") ... done") + plugin.LogSuccess("Starting UDP Server (port " + strconv.Itoa(*parameters.PORT.Value) + ") ... done") } else { - plugin.LogSuccess("Starting UDP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.UDP_PORT.Value) + ") ... done") + plugin.LogSuccess("Starting UDP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.PORT.Value) + ") ... done") } }) udpServer.Events.Shutdown.Attach(func() { @@ -40,12 +40,12 @@ func ConfigureServer(plugin *node.Plugin) { func RunServer(plugin *node.Plugin) { daemon.BackgroundWorker(func() { if *parameters.ADDRESS.Value == "0.0.0.0" { - plugin.LogInfo("Starting UDP Server (port " + strconv.Itoa(*parameters.UDP_PORT.Value) + ") ...") + plugin.LogInfo("Starting UDP Server (port " + strconv.Itoa(*parameters.PORT.Value) + ") ...") } else { - plugin.LogInfo("Starting UDP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.UDP_PORT.Value) + ") ...") + plugin.LogInfo("Starting UDP Server (" + *parameters.ADDRESS.Value + ":" + strconv.Itoa(*parameters.PORT.Value) + ") ...") } - udpServer.Listen(*parameters.ADDRESS.Value, *parameters.UDP_PORT.Value) + udpServer.Listen(*parameters.ADDRESS.Value, *parameters.PORT.Value) }) } diff --git a/plugins/autopeering/server/udp/types.go b/plugins/autopeering/server/udp/types.go index c3d055d0ae28c19229d038541f8768e3d241ae15..985e1d02da2a04aac232b50cf2eb224b692fb4d2 100644 --- a/plugins/autopeering/server/udp/types.go +++ b/plugins/autopeering/server/udp/types.go @@ -1,9 +1,9 @@ package udp import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" "net" ) diff --git a/plugins/autopeering/protocol/peer/constants.go b/plugins/autopeering/types/peer/constants.go similarity index 66% rename from plugins/autopeering/protocol/peer/constants.go rename to plugins/autopeering/types/peer/constants.go index 53d53dd5245f7a588ee4bb43894ed89f91126531..aa1567093ab5d9c4e243164596a454f1c4db45ef 100644 --- a/plugins/autopeering/protocol/peer/constants.go +++ b/plugins/autopeering/types/peer/constants.go @@ -2,34 +2,28 @@ package peer import ( "github.com/iotaledger/goshimmer/packages/identity" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" ) const ( MARSHALLED_PUBLIC_KEY_START = 0 MARSHALLED_ADDRESS_TYPE_START = MARSHALLED_PUBLIC_KEY_END MARSHALLED_ADDRESS_START = MARSHALLED_ADDRESS_TYPE_END - MARSHALLED_PEERING_PROTOCOL_TYPE_START = MARSHALLED_ADDRESS_END - MARSHALLED_PEERING_PORT_START = MARSHALLED_PEERING_PROTOCOL_TYPE_END - MARSHALLED_GOSSIP_PROTOCOL_TYPE_START = MARSHALLED_PEERING_PORT_END - MARSHALLED_GOSSIP_PORT_START = MARSHALLED_GOSSIP_PROTOCOL_TYPE_END + MARSHALLED_PEERING_PORT_START = MARSHALLED_ADDRESS_END + MARSHALLED_GOSSIP_PORT_START = MARSHALLED_PEERING_PORT_END MARSHALLED_SALT_START = MARSHALLED_GOSSIP_PORT_END MARSHALLED_PUBLIC_KEY_END = MARSHALLED_PUBLIC_KEY_START + MARSHALLED_PUBLIC_KEY_SIZE MARSHALLED_ADDRESS_TYPE_END = MARSHALLED_ADDRESS_TYPE_START + MARSHALLED_ADDRESS_TYPE_SIZE MARSHALLED_ADDRESS_END = MARSHALLED_ADDRESS_START + MARSHALLED_ADDRESS_SIZE - MARSHALLED_PEERING_PROTOCOL_TYPE_END = MARSHALLED_PEERING_PROTOCOL_TYPE_START + MARSHALLED_PEERING_PROTOCOL_TYPE_SIZE MARSHALLED_PEERING_PORT_END = MARSHALLED_PEERING_PORT_START + MARSHALLED_PEERING_PORT_SIZE - MARSHALLED_GOSSIP_PROTOCOL_TYPE_END = MARSHALLED_GOSSIP_PROTOCOL_TYPE_START + MARSHALLED_GOSSIP_PROTOCOL_TYPE_SIZE MARSHALLED_GOSSIP_PORT_END = MARSHALLED_GOSSIP_PORT_START + MARSHALLED_GOSSIP_PORT_SIZE MARSHALLED_SALT_END = MARSHALLED_SALT_START + MARSHALLED_SALT_SIZE MARSHALLED_PUBLIC_KEY_SIZE = identity.PUBLIC_KEY_BYTE_LENGTH MARSHALLED_ADDRESS_TYPE_SIZE = 1 MARSHALLED_ADDRESS_SIZE = 16 - MARSHALLED_PEERING_PROTOCOL_TYPE_SIZE = 1 MARSHALLED_PEERING_PORT_SIZE = 2 - MARSHALLED_GOSSIP_PROTOCOL_TYPE_SIZE = 1 MARSHALLED_GOSSIP_PORT_SIZE = 2 MARSHALLED_SALT_SIZE = salt.SALT_MARSHALLED_SIZE MARSHALLED_TOTAL_SIZE = MARSHALLED_SALT_END diff --git a/plugins/autopeering/protocol/peer/peer.go b/plugins/autopeering/types/peer/peer.go similarity index 67% rename from plugins/autopeering/protocol/peer/peer.go rename to plugins/autopeering/types/peer/peer.go index 8018281fac539da5cd08de3481e8181273ce157a..4604559a0c537a2705834dee44585b16ae7ab94e 100644 --- a/plugins/autopeering/protocol/peer/peer.go +++ b/plugins/autopeering/types/peer/peer.go @@ -4,7 +4,7 @@ import ( "encoding/binary" "github.com/iotaledger/goshimmer/packages/identity" "github.com/iotaledger/goshimmer/packages/network" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/salt" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" "github.com/pkg/errors" "net" @@ -15,9 +15,7 @@ import ( type Peer struct { Identity *identity.Identity Address net.IP - PeeringProtocolType types.ProtocolType PeeringPort uint16 - GossipProtocolType types.ProtocolType GossipPort uint16 Salt *salt.Salt Conn *network.ManagedConnection @@ -40,10 +38,7 @@ func Unmarshal(data []byte) (*Peer, error) { peer.Address = net.IP(data[MARSHALLED_ADDRESS_START:MARSHALLED_ADDRESS_END]).To16() } - peer.PeeringProtocolType = types.ProtocolType(data[MARSHALLED_PEERING_PROTOCOL_TYPE_START]) peer.PeeringPort = binary.BigEndian.Uint16(data[MARSHALLED_PEERING_PORT_START:MARSHALLED_PEERING_PORT_END]) - - peer.GossipProtocolType = types.ProtocolType(data[MARSHALLED_GOSSIP_PROTOCOL_TYPE_START]) peer.GossipPort = binary.BigEndian.Uint16(data[MARSHALLED_GOSSIP_PORT_START:MARSHALLED_GOSSIP_PORT_END]) if unmarshalledSalt, err := salt.Unmarshal(data[MARSHALLED_SALT_START:MARSHALLED_SALT_END]); err != nil { @@ -55,42 +50,33 @@ func Unmarshal(data []byte) (*Peer, error) { return peer, nil } -func (peer *Peer) Send(data []byte, keepConnectionAlive bool) error { - newConnection, err := peer.Connect() +// sends data and +func (peer *Peer) Send(data []byte, protocol types.ProtocolType, responseExpected bool) (bool, error) { + conn, dialed, err := peer.Connect(protocol) if err != nil { - return err + return false, err } - if _, err := peer.Conn.Write(data); err != nil { - return err + if _, err := conn.Write(data); err != nil { + return false, err } - if newConnection && !keepConnectionAlive { - peer.Conn.Close() + if dialed && !responseExpected { + conn.Close() } - return nil + return dialed, nil } -func (peer *Peer) Connect() (bool, error) { +func (peer *Peer) ConnectTCP() (*network.ManagedConnection, bool, error) { if peer.Conn == nil { peer.connectMutex.Lock() defer peer.connectMutex.Unlock() if peer.Conn == nil { - var protocolString string - switch peer.PeeringProtocolType { - case types.PROTOCOL_TYPE_TCP: - protocolString = "tcp" - case types.PROTOCOL_TYPE_UDP: - protocolString = "udp" - default: - return false, errors.New("unsupported peering protocol in peer " + peer.Address.String()) - } - - conn, err := net.Dial(protocolString, peer.Address.String()+":"+strconv.Itoa(int(peer.PeeringPort))) + conn, err := net.Dial("tcp", peer.Address.String() + ":" + strconv.Itoa(int(peer.PeeringPort))) if err != nil { - return false, errors.New("error when connecting to " + peer.Address.String() + " during peering process: " + err.Error()) + return nil, false, errors.New("error when connecting to " + peer.String() + ": " + err.Error()) } else { peer.Conn = network.NewManagedConnection(conn) @@ -98,12 +84,32 @@ func (peer *Peer) Connect() (bool, error) { peer.Conn = nil }) - return true, nil + return peer.Conn, true, nil } } } - return false, nil + return peer.Conn, false, nil +} + +func (peer *Peer) ConnectUDP() (*network.ManagedConnection, bool, error) { + conn, err := net.Dial("udp", peer.Address.String() + ":" + strconv.Itoa(int(peer.PeeringPort))) + if err != nil { + return nil, false, errors.New("error when connecting to " + peer.Address.String() + ": " + err.Error()) + } + + return network.NewManagedConnection(conn), true, nil +} + +func (peer *Peer) Connect(protocol types.ProtocolType) (*network.ManagedConnection, bool, error) { + switch protocol { + case types.PROTOCOL_TYPE_TCP: + return peer.ConnectTCP() + case types.PROTOCOL_TYPE_UDP: + return peer.ConnectUDP() + default: + return nil, false, errors.New("unsupported peering protocol in peer " + peer.Address.String()) + } } func (peer *Peer) Marshal() []byte { @@ -123,10 +129,7 @@ func (peer *Peer) Marshal() []byte { copy(result[MARSHALLED_ADDRESS_START:MARSHALLED_ADDRESS_END], peer.Address.To16()) - result[MARSHALLED_PEERING_PROTOCOL_TYPE_START] = peer.PeeringProtocolType binary.BigEndian.PutUint16(result[MARSHALLED_PEERING_PORT_START:MARSHALLED_PEERING_PORT_END], peer.PeeringPort) - - result[MARSHALLED_GOSSIP_PROTOCOL_TYPE_START] = peer.GossipProtocolType binary.BigEndian.PutUint16(result[MARSHALLED_GOSSIP_PORT_START:MARSHALLED_GOSSIP_PORT_END], peer.GossipPort) copy(result[MARSHALLED_SALT_START:MARSHALLED_SALT_END], peer.Salt.Marshal()) diff --git a/plugins/autopeering/peermanager/types/peer_list.go b/plugins/autopeering/types/peerlist/peer_list.go similarity index 87% rename from plugins/autopeering/peermanager/types/peer_list.go rename to plugins/autopeering/types/peerlist/peer_list.go index 3bcc3f86e8cb62370cbd75e8d3b73030083ebde6..f132e5f0dde664b491e28dcc3408249224a69827 100644 --- a/plugins/autopeering/peermanager/types/peer_list.go +++ b/plugins/autopeering/types/peerlist/peer_list.go @@ -1,7 +1,7 @@ -package types +package peerlist import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" "sort" ) diff --git a/plugins/autopeering/peermanager/types/peer_register.go b/plugins/autopeering/types/peerregister/peer_register.go similarity index 78% rename from plugins/autopeering/peermanager/types/peer_register.go rename to plugins/autopeering/types/peerregister/peer_register.go index c4cbbb3fac0f97e8cb5cf374539e990d9dc0e870..96c27f7366590697e9a4f5dd969c9d54e994294c 100644 --- a/plugins/autopeering/peermanager/types/peer_register.go +++ b/plugins/autopeering/types/peerregister/peer_register.go @@ -1,10 +1,11 @@ -package types +package peerregister import ( "bytes" "github.com/iotaledger/goshimmer/packages/accountability" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/request" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist" ) type PeerRegister map[string]*peer.Peer @@ -44,8 +45,8 @@ func (this PeerRegister) Filter(filterFn func(this PeerRegister, req *request.Re return filterFn(this, req) } -func (this PeerRegister) List() PeerList { - peerList := make(PeerList, len(this)) +func (this PeerRegister) List() peerlist.PeerList { + peerList := make(peerlist.PeerList, len(this)) counter := 0 for _, currentPeer := range this { diff --git a/plugins/autopeering/protocol/ping/constants.go b/plugins/autopeering/types/ping/constants.go similarity index 93% rename from plugins/autopeering/protocol/ping/constants.go rename to plugins/autopeering/types/ping/constants.go index aca4a719836d8b7f5f68b0a1f7ff2a383b066a73..f43adf9557ff61a6d8147986f1b7a1afa5dbcb97 100644 --- a/plugins/autopeering/protocol/ping/constants.go +++ b/plugins/autopeering/types/ping/constants.go @@ -2,7 +2,7 @@ package ping import ( "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" ) const ( diff --git a/plugins/autopeering/protocol/ping/errors.go b/plugins/autopeering/types/ping/errors.go similarity index 100% rename from plugins/autopeering/protocol/ping/errors.go rename to plugins/autopeering/types/ping/errors.go diff --git a/plugins/autopeering/protocol/ping/ping.go b/plugins/autopeering/types/ping/ping.go similarity index 92% rename from plugins/autopeering/protocol/ping/ping.go rename to plugins/autopeering/types/ping/ping.go index bdd6b745f8ceabe2a70365d1287d0dd471c7703b..eda383ecedc138eec9cfded85ea8402be7cc086f 100644 --- a/plugins/autopeering/protocol/ping/ping.go +++ b/plugins/autopeering/types/ping/ping.go @@ -3,14 +3,13 @@ package ping import ( "bytes" "github.com/iotaledger/goshimmer/packages/identity" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" ) type Ping struct { Issuer *peer.Peer - Neighbors types.PeerList + Neighbors []*peer.Peer Signature [MARSHALLED_SIGNATURE_SIZE]byte } diff --git a/plugins/autopeering/protocol/request/constants.go b/plugins/autopeering/types/request/constants.go similarity index 87% rename from plugins/autopeering/protocol/request/constants.go rename to plugins/autopeering/types/request/constants.go index acd24072bc7d13e2eca0441fc4e4cddf617536e9..b08423729828044b743e0794b2bd084f595830d5 100644 --- a/plugins/autopeering/protocol/request/constants.go +++ b/plugins/autopeering/types/request/constants.go @@ -1,7 +1,7 @@ package request import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" ) const ( diff --git a/plugins/autopeering/protocol/request/errors.go b/plugins/autopeering/types/request/errors.go similarity index 100% rename from plugins/autopeering/protocol/request/errors.go rename to plugins/autopeering/types/request/errors.go diff --git a/plugins/autopeering/protocol/request/request.go b/plugins/autopeering/types/request/request.go similarity index 86% rename from plugins/autopeering/protocol/request/request.go rename to plugins/autopeering/types/request/request.go index 60eae5c9c8837fd0379132bd0ff6fc7a4efdfaeb..79de558b9d2e8af37d81d1568c914b5a5d174a8e 100644 --- a/plugins/autopeering/protocol/request/request.go +++ b/plugins/autopeering/types/request/request.go @@ -3,8 +3,10 @@ package request import ( "bytes" "github.com/iotaledger/goshimmer/packages/identity" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/response" + "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" "time" ) @@ -48,18 +50,14 @@ func Unmarshal(data []byte) (*Request, error) { } func (this *Request) Accept(peers []*peer.Peer) error { - if _, err := this.Issuer.Connect(); err != nil { - return err - } - peeringResponse := &response.Response{ Type: response.TYPE_ACCEPT, - Issuer: OUTGOING_REQUEST.Issuer, + Issuer: ownpeer.INSTANCE, Peers: peers, } peeringResponse.Sign() - if err := this.Issuer.Send(peeringResponse.Marshal(), false); err != nil { + if _, err := this.Issuer.Send(peeringResponse.Marshal(), types.PROTOCOL_TYPE_TCP, false); err != nil { return err } diff --git a/plugins/autopeering/protocol/response/constants.go b/plugins/autopeering/types/response/constants.go similarity index 95% rename from plugins/autopeering/protocol/response/constants.go rename to plugins/autopeering/types/response/constants.go index 250e5c98e45d495c0beaf7a3a2d849b440fa9e9b..86f65ef94b71838d606bd6a0afc8b5d33beb86b2 100644 --- a/plugins/autopeering/protocol/response/constants.go +++ b/plugins/autopeering/types/response/constants.go @@ -2,7 +2,7 @@ package response import ( "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" ) const ( diff --git a/plugins/autopeering/protocol/response/errors.go b/plugins/autopeering/types/response/errors.go similarity index 100% rename from plugins/autopeering/protocol/response/errors.go rename to plugins/autopeering/types/response/errors.go diff --git a/plugins/autopeering/protocol/response/response.go b/plugins/autopeering/types/response/response.go similarity index 97% rename from plugins/autopeering/protocol/response/response.go rename to plugins/autopeering/types/response/response.go index c91fc91d0f23d7d95b248d98b4aabf107da8b6e8..d4ece51ad714e063b7226a4ee6d53086ed11dc00 100644 --- a/plugins/autopeering/protocol/response/response.go +++ b/plugins/autopeering/types/response/response.go @@ -3,7 +3,7 @@ package response import ( "bytes" "github.com/iotaledger/goshimmer/packages/identity" - "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer" "github.com/pkg/errors" ) diff --git a/plugins/autopeering/protocol/response/types.go b/plugins/autopeering/types/response/types.go similarity index 100% rename from plugins/autopeering/protocol/response/types.go rename to plugins/autopeering/types/response/types.go diff --git a/plugins/autopeering/protocol/salt/constants.go b/plugins/autopeering/types/salt/constants.go similarity index 100% rename from plugins/autopeering/protocol/salt/constants.go rename to plugins/autopeering/types/salt/constants.go diff --git a/plugins/autopeering/protocol/salt/salt.go b/plugins/autopeering/types/salt/salt.go similarity index 100% rename from plugins/autopeering/protocol/salt/salt.go rename to plugins/autopeering/types/salt/salt.go diff --git a/plugins/gossip/neighbormanager/accepted_neighbors.go b/plugins/gossip/neighbormanager/accepted_neighbors.go index 10f4ca957be93c07c9f5fc8f7fd855dc83c5db15..46c34007adc28a6b24220e18ae197267b201e126 100644 --- a/plugins/gossip/neighbormanager/accepted_neighbors.go +++ b/plugins/gossip/neighbormanager/accepted_neighbors.go @@ -1,7 +1,7 @@ package neighbormanager import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister" ) -var ACCEPTED_NEIGHBORS = make(types.PeerRegister) +var ACCEPTED_NEIGHBORS = make(peerregister.PeerRegister) diff --git a/plugins/gossip/neighbormanager/chosen_neighbors.go b/plugins/gossip/neighbormanager/chosen_neighbors.go index 5e68468faa2adae1d056310b3b6b5d37c3025151..974693a552721c6640b77ad20b341b09c5db926d 100644 --- a/plugins/gossip/neighbormanager/chosen_neighbors.go +++ b/plugins/gossip/neighbormanager/chosen_neighbors.go @@ -1,7 +1,7 @@ package neighbormanager import ( - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" + "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister" ) -var CHOSEN_NEIGHBORS = make(types.PeerRegister) +var CHOSEN_NEIGHBORS = make(peerregister.PeerRegister) diff --git a/plugins/statusscreen/ui_header_bar.go b/plugins/statusscreen/ui_header_bar.go index 118ad837cb1c3a26b247bc777cc157420a012704..2b4ac69c9333ec9f36915699f45d073fe336a441 100644 --- a/plugins/statusscreen/ui_header_bar.go +++ b/plugins/statusscreen/ui_header_bar.go @@ -4,7 +4,8 @@ import ( "fmt" "github.com/gdamore/tcell" "github.com/iotaledger/goshimmer/packages/accountability" - "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "github.com/rivo/tview" "math" @@ -67,7 +68,7 @@ func (headerBar *UIHeaderBar) Update() { fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintf(headerBar.InfoContainer, "[::b]Neighbors: [::d]%40v ", strconv.Itoa(len(neighbormanager.CHOSEN_NEIGHBORS)) + " chosen / " + strconv.Itoa(len(neighbormanager.ACCEPTED_NEIGHBORS)) + " accepted") fmt.Fprintln(headerBar.InfoContainer) - fmt.Fprintf(headerBar.InfoContainer, "[::b]Known Peers: [::d]%40v ", strconv.Itoa(len(peermanager.KNOWN_PEERS)) + " total / " + strconv.Itoa(len(peermanager.NEIGHBORHOOD)) + " neighborhood") + fmt.Fprintf(headerBar.InfoContainer, "[::b]Known Peers: [::d]%40v ", strconv.Itoa(len(knownpeers.INSTANCE)) + " total / " + strconv.Itoa(len(neighborhood.INSTANCE)) + " neighborhood") fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintf(headerBar.InfoContainer, "[::b]Uptime: [::d]");