forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entryBlockSync.go
321 lines (265 loc) · 7.05 KB
/
entryBlockSync.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
package identity
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/common/primitives/random"
)
// EntryBlockSync has the current eblock synced to, and the target Eblock
// It also has the blocks in between in order. This makes it so traversing only needs to happen once
type EntryBlockSync struct {
Current EntryBlockMarker
Target EntryBlockMarker
BlocksToBeParsed []EntryBlockMarker
}
func RandomEntryBlockSync() *EntryBlockSync {
s := NewEntryBlockSync()
for i := 0; i < random.RandIntBetween(0, 10); i++ {
m := RandomEntryBlockMarker()
m.Sequence = uint32(i)
m.DBHeight = uint32(i)
s.AddNewHeadMarker(*m)
}
return s
}
func NewEntryBlockSync() *EntryBlockSync {
e := new(EntryBlockSync)
e.Current = *NewEntryBlockMarker()
e.Target = *NewEntryBlockMarker()
return e
}
// Synced returns if fully synced (current == target)
func (a *EntryBlockSync) Synced() bool {
return a.Current.IsSameAs(&a.Target)
}
// NextEBlock returns the next eblock that is needed to be parsed
func (a *EntryBlockSync) NextEBlock() *EntryBlockMarker {
if len(a.BlocksToBeParsed) == 0 {
return nil
}
return &a.BlocksToBeParsed[0]
}
// BlockParsed indicates a block has been parsed. We update our current
func (a *EntryBlockSync) BlockParsed(block EntryBlockMarker) {
if !a.BlocksToBeParsed[0].IsSameAs(&block) {
panic("This block should be next in the list")
}
a.Current = block
a.BlocksToBeParsed = a.BlocksToBeParsed[1:]
}
func (a *EntryBlockSync) AddNewHead(keymr interfaces.IHash, seq uint32, ht uint32, dblockTimestamp interfaces.Timestamp) {
a.AddNewHeadMarker(EntryBlockMarker{keymr, seq, ht, dblockTimestamp})
}
// AddNewHead will add a new eblock to be parsed to the head (tail of list)
// Since the block needs to be parsed, it is the new target and added to the blocks to be parsed
func (a *EntryBlockSync) AddNewHeadMarker(marker EntryBlockMarker) {
if marker.DBHeight < a.Target.DBHeight {
return // Already added this target
}
a.BlocksToBeParsed = append(a.BlocksToBeParsed, marker)
a.Target = marker
}
func (a *EntryBlockSync) IsSameAs(b *EntryBlockSync) bool {
if !a.Current.IsSameAs(&b.Current) {
return false
}
if !a.Target.IsSameAs(&b.Target) {
return false
}
if len(a.BlocksToBeParsed) != len(b.BlocksToBeParsed) {
return false
}
for i := range a.BlocksToBeParsed {
if !a.BlocksToBeParsed[i].IsSameAs(&b.BlocksToBeParsed[i]) {
return false
}
}
return true
}
func (e *EntryBlockSync) Clone() *EntryBlockSync {
b := new(EntryBlockSync)
b.Current = *e.Current.Clone()
b.Target = *e.Target.Clone()
b.BlocksToBeParsed = make([]EntryBlockMarker, len(e.BlocksToBeParsed))
for i, eb := range e.BlocksToBeParsed {
b.BlocksToBeParsed[i] = *eb.Clone()
}
return b
}
func (e *EntryBlockSync) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "EntryBlockSync.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushBinaryMarshallable(&e.Current)
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(&e.Target)
if err != nil {
return nil, err
}
err = buf.PushInt(len(e.BlocksToBeParsed))
if err != nil {
return nil, err
}
for _, v := range e.BlocksToBeParsed {
err = buf.PushBinaryMarshallable(&v)
if err != nil {
return nil, err
}
}
return buf.DeepCopyBytes(), nil
}
func (e *EntryBlockSync) UnmarshalBinary(p []byte) error {
_, err := e.UnmarshalBinaryData(p)
return err
}
func (e *EntryBlockSync) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
buf := primitives.NewBuffer(p)
newData = p
err = buf.PopBinaryMarshallable(&e.Current)
if err != nil {
return
}
err = buf.PopBinaryMarshallable(&e.Target)
if err != nil {
return
}
// blockLimit is the maximum number of Entry Blocks that could fit in the
// buffer.
tmp := NewEntryBlockMarker()
blockLimit := buf.Len() / tmp.Size()
blockCount, err := buf.PopInt()
if err != nil {
return
}
if blockCount > blockLimit {
return nil, fmt.Errorf(
"Error: EntryBlockSync.UnmarshalBinary: block count %d is greater "+
"than remaining space in buffer %d (uint underflow?)",
blockCount, blockLimit,
)
}
e.BlocksToBeParsed = make([]EntryBlockMarker, blockCount)
for i := 0; i < blockCount; i++ {
var b EntryBlockMarker
err = buf.PopBinaryMarshallable(&b)
if err != nil {
return
}
e.BlocksToBeParsed[i] = b
}
newData = buf.DeepCopyBytes()
return
}
type EntryBlockMarkerList []EntryBlockMarker
func (p EntryBlockMarkerList) Len() int {
return len(p)
}
func (p EntryBlockMarkerList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p EntryBlockMarkerList) Less(i, j int) bool {
return p[i].Sequence < p[j].Sequence
}
type EntryBlockMarker struct {
KeyMr interfaces.IHash
Sequence uint32
DBHeight uint32
DblockTimestamp interfaces.Timestamp
}
func NewEntryBlockMarker() *EntryBlockMarker {
e := new(EntryBlockMarker)
e.KeyMr = primitives.NewZeroHash()
e.DblockTimestamp = new(primitives.Timestamp)
return e
}
func RandomEntryBlockMarker() *EntryBlockMarker {
m := NewEntryBlockMarker()
m.KeyMr = primitives.RandomHash()
m.DblockTimestamp = primitives.NewTimestampNow()
return m
}
func (a *EntryBlockMarker) IsSameAs(b *EntryBlockMarker) bool {
if !a.KeyMr.IsSameAs(b.KeyMr) {
return false
}
if a.Sequence != b.Sequence {
return false
}
if a.DBHeight != b.DBHeight {
return false
}
if !a.DblockTimestamp.IsSameAs(b.DblockTimestamp) {
return false
}
return true
}
func (e *EntryBlockMarker) Clone() *EntryBlockMarker {
b := new(EntryBlockMarker)
b.KeyMr = e.KeyMr.Copy()
b.Sequence = e.Sequence
b.DBHeight = e.DBHeight
b.DblockTimestamp = e.DblockTimestamp
return b
}
func (e *EntryBlockMarker) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "EntryBlockMarker.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushIHash(e.KeyMr)
if err != nil {
return nil, err
}
err = buf.PushUInt32(e.Sequence)
if err != nil {
return nil, err
}
err = buf.PushUInt32(e.DBHeight)
if err != nil {
return nil, err
}
err = buf.PushTimestamp(e.DblockTimestamp)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
// Returns the byte size when marshaled
func (e *EntryBlockMarker) Size() int {
// If you count it, it's 46. However, PushIHash is actually 33 bytes. and PushTimestamp is actually 8, rather than 6.
return 49
}
func (e *EntryBlockMarker) UnmarshalBinary(p []byte) error {
_, err := e.UnmarshalBinaryData(p)
return err
}
func (e *EntryBlockMarker) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
buf := primitives.NewBuffer(p)
newData = p
e.KeyMr, err = buf.PopIHash()
if err != nil {
return
}
e.Sequence, err = buf.PopUInt32()
if err != nil {
return
}
e.DBHeight, err = buf.PopUInt32()
if err != nil {
return
}
e.DblockTimestamp, err = buf.PopTimestamp()
if err != nil {
return
}
newData = buf.DeepCopyBytes()
return
}