From 821e9fb6358111821bba3196048f02a65da5901b Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Thu, 5 Dec 2019 13:09:09 +0000
Subject: [PATCH] :construction: WIP

---
 packages/gossip/manager.go                    |  6 ++--
 packages/gossip/manager_test.go               |  4 +--
 packages/gossip/neighbor/neighbor.go          |  2 +-
 packages/gossip/proto/message.proto           |  2 +-
 packages/gossip/transport/handshake.go        |  2 +-
 .../gossip/transport/proto/handshake.proto    |  2 +-
 .../transactionspammer/transactionspammer.go  |  9 ++++--
 plugins/autopeering/plugin.go                 | 32 ++-----------------
 plugins/gossip/gossip.go                      | 18 ++++++++---
 plugins/metrics/plugin.go                     |  5 ++-
 plugins/statusscreen-tps/plugin.go            |  5 ++-
 plugins/tangle/solidifier.go                  |  8 +++--
 plugins/tangle/solidifier_test.go             | 28 ++++++++++++----
 plugins/ui/ui.go                              |  5 ++-
 14 files changed, 63 insertions(+), 65 deletions(-)

diff --git a/packages/gossip/manager.go b/packages/gossip/manager.go
index 6c889e36..681f6b07 100644
--- a/packages/gossip/manager.go
+++ b/packages/gossip/manager.go
@@ -3,11 +3,11 @@ package gossip
 import (
 	"net"
 
-	"github.com/capossele/gossip/neighbor"
-	pb "github.com/capossele/gossip/proto"
-	"github.com/capossele/gossip/transport"
 	"github.com/golang/protobuf/proto"
 	"github.com/iotaledger/autopeering-sim/peer"
+	"github.com/iotaledger/goshimmer/packages/gossip/neighbor"
+	pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
+	"github.com/iotaledger/goshimmer/packages/gossip/transport"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/pkg/errors"
 	"go.uber.org/zap"
diff --git a/packages/gossip/manager_test.go b/packages/gossip/manager_test.go
index e037c22d..51df26e9 100644
--- a/packages/gossip/manager_test.go
+++ b/packages/gossip/manager_test.go
@@ -6,11 +6,11 @@ import (
 	"testing"
 	"time"
 
-	pb "github.com/capossele/gossip/proto"
-	"github.com/capossele/gossip/transport"
 	"github.com/golang/protobuf/proto"
 	"github.com/iotaledger/autopeering-sim/peer"
 	"github.com/iotaledger/autopeering-sim/peer/service"
+	pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
+	"github.com/iotaledger/goshimmer/packages/gossip/transport"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
diff --git a/packages/gossip/neighbor/neighbor.go b/packages/gossip/neighbor/neighbor.go
index 41d2d25f..e3ec6bda 100644
--- a/packages/gossip/neighbor/neighbor.go
+++ b/packages/gossip/neighbor/neighbor.go
@@ -3,8 +3,8 @@ package neighbor
 import (
 	"sync"
 
-	"github.com/capossele/gossip/transport"
 	"github.com/iotaledger/autopeering-sim/peer"
+	"github.com/iotaledger/goshimmer/packages/gossip/transport"
 )
 
 // Neighbor defines a neighbor
diff --git a/packages/gossip/proto/message.proto b/packages/gossip/proto/message.proto
index b01b9368..6be94fdf 100644
--- a/packages/gossip/proto/message.proto
+++ b/packages/gossip/proto/message.proto
@@ -1,6 +1,6 @@
 syntax = "proto3";
 
-option go_package = "github.com/capossele/gossip/proto";
+option go_package = "github.com/iotaledger/goshimmer/packages/gossip/proto";
 
 package proto;
 
diff --git a/packages/gossip/transport/handshake.go b/packages/gossip/transport/handshake.go
index c6e253ff..ef3e8608 100644
--- a/packages/gossip/transport/handshake.go
+++ b/packages/gossip/transport/handshake.go
@@ -4,9 +4,9 @@ import (
 	"bytes"
 	"time"
 
-	pb "github.com/capossele/gossip/transport/proto"
 	"github.com/golang/protobuf/proto"
 	"github.com/iotaledger/autopeering-sim/server"
+	pb "github.com/iotaledger/goshimmer/packages/gossip/transport/proto"
 )
 
 const (
diff --git a/packages/gossip/transport/proto/handshake.proto b/packages/gossip/transport/proto/handshake.proto
index 2f9c6281..68537177 100644
--- a/packages/gossip/transport/proto/handshake.proto
+++ b/packages/gossip/transport/proto/handshake.proto
@@ -1,6 +1,6 @@
 syntax = "proto3";
 
-option go_package = "github.com/capossele/gossip/transport/proto";
+option go_package = "github.com/iotaledger/goshimmer/packages/gossip/transport/proto";
 
 package proto;
 
diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go
index ac1834f6..f37a2fc3 100644
--- a/packages/transactionspammer/transactionspammer.go
+++ b/packages/transactionspammer/transactionspammer.go
@@ -4,8 +4,9 @@ import (
 	"sync"
 	"time"
 
-	"github.com/iotaledger/goshimmer/plugins/gossip"
-
+	"github.com/golang/protobuf/proto"
+	"github.com/iotaledger/goshimmer/packages/gossip"
+	pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
 	"github.com/iotaledger/goshimmer/plugins/tipselection"
 	"github.com/iotaledger/hive.go/daemon"
@@ -50,7 +51,9 @@ func Start(tps uint) {
 							tx.SetBranchTransactionHash(tipselection.GetRandomTip())
 							tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
 
-							gossip.Events.ReceiveTransaction.Trigger(tx.MetaTransaction)
+							mtx := &pb.Transaction{Body: tx.MetaTransaction.GetBytes()}
+							b, _ := proto.Marshal(mtx)
+							gossip.Event.NewTransaction.Trigger(b)
 
 							if sentCounter >= tps {
 								duration := time.Since(start)
diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go
index 47fe0934..eefa8de3 100644
--- a/plugins/autopeering/plugin.go
+++ b/plugins/autopeering/plugin.go
@@ -1,12 +1,9 @@
 package autopeering
 
 import (
-	"net"
-
 	"github.com/iotaledger/autopeering-sim/discover"
-	"github.com/iotaledger/autopeering-sim/peer/service"
-	"github.com/iotaledger/autopeering-sim/selection"
 	"github.com/iotaledger/goshimmer/packages/gossip"
+	"github.com/iotaledger/goshimmer/packages/gossip/neighbor"
 	"github.com/iotaledger/hive.go/daemon"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/iotaledger/hive.go/logger"
@@ -29,37 +26,12 @@ func run(plugin *node.Plugin) {
 }
 
 func configureLogging(plugin *node.Plugin) {
-	gossip.Event.DropNeighbor.Attach(events.NewClosure(func(peer *gossip.Neighbor) {
+	gossip.Event.DropNeighbor.Attach(events.NewClosure(func(peer *neighbor.Neighbor) {
 		if Selection != nil {
 			Selection.DropPeer(peer.Peer)
 		}
 	}))
 
-	selection.Events.Dropped.Attach(events.NewClosure(func(ev *selection.DroppedEvent) {
-		log.Info("neighbor removed: " + ev.DroppedID.String())
-		gossip.RemoveNeighbor(ev.DroppedID.String())
-	}))
-
-	selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
-		log.Info("accepted neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
-		log.Info("services: " + ev.Peer.Services().CreateRecord().String())
-		gossipService := ev.Peer.Services().Get(service.GossipKey)
-		if gossipService != nil {
-			address, port, _ := net.SplitHostPort(ev.Peer.Services().Get(service.GossipKey).String())
-			gossip.AddNeighbor(gossip.NewNeighbor(ev.Peer, address, port))
-		}
-	}))
-
-	selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
-		log.Info("chosen neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
-		log.Info("services: " + ev.Peer.Services().CreateRecord().String())
-		gossipService := ev.Peer.Services().Get(service.GossipKey)
-		if gossipService != nil {
-			address, port, _ := net.SplitHostPort(ev.Peer.Services().Get(service.GossipKey).String())
-			gossip.AddNeighbor(gossip.NewNeighbor(ev.Peer, address, port))
-		}
-	}))
-
 	discover.Events.PeerDiscovered.Attach(events.NewClosure(func(ev *discover.DiscoveredEvent) {
 		log.Info("new peer discovered: " + ev.Peer.Address() + " / " + ev.Peer.ID().String())
 	}))
diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go
index 0ca491a8..89683109 100644
--- a/plugins/gossip/gossip.go
+++ b/plugins/gossip/gossip.go
@@ -1,11 +1,13 @@
 package gossip
 
 import (
+
 	"github.com/iotaledger/goshimmer/packages/gossip/transport"
 	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
 	gp "github.com/iotaledger/goshimmer/packages/gossip"
 	pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
 	"github.com/iotaledger/goshimmer/plugins/autopeering/local"
+	"github.com/iotaledger/autopeering-sim/peer/service"
 	"github.com/iotaledger/autopeering-sim/selection"
 	"github.com/iotaledger/goshimmer/plugins/tangle"
 	"go.uber.org/zap"
@@ -58,13 +60,21 @@ func configureEvents() {
 	}))
 
 	selection.Events.IncomingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
-		log.Debug("accepted neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
-		AddInbound(ev.Peer)
+		gossipService := ev.Peer.Services().Get(service.GossipKey)
+		if gossipService != nil {
+			log.Debug("accepted neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
+			//address, port, _ := net.SplitHostPort(ev.Peer.Services().Get(service.GossipKey).String())
+			AddInbound(ev.Peer)
+		}
 	}))
 
 	selection.Events.OutgoingPeering.Attach(events.NewClosure(func(ev *selection.PeeringEvent) {
-		log.Debug("chosen neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
-		AddOutbound(ev.Peer)
+		gossipService := ev.Peer.Services().Get(service.GossipKey)
+		if gossipService != nil {
+			log.Debug("chosen neighbor added: " + ev.Peer.Address() + " / " + ev.Peer.String())
+			//address, port, _ := net.SplitHostPort(ev.Peer.Services().Get(service.GossipKey).String())
+			AddOutbound(ev.Peer)
+		}
 	}))
 
 	// mgr.Events.NewTransaction.Attach(events.NewClosure(func(ev *gp.NewTransactionEvent) {
diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go
index c483a42a..b379f012 100644
--- a/plugins/metrics/plugin.go
+++ b/plugins/metrics/plugin.go
@@ -3,9 +3,8 @@ package metrics
 import (
 	"time"
 
-	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
+	"github.com/iotaledger/goshimmer/packages/gossip"
 	"github.com/iotaledger/goshimmer/packages/timeutil"
-	"github.com/iotaledger/goshimmer/plugins/gossip"
 	"github.com/iotaledger/hive.go/daemon"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/iotaledger/hive.go/node"
@@ -15,7 +14,7 @@ var PLUGIN = node.NewPlugin("Metrics", node.Enabled, configure, run)
 
 func configure(plugin *node.Plugin) {
 	// increase received TPS counter whenever we receive a new transaction
-	gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) { increaseReceivedTPSCounter() }))
+	gossip.Event.NewTransaction.Attach(events.NewClosure(func(_ *gossip.NewTransactionEvent) { increaseReceivedTPSCounter() }))
 }
 
 func run(plugin *node.Plugin) {
diff --git a/plugins/statusscreen-tps/plugin.go b/plugins/statusscreen-tps/plugin.go
index 15f4d1ba..c05fedb2 100644
--- a/plugins/statusscreen-tps/plugin.go
+++ b/plugins/statusscreen-tps/plugin.go
@@ -5,9 +5,8 @@ import (
 	"sync/atomic"
 	"time"
 
-	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
+	"github.com/iotaledger/goshimmer/packages/gossip"
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
-	"github.com/iotaledger/goshimmer/plugins/gossip"
 	"github.com/iotaledger/goshimmer/plugins/statusscreen"
 	"github.com/iotaledger/goshimmer/plugins/tangle"
 	"github.com/iotaledger/hive.go/daemon"
@@ -24,7 +23,7 @@ var receivedTps uint64
 var solidTps uint64
 
 var PLUGIN = node.NewPlugin("Statusscreen TPS", node.Enabled, func(plugin *node.Plugin) {
-	gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) {
+	gossip.Event.NewTransaction.Attach(events.NewClosure(func(_ *gossip.NewTransactionEvent) {
 		atomic.AddUint64(&receivedTpsCounter, 1)
 	}))
 
diff --git a/plugins/tangle/solidifier.go b/plugins/tangle/solidifier.go
index 53a0fe61..899764fc 100644
--- a/plugins/tangle/solidifier.go
+++ b/plugins/tangle/solidifier.go
@@ -4,12 +4,12 @@ import (
 	"runtime"
 
 	"github.com/iotaledger/goshimmer/packages/errors"
+	"github.com/iotaledger/goshimmer/packages/gossip"
 	"github.com/iotaledger/goshimmer/packages/model/approvers"
 	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
 	"github.com/iotaledger/goshimmer/packages/model/transactionmetadata"
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
 	"github.com/iotaledger/goshimmer/packages/workerpool"
-	"github.com/iotaledger/goshimmer/plugins/gossip"
 	"github.com/iotaledger/hive.go/daemon"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/iotaledger/hive.go/node"
@@ -27,8 +27,10 @@ func configureSolidifier(plugin *node.Plugin) {
 		task.Return(nil)
 	}, workerpool.WorkerCount(WORKER_COUNT), workerpool.QueueSize(10000))
 
-	gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(rawTransaction *meta_transaction.MetaTransaction) {
-		workerPool.Submit(rawTransaction)
+	gossip.Event.NewTransaction.Attach(events.NewClosure(func(ev *gossip.NewTransactionEvent) {
+		tx := ev.Body
+		metaTx := meta_transaction.FromBytes(tx)
+		workerPool.Submit(metaTx)
 	}))
 
 	daemon.Events.Shutdown.Attach(events.NewClosure(func() {
diff --git a/plugins/tangle/solidifier_test.go b/plugins/tangle/solidifier_test.go
index f13daf1b..9062eda4 100644
--- a/plugins/tangle/solidifier_test.go
+++ b/plugins/tangle/solidifier_test.go
@@ -1,20 +1,23 @@
 package tangle
 
 import (
-	"github.com/iotaledger/hive.go/parameter"
 	"os"
 	"sync"
 	"testing"
 
+	"github.com/golang/protobuf/proto"
+	"github.com/iotaledger/hive.go/parameter"
+
+	"github.com/iotaledger/goshimmer/packages/gossip"
+	pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
-	"github.com/iotaledger/goshimmer/plugins/gossip"
 	"github.com/iotaledger/hive.go/events"
 	"github.com/iotaledger/hive.go/node"
 	"github.com/iotaledger/iota.go/trinary"
 )
 
 func TestMain(m *testing.M) {
-	parameter.FetchConfig()
+	parameter.FetchConfig(false)
 	os.Exit(m.Run())
 }
 
@@ -43,10 +46,21 @@ func TestSolidifier(t *testing.T) {
 
 	// issue transactions
 	wg.Add(4)
-	gossip.Events.ReceiveTransaction.Trigger(transaction1.MetaTransaction)
-	gossip.Events.ReceiveTransaction.Trigger(transaction2.MetaTransaction)
-	gossip.Events.ReceiveTransaction.Trigger(transaction3.MetaTransaction)
-	gossip.Events.ReceiveTransaction.Trigger(transaction4.MetaTransaction)
+	tx := &pb.Transaction{Body: transaction1.MetaTransaction.GetBytes()}
+	b, _ := proto.Marshal(tx)
+	gossip.Event.NewTransaction.Trigger(b)
+
+	tx = &pb.Transaction{Body: transaction2.MetaTransaction.GetBytes()}
+	b, _ = proto.Marshal(tx)
+	gossip.Event.NewTransaction.Trigger(b)
+
+	tx = &pb.Transaction{Body: transaction3.MetaTransaction.GetBytes()}
+	b, _ = proto.Marshal(tx)
+	gossip.Event.NewTransaction.Trigger(b)
+
+	tx = &pb.Transaction{Body: transaction4.MetaTransaction.GetBytes()}
+	b, _ = proto.Marshal(tx)
+	gossip.Event.NewTransaction.Trigger(b)
 
 	// wait until all are solid
 	wg.Wait()
diff --git a/plugins/ui/ui.go b/plugins/ui/ui.go
index 2bc5d365..66ef3099 100644
--- a/plugins/ui/ui.go
+++ b/plugins/ui/ui.go
@@ -6,9 +6,8 @@ import (
 	"sync/atomic"
 	"time"
 
-	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
+	"github.com/iotaledger/goshimmer/packages/gossip"
 	"github.com/iotaledger/goshimmer/packages/model/value_transaction"
-	"github.com/iotaledger/goshimmer/plugins/gossip"
 	"github.com/iotaledger/goshimmer/plugins/tangle"
 	"github.com/iotaledger/goshimmer/plugins/webapi"
 	"github.com/iotaledger/hive.go/daemon"
@@ -35,7 +34,7 @@ func configure(plugin *node.Plugin) {
 		return c.JSON(http.StatusOK, tpsQueue)
 	})
 
-	gossip.Events.ReceiveTransaction.Attach(events.NewClosure(func(_ *meta_transaction.MetaTransaction) {
+	gossip.Event.NewTransaction.Attach(events.NewClosure(func(_ *gossip.NewTransactionEvent) {
 		atomic.AddUint64(&receivedTpsCounter, 1)
 	}))
 	tangle.Events.TransactionSolid.Attach(events.NewClosure(func(_ *value_transaction.ValueTransaction) {
-- 
GitLab