-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbstateMissing.go
315 lines (259 loc) · 7.91 KB
/
dbstateMissing.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
// 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"
"reflect"
"github.com/DCNT-Hammer/dcnt/common/constants"
"github.com/DCNT-Hammer/dcnt/common/interfaces"
"github.com/DCNT-Hammer/dcnt/common/primitives"
"github.com/DCNT-Hammer/dcnt/common/messages/msgbase"
log "github.com/sirupsen/logrus"
)
// Communicate a Directory Block State
type DBStateMissing struct {
msgbase.MessageBase
Timestamp interfaces.Timestamp
DBHeightStart uint32 // First block missing
DBHeightEnd uint32 // Last block missing.
//Not signed!
}
var _ interfaces.IMsg = (*DBStateMissing)(nil)
func (a *DBStateMissing) IsSameAs(b *DBStateMissing) bool {
if b == nil {
return false
}
if a.Timestamp.GetTimeMilli() != b.Timestamp.GetTimeMilli() {
return false
}
if a.DBHeightStart != b.DBHeightStart {
return false
}
if a.DBHeightEnd != b.DBHeightEnd {
return false
}
return true
}
func (m *DBStateMissing) GetRepeatHash() (rval interfaces.IHash) {
defer func() {
if rval != nil && reflect.ValueOf(rval).IsNil() {
rval = nil // convert an interface that is nil to a nil interface
primitives.LogNilHashBug("DBStateMissing.GetRepeatHash() saw an interface that was nil")
}
}()
return m.GetMsgHash()
}
func (m *DBStateMissing) GetHash() (rval interfaces.IHash) {
defer func() {
if rval != nil && reflect.ValueOf(rval).IsNil() {
rval = nil // convert an interface that is nil to a nil interface
primitives.LogNilHashBug("DBStateMissing.GetHash() saw an interface that was nil")
}
}()
return m.GetMsgHash()
}
func (m *DBStateMissing) GetMsgHash() (rval interfaces.IHash) {
defer func() {
if rval != nil && reflect.ValueOf(rval).IsNil() {
rval = nil // convert an interface that is nil to a nil interface
primitives.LogNilHashBug("DBStateMissing.GetMsgHash() saw an interface that was nil")
}
}()
if m.MsgHash == nil {
data, err := m.MarshalBinary()
if err != nil {
return nil
}
m.MsgHash = primitives.Sha(data)
}
return m.MsgHash
}
func (m *DBStateMissing) Type() byte {
return constants.DBSTATE_MISSING_MSG
}
func (m *DBStateMissing) GetTimestamp() interfaces.Timestamp {
return m.Timestamp
}
// 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 *DBStateMissing) Validate(state interfaces.IState) int {
if m.DBHeightStart > m.DBHeightEnd {
return -1
}
return 1
}
func (m *DBStateMissing) ComputeVMIndex(state interfaces.IState) {
}
// Execute the leader functions of the given message
func (m *DBStateMissing) LeaderExecute(state interfaces.IState) {
m.FollowerExecute(state)
}
// Only send the same block again after 15 seconds.
func (m *DBStateMissing) send(dbheight uint32, state interfaces.IState) (msglen int) {
send := true
now := state.GetTimestamp()
sents := state.GetDBStatesSent()
var keeps []*interfaces.DBStateSent
for _, v := range sents {
if now.GetTimeSeconds()-v.Sent.GetTimeSeconds() < 10 {
if v.DBHeight == dbheight {
send = false
}
keeps = append(keeps, v)
}
}
if send {
msg, err := state.LoadDBState(dbheight)
if err != nil {
state.LogPrintf("executeMsg", "DBStateMissing.send() %v", err)
return
}
if msg == nil {
return
}
dbstatemsg := msg.(*DBStateMsg)
dbstatemsg.IsInDB = false // else validateSignatures would approve it automatically
if dbstatemsg.ValidateSignatures(state) != 1 {
return // the last DBState we have saved may not have any or all the signatures so we can't share
}
b, err := msg.MarshalBinary()
if err != nil {
state.LogPrintf("executeMsg", "DBStateMissing.send() %v", err)
return
}
msglen = len(b)
msg.SetOrigin(m.GetOrigin())
msg.SetNetworkOrigin(m.GetNetworkOrigin())
msg.SetNoResend(false)
msg.SendOut(state, msg)
state.IncDBStateAnswerCnt()
v := new(interfaces.DBStateSent)
v.DBHeight = dbheight
v.Sent = now
keeps = append(keeps, v)
state.SetDBStatesSent(keeps)
}
return
}
func NewEnd(inLen int, start uint32, end uint32) (s uint32, e uint32) {
switch {
case inLen > constants.INMSGQUEUE_HIGH:
return 0, 0
case inLen > constants.INMSGQUEUE_MED && end-start > constants.DBSTATE_REQUEST_LIM_MED:
end = start + constants.DBSTATE_REQUEST_LIM_MED
case end-start > constants.DBSTATE_REQUEST_LIM_HIGH:
end = start + constants.DBSTATE_REQUEST_LIM_HIGH
}
return start, end
}
func (m *DBStateMissing) FollowerExecute(state interfaces.IState) {
if state.NetworkOutMsgQueue().Length() > state.NetworkOutMsgQueue().Cap()*99/100 {
return
}
// TODO: Likely need to consider a limit on how many blocks we reply with. For now,
// just give them what they ask for.
start := m.DBHeightStart
end := m.DBHeightEnd
hsb := state.GetHighestSavedBlk()
// Can't serve up block we don't have
if start >= hsb {
return
}
if end >= hsb {
end = hsb
}
if end == 0 {
return
}
// Look at our backlog of messages from the network. If we are really behind, ignore completely.
// Otherwise, dial back our response, or give them as much as we can. In any event, limit to
// just a bit over 1 MB
start, end = NewEnd(state.InMsgQueue().Length(), start, end)
sent := 0
for dbs := start; dbs <= end && sent < 1024*1024; dbs++ {
sent += m.send(dbs, state)
}
return
}
// Acknowledgements do not go into the process list.
func (e *DBStateMissing) Process(dbheight uint32, state interfaces.IState) bool {
panic("Ack object should never have its Process() method called")
}
func (e *DBStateMissing) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *DBStateMissing) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (m *DBStateMissing) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling Directory Block State Missing Message: %v", r)
}
}()
newData = data
if newData[0] != m.Type() {
return nil, fmt.Errorf("Invalid Message type")
}
newData = newData[1:]
m.Peer2Peer = true // This is always a Peer2peer message
m.Timestamp = new(primitives.Timestamp)
newData, err = m.Timestamp.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
m.DBHeightStart, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
m.DBHeightEnd, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
return
}
func (m *DBStateMissing) UnmarshalBinary(data []byte) error {
_, err := m.UnmarshalBinaryData(data)
return err
}
func (m *DBStateMissing) MarshalForSignature() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "DBStateMissing.MarshalForSignature err:%v", *pe)
}
}(&err)
var buf primitives.Buffer
binary.Write(&buf, binary.BigEndian, m.Type())
t := m.GetTimestamp()
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
binary.Write(&buf, binary.BigEndian, m.DBHeightStart)
binary.Write(&buf, binary.BigEndian, m.DBHeightEnd)
return buf.DeepCopyBytes(), nil
}
func (m *DBStateMissing) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "DBStateMissing.MarshalBinary err:%v", *pe)
}
}(&err)
return m.MarshalForSignature()
}
func (m *DBStateMissing) String() string {
return fmt.Sprintf("DBStateMissing: %d-%d", m.DBHeightStart, m.DBHeightEnd)
}
func (m *DBStateMissing) LogFields() log.Fields {
return log.Fields{"category": "message", "messagetype": "dbstatemissing",
"dbheightstart": m.DBHeightStart,
"dbheightend": m.DBHeightEnd}
}
func NewDBStateMissing(state interfaces.IState, dbheightStart uint32, dbheightEnd uint32) interfaces.IMsg {
msg := new(DBStateMissing)
msg.Peer2Peer = true // Always a peer2peer request.
msg.Timestamp = state.GetTimestamp()
msg.DBHeightStart = dbheightStart
msg.DBHeightEnd = dbheightEnd
return msg
}