/
directoryBlockSignature.go
384 lines (321 loc) · 9.58 KB
/
directoryBlockSignature.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package messages
import (
"encoding/binary"
"fmt"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/directoryBlock"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
type DirectoryBlockSignature struct {
MessageBase
Timestamp interfaces.Timestamp
DBHeight uint32
//DirectoryBlockKeyMR interfaces.IHash
DirectoryBlockHeader interfaces.IDirectoryBlockHeader
ServerIdentityChainID interfaces.IHash
Signature interfaces.IFullSignature
// Signature that goes into the admin block
// Signature of directory block header
DBSignature interfaces.IFullSignature
SysHeight uint32
SysHash interfaces.IHash
//Not marshalled
Matches bool
hash interfaces.IHash
}
var _ interfaces.IMsg = (*DirectoryBlockSignature)(nil)
var _ Signable = (*DirectoryBlockSignature)(nil)
func (a *DirectoryBlockSignature) IsSameAs(b *DirectoryBlockSignature) bool {
if b == nil {
return false
}
if a.Timestamp.GetTimeMilli() != b.Timestamp.GetTimeMilli() {
return false
}
if a.DBHeight != b.DBHeight {
return false
}
if a.DirectoryBlockHeader == nil && b.DirectoryBlockHeader != nil {
return false
}
if a.DirectoryBlockHeader != nil {
if a.DirectoryBlockHeader.GetPrevFullHash().IsSameAs(b.DirectoryBlockHeader.GetPrevFullHash()) == false {
return false
}
}
if a.ServerIdentityChainID == nil && b.ServerIdentityChainID != nil {
return false
}
if a.ServerIdentityChainID != nil {
if a.ServerIdentityChainID.IsSameAs(b.ServerIdentityChainID) == false {
return false
}
}
if a.Signature == nil && b.Signature != nil {
return false
}
if a.Signature != nil {
if a.Signature.IsSameAs(b.Signature) == false {
return false
}
}
return true
}
func (e *DirectoryBlockSignature) Process(dbheight uint32, state interfaces.IState) bool {
return state.ProcessDBSig(dbheight, e)
}
func (m *DirectoryBlockSignature) GetRepeatHash() interfaces.IHash {
if m.RepeatHash == nil {
data, err := m.MarshalBinary()
if err != nil {
return nil
}
m.RepeatHash = primitives.Sha(data)
}
return m.RepeatHash
}
func (m *DirectoryBlockSignature) GetHash() interfaces.IHash {
return m.GetMsgHash()
}
func (m *DirectoryBlockSignature) GetMsgHash() interfaces.IHash {
if m.MsgHash == nil {
data, _ := m.MarshalForSignature()
if data == nil {
return nil
}
m.MsgHash = primitives.Sha(data)
}
return m.MsgHash
}
func (m *DirectoryBlockSignature) GetTimestamp() interfaces.Timestamp {
if m.Timestamp == nil {
m.Timestamp = new(primitives.Timestamp)
}
return m.Timestamp
}
func (m *DirectoryBlockSignature) Type() byte {
return constants.DIRECTORY_BLOCK_SIGNATURE_MSG
}
// Validate the message, given the state. Three possible results:
// < 0 -- Message is invalid. Discard
// 0 -- Cannot tell if message is Valid
// 1 -- Message is valid
func (m *DirectoryBlockSignature) Validate(state interfaces.IState) int {
if m.IsValid() {
return 1
}
raw, _ := m.MarshalBinary()
if m.DBHeight <= state.GetHighestSavedBlk() {
state.Logf("error", "DirectoryBlockSignature: Fail dbstate ht: %v < dbht: %v %s\n [%s] RAW: %x", m.DBHeight, state.GetHighestSavedBlk(), m.String(), m.GetMsgHash().String(), raw)
return -1
}
found, _ := state.GetVirtualServers(m.DBHeight, 9, m.ServerIdentityChainID)
if found == false {
state.AddStatus(fmt.Sprintf("DirectoryBlockSignature: Fail dbht: %v Server not found %x %s",
state.GetLLeaderHeight(),
m.ServerIdentityChainID.Bytes()[3:5],
m.String()))
return 0
}
if m.IsLocal() {
m.SetValid()
return 1
}
isVer, err := m.VerifySignature()
if err != nil || !isVer {
state.Logf("error", "DirectoryBlockSignature: Fail to Verify Sig dbht: %v %s\n [%s] RAW: %x", state.GetLLeaderHeight(), m.String(), m.GetMsgHash().String(), raw)
// if there is an error during signature verification
// or if the signature is invalid
// the message is considered invalid
return -1
}
marshalledMsg, _ := m.MarshalForSignature()
authorityLevel, err := state.VerifyAuthoritySignature(marshalledMsg, m.Signature.GetSignature(), m.DBHeight)
if err != nil || authorityLevel < 1 {
//This authority is not a Fed Server (it's either an Audit or not an Authority at all)
state.Logf("error", "DirectoryBlockSignature: Fail to Verify Sig (not from a Fed Server) dbht: %v %s\n [%s] RAW: %x", state.GetLLeaderHeight(), m.String(), m.GetMsgHash().String(), raw)
state.AddStatus(fmt.Sprintf("DirectoryBlockSignature: Fail to Verify Sig (not from a Fed Server) dbht: %v %s", state.GetLLeaderHeight(), m.String()))
return authorityLevel
}
state.Logf("info", "DirectoryBlockSignature: VALID dbht: %v %s. MsgHash: %s\n [%s] RAW: %x ", state.GetLLeaderHeight(), m.String(), m.GetMsgHash().String(), m.GetMsgHash().String(), raw)
m.SetValid()
return 1
}
// Returns true if this is a message for this server to execute as
// a leader.
func (m *DirectoryBlockSignature) ComputeVMIndex(state interfaces.IState) {
}
// Execute the leader functions of the given message
func (m *DirectoryBlockSignature) LeaderExecute(state interfaces.IState) {
state.LeaderExecuteDBSig(m)
}
func (m *DirectoryBlockSignature) FollowerExecute(state interfaces.IState) {
state.FollowerExecuteMsg(m)
}
func (m *DirectoryBlockSignature) Sign(key interfaces.Signer) error {
// Signature that goes into admin block
err := m.SignHeader(key)
if err != nil {
return err
}
signature, err := SignSignable(m, key)
if err != nil {
return err
}
m.Signature = signature
return nil
}
func (m *DirectoryBlockSignature) SignHeader(key interfaces.Signer) error {
header, err := m.DirectoryBlockHeader.MarshalBinary()
if err != nil {
return err
}
m.DBSignature = key.Sign(header)
return nil
}
func (m *DirectoryBlockSignature) GetSignature() interfaces.IFullSignature {
return m.Signature
}
func (m *DirectoryBlockSignature) VerifySignature() (bool, error) {
return VerifyMessage(m)
}
func (m *DirectoryBlockSignature) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
if newData[0] != m.Type() {
return nil, fmt.Errorf("Invalid Message type")
}
newData = newData[1:]
// TimeStamp
m.Timestamp = new(primitives.Timestamp)
newData, err = m.Timestamp.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.SysHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
hash := new(primitives.Hash)
newData, err = hash.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.SysHash = hash
m.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.VMIndex, newData = int(newData[0]), newData[1:]
header := directoryBlock.NewDBlockHeader()
newData, err = header.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.DirectoryBlockHeader = header
hash = new(primitives.Hash)
newData, err = hash.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.ServerIdentityChainID = hash
//if len(newData) > 0 {
sig := new(primitives.Signature)
newData, err = sig.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.DBSignature = sig
//}
if len(newData) > 0 {
sig := new(primitives.Signature)
newData, err = sig.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.Signature = sig
}
return nil, nil
}
func (m *DirectoryBlockSignature) UnmarshalBinary(data []byte) error {
_, err := m.UnmarshalBinaryData(data)
return err
}
func (m *DirectoryBlockSignature) MarshalForSignature() ([]byte, error) {
if m.DirectoryBlockHeader == nil {
m.DirectoryBlockHeader = directoryBlock.NewDBlockHeader()
}
var buf primitives.Buffer
buf.Write([]byte{m.Type()})
t := m.GetTimestamp()
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
binary.Write(&buf, binary.BigEndian, m.SysHeight)
if m.SysHash == nil {
m.SysHash = primitives.NewZeroHash()
}
hash, err := m.SysHash.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(hash)
binary.Write(&buf, binary.BigEndian, m.DBHeight)
binary.Write(&buf, binary.BigEndian, byte(m.VMIndex))
header, err := m.DirectoryBlockHeader.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(header)
hash, err = m.ServerIdentityChainID.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(hash)
if m.DBSignature != nil {
dbSig, err := m.DBSignature.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(dbSig)
} else {
blankSig := make([]byte, constants.SIGNATURE_LENGTH)
buf.Write(blankSig)
buf.Write(blankSig[:32])
}
return buf.DeepCopyBytes(), nil
}
func (m *DirectoryBlockSignature) MarshalBinary() (data []byte, err error) {
var sig interfaces.IFullSignature
resp, err := m.MarshalForSignature()
if err == nil {
sig = m.GetSignature()
}
if sig != nil {
sigBytes, err := sig.MarshalBinary()
if err != nil {
return resp, nil
}
return append(resp, sigBytes...), nil
}
return resp, nil
}
func (m *DirectoryBlockSignature) String() string {
return fmt.Sprintf("%6s-VM%3d: DBHt:%5d -- Signer[:3]=%x PrevDBKeyMR[:3]=%x hash[:3]=%x",
"DBSig",
m.VMIndex,
m.DBHeight,
m.ServerIdentityChainID.Bytes()[2:6],
m.DirectoryBlockHeader.GetPrevKeyMR().Bytes()[:3],
m.GetHash().Bytes()[:3])
}
func (e *DirectoryBlockSignature) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *DirectoryBlockSignature) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}