diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go index f95573c73ed68f2a1659393c549a3f4300090c99..aaf868830f17c8571fbaca82c170abbf7ceaf6af 100644 --- a/packages/transactionspammer/transactionspammer.go +++ b/packages/transactionspammer/transactionspammer.go @@ -4,10 +4,11 @@ import ( "sync" "time" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/goshimmer/plugins/tipselection" "github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/logger" @@ -62,7 +63,7 @@ func Start(tps uint) { continue } - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.INSTANCE.Peer}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.GetInstance().Peer}) if sentCounter >= tps { duration := time.Since(start) diff --git a/plugins/analysis/client/plugin.go b/plugins/analysis/client/plugin.go index 32bc5cea951f9a800850de2f4203de2aa2379ee4..8d9d58baa33975801c5f5891135b19e1e7b8e470 100644 --- a/plugins/analysis/client/plugin.go +++ b/plugins/analysis/client/plugin.go @@ -5,6 +5,8 @@ import ( "net" "time" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/iotaledger/autopeering-sim/discover" "github.com/iotaledger/autopeering-sim/selection" "github.com/iotaledger/goshimmer/packages/network" @@ -15,7 +17,6 @@ import ( "github.com/iotaledger/goshimmer/plugins/analysis/types/ping" "github.com/iotaledger/goshimmer/plugins/analysis/types/removenode" "github.com/iotaledger/goshimmer/plugins/autopeering" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/logger" @@ -71,8 +72,8 @@ func getEventDispatchers(conn *network.ManagedConnection) *EventDispatchers { } func reportCurrentStatus(eventDispatchers *EventDispatchers) { - if local.INSTANCE != nil { - eventDispatchers.AddNode(local.INSTANCE.ID().Bytes()) + if local.GetInstance() != nil { + eventDispatchers.AddNode(local.GetInstance().ID().Bytes()) } reportChosenNeighbors(eventDispatchers) @@ -92,18 +93,18 @@ func setupHooks(plugin *node.Plugin, conn *network.ManagedConnection, eventDispa }) onAddAcceptedNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) { - log.Info("onAddAcceptedNeighbor: " + hex.EncodeToString(ev.Peer.ID().Bytes()) + " - " + hex.EncodeToString(local.INSTANCE.ID().Bytes())) - eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), local.INSTANCE.ID().Bytes()) + log.Info("onAddAcceptedNeighbor: " + hex.EncodeToString(ev.Peer.ID().Bytes()) + " - " + hex.EncodeToString(local.GetInstance().ID().Bytes())) + eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), local.GetInstance().ID().Bytes()) }) onRemoveNeighbor := events.NewClosure(func(ev *selection.DroppedEvent) { - log.Info("onRemoveNeighbor: " + hex.EncodeToString(ev.DroppedID.Bytes()) + " - " + hex.EncodeToString(local.INSTANCE.ID().Bytes())) - eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), local.INSTANCE.ID().Bytes()) + log.Info("onRemoveNeighbor: " + hex.EncodeToString(ev.DroppedID.Bytes()) + " - " + hex.EncodeToString(local.GetInstance().ID().Bytes())) + eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), local.GetInstance().ID().Bytes()) }) onAddChosenNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) { - log.Info("onAddChosenNeighbor: " + hex.EncodeToString(local.INSTANCE.ID().Bytes()) + " - " + hex.EncodeToString(ev.Peer.ID().Bytes())) - eventDispatchers.ConnectNodes(local.INSTANCE.ID().Bytes(), ev.Peer.ID().Bytes()) + log.Info("onAddChosenNeighbor: " + hex.EncodeToString(local.GetInstance().ID().Bytes()) + " - " + hex.EncodeToString(ev.Peer.ID().Bytes())) + eventDispatchers.ConnectNodes(local.GetInstance().ID().Bytes(), ev.Peer.ID().Bytes()) }) // setup hooks ///////////////////////////////////////////////////////////////////////////////////////////////////// @@ -132,7 +133,7 @@ func reportChosenNeighbors(dispatchers *EventDispatchers) { if autopeering.Selection != nil { for _, chosenNeighbor := range autopeering.Selection.GetOutgoingNeighbors() { dispatchers.AddNode(chosenNeighbor.ID().Bytes()) - dispatchers.ConnectNodes(local.INSTANCE.ID().Bytes(), chosenNeighbor.ID().Bytes()) + dispatchers.ConnectNodes(local.GetInstance().ID().Bytes(), chosenNeighbor.ID().Bytes()) } } } diff --git a/plugins/autopeering/autopeering.go b/plugins/autopeering/autopeering.go index a7759994b54a07a499ef123eee6766cc9ab534a6..6875acbec521332ec7f3d85d314ef7065f93d6ca 100644 --- a/plugins/autopeering/autopeering.go +++ b/plugins/autopeering/autopeering.go @@ -3,12 +3,11 @@ package autopeering import ( "encoding/base64" "fmt" - "io/ioutil" "net" - "net/http" - "strconv" "strings" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/iotaledger/autopeering-sim/discover" "github.com/iotaledger/autopeering-sim/logger" "github.com/iotaledger/autopeering-sim/peer" @@ -16,8 +15,6 @@ import ( "github.com/iotaledger/autopeering-sim/selection" "github.com/iotaledger/autopeering-sim/server" "github.com/iotaledger/autopeering-sim/transport" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" - "github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/parameter" "github.com/pkg/errors" @@ -53,40 +50,6 @@ const defaultZLC = `{ var zLogger = logger.NewLogger(defaultZLC, logLevel) -func configureLocal() { - ip := net.ParseIP(parameter.NodeConfig.GetString(CFG_ADDRESS)) - if ip == nil { - log.Fatalf("Invalid IP address: %s", parameter.NodeConfig.GetString(CFG_ADDRESS)) - } - if ip.IsUnspecified() { - myIp, err := getMyIP() - if err != nil { - log.Fatalf("Could not query public IP: %v", err) - } - ip = myIp - } - - apPort := strconv.Itoa(parameter.NodeConfig.GetInt(CFG_PORT)) - gossipPort := strconv.Itoa(parameter.NodeConfig.GetInt(gossip.GOSSIP_PORT)) - - // create a new local node - db := peer.NewPersistentDB(zLogger.Named("db")) - - var err error - local.INSTANCE, err = peer.NewLocal(NETWORK, net.JoinHostPort(ip.String(), apPort), db) - if err != nil { - log.Fatalf("NewLocal: %v", err) - } - - // add a service for the gossip - if parameter.NodeConfig.GetBool(CFG_SELECTION) { - err = local.INSTANCE.UpdateService(service.GossipKey, "tcp", net.JoinHostPort(ip.String(), gossipPort)) - if err != nil { - log.Fatalf("UpdateService: %v", err) - } - } -} - func configureAP() { masterPeers, err := parseEntryNodes() if err != nil { @@ -94,13 +57,13 @@ func configureAP() { } log.Debugf("Master peers: %v", masterPeers) - Discovery = discover.New(local.INSTANCE, discover.Config{ + Discovery = discover.New(local.GetInstance(), discover.Config{ Log: zLogger.Named("disc"), MasterPeers: masterPeers, }) if parameter.NodeConfig.GetBool(CFG_SELECTION) { - Selection = selection.New(local.INSTANCE, Discovery, selection.Config{ + Selection = selection.New(local.GetInstance(), Discovery, selection.Config{ Log: zLogger.Named("sel"), Param: &selection.Parameters{ SaltLifetime: selection.DefaultSaltLifetime, @@ -113,7 +76,7 @@ func configureAP() { func start() { defer log.Info("Stopping Auto Peering server ... done") - addr := local.INSTANCE.Services().Get(service.PeeringKey) + addr := local.GetInstance().Services().Get(service.PeeringKey) udpAddr, err := net.ResolveUDPAddr(addr.Network(), addr.String()) if err != nil { log.Fatalf("ResolveUDPAddr: %v", err) @@ -143,7 +106,7 @@ func start() { } // start a server doing discovery and peering - srv := server.Listen(local.INSTANCE, trans, zLogger.Named("srv"), handlers...) + srv := server.Listen(local.GetInstance(), trans, zLogger.Named("srv"), handlers...) defer srv.Close() // start the discovery on that connection @@ -156,7 +119,7 @@ func start() { defer Selection.Close() } - log.Infof("Auto Peering server started: ID=%x, address=%s", local.INSTANCE.ID(), srv.LocalAddr()) + log.Infof("Auto Peering server started: ID=%x, address=%s", local.GetInstance().ID(), srv.LocalAddr()) <-daemon.ShutdownSignal log.Info("Stopping Auto Peering server ...") @@ -185,25 +148,3 @@ func parseEntryNodes() (result []*peer.Peer, err error) { return result, nil } - -func getMyIP() (net.IP, error) { - url := "https://api.ipify.org?format=text" - resp, err := http.Get(url) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - // the body only consists of the ip address - ip := net.ParseIP(string(body)) - if ip == nil { - return nil, fmt.Errorf("not an IP: %s", body) - } - - return ip, nil -} diff --git a/plugins/autopeering/local/local.go b/plugins/autopeering/local/local.go index d384c80735859ecb415a1f15f5cd11552d1dcaa9..095648e5708e40af8546de79a4df48d2c2ddc100 100644 --- a/plugins/autopeering/local/local.go +++ b/plugins/autopeering/local/local.go @@ -1,5 +1,77 @@ package local -import "github.com/iotaledger/autopeering-sim/peer" +import ( + "fmt" + "io/ioutil" + "log" + "net" + "net/http" + "strconv" + "sync" -var INSTANCE *peer.Local + "github.com/iotaledger/autopeering-sim/peer" + "github.com/iotaledger/hive.go/parameter" + "go.uber.org/zap" +) + +var ( + instance *peer.Local + once sync.Once +) + +func configureLocal() *peer.Local { + ip := net.ParseIP(parameter.NodeConfig.GetString(CFG_ADDRESS)) + if ip == nil { + log.Fatalf("Invalid IP address: %s", parameter.NodeConfig.GetString(CFG_ADDRESS)) + } + if ip.IsUnspecified() { + myIp, err := getMyIP() + if err != nil { + log.Fatalf("Could not query public IP: %v", err) + } + ip = myIp + } + + port := strconv.Itoa(parameter.NodeConfig.GetInt(CFG_PORT)) + + // create a new local node + logger, err := zap.NewProduction() + if err != nil { + log.Fatalf("Could not create logger: %v", err) + } + db := peer.NewPersistentDB(logger.Named("db").Sugar()) + + local, err := peer.NewLocal("udp", net.JoinHostPort(ip.String(), port), db) + if err != nil { + log.Fatalf("NewLocal: %v", err) + } + + return local +} + +func getMyIP() (net.IP, error) { + url := "https://api.ipify.org?format=text" + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + // the body only consists of the ip address + ip := net.ParseIP(string(body)) + if ip == nil { + return nil, fmt.Errorf("not an IP: %s", body) + } + + return ip, nil +} + +func GetInstance() *peer.Local { + once.Do(func() { instance = configureLocal() }) + return instance +} diff --git a/plugins/autopeering/local/parameters.go b/plugins/autopeering/local/parameters.go new file mode 100644 index 0000000000000000000000000000000000000000..1bb7847d5b2c100fc5e0fe1032b8acbe6ae19c87 --- /dev/null +++ b/plugins/autopeering/local/parameters.go @@ -0,0 +1,15 @@ +package local + +import ( + flag "github.com/spf13/pflag" +) + +const ( + CFG_ADDRESS = "autopeering.address" + CFG_PORT = "autopeering.port" +) + +func init() { + flag.String(CFG_ADDRESS, "0.0.0.0", "address to bind for incoming peering requests") + flag.Int(CFG_PORT, 14626, "udp port for incoming peering requests") +} diff --git a/plugins/autopeering/parameters.go b/plugins/autopeering/parameters.go index 27b117006e402cc47d0d92f8c5ca8e3bedd97edf..0c2454272e4a8e1a6b120009ae364389275c454e 100644 --- a/plugins/autopeering/parameters.go +++ b/plugins/autopeering/parameters.go @@ -5,15 +5,11 @@ import ( ) const ( - CFG_ADDRESS = "autopeering.address" CFG_ENTRY_NODES = "autopeering.entryNodes" - CFG_PORT = "autopeering.port" CFG_SELECTION = "autopeering.selection" ) func init() { - flag.String(CFG_ADDRESS, "0.0.0.0", "address to bind for incoming peering requests") flag.StringSlice(CFG_ENTRY_NODES, []string{"V8LYtWWcPYYDTTXLeIEFjJEuWlsjDiI0+Pq/Cx9ai6g=@116.202.49.178:14626"}, "list of trusted entry nodes for auto peering") - flag.Int(CFG_PORT, 14626, "udp port for incoming peering requests") flag.Bool(CFG_SELECTION, true, "enable peer selection") } diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go index 3a71eae9c0fe46d823c23dc8de2140cb62e40e53..1deaa3684bc763aaef1bd8ff2cd0bb1bb84ff74b 100644 --- a/plugins/autopeering/plugin.go +++ b/plugins/autopeering/plugin.go @@ -10,9 +10,6 @@ import ( ) const ( - // NETWORK defines the network type used for the autopeering. - NETWORK = "udp" - name = "Autopeering" // name of the plugin logLevel = "info" ) @@ -23,7 +20,6 @@ var log = logger.NewLogger(name) func configure(*node.Plugin) { configureEvents() - configureLocal() configureAP() } diff --git a/plugins/dashboard/tps.go b/plugins/dashboard/tps.go index 6f6710bdc1dc57dd95002ec0741c411c4c40efad..656998c2bfadc0c0c9a39f1af4937bc820ab04d8 100644 --- a/plugins/dashboard/tps.go +++ b/plugins/dashboard/tps.go @@ -10,9 +10,10 @@ import ( "sync" "time" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/gorilla/websocket" "github.com/iotaledger/goshimmer/plugins/autopeering" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/goshimmer/plugins/metrics" "github.com/iotaledger/hive.go/events" ) @@ -83,7 +84,7 @@ func GetStatus() *Status { } return &Status{ - Id: local.INSTANCE.ID().String(), + Id: local.GetInstance().ID().String(), Neighbor: "Neighbors: " + outgoing + " chosen / " + incoming + " accepted / " + neighbors + " total", KnownPeer: "Known Peers: " + knownPeers + " total", Uptime: uptime, diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go index 834a4edfb05629e91f6a79b9ff2c2c7d5d7b32c7..dae769cf05ce83c4935284f168ad7d086a8cd64e 100644 --- a/plugins/gossip/gossip.go +++ b/plugins/gossip/gossip.go @@ -2,8 +2,11 @@ package gossip import ( "fmt" + "net" + "strconv" "github.com/iotaledger/autopeering-sim/logger" + "github.com/iotaledger/autopeering-sim/peer/service" "github.com/iotaledger/goshimmer/packages/errors" gp "github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/goshimmer/packages/gossip/server" @@ -11,6 +14,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/goshimmer/plugins/tangle" "github.com/iotaledger/hive.go/daemon" + "github.com/iotaledger/hive.go/parameter" ) var ( @@ -41,13 +45,26 @@ const defaultZLC = `{ var zLogger = logger.NewLogger(defaultZLC, logLevel) func configureGossip() { - mgr = gp.NewManager(local.INSTANCE, getTransaction, zLogger) + lPeer := local.GetInstance() + + port := strconv.Itoa(parameter.NodeConfig.GetInt(GOSSIP_PORT)) + + host, _, err := net.SplitHostPort(lPeer.Address()) + if err != nil { + log.Fatalf("invalid peering address: %v", err) + } + err = lPeer.UpdateService(service.GossipKey, "tcp", net.JoinHostPort(host, port)) + if err != nil { + log.Fatalf("could not update services: %v", err) + } + + mgr = gp.NewManager(lPeer, getTransaction, zLogger) } func start() { defer log.Info("Stopping Gossip ... done") - srv, err := server.ListenTCP(local.INSTANCE, zLogger) + srv, err := server.ListenTCP(local.GetInstance(), zLogger) if err != nil { log.Fatalf("ListenTCP: %v", err) } diff --git a/plugins/statusscreen/ui_header_bar.go b/plugins/statusscreen/ui_header_bar.go index 4cff25dd17781c9b7af171d00678e4d73087773a..607be9d4e6b27ba033fb24c4b3a4f5163541d08c 100644 --- a/plugins/statusscreen/ui_header_bar.go +++ b/plugins/statusscreen/ui_header_bar.go @@ -5,12 +5,13 @@ import ( "math" "strconv" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + //"strconv" "time" "github.com/gdamore/tcell" "github.com/iotaledger/goshimmer/plugins/autopeering" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/rivo/tview" ) @@ -90,8 +91,8 @@ func (headerBar *UIHeaderBar) Update() { if autopeering.Discovery != nil { total = strconv.Itoa(len(autopeering.Discovery.GetVerifiedPeers())) } - if local.INSTANCE != nil { - myID = local.INSTANCE.ID().String() + if local.GetInstance() != nil { + myID = local.GetInstance().ID().String() } fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", myID) diff --git a/plugins/ui/nodeInfo.go b/plugins/ui/nodeInfo.go index 3c3f09c02f91387352f1cc742b1672a6897fc76d..7f3226236cb82129a2436d5b34dc289e280a804d 100644 --- a/plugins/ui/nodeInfo.go +++ b/plugins/ui/nodeInfo.go @@ -5,8 +5,9 @@ import ( "sync/atomic" "time" - "github.com/iotaledger/goshimmer/plugins/autopeering" "github.com/iotaledger/goshimmer/plugins/autopeering/local" + + "github.com/iotaledger/goshimmer/plugins/autopeering" ) var start = time.Now() @@ -61,7 +62,7 @@ func gatherInfo() nodeInfo { receivedTps, solidTps := updateTpsCounters() duration := time.Since(start) / time.Second info := nodeInfo{ - ID: local.INSTANCE.ID().String(), + ID: local.GetInstance().ID().String(), ChosenNeighbors: chosenNeighbors, AcceptedNeighbors: acceptedNeighbors, KnownPeersSize: knownPeers, diff --git a/plugins/webapi-send-data/plugin.go b/plugins/webapi-send-data/plugin.go index b36da53608427b3d301fc168fa593dd6bc43fdc8..778a41b27b1deae8f5d88c0e8b9a21dd3f64f9d9 100644 --- a/plugins/webapi-send-data/plugin.go +++ b/plugins/webapi-send-data/plugin.go @@ -4,10 +4,11 @@ import ( "net/http" "time" + "github.com/iotaledger/goshimmer/plugins/autopeering/local" + "github.com/iotaledger/goshimmer/packages/gossip" "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/model/value_transaction" - "github.com/iotaledger/goshimmer/plugins/autopeering/local" "github.com/iotaledger/goshimmer/plugins/tipselection" "github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/hive.go/logger" @@ -57,7 +58,7 @@ func SendDataHandler(c echo.Context) error { log.Warning("PoW failed", err) } - gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.INSTANCE.Peer}) + gossip.Events.TransactionReceived.Trigger(&gossip.TransactionReceivedEvent{Data: tx.GetBytes(), Peer: &local.GetInstance().Peer}) return requestSuccessful(c, tx.GetHash()) }