forked from FactomProject/factomd
/
ack.go
494 lines (401 loc) · 11.8 KB
/
ack.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// 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"
"os"
"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"
llog "github.com/FactomProject/factomd/log"
log "github.com/sirupsen/logrus"
)
// packageLogger is the general logger for all message related logs. You can add additional fields,
// or create more context loggers off of this
var packageLogger = log.WithFields(log.Fields{"package": "messages"})
//General acknowledge message
type Ack struct {
msgbase.MessageBase
Timestamp interfaces.Timestamp // Timestamp of Ack by Leader
Salt [8]byte // Eight bytes of the salt
SaltNumber uint32 // Secret Number used to detect multiple servers with the same ID
MessageHash 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
SerialHash interfaces.IHash // Serial hash including previous ack
DataAreaSize uint64 // Size of the Data Area
DataArea []byte // Data Area
Signature interfaces.IFullSignature
//Not marshalled
hash interfaces.IHash
authvalid bool
Response bool // A response to a missing data request
BalanceHash interfaces.IHash
marshalCache []byte
}
var _ interfaces.IMsg = (*Ack)(nil)
var _ interfaces.Signable = (*Ack)(nil)
var AckBalanceHash = true
func (m *Ack) GetRepeatHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "Ack.GetRepeatHash") }()
return m.GetMsgHash()
}
// We have to return the hash of the underlying message.
func (m *Ack) GetHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "Ack.GetHash") }()
return m.MessageHash
}
func (m *Ack) GetMsgHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "Ack.GetMsgHash") }()
if m.MsgHash == nil {
data, err := m.MarshalForSignature()
if err != nil {
return nil
}
m.MsgHash = primitives.Sha(data)
}
return m.MsgHash
}
func (m *Ack) Type() byte {
return constants.ACK_MSG
}
func (m *Ack) GetTimestamp() interfaces.Timestamp {
return m.Timestamp.Clone()
}
func (m *Ack) VerifySignature() (bool, error) {
return msgbase.VerifyMessage(m)
}
// 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 *Ack) Validate(s interfaces.IState) int {
// If too old, it isn't valid.
if m.DBHeight < s.GetLLeaderHeight() {
s.LogMessage("executeMsg", "drop, from past", m)
return -1
}
// Update the highest known ack to start requesting
// DBState blocks if necessary
if s.GetHighestAck() < m.DBHeight {
s.SetHighestAck(m.DBHeight)
}
// drop future acks that are far in the future and not near the block we expect to build as soon as the boot finishes.
if m.DBHeight-s.GetLLeaderHeight() > 5 && m.DBHeight != s.GetHighestKnownBlock() && m.DBHeight != s.GetHighestKnownBlock()+1 {
s.LogMessage("executeMsg", "drop, from far future", m)
return -1
}
if m.DBHeight > s.GetLLeaderHeight() {
return s.HoldForHeight(m.DBHeight, 0, m) // release the ACKs at the start of minute 0 of their block
}
// Only new acks are valid. Of course, the VMIndex has to be valid too.
msg, _ := s.GetMsg(m.VMIndex, int(m.DBHeight), int(m.Height))
if msg != nil {
if !msg.GetMsgHash().IsSameAs(m.GetHash()) {
s.LogMessage("executeMsg", "Ack slot taken", m)
s.LogMessage("executeMsg", "found:", msg)
} else {
s.LogPrintf("executeMsg", "duplicate at %d/%d/%d", int(m.DBHeight), m.VMIndex, int(m.Height))
}
return -1
}
if !m.authvalid {
// Check signature
bytes, err := m.MarshalForSignature()
if err != nil {
s.LogPrintf("executeMsg", "Validate Marshal Failed %v", err)
//fmt.Println("Err is not nil on Ack sig check: ", err)
return -1
}
ackSigned, err := s.FastVerifyAuthoritySignature(bytes, m.Signature, m.DBHeight)
//ackSigned, err := m.VerifySignature()
if err != nil {
s.LogPrintf("executeMsg", "VerifyAuthoritySignature Failed %v", err)
// Don't return fail here because the message might be a future message and thus become valid in the future.
}
if ackSigned <= 0 {
if m.DBHeight == s.GetLLeaderHeight() && m.Minute < byte(s.GetCurrentMinute()) {
s.LogPrintf("executeMsg", "Hold, Not signed by a leader")
return 0
} else {
s.LogPrintf("executeMsg", "Drop, Not signed by a leader")
return -1
}
}
}
m.authvalid = true
s.LogMessage("executeMsg", "Valid", m)
return 1
}
// Returns true if this is a message for this server to execute as
// a leader.
func (m *Ack) ComputeVMIndex(state interfaces.IState) {
}
// Execute the leader functions of the given message
// Leader, follower, do the same thing.
func (m *Ack) LeaderExecute(state interfaces.IState) {
m.FollowerExecute(state)
}
func (m *Ack) FollowerExecute(state interfaces.IState) {
state.FollowerExecuteAck(m)
}
// Acknowledgements do not go into the process list.
func (e *Ack) Process(dbheight uint32, state interfaces.IState) bool {
panic("Ack object should never have its Process() method called")
}
func (e *Ack) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *Ack) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (m *Ack) Sign(key interfaces.Signer) error {
signature, err := msgbase.SignSignable(m, key)
if err != nil {
return err
}
m.Signature = signature
return nil
}
func (m *Ack) GetSignature() interfaces.IFullSignature {
return m.Signature
}
func (m *Ack) 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)
}
}()
newData = data
if newData[0] != m.Type() {
return nil, fmt.Errorf("Invalid Message type")
}
newData = newData[1:]
m.VMIndex, newData = int(newData[0]), newData[1:]
m.Timestamp = new(primitives.Timestamp)
newData, err = m.Timestamp.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
copy(m.Salt[:], newData[:8])
newData = newData[8:]
m.SaltNumber, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.MessageHash = new(primitives.Hash)
newData, err = m.MessageHash.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
newData, err = m.GetFullMsgHash().UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.LeaderChainID = new(primitives.Hash)
newData, err = m.LeaderChainID.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.Height, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.Minute, newData = newData[0], newData[1:]
if m.SerialHash == nil {
m.SerialHash = primitives.NewHash(constants.ZERO_HASH)
}
newData, err = m.SerialHash.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
if AckBalanceHash {
m.DataAreaSize, newData = primitives.DecodeVarInt(newData)
if m.DataAreaSize > 0 {
das := newData[:int(m.DataAreaSize)]
lenb := uint64(0)
for len(das) > 0 {
typeb := das[0]
lenb, das = primitives.DecodeVarInt(das[1:])
switch typeb {
case 1:
m.BalanceHash = primitives.NewHash(das[:32])
}
das = das[lenb:]
}
m.DataArea = append(m.DataArea[:0], newData[:m.DataAreaSize]...)
newData = newData[int(m.DataAreaSize):]
}
}
b, newData := newData[0], newData[1:]
if b > 0 {
m.Signature = new(primitives.Signature)
newData, err = m.Signature.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
}
m.marshalCache = append(m.marshalCache, data[:len(data)-len(newData)]...)
return
}
func (m *Ack) UnmarshalBinary(data []byte) error {
_, err := m.UnmarshalBinaryData(data)
return err
}
func (m *Ack) MarshalForSignature() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "Ack.MarshalForSignature err:%v", *pe)
}
}(&err)
var buf primitives.Buffer
binary.Write(&buf, binary.BigEndian, m.Type())
binary.Write(&buf, binary.BigEndian, byte(m.VMIndex))
t := m.GetTimestamp()
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
buf.Write(m.Salt[:8])
binary.Write(&buf, binary.BigEndian, m.SaltNumber)
data, err = m.MessageHash.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
data, err = m.GetFullMsgHash().MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
data, err = m.LeaderChainID.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
binary.Write(&buf, binary.BigEndian, m.DBHeight)
binary.Write(&buf, binary.BigEndian, m.Height)
binary.Write(&buf, binary.BigEndian, m.Minute)
data, err = m.SerialHash.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
if AckBalanceHash {
if m.BalanceHash == nil {
primitives.EncodeVarInt(&buf, 0)
m.DataArea = nil
} else {
// Figure out all the data we are going to write out.
var area primitives.Buffer
area.WriteByte(1)
primitives.EncodeVarInt(&area, 32)
area.Write(m.BalanceHash.Bytes())
// Write out the size of said data, and then the data.
m.DataAreaSize = uint64(len(area.Bytes()))
primitives.EncodeVarInt(&buf, m.DataAreaSize)
buf.Write(area.Bytes())
}
}
return buf.DeepCopyBytes(), nil
}
func (m *Ack) MarshalBinary() (data []byte, err error) {
if m.marshalCache != nil {
return m.marshalCache, nil
}
resp, err := m.MarshalForSignature()
if err != nil {
return nil, err
}
sig := m.GetSignature()
if sig != nil {
resp = append(resp, 1)
sigBytes, err := sig.MarshalBinary()
if err != nil {
return nil, err
}
return append(resp, sigBytes...), nil
} else {
resp = append(resp, 0)
}
return resp, nil
}
func (m *Ack) String() string {
return fmt.Sprintf("%6s-%27s -- Leader[%x] hash[%x]",
"ACK",
fmt.Sprintf("DBh/VMh/h %d/%d/%d ", m.DBHeight, m.VMIndex, m.Height),
m.LeaderChainID.Bytes()[3:6],
m.GetHash().Bytes()[:3])
}
func (m *Ack) LogFields() log.Fields {
return log.Fields{"category": "message", "messagetype": "ack", "dbheight": m.DBHeight, "vm": m.VMIndex,
"vmheight": m.Height, "server": m.LeaderChainID.String(),
"hash": m.GetHash().String()}
}
func (a *Ack) IsSameAs(b *Ack) bool {
if a == nil && b == nil {
return true
}
if a == nil {
return false
}
if a.VMIndex != b.VMIndex {
return false
}
if a.Minute != b.Minute {
return false
}
if a.DBHeight != b.DBHeight {
return false
}
if a.Height != b.Height {
return false
}
if a.Timestamp.GetTimeMilli() != b.Timestamp.GetTimeMilli() {
return false
}
if a.Salt != b.Salt {
return false
}
if a.SaltNumber != b.SaltNumber {
return false
}
if a.GetFullMsgHash().IsSameAs(b.GetFullMsgHash()) == false {
return false
}
if a.MessageHash.IsSameAs(b.MessageHash) == false {
return false
}
if a.SerialHash.IsSameAs(b.SerialHash) == false {
return false
}
if a.DataAreaSize != b.DataAreaSize {
return false
}
if a.Signature != nil {
if a.Signature.IsSameAs(b.Signature) == false {
return false
}
}
if a.LeaderChainID == nil && b.LeaderChainID != nil {
return false
}
if a.LeaderChainID != nil {
if a.LeaderChainID.IsSameAs(b.LeaderChainID) == false {
return false
}
}
if a.BalanceHash == nil && b.BalanceHash != nil {
return false
}
if b.BalanceHash == nil && a.BalanceHash != nil {
return false
}
if a.BalanceHash == nil && b.BalanceHash == nil {
return true
}
if a.BalanceHash.Fixed() != b.BalanceHash.Fixed() {
return false
}
return true
}