Skip to content
Snippets Groups Projects
Commit c7080aca authored by capossele's avatar capossele
Browse files

:construction: WIP

parent 2c95e07d
No related branches found
No related tags found
No related merge requests found
Showing
with 202 additions and 125 deletions
......@@ -13,6 +13,7 @@
# Logs
logs/*
testNodes/*
# Project files
.idea
......
......@@ -9,7 +9,7 @@ require (
github.com/go-zeromq/zmq4 v0.6.2
github.com/google/open-location-code/go v0.0.0-20190903173953-119bc96a3a51
github.com/gorilla/websocket v1.4.1
github.com/iotaledger/autopeering-sim v0.0.0-20191121125328-c607091f6bc8
github.com/iotaledger/autopeering-sim v0.0.0-20191125082010-418faee91e5a
github.com/iotaledger/hive.go v0.0.0-20191118130432-89eebe8fe8eb
github.com/iotaledger/iota.go v1.0.0-beta.10
github.com/labstack/echo v3.3.10+incompatible
......@@ -19,10 +19,12 @@ require (
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-runewidth v0.0.6 // indirect
github.com/pkg/errors v0.8.1
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2
github.com/rivo/tview v0.0.0-20191121195645-2d957c4be01d
github.com/valyala/fasttemplate v1.1.0 // indirect
go.uber.org/zap v1.13.0
golang.org/x/crypto v0.0.0-20191119213627-4f8c1d86b1ba
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914
golang.org/x/net v0.0.0-20191124235446-72fef5d5e266
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
)
......@@ -94,6 +94,18 @@ github.com/iotaledger/autopeering-sim v0.0.0-20191121112351-05d62de2edf2 h1:CGGJ
github.com/iotaledger/autopeering-sim v0.0.0-20191121112351-05d62de2edf2/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191121125328-c607091f6bc8 h1:4G8MSFJhykckV4n5nQ48lx/qg3sf4MqHP6X4yoii2sc=
github.com/iotaledger/autopeering-sim v0.0.0-20191121125328-c607091f6bc8/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191121203423-98315403e684 h1:+zVGrC8o9/UrCZvmwgi5sUIgN7UeBWufowRX/ATGS3Q=
github.com/iotaledger/autopeering-sim v0.0.0-20191121203423-98315403e684/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191122094050-3251b06f764a h1:GayngK7kqD+Xw1+jVTVn0MCkwsdeURcNvXTqPfA07Cg=
github.com/iotaledger/autopeering-sim v0.0.0-20191122094050-3251b06f764a/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191122165813-905dac7a7390 h1:mBRYX/C8/BR9dTw4Jkgw7HyvYUB50jLYW1LaAG7kXg0=
github.com/iotaledger/autopeering-sim v0.0.0-20191122165813-905dac7a7390/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191122182429-a6129bebaa09 h1:97V2yiOqnz5u8+8g2yPuoet5BFt1gOXnXWTRPb7I2Cg=
github.com/iotaledger/autopeering-sim v0.0.0-20191122182429-a6129bebaa09/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191122190327-b1fcfa1bf02b h1:ri8xe7feJk9GzT4c4HCy2SqBTb6fWoGzZ2aRMU3LjSk=
github.com/iotaledger/autopeering-sim v0.0.0-20191122190327-b1fcfa1bf02b/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/autopeering-sim v0.0.0-20191125082010-418faee91e5a h1:4/GYRv+ClV261Dq53B4toHbWq/mdzLEAPaZ9g9Ytio8=
github.com/iotaledger/autopeering-sim v0.0.0-20191125082010-418faee91e5a/go.mod h1:JiaqaxLkQVnd8e/sya9y/LlRW56WlRKRl2TQXQCVssI=
github.com/iotaledger/goshimmer v0.0.0-20191113134331-c2d1b2f9d533/go.mod h1:7vYiofXphp9+PkgVAEM0pvw3aoi4ksrZ7lrEgX50XHs=
github.com/iotaledger/hive.go v0.0.0-20191118130432-89eebe8fe8eb h1:nuS/LETRJ8obUyBIZeyxeei0ZPlyOMj8YPziOgSM4Og=
github.com/iotaledger/hive.go v0.0.0-20191118130432-89eebe8fe8eb/go.mod h1:1Thhlil4lHzuy53EVvmEbEvWBFY0Tasp4kCBfxBCPIk=
......@@ -159,6 +171,8 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rivo/tview v0.0.0-20190829161255-f8bc69b90341/go.mod h1:+rKjP5+h9HMwWRpAfhIkkQ9KE3m3Nz5rwn7YtUpwgqk=
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2 h1:GVXSfgXOMAeLvFH7IrpY3yYM8H3YekZEFcZ14q9gQXM=
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0=
github.com/rivo/tview v0.0.0-20191121195645-2d957c4be01d h1:dPWYyMzc2VB5XX7eA/Pe5TXBGzhlVZZr54GhRJLTbts=
github.com/rivo/tview v0.0.0-20191121195645-2d957c4be01d/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0=
github.com/rivo/uniseg v0.0.0-20190513083848-b9f5b9457d44/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
......@@ -237,6 +251,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 h1:MlY3mEfbnWGmUi4rtHOtNnnnN4UJRGSyLPx+DXA5Sq4=
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191124235446-72fef5d5e266 h1:QuOiA7GCO0OSDzlNlFyOWOywDsjuzW8M2yvBfCqw+cY=
golang.org/x/net v0.0.0-20191124235446-72fef5d5e266/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
......@@ -277,6 +293,14 @@ golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191121040551-947d4aa89328 h1:t3X42h9e6xdbrCD/gPyWqAXr2BEpdJqRd1brThaaxII=
golang.org/x/tools v0.0.0-20191121040551-947d4aa89328/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191122080028-f774e2e2e5be h1:6d2MOtryvuMarrCTOqjhCZCVONST5uq4zNzBWKxzOls=
golang.org/x/tools v0.0.0-20191122080028-f774e2e2e5be/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191122161556-0ae87fff1b85 h1:T0S3IX2zGoozKqzqe8CQZRrJooHPr2aV/HmirRBwnRM=
golang.org/x/tools v0.0.0-20191122161556-0ae87fff1b85/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191122182703-035a8167be0b h1:CeeSPSpqT/6nJ9vHD7VQV5SkrnGMNnpYtwYajVX+29I=
golang.org/x/tools v0.0.0-20191122182703-035a8167be0b/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3 h1:aHkNOJLg6a84bdLJN1yjqMSTadeAuaudhEPNSkLAWoA=
golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
......
package accountability
import (
"sync"
"github.com/dgraph-io/badger"
"github.com/iotaledger/goshimmer/packages/identity"
"github.com/iotaledger/goshimmer/packages/settings"
)
// Name of the key under which the node identity is stored.
const identityKey = "IDENTITY"
var ownId *identity.Identity
var lazyInit sync.Once
func OwnId() *identity.Identity {
lazyInit.Do(initOwnId)
return ownId
}
func initOwnId() {
ownId = getIdentity()
}
func generateNewIdentity() *identity.Identity {
newIdentity := identity.GeneratePrivateIdentity()
key := []byte(identityKey)
value := newIdentity.Marshal()
if err := settings.Set(key, value); err != nil {
panic(err)
}
return newIdentity
}
func getIdentity() *identity.Identity {
key := []byte(identityKey)
value, err := settings.Get(key)
if err != nil {
if err == badger.ErrKeyNotFound {
return generateNewIdentity()
} else {
panic(err)
}
}
result, err := identity.Unmarshal(value)
if err != nil {
panic(err)
}
return result
}
package client
import (
"encoding/hex"
"net"
"time"
"github.com/iotaledger/autopeering-sim/discover"
"github.com/iotaledger/autopeering-sim/selection"
"github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/goshimmer/packages/node"
......@@ -15,10 +15,16 @@ import (
"github.com/iotaledger/goshimmer/plugins/analysis/types/connectnodes"
"github.com/iotaledger/goshimmer/plugins/analysis/types/disconnectnodes"
"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/events"
)
var debug *node.Plugin
func Run(plugin *node.Plugin) {
debug = plugin
daemon.BackgroundWorker("Analysis Client", func() {
shuttingDown := false
......@@ -37,7 +43,7 @@ func Run(plugin *node.Plugin) {
eventDispatchers := getEventDispatchers(managedConn)
reportCurrentStatus(eventDispatchers)
setupHooks(managedConn, eventDispatchers)
setupHooks(plugin, managedConn, eventDispatchers)
shuttingDown = keepConnectionAlive(managedConn)
}
......@@ -49,46 +55,72 @@ func Run(plugin *node.Plugin) {
func getEventDispatchers(conn *network.ManagedConnection) *EventDispatchers {
return &EventDispatchers{
AddNode: func(nodeId []byte) {
conn.Write((&addnode.Packet{NodeId: nodeId}).Marshal())
_, err := conn.Write((&addnode.Packet{NodeId: nodeId}).Marshal())
if err != nil {
debug.LogFailure(err.Error())
}
},
RemoveNode: func(nodeId []byte) {
_, err := conn.Write((&removenode.Packet{NodeId: nodeId}).Marshal())
if err != nil {
debug.LogFailure(err.Error())
}
},
ConnectNodes: func(sourceId []byte, targetId []byte) {
conn.Write((&connectnodes.Packet{SourceId: sourceId, TargetId: targetId}).Marshal())
_, err := conn.Write((&connectnodes.Packet{SourceId: sourceId, TargetId: targetId}).Marshal())
if err != nil {
debug.LogFailure(err.Error())
}
},
DisconnectNodes: func(sourceId []byte, targetId []byte) {
conn.Write((&disconnectnodes.Packet{SourceId: sourceId, TargetId: targetId}).Marshal())
_, err := conn.Write((&disconnectnodes.Packet{SourceId: sourceId, TargetId: targetId}).Marshal())
if err != nil {
debug.LogFailure(err.Error())
}
},
}
}
func reportCurrentStatus(eventDispatchers *EventDispatchers) {
eventDispatchers.AddNode(accountability.OwnId().Identifier.Bytes())
if local.INSTANCE != nil {
eventDispatchers.AddNode(local.INSTANCE.ID().Bytes())
}
reportChosenNeighbors(eventDispatchers)
}
func setupHooks(conn *network.ManagedConnection, eventDispatchers *EventDispatchers) {
func setupHooks(plugin *node.Plugin, conn *network.ManagedConnection, eventDispatchers *EventDispatchers) {
// define hooks ////////////////////////////////////////////////////////////////////////////////////////////////////
onDiscoverPeer := events.NewClosure(func(ev *discover.DiscoveredEvent) {
go eventDispatchers.AddNode(ev.Peer.ID().Bytes())
plugin.LogInfo("onDiscoverPeer: " + hex.EncodeToString(ev.Peer.ID().Bytes()))
eventDispatchers.AddNode(ev.Peer.ID().Bytes())
})
onDeletePeer := events.NewClosure(func(ev *discover.DeletedEvent) {
plugin.LogInfo("onDeletePeer: " + hex.EncodeToString(ev.Peer.ID().Bytes()))
eventDispatchers.RemoveNode(ev.Peer.ID().Bytes())
})
onAddAcceptedNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) {
eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), accountability.OwnId().Identifier.Bytes())
plugin.LogInfo("onAddAcceptedNeighbor: " + hex.EncodeToString(ev.Peer.ID().Bytes()) + " - " + hex.EncodeToString(local.INSTANCE.ID().Bytes()))
eventDispatchers.ConnectNodes(ev.Peer.ID().Bytes(), local.INSTANCE.ID().Bytes())
})
onRemoveNeighbor := events.NewClosure(func(ev *selection.DroppedEvent) {
eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), accountability.OwnId().Identifier.Bytes())
eventDispatchers.DisconnectNodes(accountability.OwnId().Identifier.Bytes(), ev.DroppedID.Bytes())
plugin.LogInfo("onRemoveNeighbor: " + hex.EncodeToString(ev.DroppedID.Bytes()) + " - " + hex.EncodeToString(local.INSTANCE.ID().Bytes()))
eventDispatchers.DisconnectNodes(ev.DroppedID.Bytes(), local.INSTANCE.ID().Bytes())
})
onAddChosenNeighbor := events.NewClosure(func(ev *selection.PeeringEvent) {
eventDispatchers.ConnectNodes(accountability.OwnId().Identifier.Bytes(), ev.Peer.ID().Bytes())
plugin.LogInfo("onAddChosenNeighbor: " + hex.EncodeToString(local.INSTANCE.ID().Bytes()) + " - " + hex.EncodeToString(ev.Peer.ID().Bytes()))
eventDispatchers.ConnectNodes(local.INSTANCE.ID().Bytes(), ev.Peer.ID().Bytes())
})
// setup hooks /////////////////////////////////////////////////////////////////////////////////////////////////////
discover.Events.PeerDiscovered.Attach(onDiscoverPeer)
discover.Events.PeerDeleted.Attach(onDeletePeer)
selection.Events.IncomingPeering.Attach(onAddAcceptedNeighbor)
selection.Events.OutgoingPeering.Attach(onAddChosenNeighbor)
selection.Events.Dropped.Attach(onRemoveNeighbor)
......@@ -108,12 +140,12 @@ func setupHooks(conn *network.ManagedConnection, eventDispatchers *EventDispatch
}
func reportChosenNeighbors(dispatchers *EventDispatchers) {
// for _, chosenNeighbor := range chosenneighbors.INSTANCE.Peers.GetMap() {
// dispatchers.AddNode(chosenNeighbor.GetIdentity().Identifier)
// }
// for _, chosenNeighbor := range chosenneighbors.INSTANCE.Peers.GetMap() {
// dispatchers.ConnectNodes(accountability.OwnId().Identifier, chosenNeighbor.GetIdentity().Identifier)
// }
if autopeering.Selection != nil {
for _, chosenNeighbor := range autopeering.Selection.GetOutgoingNeighbors() {
dispatchers.AddNode(chosenNeighbor.ID().Bytes())
dispatchers.ConnectNodes(local.INSTANCE.ID().Bytes(), chosenNeighbor.ID().Bytes())
}
}
}
func keepConnectionAlive(conn *network.ManagedConnection) bool {
......
......@@ -2,6 +2,7 @@ package client
type EventDispatchers struct {
AddNode func(nodeId []byte)
RemoveNode func(nodeId []byte)
ConnectNodes func(sourceId []byte, targetId []byte)
DisconnectNodes func(sourceId []byte, targetId []byte)
}
......@@ -6,7 +6,6 @@ import (
"strconv"
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/goshimmer/packages/network/tcp"
"github.com/iotaledger/goshimmer/packages/node"
......@@ -15,12 +14,16 @@ import (
"github.com/iotaledger/goshimmer/plugins/analysis/types/disconnectnodes"
"github.com/iotaledger/goshimmer/plugins/analysis/types/ping"
"github.com/iotaledger/goshimmer/plugins/analysis/types/removenode"
"github.com/iotaledger/hive.go/events"
"github.com/pkg/errors"
)
var server *tcp.Server
var debug *node.Plugin
func Configure(plugin *node.Plugin) {
debug = plugin
server = tcp.NewServer()
server.Events.Connect.Attach(events.NewClosure(HandleConnection))
......@@ -153,7 +156,7 @@ func processIncomingPacket(connectionState *byte, receiveBuffer *[]byte, conn *n
processIncomingDisconnectNodesPacket(connectionState, receiveBuffer, conn, data, offset, connectedNodeId)
case STATE_REMOVE_NODE:
processIncomingAddNodePacket(connectionState, receiveBuffer, conn, data, offset, connectedNodeId)
processIncomingRemoveNodePacket(connectionState, receiveBuffer, conn, data, offset, connectedNodeId)
}
}
......@@ -309,3 +312,32 @@ func processIncomingDisconnectNodesPacket(connectionState *byte, receiveBuffer *
}
}
}
func processIncomingRemoveNodePacket(connectionState *byte, receiveBuffer *[]byte, conn *network.ManagedConnection, data []byte, offset *int, connectedNodeId *string) {
remainingCapacity := int(math.Min(float64(removenode.MARSHALED_TOTAL_SIZE-*offset), float64(len(data))))
copy((*receiveBuffer)[*offset:], data[:remainingCapacity])
if *offset+len(data) < removenode.MARSHALED_TOTAL_SIZE {
*offset += len(data)
} else {
if removeNodePacket, err := removenode.Unmarshal(*receiveBuffer); err != nil {
Events.Error.Trigger(err)
conn.Close()
return
} else {
nodeId := hex.EncodeToString(removeNodePacket.NodeId)
Events.RemoveNode.Trigger(nodeId)
}
*connectionState = STATE_CONSECUTIVE
if *offset+len(data) > addnode.MARSHALED_TOTAL_SIZE {
processIncomingPacket(connectionState, receiveBuffer, conn, data[remainingCapacity:], offset, connectedNodeId)
}
}
}
package addnode
import "crypto/sha256"
const (
MARSHALED_PACKET_HEADER = 0x01
......@@ -8,7 +10,7 @@ const (
MARSHALED_PACKET_HEADER_END = MARSHALED_PACKET_HEADER_START + MARSHALED_PACKET_HEADER_SIZE
MARSHALED_ID_START = MARSHALED_PACKET_HEADER_END
MARSHALED_ID_SIZE = 20
MARSHALED_ID_SIZE = sha256.Size
MARSHALED_ID_END = MARSHALED_ID_START + MARSHALED_ID_SIZE
MARSHALED_TOTAL_SIZE = MARSHALED_ID_END
......
package connectnodes
import "crypto/sha256"
const (
MARSHALED_PACKET_HEADER = 0x03
......@@ -8,11 +10,11 @@ const (
MARSHALED_PACKET_HEADER_END = MARSHALED_PACKET_HEADER_START + MARSHALED_PACKET_HEADER_SIZE
MARSHALED_SOURCE_ID_START = MARSHALED_PACKET_HEADER_END
MARSHALED_SOURCE_ID_SIZE = 20
MARSHALED_SOURCE_ID_SIZE = sha256.Size
MARSHALED_SOURCE_ID_END = MARSHALED_SOURCE_ID_START + MARSHALED_SOURCE_ID_SIZE
MARSHALED_TARGET_ID_START = MARSHALED_SOURCE_ID_END
MARSHALED_TARGET_ID_SIZE = 20
MARSHALED_TARGET_ID_SIZE = sha256.Size
MARSHALED_TARGET_ID_END = MARSHALED_TARGET_ID_START + MARSHALED_TARGET_ID_SIZE
MARSHALED_TOTAL_SIZE = MARSHALED_TARGET_ID_END
......
package disconnectnodes
import "crypto/sha256"
const (
MARSHALED_PACKET_HEADER = 0x04
......@@ -8,11 +10,11 @@ const (
MARSHALED_PACKET_HEADER_END = MARSHALED_PACKET_HEADER_START + MARSHALED_PACKET_HEADER_SIZE
MARSHALED_SOURCE_ID_START = MARSHALED_PACKET_HEADER_END
MARSHALED_SOURCE_ID_SIZE = 20
MARSHALED_SOURCE_ID_SIZE = sha256.Size
MARSHALED_SOURCE_ID_END = MARSHALED_SOURCE_ID_START + MARSHALED_SOURCE_ID_SIZE
MARSHALED_TARGET_ID_START = MARSHALED_SOURCE_ID_END
MARSHALED_TARGET_ID_SIZE = 20
MARSHALED_TARGET_ID_SIZE = sha256.Size
MARSHALED_TARGET_ID_END = MARSHALED_TARGET_ID_START + MARSHALED_TARGET_ID_SIZE
MARSHALED_TOTAL_SIZE = MARSHALED_TARGET_ID_END
......
package removenode
import "crypto/sha256"
const (
MARSHALED_PACKET_HEADER = 0x02
......@@ -8,7 +10,7 @@ const (
MARSHALED_PACKET_HEADER_END = MARSHALED_PACKET_HEADER_START + MARSHALED_PACKET_HEADER_SIZE
MARSHALED_ID_START = MARSHALED_PACKET_HEADER_END
MARSHALED_ID_SIZE = 20
MARSHALED_ID_SIZE = sha256.Size
MARSHALED_ID_END = MARSHALED_ID_START + MARSHALED_ID_SIZE
MARSHALED_TOTAL_SIZE = MARSHALED_ID_END
......
......@@ -3,10 +3,10 @@ package httpserver
import (
"fmt"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/plugins/analysis/server"
"github.com/iotaledger/goshimmer/plugins/analysis/webinterface/recordedevents"
"github.com/iotaledger/goshimmer/plugins/analysis/webinterface/types"
"github.com/iotaledger/hive.go/events"
"golang.org/x/net/websocket"
)
......
......@@ -26,6 +26,7 @@ func index(w http.ResponseWriter, r *http.Request) {
};
socket.onmessage = function (e) {
console.log("Len: ", data.nodes.length);
switch (e.data[0]) {
case "_":
// do nothing - its just a ping
......@@ -33,26 +34,32 @@ func index(w http.ResponseWriter, r *http.Request) {
case "A":
addNode(e.data.substr(1));
console.log("Add node:",e.data.substr(1));
break;
case "a":
removeNode(e.data.substr(1));
console.log("Remove node:", e.data.substr(1));
break;
case "C":
connectNodes(e.data.substr(1, 40), e.data.substr(41, 40));
connectNodes(e.data.substr(1, 64), e.data.substr(65, 128));
console.log("Connect nodes:",e.data.substr(1, 64), " - ", e.data.substr(65, 128));
break;
case "c":
disconnectNodes(e.data.substr(1, 40), e.data.substr(41, 40));
disconnectNodes(e.data.substr(1, 64), e.data.substr(65, 128));
console.log("Disconnect nodes:",e.data.substr(1, 64), " - ", e.data.substr(65, 128));
break;
case "O":
setNodeOnline(e.data.substr(1));
console.log("setNodeOnline:",e.data.substr(1));
break;
case "o":
setNodeOffline(e.data.substr(1));
console.log("setNodeOffline:",e.data.substr(1));
break;
}
};
......@@ -96,6 +103,7 @@ func index(w http.ResponseWriter, r *http.Request) {
data.nodes = [...data.nodes, node];
nodesById[node.id] = node;
nodesById[nodeId].online = true;
updateGraph();
}
......@@ -128,6 +136,14 @@ func index(w http.ResponseWriter, r *http.Request) {
function connectNodes(sourceNodeId, targetNodeId) {
if(existingLinks[sourceNodeId + targetNodeId] == undefined && existingLinks[targetNodeId + sourceNodeId] == undefined) {
if (!(sourceNodeId in nodesById)) {
addNode(sourceNodeId);
}
if (!(targetNodeId in nodesById)) {
addNode(targetNodeId);
}
nodesById[sourceNodeId].online = true;
nodesById[targetNodeId].online = true;
data.links = [...data.links, { source: sourceNodeId, target: targetNodeId }];
updateGraph();
......@@ -136,7 +152,6 @@ func index(w http.ResponseWriter, r *http.Request) {
function disconnectNodes(sourceNodeId, targetNodeId) {
data.links = data.links.filter(l => !(l.source.id == sourceNodeId && l.target.id == targetNodeId) && !(l.source.id == targetNodeId && l.target.id == sourceNodeId));
delete existingLinks[sourceNodeId + targetNodeId];
delete existingLinks[targetNodeId + sourceNodeId];
......@@ -144,6 +159,9 @@ func index(w http.ResponseWriter, r *http.Request) {
}
function removeNodeX(node) {
if (!(node.id in nodesById)) {
addNode(sourceNodeId);
}
removeNode(node.id)
}
</script>
......
......@@ -5,8 +5,8 @@ import (
"time"
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/hive.go/events"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
)
......
package recordedevents
import (
"strconv"
"sync"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/analysis/server"
"github.com/iotaledger/goshimmer/plugins/analysis/webinterface/types"
"github.com/iotaledger/hive.go/events"
)
var nodes = make(map[string]bool)
......@@ -16,6 +17,7 @@ var lock sync.Mutex
func Configure(plugin *node.Plugin) {
server.Events.AddNode.Attach(events.NewClosure(func(nodeId string) {
plugin.LogInfo("AddNode: " + nodeId + " sizeof " + strconv.Itoa(len(nodeId)))
if _, exists := nodes[nodeId]; !exists {
lock.Lock()
defer lock.Unlock()
......@@ -27,6 +29,7 @@ func Configure(plugin *node.Plugin) {
}))
server.Events.RemoveNode.Attach(events.NewClosure(func(nodeId string) {
plugin.LogInfo("RemoveNode: " + nodeId)
lock.Lock()
defer lock.Unlock()
......@@ -34,6 +37,7 @@ func Configure(plugin *node.Plugin) {
}))
server.Events.NodeOnline.Attach(events.NewClosure(func(nodeId string) {
plugin.LogInfo("NodeOnline: " + nodeId)
lock.Lock()
defer lock.Unlock()
......@@ -41,6 +45,7 @@ func Configure(plugin *node.Plugin) {
}))
server.Events.NodeOffline.Attach(events.NewClosure(func(nodeId string) {
plugin.LogInfo("NodeOffline: " + nodeId)
lock.Lock()
defer lock.Unlock()
......@@ -48,6 +53,7 @@ func Configure(plugin *node.Plugin) {
}))
server.Events.ConnectNodes.Attach(events.NewClosure(func(sourceId string, targetId string) {
plugin.LogInfo("ConnectNodes: " + sourceId + " - " + targetId)
lock.Lock()
defer lock.Unlock()
......@@ -61,6 +67,7 @@ func Configure(plugin *node.Plugin) {
}))
server.Events.DisconnectNodes.Attach(events.NewClosure(func(sourceId string, targetId string) {
plugin.LogInfo("DisconnectNodes: " + sourceId + " - " + targetId)
lock.Lock()
defer lock.Unlock()
......
......@@ -8,6 +8,7 @@ import (
"net"
"net/http"
"strconv"
"time"
"github.com/iotaledger/autopeering-sim/discover"
"github.com/iotaledger/autopeering-sim/logger"
......@@ -16,13 +17,15 @@ import (
"github.com/iotaledger/autopeering-sim/server"
"github.com/iotaledger/autopeering-sim/transport"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/gossip"
"go.uber.org/zap"
)
var (
PLUGIN = node.NewPlugin("Auto Peering", node.Enabled, configure, run)
debugLevel = "debug"
debugLevel = "info"
close = make(chan struct{}, 1)
srv *server.Server
Discovery *discover.Protocol
......@@ -32,7 +35,7 @@ var (
const defaultZLC = `{
"level": "info",
"development": false,
"outputPaths": ["./logs/autopeering.log"],
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoding": "console",
"encoderConfig": {
......@@ -94,26 +97,26 @@ func start() {
// create a new local node
db := peer.NewPersistentDB(logger.Named("db"))
defer db.Close()
local, err := peer.NewLocal(db)
local.INSTANCE, err = peer.NewLocal(db)
if err != nil {
log.Fatalf("ListenUDP: %v", err)
}
// add a service for the peering
local.Services()["peering"] = peer.NetworkAddress{Network: "udp", Address: listenAddr}
local.INSTANCE.Services()["peering"] = peer.NetworkAddress{Network: "udp", Address: listenAddr}
// add a service for the gossip
local.Services()["gossip"] = peer.NetworkAddress{Network: "tcp", Address: gossipAddr}
local.INSTANCE.Services()["gossip"] = peer.NetworkAddress{Network: "tcp", Address: gossipAddr}
Discovery = discover.New(local, discover.Config{
Discovery = discover.New(local.INSTANCE, discover.Config{
Log: logger.Named("disc"),
MasterPeers: masterPeers,
})
Selection = selection.New(local, Discovery, selection.Config{
Selection = selection.New(local.INSTANCE, Discovery, selection.Config{
Log: logger.Named("sel"),
SaltLifetime: selection.DefaultSaltLifetime,
})
// start a server doing discovery and peering
srv = server.Listen(local, trans, logger.Named("srv"), Discovery, Selection)
srv = server.Listen(local.INSTANCE, trans, logger.Named("srv"), Discovery, Selection)
defer srv.Close()
// start the discovery on that connection
......@@ -124,29 +127,19 @@ func start() {
Selection.Start(srv)
defer Selection.Close()
id := base64.StdEncoding.EncodeToString(local.PublicKey())
id := base64.StdEncoding.EncodeToString(local.INSTANCE.PublicKey())
a, b, _ := net.SplitHostPort(srv.LocalAddr())
logger.Info("Discovery protocol started: ID="+id+", address="+srv.LocalAddr(), a, b)
<-close
go func() {
for t := range time.NewTicker(2 * time.Second).C {
_ = t
printReport(logger)
}
}()
// func parseMaster(s string) (*peer.Peer, error) {
// if len(s) == 0 {
// return nil, nil
// }
// parts := strings.Split(s, "@")
// if len(parts) != 2 {
// return nil, errors.New("parseMaster")
// }
// pubKey, err := base64.StdEncoding.DecodeString(parts[0])
// if err != nil {
// return nil, errors.Wrap(err, "parseMaster")
// }
// return peer.NewPeer(pubKey, parts[1]), nil
// }
<-close
}
func getMyIP() string {
url := "https://api.ipify.org?format=text"
......@@ -161,3 +154,15 @@ func getMyIP() string {
}
return fmt.Sprintf("%s", ip)
}
func printReport(log *zap.SugaredLogger) {
if Discovery == nil || Selection == nil {
return
}
knownPeers := Discovery.GetVerifiedPeers()
incoming := Selection.GetIncomingNeighbors()
outgoing := Selection.GetOutgoingNeighbors()
log.Info("Known peers:", len(knownPeers))
log.Info("Chosen:", len(outgoing), outgoing)
log.Info("Accepted:", len(incoming), incoming)
}
package local
import "github.com/iotaledger/autopeering-sim/peer"
var INSTANCE *peer.Local
......@@ -11,8 +11,8 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/iotaledger/goshimmer/packages/accountability"
"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"
)
......@@ -70,7 +70,7 @@ func GetStatus() *Status {
uptime += fmt.Sprintf("%02ds ", int(duration.Seconds())%60)
return &Status{
Id: accountability.OwnId().StringIdentifier,
Id: local.INSTANCE.ID().String(),
Neighbor: "Neighbors: " + strconv.Itoa(len(autopeering.Selection.GetOutgoingNeighbors())) + " chosen / " + strconv.Itoa(len(autopeering.Selection.GetIncomingNeighbors())) + " accepted / " + strconv.Itoa(len(autopeering.Selection.GetNeighbors())) + " total",
KnownPeer: "Known Peers: " + strconv.Itoa(len(autopeering.Discovery.GetVerifiedPeers())) + " total",
Uptime: uptime,
......
......@@ -8,18 +8,19 @@ import (
"time"
"github.com/iotaledger/autopeering-sim/peer"
"github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/identity"
"github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/hive.go/events"
)
func configureNeighbors(plugin *node.Plugin) {
Events.AddNeighbor.Attach(events.NewClosure(func(neighbor *Neighbor) {
plugin.LogSuccess("new neighbor added " + neighbor.GetIdentity().StringIdentifier + "@" + neighbor.GetAddress().String() + ":" + neighbor.GetPort())
//plugin.LogSuccess("new neighbor added " + hex.EncodeToString(neighbor.Peer.ID().Bytes()) + "@" + neighbor.GetAddress().String() + ":" + neighbor.GetPort())
}))
Events.UpdateNeighbor.Attach(events.NewClosure(func(neighbor *Neighbor) {
......@@ -221,7 +222,7 @@ func (neighbor *Neighbor) Connect() (*protocol, bool, errors.IdentifiableError)
// drop the "secondary" connection upon successful handshake
neighbor.GetInitiatedProtocol().Events.HandshakeCompleted.Attach(events.NewClosure(func() {
if accountability.OwnId().StringIdentifier <= neighbor.GetIdentity().StringIdentifier {
if local.INSTANCE.ID().String() <= neighbor.Peer.ID().String() {
var acceptedProtocolConn *network.ManagedConnection
if neighbor.GetAcceptedProtocol() != nil {
acceptedProtocolConn = neighbor.GetAcceptedProtocol().Conn
......
......@@ -5,8 +5,8 @@ import (
"sync"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/hive.go/events"
)
// region constants and variables //////////////////////////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment