Skip to content

Commit 5e26058

Browse files
committed
Assign unique ids the proposal groups
Before, they were referenced by their array index, but at the same time, the array was limited to 5 proposals groups and shifted, so a wrong proposal group could be accepted.
1 parent c1e1f89 commit 5e26058

File tree

7 files changed

+41
-14
lines changed

7 files changed

+41
-14
lines changed

frontend/src/proto/ssl_gc_state.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ export interface ProposalGroup {
208208
proposals?: Proposal[];
209209
/** Whether the proposal group was accepted */
210210
accepted?: boolean;
211+
/** unique id of the proposal group */
212+
id?: number;
211213
}
212214

213215
export interface TeamInfo {
@@ -376,6 +378,7 @@ export const ProposalGroup = {
376378
return {
377379
proposals: Array.isArray(object?.proposals) ? object.proposals.map((e: any) => Proposal.fromJSON(e)) : [],
378380
accepted: isSet(object.accepted) ? Boolean(object.accepted) : false,
381+
id: isSet(object.id) ? Number(object.id) : 0,
379382
};
380383
},
381384

@@ -387,6 +390,7 @@ export const ProposalGroup = {
387390
obj.proposals = [];
388391
}
389392
message.accepted !== undefined && (obj.accepted = message.accepted);
393+
message.id !== undefined && (obj.id = Math.round(message.id));
390394
return obj;
391395
},
392396
};

internal/app/engine/process_proposals.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
const maxProposals = 5
1111

1212
func (e *Engine) processProposals() {
13-
for i, group := range e.currentState.ProposalGroups {
13+
for _, group := range e.currentState.ProposalGroups {
1414
latestGameEvent := group.Proposals[len(group.Proposals)-1].GameEvent
1515
if *group.Accepted ||
1616
e.groupIsStillAcceptingProposals(group) ||
@@ -23,13 +23,12 @@ func (e *Engine) processProposals() {
2323

2424
if numProposals > majority {
2525
log.Printf("Majority of %v reached with %v out of %v for %v.", majority, numProposals, numAutoRefs, latestGameEvent.Type)
26-
groupId := uint32(i)
2726
acceptedBy := "Majority"
2827
e.Enqueue(&statemachine.Change{
2928
Origin: &changeOriginEngine,
3029
Change: &statemachine.Change_AcceptProposalGroupChange{
3130
AcceptProposalGroupChange: &statemachine.Change_AcceptProposalGroup{
32-
GroupId: &groupId,
31+
GroupId: group.Id,
3332
AcceptedBy: &acceptedBy,
3433
},
3534
},

internal/app/state/ssl_gc_state.pb.go

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/statemachine/change_acceptproposal.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import (
88

99
func (s *StateMachine) processChangeAcceptProposals(newState *state.State, change *Change_AcceptProposalGroup) (changes []*Change) {
1010

11-
numGroups := len(newState.ProposalGroups)
12-
if int(*change.GroupId) >= numGroups {
13-
log.Printf("Proposal group id %v invalid, there are only %v groups", *change.GroupId, numGroups)
11+
group := findGroupById(newState.ProposalGroups, *change.GroupId)
12+
if group == nil {
13+
log.Printf("Proposal group with id %v can not be accepted, as it was not found", *change.GroupId)
1414
return
1515
}
1616

17-
group := newState.ProposalGroups[*change.GroupId]
1817
majorityEvent := s.createMergedGameEvent(group.Proposals, change.AcceptedBy)
1918
changes = append(changes, &Change{
2019
Change: &Change_AddGameEventChange{
@@ -29,6 +28,15 @@ func (s *StateMachine) processChangeAcceptProposals(newState *state.State, chang
2928
return
3029
}
3130

31+
func findGroupById(groups []*state.ProposalGroup, id uint32) *state.ProposalGroup {
32+
for _, group := range groups {
33+
if *group.Id == id {
34+
return group
35+
}
36+
}
37+
return nil
38+
}
39+
3240
func (s *StateMachine) createMergedGameEvent(proposals []*state.Proposal, acceptedBy *string) *state.GameEvent {
3341
event := new(state.GameEvent)
3442
proto.Merge(event, proposals[0].GameEvent)

internal/app/statemachine/change_proposedgameevent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ func (s *StateMachine) processChangeAddProposal(newState *state.State, change *C
1313
if ok, pid := findGroup(change.Proposal, newState.ProposalGroups, s.gameConfig.AutoRefProposalTimeout); ok {
1414
newState.ProposalGroups[pid].Proposals = append(newState.ProposalGroups[pid].Proposals, change.Proposal)
1515
} else {
16+
groupId := s.nextProposalGroupId
17+
s.nextProposalGroupId++
1618
newState.ProposalGroups = append(newState.ProposalGroups, &state.ProposalGroup{
1719
Proposals: []*state.Proposal{change.Proposal},
1820
Accepted: new(bool),
21+
Id: &groupId,
1922
})
2023
}
2124

internal/app/statemachine/statemachine.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ var changeOriginStateMachine = "StateMachine"
1414

1515
// StateMachine describes the state machine that translates changes into new states
1616
type StateMachine struct {
17-
gameConfig config.Game
18-
Geometry config.Geometry
19-
stageTimes map[state.Referee_Stage]time.Duration
20-
rand *rand.Rand
21-
timeProvider timer.TimeProvider
17+
gameConfig config.Game
18+
Geometry config.Geometry
19+
stageTimes map[state.Referee_Stage]time.Duration
20+
rand *rand.Rand
21+
timeProvider timer.TimeProvider
22+
nextProposalGroupId uint32
2223
}
2324

2425
// NewStateMachine creates a new state machine

proto/ssl_gc_state.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ message ProposalGroup {
7575
repeated Proposal proposals = 1;
7676
// Whether the proposal group was accepted
7777
optional bool accepted = 2;
78+
// unique id of the proposal group
79+
optional uint32 id = 3;
7880
}
7981

8082
message TeamInfo {

0 commit comments

Comments
 (0)