forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
missingEntry.go
190 lines (160 loc) · 3.93 KB
/
missingEntry.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"fmt"
"os"
"time"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/common/primitives/random"
)
type MissingEntryBlock struct {
EBHash interfaces.IHash
DBHeight uint32
}
var _ interfaces.BinaryMarshallable = (*MissingEntryBlock)(nil)
func RandomMissingEntryBlock() *MissingEntryBlock {
meb := new(MissingEntryBlock)
meb.EBHash = primitives.RandomHash()
meb.DBHeight = random.RandUInt32()
return meb
}
func (s *MissingEntryBlock) IsSameAs(b *MissingEntryBlock) bool {
if s.EBHash.IsSameAs(b.EBHash) == false {
return false
}
return s.DBHeight == b.DBHeight
}
func (s *MissingEntryBlock) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "MissingEntryBlock.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushBinaryMarshallable(s.EBHash)
if err != nil {
return nil, err
}
err = buf.PushUInt32(s.DBHeight)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (s *MissingEntryBlock) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
s.EBHash = primitives.NewZeroHash()
newData = p
buf := primitives.NewBuffer(p)
err = buf.PopBinaryMarshallable(s.EBHash)
if err != nil {
return
}
s.DBHeight, err = buf.PopUInt32()
if err != nil {
return
}
newData = buf.DeepCopyBytes()
return
}
func (s *MissingEntryBlock) UnmarshalBinary(p []byte) error {
_, err := s.UnmarshalBinaryData(p)
return err
}
type MissingEntry struct {
Cnt int
LastTime time.Time
EBHash interfaces.IHash
EntryHash interfaces.IHash
DBHeight uint32
}
var _ interfaces.BinaryMarshallable = (*MissingEntry)(nil)
func RandomMissingEntry() *MissingEntry {
me := new(MissingEntry)
me.Cnt = random.RandIntBetween(0, 1000000)
me.LastTime = time.Unix(random.RandInt64Between(0, 1000000), random.RandInt64Between(0, 1000000))
me.EBHash = primitives.RandomHash()
me.EntryHash = primitives.RandomHash()
me.DBHeight = random.RandUInt32()
return me
}
func (s *MissingEntry) IsSameAs(b *MissingEntry) bool {
if s.EBHash.IsSameAs(b.EBHash) == false {
return false
}
if s.EntryHash.IsSameAs(b.EntryHash) == false {
return false
}
if s.Cnt != b.Cnt {
return false
}
if s.LastTime.Sub(b.LastTime).Nanoseconds() != 0 {
return false
}
return s.DBHeight == b.DBHeight
}
func (s *MissingEntry) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "MissingEntry.MarshalBinary err:%v", *pe)
}
}(&err)
buf := primitives.NewBuffer(nil)
err = buf.PushVarInt(uint64(s.Cnt))
if err != nil {
return nil, err
}
err = buf.PushVarInt(uint64(s.LastTime.UnixNano()))
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(s.EBHash)
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(s.EntryHash)
if err != nil {
return nil, err
}
err = buf.PushUInt32(s.DBHeight)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (s *MissingEntry) UnmarshalBinaryData(p []byte) (newData []byte, err error) {
s.EBHash = primitives.NewZeroHash()
s.EntryHash = primitives.NewZeroHash()
newData = p
buf := primitives.NewBuffer(p)
i, err := buf.PopVarInt()
if err != nil {
return
}
s.Cnt = int(i)
i, err = buf.PopVarInt()
if err != nil {
return
}
s.LastTime = time.Unix(int64(i/1000000000), int64(i%1000000000))
err = buf.PopBinaryMarshallable(s.EBHash)
if err != nil {
return
}
err = buf.PopBinaryMarshallable(s.EntryHash)
if err != nil {
return
}
s.DBHeight, err = buf.PopUInt32()
if err != nil {
return
}
newData = buf.DeepCopyBytes()
return
}
func (s *MissingEntry) UnmarshalBinary(p []byte) error {
_, err := s.UnmarshalBinaryData(p)
return err
}