/
removeAuditInternal.go
175 lines (142 loc) · 4.74 KB
/
removeAuditInternal.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
// 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"
llog "github.com/FactomProject/factomd/log"
log "github.com/sirupsen/logrus"
)
//General acknowledge message
type RemoveAuditInternal struct {
msgbase.MessageBase
NName string
ServerID interfaces.IHash // Hash of message acknowledged
DBHeight uint32 // Directory Block Height that owns this ack
Height uint32 // Height of this ack in this process list
}
var _ interfaces.IMsg = (*RemoveAuditInternal)(nil)
var _ interfaces.IElectionMsg = (*RemoveAuditInternal)(nil)
func (m *RemoveAuditInternal) MarshalBinary() (data []byte, err error) {
var buf primitives.Buffer
if err = buf.PushByte(constants.INTERNALREMOVEAUDIT); err != nil {
return nil, err
}
if e := buf.PushIHash(m.ServerID); e != nil {
return nil, e
}
if e := buf.PushInt(int(m.DBHeight)); e != nil {
return nil, e
}
if e := buf.PushByte(m.Minute); e != nil {
return nil, e
}
if e := buf.PushByte(m.Minute); e != nil {
return nil, e
}
data = buf.Bytes()
return data, nil
}
func (m *RemoveAuditInternal) GetMsgHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "RemoveAuditInternal.GetMsgHash") }()
if m.MsgHash == nil {
data, err := m.MarshalBinary()
if err != nil {
return nil
}
m.MsgHash = primitives.Sha(data)
}
return m.MsgHash
}
func (m *RemoveAuditInternal) ElectionProcess(state interfaces.IState, elect interfaces.IElections) {
e, ok := elect.(*elections.Elections)
if !ok {
panic("Invalid elections object")
}
idx := -1
for i, s := range e.Audit {
if s.GetChainID().IsSameAs(m.GetServerID()) {
idx = i
break
}
}
if idx != -1 {
e.Audit = append(e.Audit[:idx], e.Audit[idx+1:]...)
}
}
func (m *RemoveAuditInternal) GetServerID() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "RemoveAuditInternal.GetServerID") }()
return m.ServerID
}
func (m *RemoveAuditInternal) LogFields() log.Fields {
return log.Fields{"category": "message", "messagetype": "RemoveAuditInternal", "dbheight": m.DBHeight, "newleader": m.ServerID.String()[4:12]}
}
func (m *RemoveAuditInternal) GetRepeatHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "RemoveAuditInternal.GetRepeatHash") }()
return m.GetMsgHash()
}
// We have to return the hash of the underlying message.
func (m *RemoveAuditInternal) GetHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "RemoveAuditInternal.GetHash") }()
return m.GetMsgHash()
}
func (m *RemoveAuditInternal) GetTimestamp() interfaces.Timestamp {
return primitives.NewTimestampNow()
}
func (m *RemoveAuditInternal) Type() byte {
return constants.INTERNALREMOVEAUDIT
}
func (m *RemoveAuditInternal) Validate(state interfaces.IState) int {
return 1
}
func (m *RemoveAuditInternal) ElectionValidate(ie interfaces.IElections) int {
return 1
}
// Returns true if this is a message for this server to execute as
// a leader.
func (m *RemoveAuditInternal) ComputeVMIndex(state interfaces.IState) {
}
// Execute the leader functions of the given message
// Leader, follower, do the same thing.
func (m *RemoveAuditInternal) LeaderExecute(state interfaces.IState) {
m.FollowerExecute(state)
}
func (m *RemoveAuditInternal) FollowerExecute(state interfaces.IState) {
}
// Acknowledgements do not go into the process list.
func (e *RemoveAuditInternal) Process(dbheight uint32, state interfaces.IState) bool {
panic("Ack object should never have its Process() method called")
}
func (e *RemoveAuditInternal) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *RemoveAuditInternal) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (m *RemoveAuditInternal) 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
}
func (m *RemoveAuditInternal) UnmarshalBinary(data []byte) error {
_, err := m.UnmarshalBinaryData(data)
return err
}
func (m *RemoveAuditInternal) String() string {
if m.LeaderChainID == nil {
m.LeaderChainID = primitives.NewZeroHash()
}
return fmt.Sprintf("%20s %x %10s dbheight %d", "Remove Audit Internal", m.ServerID.Bytes(), m.NName, m.DBHeight)
}
func (a *RemoveAuditInternal) IsSameAs(b *RemoveAuditInternal) bool {
return true
}