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

:lipstick: Fix json structs

parent 7bab19e8
No related branches found
No related tags found
No related merge requests found
......@@ -3,52 +3,52 @@ package dashboard
// conflictSet is defined as a a map of conflict IDs and their conflict.
type conflictSet = map[string]conflict
// Conflict defines the struct for the opinions of the nodes regarding a given conflict.
// conflict defines the struct for the opinions of the nodes regarding a given conflict.
type conflict struct {
nodesView map[string]voteContext `json:"nodesview"`
NodesView map[string]voteContext `json:"nodesview"`
}
type voteContext struct {
nodeID string `json:"nodeid"`
rounds int `json:"rounds"`
opinions []int32 `json:"opinions"`
status int32 `json:"status"`
NodeID string `json:"nodeid"`
Rounds int `json:"rounds"`
Opinions []int32 `json:"opinions"`
Status int32 `json:"status"`
}
func newConflict() conflict {
return conflict{
nodesView: make(map[string]voteContext),
NodesView: make(map[string]voteContext),
}
}
// isFinalized return true if all the nodes have finalized a given conflict.
// It also returns false if the given conflict has an empty nodesView.
func (c conflict) isFinalized() bool {
if len(c.nodesView) == 0 {
if len(c.NodesView) == 0 {
return false
}
count := 0
for _, context := range c.nodesView {
if context.status == liked || context.status == disliked {
for _, context := range c.NodesView {
if context.Status == liked || context.Status == disliked {
count++
}
}
return (count == len(c.nodesView))
return (count == len(c.NodesView))
}
// finalizationStatus returns the ratio of nodes that have finlized a given conflict.
func (c conflict) finalizationStatus() float64 {
if len(c.nodesView) == 0 {
if len(c.NodesView) == 0 {
return 0
}
count := 0
for _, context := range c.nodesView {
if context.status == liked || context.status == disliked {
for _, context := range c.NodesView {
if context.Status == liked || context.Status == disliked {
count++
}
}
return (float64(count) / float64(len(c.nodesView)))
return (float64(count) / float64(len(c.NodesView)))
}
......@@ -27,7 +27,7 @@ func (cr *conflictRecord) ToFPCUpdate() *FPCUpdate {
// }
return &FPCUpdate{
conflicts: cr.conflictSet,
Conflicts: cr.conflictSet,
}
}
......@@ -46,7 +46,7 @@ func (cr *conflictRecord) update(ID string, c conflict) {
}
}
for nodeID, context := range c.nodesView {
cr.conflictSet[ID].nodesView[nodeID] = context
for nodeID, context := range c.NodesView {
cr.conflictSet[ID].NodesView[nodeID] = context
}
}
......@@ -13,12 +13,12 @@ func TestConflictRecordUpdate(t *testing.T) {
// test first new update
conflictA := conflict{
nodesView: map[string]voteContext{
NodesView: map[string]voteContext{
"nodeA": {
nodeID: "nodeA",
rounds: 3,
opinions: []int32{disliked, liked, disliked},
status: liked,
NodeID: "nodeA",
Rounds: 3,
Opinions: []int32{disliked, liked, disliked},
Status: liked,
},
},
}
......@@ -30,12 +30,12 @@ func TestConflictRecordUpdate(t *testing.T) {
// test second new update
conflictB := conflict{
nodesView: map[string]voteContext{
NodesView: map[string]voteContext{
"nodeB": {
nodeID: "nodeB",
rounds: 3,
opinions: []int32{disliked, liked, disliked},
status: liked,
NodeID: "nodeB",
Rounds: 3,
Opinions: []int32{disliked, liked, disliked},
Status: liked,
},
},
}
......@@ -47,12 +47,12 @@ func TestConflictRecordUpdate(t *testing.T) {
// test modify existing entry
conflictB = conflict{
nodesView: map[string]voteContext{
NodesView: map[string]voteContext{
"nodeB": {
nodeID: "nodeB",
rounds: 4,
opinions: []int32{disliked, liked, disliked, liked},
status: liked,
NodeID: "nodeB",
Rounds: 4,
Opinions: []int32{disliked, liked, disliked, liked},
Status: liked,
},
},
}
......@@ -64,12 +64,12 @@ func TestConflictRecordUpdate(t *testing.T) {
// test last update and first update entry removal
conflictC := conflict{
nodesView: map[string]voteContext{
NodesView: map[string]voteContext{
"nodeC": {
nodeID: "nodeC",
rounds: 3,
opinions: []int32{disliked, liked, disliked},
status: liked,
NodeID: "nodeC",
Rounds: 3,
Opinions: []int32{disliked, liked, disliked},
Status: liked,
},
},
}
......
......@@ -14,17 +14,17 @@ func TestIsFinalized(t *testing.T) {
}{
{
conflict: conflict{
nodesView: map[string]voteContext{
"one": {status: liked},
"two": {status: disliked},
NodesView: map[string]voteContext{
"one": {Status: liked},
"two": {Status: disliked},
},
},
want: true,
},
{
conflict: conflict{
nodesView: map[string]voteContext{
"one": {status: liked},
NodesView: map[string]voteContext{
"one": {Status: liked},
"two": {},
},
},
......@@ -50,17 +50,17 @@ func TestFinalizationStatus(t *testing.T) {
}{
{
conflict: conflict{
nodesView: map[string]voteContext{
"one": {status: liked},
"two": {status: disliked},
NodesView: map[string]voteContext{
"one": {Status: liked},
"two": {Status: disliked},
},
},
want: 1,
},
{
conflict: conflict{
nodesView: map[string]voteContext{
"one": {status: liked},
NodesView: map[string]voteContext{
"one": {Status: liked},
"two": {},
},
},
......
......@@ -31,7 +31,7 @@ var (
// FPCUpdate contains an FPC update.
type FPCUpdate struct {
conflicts conflictSet `json:"conflictset"`
Conflicts conflictSet `json:"conflictset"`
}
func configureFPCLiveFeed() {
......@@ -74,27 +74,27 @@ func createFPCUpdate(hb *packet.FPCHeartbeat, recordEvent bool) *FPCUpdate {
nodeID := base58.Encode(hb.OwnID)
for ID, context := range hb.RoundStats.ActiveVoteContexts {
newVoteContext := voteContext{
nodeID: nodeID,
rounds: context.Rounds,
opinions: vote.ConvertOpinionsToInts32(context.Opinions),
NodeID: nodeID,
Rounds: context.Rounds,
Opinions: vote.ConvertOpinionsToInts32(context.Opinions),
}
// check conflict has been finalized
if _, ok := hb.Finalized[ID]; ok {
newVoteContext.status = vote.ConvertOpinionToInt32(hb.Finalized[ID])
newVoteContext.Status = vote.ConvertOpinionToInt32(hb.Finalized[ID])
}
conflicts[ID] = newConflict()
conflicts[ID].nodesView[nodeID] = newVoteContext
conflicts[ID].NodesView[nodeID] = newVoteContext
if recordEvent {
// update recorded events
recordedConflicts.update(ID, conflict{nodesView: map[string]voteContext{nodeID: newVoteContext}})
recordedConflicts.update(ID, conflict{NodesView: map[string]voteContext{nodeID: newVoteContext}})
}
}
return &FPCUpdate{
conflicts: conflicts,
Conflicts: conflicts,
}
}
......
......@@ -35,14 +35,14 @@ func TestCreateFPCUpdate(t *testing.T) {
// create a matching FPCUpdate
want := &FPCUpdate{
conflicts: conflictSet{
Conflicts: conflictSet{
"one": {
nodesView: map[string]voteContext{
NodesView: map[string]voteContext{
base58OwnID: {
nodeID: base58OwnID,
rounds: 3,
opinions: []int32{disliked, liked, disliked},
status: liked,
NodeID: base58OwnID,
Rounds: 3,
Opinions: []int32{disliked, liked, disliked},
Status: liked,
},
},
},
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment