From f69d4bc80ccd5e984506442c38c52a040d11a97d Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Mon, 8 Jun 2020 10:19:57 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Split=20vote=20context=20to=20fi?=
 =?UTF-8?q?t=20into=20an=20FPC=20update?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 plugins/analysis/client/plugin.go | 66 ++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 18 deletions(-)

diff --git a/plugins/analysis/client/plugin.go b/plugins/analysis/client/plugin.go
index 41f99931..bcbd601a 100644
--- a/plugins/analysis/client/plugin.go
+++ b/plugins/analysis/client/plugin.go
@@ -29,6 +29,8 @@ const (
 	CfgServerAddress = "analysis.client.serverAddress"
 	// defines the report interval of the reporting in seconds.
 	reportIntervalSec = 5
+	// maxVoteContext defines the maximum number of vote context to fit into an FPC update
+	maxVoteContext = 50
 )
 
 func init() {
@@ -148,29 +150,57 @@ func onRoundExecuted(roundStats *vote.RoundStats) {
 		nodeID = local.GetInstance().ID().Bytes()
 	}
 
-	rs := vote.RoundStats{
-		Duration:           roundStats.Duration,
-		RandUsed:           roundStats.RandUsed,
-		ActiveVoteContexts: roundStats.ActiveVoteContexts,
-	}
+	chunks := splitFPCVoteContext(roundStats.ActiveVoteContexts)
 
-	hb := &packet.FPCHeartbeat{
-		OwnID:      nodeID,
-		RoundStats: rs,
-	}
+	connLock.Lock()
+	defer connLock.Unlock()
 
-	data, err := packet.NewFPCHeartbeatMessage(hb)
-	if err != nil {
-		log.Info(err, " - FPC heartbeat message skipped")
-		return
+	for _, chunk := range chunks {
+		rs := vote.RoundStats{
+			Duration:           roundStats.Duration,
+			RandUsed:           roundStats.RandUsed,
+			ActiveVoteContexts: chunk,
+		}
+
+		hb := &packet.FPCHeartbeat{
+			OwnID:      nodeID,
+			RoundStats: rs,
+		}
+
+		data, err := packet.NewFPCHeartbeatMessage(hb)
+		if err != nil {
+			log.Info(err, " - FPC heartbeat message skipped")
+			return
+		}
+
+		log.Info("Client: onRoundExecuted data size: ", len(data))
+
+		if _, err = managedConn.Write(data); err != nil {
+			log.Debugw("Error while writing to connection", "Description", err)
+			return
+		}
 	}
+}
 
-	log.Info("Client: onRoundExecuted data size: ", len(data))
+func splitFPCVoteContext(ctx map[string]*vote.Context) (chunk []map[string]*vote.Context) {
+	chunk = make([]map[string]*vote.Context, 1)
+	i, counter := 0, 0
+	chunk[i] = make(map[string]*vote.Context)
 
-	connLock.Lock()
-	defer connLock.Unlock()
-	if _, err = managedConn.Write(data); err != nil {
-		log.Debugw("Error while writing to connection", "Description", err)
+	if len(ctx) < maxVoteContext {
+		chunk[i] = ctx
 		return
 	}
+
+	for conflictID, voteCtx := range ctx {
+		counter++
+		if counter >= maxVoteContext {
+			counter = 0
+			i++
+			chunk = append(chunk, make(map[string]*vote.Context))
+		}
+		chunk[i][conflictID] = voteCtx
+	}
+
+	return
 }
-- 
GitLab