Skip to content
Snippets Groups Projects
Commit 73f685d7 authored by RUANO RINCON Santiago's avatar RUANO RINCON Santiago
Browse files

Add count occurrencies function

parent 184554a3
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ import (
"github.com/cockroachdb/errors"
"github.com/iotaledger/goshimmer/packages/tangle/payload"
gossipPkg "github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
)
......@@ -51,12 +52,13 @@ type Payload struct {
}
// NewMasterPoWPayload creates a new test echo request payload
func NewMasterPoWPayload(cpuUsage float64, nodeDst string, powdCast uint8) *Payload {
//func NewMasterPoWPayload(cpuUsage float64, nodeDst string, powdCast uint8) *Payload {
func NewMasterPoWPayload(cpuUsage float64, nodeDst *gossipPkg.Neighbor, powdCast uint8) *Payload {
return &Payload{
subType: uint32(1234),
cpuUsage: cpuUsage,
nodeDst: nodeDst,
nodeDstLen: uint32(len([]byte(nodeDst))),
nodeDst: nodeDst.Peer.ID().String(),
nodeDstLen: uint32(len([]byte(nodeDst.Peer.ID().String()))),
powdCast: powdCast,
}
......
......@@ -31,13 +31,16 @@ const (
// TODO: use it or remove it
func neighborAdd(neighbor *gossipPkg.Neighbor) {
log.Debugf("neighbor to be added: %s", neighbor.Peer.ID().String())
currentDifficulty[neighbor.Peer.ID().String()] = 1
log.Infof("neighbor to be added: %s", neighbor.Peer.PublicKey().String())
//currentDifficulty[neighbor.Peer.PublicKey().String()] = 1
//neighbor or neighbor.Peer?
currentDifficulty[neighbor] = 1
}
func neighborRemove(neighbor *gossipPkg.Neighbor) {
log.Debugf("neighbor to be removed: %s", neighbor.Peer.ID().String())
delete(currentDifficulty, neighbor.Peer.ID().String())
log.Infof("neighbor to be removed: %s", neighbor.Peer.PublicKey().String())
//delete(currentDifficulty, neighbor.Peer.PublicKey().String())
delete(currentDifficulty, neighbor)
}
var (
......@@ -48,7 +51,7 @@ var (
//TODO: this should be actually something like
//map[pubKey]int
currentDifficulty = make(map[string]int)
currentDifficulty = make(map[*gossipPkg.Neighbor]int)
// Map that keeps count of the amount of messages sent by each neighbor
senderOccurrences = make(map[string]int)
......@@ -88,23 +91,46 @@ func configure(_ *node.Plugin) {
localhost = local.GetInstance()
}
func increaseOccurrencyCounts(msg *tangle.Message) {
messagePayload := msg.Payload()
/* TODO: Filter specific types of messages
*/
if messagePayload.Type() != Type {
func increaseOccurrencyCounts(msg *tangle.Message) {
/*
// TODO: Filter specific types of messages
if msg.Payload().Type() != Type {
return
}
*/
log.Infof("issuer: %s", msg.issuerPublicKey)
log.Debugf("issuer: %s", msg.IssuerPublicKey())
msgIssuerPublicKey := msg.IssuerPublicKey().String()
for n, _ := range currentDifficulty {
// check if the sender is in our neighbor map
if n.Peer.PublicKey().String() == msgIssuerPublicKey {
senderOccurrences[msgIssuerPublicKey]++
//log.Infof("%s: %d", msgIssuerPublicKey, senderOccurrences[msgIssuerPublicKey])
}
}
}
func countOccurrencies() {
for issuer, occurrencies := range senderOccurrences{
log.Infof("%s: %d", issuer, occurrencies)
}
}
/*
messagelayer.Tangle().Events.MessageSolid.Attach(events.NewClosure(func(cachedMsgEvent *tangle.CachedMessageEvent) { // When receiving a message solid event do
defer cachedMsgEvent.MessageMetadata.Release()
cachedMsgEvent.Message.Consume(func(message *tangle.Message)
*/
func configureEvents() {
messagelayer.Tangle().Solidifier.Events.MessageSolid.Attach(events.NewClosure(func(messageID tangle.MessageID) {
messagelayer.Tangle().Storage.Message(messageID).Consume(func(msg *tangle.Message) {
increaseOccurrencyCounts(msg)
//log.Infof("issuer: %s", msg.IssuerPublicKey())
// Move the rest to a separate function
messagePayload := msg.Payload()
if messagePayload.Type() != Type {
......@@ -119,7 +145,7 @@ func configureEvents() {
log.Infof("Master pow message received for nodeDst: %s", msgPayload.nodeDst)
if msgPayload.nodeDst == localhost.ID().String() {
if msgPayload.nodeDst == localhost.PublicKey().String() {
pow.Tune(int(msgPayload.PoWdCast()))
log.Info("PoWCast received")
}
......@@ -155,13 +181,14 @@ func broadcastMasterPoWPayload() (doneSignal chan struct{}) {
func run(_ *node.Plugin) {
if err := daemon.BackgroundWorker("MasterPoW", func(shutdownSignal <-chan struct{}) {
ticker := time.NewTicker(42 * time.Second)
defer ticker.Stop()
tickerBroadcast := time.NewTicker(42 * time.Second)
defer tickerBroadcast.Stop()
tickerCount := time.NewTicker(60 * time.Second)
for {
select {
case <-shutdownSignal:
return
case <-ticker.C:
case <-tickerBroadcast.C:
doneSignal := broadcastMasterPoWPayload()
select {
......@@ -170,6 +197,8 @@ func run(_ *node.Plugin) {
case <-doneSignal:
// continue with the next beacon
}
case <-tickerCount.C:
countOccurrencies()
}
}
}, shutdown.PrioritySynchronization); err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment