Skip to content
Snippets Groups Projects
Unverified Commit 01594440 authored by jonastheis's avatar jonastheis
Browse files

Quick and dirty debug setting for gossip neighbors issue #243

parent 3f2b29b7
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ WORKDIR /goshimmer ...@@ -14,6 +14,7 @@ WORKDIR /goshimmer
# Use Go Modules # Use Go Modules
COPY go.mod . COPY go.mod .
COPY go.sum . COPY go.sum .
COPY hive.go .
ENV GO111MODULE=on ENV GO111MODULE=on
RUN go mod download RUN go mod download
......
...@@ -8,7 +8,8 @@ import ( ...@@ -8,7 +8,8 @@ import (
) )
const ( const (
routeGetNeighbors = "autopeering/neighbors" routeGetNeighbors = "autopeering/neighbors"
routeGetGossipNeighbors = "getGossipNeighbors"
) )
// GetNeighbors gets the chosen/accepted neighbors. // GetNeighbors gets the chosen/accepted neighbors.
...@@ -25,3 +26,11 @@ func (api *GoShimmerAPI) GetNeighbors(knownPeers bool) (*webapi_autopeering.Resp ...@@ -25,3 +26,11 @@ func (api *GoShimmerAPI) GetNeighbors(knownPeers bool) (*webapi_autopeering.Resp
} }
return res, nil return res, nil
} }
func (api *GoShimmerAPI) GetGossipNeighbors() (*webapi_autopeering.GossipResponse, error) {
res := &webapi_autopeering.GossipResponse{}
if err := api.do(http.MethodGet, routeGetGossipNeighbors, nil, res); err != nil {
return nil, err
}
return res, nil
}
...@@ -32,3 +32,6 @@ require ( ...@@ -32,3 +32,6 @@ require (
golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe // indirect golang.org/x/tools v0.0.0-20200330040139-fa3cc9eebcfe // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/src-d/go-git.v4 v4.13.1
) )
replace github.com/iotaledger/hive.go => ./hive.go
\ No newline at end of file
package autopeering package autopeering
import ( import (
"encoding/base64" "fmt"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"github.com/iotaledger/goshimmer/plugins/autopeering" "github.com/iotaledger/goshimmer/plugins/autopeering"
"github.com/iotaledger/goshimmer/plugins/gossip"
"github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/goshimmer/plugins/webapi"
"github.com/iotaledger/hive.go/autopeering/peer" "github.com/iotaledger/hive.go/autopeering/peer"
"github.com/iotaledger/hive.go/autopeering/peer/service" "github.com/iotaledger/hive.go/autopeering/peer/service"
...@@ -18,6 +19,7 @@ var PLUGIN = node.NewPlugin("WebAPI autopeering Endpoint", node.Enabled, configu ...@@ -18,6 +19,7 @@ var PLUGIN = node.NewPlugin("WebAPI autopeering Endpoint", node.Enabled, configu
func configure(plugin *node.Plugin) { func configure(plugin *node.Plugin) {
webapi.Server.GET("autopeering/neighbors", getNeighbors) webapi.Server.GET("autopeering/neighbors", getNeighbors)
webapi.Server.GET("getGossipNeighbors", getGossipNeighbors)
} }
// getNeighbors returns the chosen and accepted neighbors of the node // getNeighbors returns the chosen and accepted neighbors of the node
...@@ -39,7 +41,7 @@ func getNeighbors(c echo.Context) error { ...@@ -39,7 +41,7 @@ func getNeighbors(c echo.Context) error {
for _, peer := range autopeering.Discovery.GetVerifiedPeers() { for _, peer := range autopeering.Discovery.GetVerifiedPeers() {
n := Neighbor{ n := Neighbor{
ID: peer.ID().String(), ID: peer.ID().String(),
PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), PublicKey: peer.PublicKey().String(),
} }
n.Services = getServices(peer) n.Services = getServices(peer)
knownPeers = append(knownPeers, n) knownPeers = append(knownPeers, n)
...@@ -49,7 +51,7 @@ func getNeighbors(c echo.Context) error { ...@@ -49,7 +51,7 @@ func getNeighbors(c echo.Context) error {
for _, peer := range autopeering.Selection.GetOutgoingNeighbors() { for _, peer := range autopeering.Selection.GetOutgoingNeighbors() {
n := Neighbor{ n := Neighbor{
ID: peer.ID().String(), ID: peer.ID().String(),
PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), PublicKey: peer.PublicKey().String(),
} }
n.Services = getServices(peer) n.Services = getServices(peer)
chosen = append(chosen, n) chosen = append(chosen, n)
...@@ -57,7 +59,7 @@ func getNeighbors(c echo.Context) error { ...@@ -57,7 +59,7 @@ func getNeighbors(c echo.Context) error {
for _, peer := range autopeering.Selection.GetIncomingNeighbors() { for _, peer := range autopeering.Selection.GetIncomingNeighbors() {
n := Neighbor{ n := Neighbor{
ID: peer.ID().String(), ID: peer.ID().String(),
PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), PublicKey: peer.PublicKey().String(),
} }
n.Services = getServices(peer) n.Services = getServices(peer)
accepted = append(accepted, n) accepted = append(accepted, n)
...@@ -66,6 +68,26 @@ func getNeighbors(c echo.Context) error { ...@@ -66,6 +68,26 @@ func getNeighbors(c echo.Context) error {
return c.JSON(http.StatusOK, Response{KnownPeers: knownPeers, Chosen: chosen, Accepted: accepted}) return c.JSON(http.StatusOK, Response{KnownPeers: knownPeers, Chosen: chosen, Accepted: accepted})
} }
func getGossipNeighbors(c echo.Context) error {
neighbors := gossip.GetAllNeighbors()
if neighbors == nil {
return c.JSON(http.StatusBadRequest, GossipResponse{Error: "Gossip not enabled."})
}
neighborsResp := make([]Neighbor, len(neighbors))
for _, n := range neighbors {
neighborsResp = append(
neighborsResp,
Neighbor{
ID: n.ID().String(),
PublicKey: n.PublicKey().String(),
Services: nil,
})
}
return c.JSON(http.StatusOK, GossipResponse{Neighbors: neighborsResp})
}
type Response struct { type Response struct {
KnownPeers []Neighbor `json:"known,omitempty"` KnownPeers []Neighbor `json:"known,omitempty"`
Chosen []Neighbor `json:"chosen"` Chosen []Neighbor `json:"chosen"`
...@@ -73,12 +95,21 @@ type Response struct { ...@@ -73,12 +95,21 @@ type Response struct {
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
type GossipResponse struct {
Neighbors []Neighbor `json:"neighbors,omitempty"`
Error string `json:"error,omitempty"`
}
type Neighbor struct { type Neighbor struct {
ID string `json:"id"` // comparable node identifier ID string `json:"id"` // comparable node identifier
PublicKey string `json:"publicKey"` // public key used to verify signatures PublicKey string `json:"publicKey"` // public key used to verify signatures
Services []peerService `json:"services,omitempty"` Services []peerService `json:"services,omitempty"`
} }
func (n *Neighbor) String() string {
return fmt.Sprintf("Neighbor{ID: %s, PublicKey: %s}", n.ID, n.PublicKey)
}
type peerService struct { type peerService struct {
ID string `json:"id"` // ID of the service ID string `json:"id"` // ID of the service
Address string `json:"address"` // network address of the service Address string `json:"address"` // network address of the service
......
...@@ -97,3 +97,11 @@ func (f *Framework) Peers() []*Peer { ...@@ -97,3 +97,11 @@ func (f *Framework) Peers() []*Peer {
func (f *Framework) RandomPeer() *Peer { func (f *Framework) RandomPeer() *Peer {
return f.peers[rand.Intn(len(f.peers))] return f.peers[rand.Intn(len(f.peers))]
} }
//TODO:
//func (f *Framework) GetPeerByID(id string) *Peer {
// for _, p := range f.peers {
// if p.
// }
// return nil
//}
package tests package tests
import ( //
"bufio" //import (
"regexp" // "bufio"
"testing" // "regexp"
// "testing"
"github.com/stretchr/testify/assert" //
"github.com/stretchr/testify/require" // "github.com/stretchr/testify/assert"
) // "github.com/stretchr/testify/require"
//)
// TestDockerLogs simply verifies that a peer's log message contains "GoShimmer". //
// Using the combination of logs and regular expressions can be useful to test a certain peer's behavior. //// TestDockerLogs simply verifies that a peer's log message contains "GoShimmer".
func TestDockerLogs(t *testing.T) { //// Using the combination of logs and regular expressions can be useful to test a certain peer's behavior.
r := regexp.MustCompile("GoShimmer") //func TestDockerLogs(t *testing.T) {
// r := regexp.MustCompile("GoShimmer")
for _, p := range f.Peers() { //
log, err := p.Logs() // for _, p := range f.Peers() {
require.NoError(t, err) // log, err := p.Logs()
// require.NoError(t, err)
assert.True(t, r.MatchReader(bufio.NewReader(log))) //
} // assert.True(t, r.MatchReader(bufio.NewReader(log)))
} // }
//}
package tests
import (
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/iotaledger/goshimmer/plugins/webapi/autopeering"
"github.com/iotaledger/goshimmer/tools/integration-tests/tester/framework"
"github.com/stretchr/testify/require"
)
func TestNeighbors(t *testing.T) {
for {
for _, p := range f.Peers() {
// get gossip neighbors
resp, err := p.GetGossipNeighbors()
require.NoError(t, err)
gossipNeighbors := resp.Neighbors
var gossipNeighborsClean []autopeering.Neighbor
for _, n := range gossipNeighbors {
if len(n.ID) == 0 {
continue
}
gossipNeighborsClean = append(gossipNeighborsClean, n)
}
sort.Slice(gossipNeighborsClean, func(i, j int) bool {
return strings.Compare(gossipNeighborsClean[i].ID, gossipNeighborsClean[j].ID) < 0
})
// get autopeering neighbors
resp2, err := p.GetNeighbors(false)
require.NoError(t, err)
var autopeeringNeighbors []autopeering.Neighbor
autopeeringNeighbors = append(autopeeringNeighbors, resp2.Accepted...)
autopeeringNeighbors = append(autopeeringNeighbors, resp2.Chosen...)
sort.Slice(autopeeringNeighbors, func(i, j int) bool {
return strings.Compare(autopeeringNeighbors[i].ID, autopeeringNeighbors[j].ID) < 0
})
if len(gossipNeighborsClean) != len(autopeeringNeighbors) {
printPeerWithNeighbors(p, gossipNeighborsClean, autopeeringNeighbors)
}
}
fmt.Println("Waiting 10 secs...")
time.Sleep(10 * time.Second)
}
}
func printPeerWithNeighbors(peer *framework.Peer, gossipNeighbors, autopeeringNeighbors []autopeering.Neighbor) {
fmt.Printf("-----------------------------\n")
fmt.Println(peer.String())
fmt.Printf("Gossip: %d\n", len(gossipNeighbors))
for i, n := range gossipNeighbors {
fmt.Printf("%d: %s\n", i, n.String())
}
fmt.Printf("Autopeering: %d\n", len(autopeeringNeighbors))
for i, n := range autopeeringNeighbors {
fmt.Printf("%d: %s\n", i, n.String())
}
fmt.Printf("-----------------------------\n\n")
}
package tests package tests
import ( //
"testing" //import (
"time" // "testing"
// "time"
"github.com/stretchr/testify/assert" //
"github.com/stretchr/testify/require" // "github.com/stretchr/testify/assert"
) // "github.com/stretchr/testify/require"
//)
// TestRelayMessages checks whether messages are actually relayed/gossiped through the network //
// by checking the messages' existence on all nodes after a cool down. //// TestRelayMessages checks whether messages are actually relayed/gossiped through the network
func TestRelayMessages(t *testing.T) { //// by checking the messages' existence on all nodes after a cool down.
numMessages := 100 //func TestRelayMessages(t *testing.T) {
ids := make([]string, numMessages) // numMessages := 100
// ids := make([]string, numMessages)
data := []byte("Test") //
// data := []byte("Test")
// create messages on random peers //
for i := 0; i < numMessages; i++ { // // create messages on random peers
id, err := f.RandomPeer().Data(data) // for i := 0; i < numMessages; i++ {
require.NoError(t, err) // id, err := f.RandomPeer().Data(data)
// require.NoError(t, err)
ids[i] = id //
} // ids[i] = id
// }
// wait for messages to be gossiped //
time.Sleep(5 * time.Second) // // wait for messages to be gossiped
// time.Sleep(5 * time.Second)
// check for messages on every peer //
for _, peer := range f.Peers() { // // check for messages on every peer
resp, err := peer.FindMessageById(ids) // for _, peer := range f.Peers() {
require.NoError(t, err) // resp, err := peer.FindMessageById(ids)
// require.NoError(t, err)
// check for the count of messages //
assert.Equal(t, numMessages, len(resp.Messages)) // // check for the count of messages
// assert.Equal(t, numMessages, len(resp.Messages))
// check that all messages are present in response //
outer: // // check that all messages are present in response
for _, id := range ids { // outer:
for _, msg := range resp.Messages { // for _, id := range ids {
// if message found skip to next // for _, msg := range resp.Messages {
if msg.Id == id { // // if message found skip to next
continue outer // if msg.Id == id {
} // continue outer
} // }
// }
t.Errorf("MessageId=%s not found in peer %s.", id, peer.String()) //
} // t.Errorf("MessageId=%s not found in peer %s.", id, peer.String())
} // }
} // }
//}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment