/
startElectionInternal.go
181 lines (145 loc) · 5.37 KB
/
startElectionInternal.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package electionMsgs
import (
"fmt"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages/msgbase"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/elections"
"github.com/FactomProject/factomd/state"
llog "github.com/FactomProject/factomd/log"
log "github.com/sirupsen/logrus"
)
//General acknowledge message
type StartElectionInternal struct {
msgbase.MessageBase
VMHeight int
DBHeight uint32
PreviousDBHash interfaces.IHash
SigType bool
IsLeader bool
}
var _ interfaces.IMsg = (*StartElectionInternal)(nil)
var _ interfaces.IElectionMsg = (*StartElectionInternal)(nil)
func (m *StartElectionInternal) ElectionProcess(s interfaces.IState, elect interfaces.IElections) {
e := elect.(*elections.Elections)
// If the electing is set to -1, that election has ended before we got to start it.
// Still trigger the Fault loop, it will self terminate if we've moved forward
if e.Electing == -1 {
go Fault(e, e.DBHeight, e.Minute, e.FaultId.Load(), &e.FaultId, m.SigType, e.RoundTimeout)
return
}
e.Adapter = NewElectionAdapter(e, m.PreviousDBHash)
s.LogPrintf("election", "Create Election Adapter")
// An election that finishes may make us a leader. We need to know that for the next election that
// takes place. So use the election's list of fed servers to determine if we are a leader
for _, id := range e.Federated {
if id.GetChainID().IsSameAs(s.GetIdentityChainID()) {
e.Adapter.SetObserver(false)
break
}
e.Adapter.SetObserver(true)
}
go Fault(e, e.DBHeight, e.Minute, e.FaultId.Load(), &e.FaultId, m.SigType, e.RoundTimeout)
}
// Execute the leader functions of the given message
// Leader, follower, do the same thing.
func (m *StartElectionInternal) LeaderExecute(state interfaces.IState) {
m.FollowerExecute(state)
}
func (m *StartElectionInternal) FollowerExecute(is interfaces.IState) {
s := is.(*state.State)
m.IsLeader = is.IsLeader()
// TODO: State related things about starting an election
pl := s.ProcessLists.Get(m.DBHeight)
if pl == nil {
//s.Holding[m.GetHash().Fixed()] = m
s.AddToHolding(m.GetMsgHash().Fixed(), m) // StartElectionInternal.FollowerExecute
return
}
vm := pl.VMs[m.VMIndex]
if vm == nil {
return
}
// Process all the messages that we can
for s.LeaderPL.Process(s) {
}
m.VMHeight = vm.Height
// Send to elections
is.ElectionsQueue().Enqueue(m)
}
func (m *StartElectionInternal) ElectionValidate(ie interfaces.IElections) int {
if int(m.DBHeight) < ie.(*elections.Elections).DBHeight {
return -1
}
return 1
}
func (m *StartElectionInternal) Validate(state interfaces.IState) int {
return 1
}
func (m *StartElectionInternal) MarshalBinary() (data []byte, err error) {
//var buf primitives.Buffer
return nil, fmt.Errorf("Not implmented for StartElectionInternal")
}
func (m *StartElectionInternal) GetMsgHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "StartElectionInternal.GetMsgHash") }()
// Internal messages don't have marshal code. Give them some hash to be happy
if m.MsgHash == nil {
m.MsgHash = primitives.RandomHash()
}
return m.MsgHash
}
func (m *StartElectionInternal) LogFields() log.Fields {
return log.Fields{"category": "message", "messagetype": "StartElectionInternal", "dbheight": m.DBHeight}
}
func (m *StartElectionInternal) GetRepeatHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "StartElectionInternal.GetRepeatHash") }()
return m.GetMsgHash()
}
// We have to return the hash of the underlying message.
func (m *StartElectionInternal) GetHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "StartElectionInternal.GetHash") }()
return m.GetMsgHash()
}
func (m *StartElectionInternal) GetTimestamp() interfaces.Timestamp {
return primitives.NewTimestampNow()
}
func (m *StartElectionInternal) Type() byte {
return constants.INTERNALSTARTELECTION
}
// Returns true if this is a message for this server to execute as
// a leader.
func (m *StartElectionInternal) ComputeVMIndex(state interfaces.IState) {
}
// Acknowledgements do not go into the process list.
func (e *StartElectionInternal) Process(dbheight uint32, state interfaces.IState) bool {
panic("Ack object should never have its Process() method called")
}
func (e *StartElectionInternal) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *StartElectionInternal) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (m *StartElectionInternal) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
llog.LogPrintf("recovery", "Error unmarshalling: %v", r)
}
}()
return nil, fmt.Errorf("Not implmented for StartElectionInternal")
}
func (m *StartElectionInternal) UnmarshalBinary(data []byte) error {
_, err := m.UnmarshalBinaryData(data)
return err
}
func (m *StartElectionInternal) String() string {
return fmt.Sprintf("%20s dbheight %d min %d vm %d", "Start Election Internal", m.DBHeight, int(m.Minute), m.VMIndex)
}
func (a *StartElectionInternal) IsSameAs(b *StartElectionInternal) bool {
return true
}