-
Notifications
You must be signed in to change notification settings - Fork 148
/
var_aux_vals.go
141 lines (130 loc) · 3.84 KB
/
var_aux_vals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package mostefaoui
import (
"fmt"
"github.com/iotaledger/wasp/packages/gpa"
)
// Here we implement the derivation of the `vals` (auxVals) variable in the following:
//
// > ∗ wait until at least (N − f) AUX_r messages have been
// > received, such that the set of values carried by these
// > messages, vals are a subset of bin_values_r (note that
// > bin_values_r may continue to change as BVAL_r messages
// > are received, thus this condition may be triggered upon
// > arrival of either an AUX_r or a BVAL_r message)
//
// For this we have to get updates to the binValues variable and exchange the AUX messages.
type varAuxVals struct {
n int
f int
nodeIDs []gpa.NodeID
recv map[gpa.NodeID]bool
readyCB func(auxVals []bool) gpa.OutMessages
ready bool
round int
sent bool
binValues []bool
}
func newAuxVals(nodeIDs []gpa.NodeID, f int, readyCB func(auxVals []bool) gpa.OutMessages) *varAuxVals {
v := &varAuxVals{
n: len(nodeIDs),
f: f,
nodeIDs: nodeIDs,
recv: map[gpa.NodeID]bool{},
readyCB: readyCB,
ready: false,
round: -1,
sent: false,
binValues: nil,
}
return v
}
func (v *varAuxVals) startRound(round int) {
v.recv = map[gpa.NodeID]bool{}
v.ready = false
v.round = round
v.sent = false
v.binValues = nil
}
// > – wait until bin_values_r != {}, then
// > ∗ multicast AUX_r(w) where w ∈ bin_values_r
// ...
// > (note that
// > bin_values_r may continue to change as BVAL_r messages
// > are received, thus this condition may be triggered upon
// > arrival of either an AUX_r or a BVAL_r message)
func (v *varAuxVals) binValuesUpdated(binValues []bool) gpa.OutMessages {
msgs := gpa.NoMessages()
if len(binValues) == 1 {
msgs.AddAll(v.multicast(binValues[0]))
}
v.binValues = binValues
return msgs.AddAll(v.tryOutput())
}
// > ∗ wait until at least (N − f) AUX_r messages have been
// > received, such that the set of values carried by these
// > messages, vals are a subset of bin_values_r ...
func (v *varAuxVals) msgVoteAUXReceived(msg *msgVote) gpa.OutMessages {
if _, ok := v.recv[msg.sender]; ok {
return nil // Duplicate.
}
v.recv[msg.sender] = msg.value
return v.tryOutput()
}
// > ∗ wait until at least (N − f) AUX_r messages have been
// > received, such that the set of values carried by these
// > messages, vals are a subset of bin_values_r (note that
// > bin_values_r may continue to change as BVAL_r messages
// > are received, thus this condition may be triggered upon
// > arrival of either an AUX_r or a BVAL_r message)
func (v *varAuxVals) tryOutput() gpa.OutMessages {
if v.ready || len(v.recv) < v.n-v.f || v.binValues == nil {
return nil
}
hasBinValsT := false
hasBinValsF := false
for _, b := range v.binValues {
if b {
hasBinValsT = true
} else {
hasBinValsF = true
}
}
count := 0
hasAuxValsT := false
hasAuxValsF := false
for _, vote := range v.recv {
if vote && hasBinValsT {
count++
hasAuxValsT = true
continue
}
if !vote && hasBinValsF {
hasAuxValsF = true
count++
}
}
if count >= v.n-v.f {
auxVals := make([]bool, 0, 2)
if hasAuxValsT {
auxVals = append(auxVals, true)
}
if hasAuxValsF {
auxVals = append(auxVals, false)
}
v.ready = true
return v.readyCB(auxVals)
}
return nil
}
func (v *varAuxVals) multicast(value bool) gpa.OutMessages {
if v.sent {
return nil
}
v.sent = true
return multicastMsgVote(v.nodeIDs, v.round, AUX, value)
}
func (v *varAuxVals) statusString() string {
return fmt.Sprintf("AUX(N=%v,recv=%v)", len(v.nodeIDs), len(v.recv))
}