forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
136 lines (96 loc) · 2.24 KB
/
entry.go
File metadata and controls
136 lines (96 loc) · 2.24 KB
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
package notaryapi
import (
"bytes"
"encoding/binary"
"time"
)
type Entry struct {
ChainID Hash
//ExtHashes []Hash
ExtIDs [][]byte
Data []byte
timeStamp int64
}
type EntryInfo struct {
EntryHash *Hash
EBHash *Hash
EBBlockNum uint64
//EBOffset uint64
}
type EntryInfoBranch struct {
EntryHash *Hash
EntryInfo *EntryInfo
EBInfo *EBInfo
DBBatch *DBBatch
}
func (e *Entry) StampTime() {
e.timeStamp = time.Now().Unix()
}
func (e *Entry) TimeStamp() int64 {
return e.timeStamp
}
func (e *Entry) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
data,_ := e.ChainID.MarshalBinary()
buf.Write(data)
count := len(e.ExtIDs)
binary.Write(&buf, binary.BigEndian, uint8(count))
for _, bytes := range e.ExtIDs {
count = len(bytes)
binary.Write(&buf, binary.BigEndian, uint32(count))
buf.Write(bytes)
}
buf.Write(e.Data)
return buf.Bytes(), nil
}
func (e *Entry) MarshalledSize() uint64 {
var size uint64 = 0
size += e.ChainID.MarshalledSize() //33
size += 1
for _, bytes := range e.ExtIDs {
size += 4
size += uint64(len(bytes))
}
size += uint64(len(e.Data))
return size
}
func (e *Entry) UnmarshalBinary(data []byte) (err error) {
e.ChainID.UnmarshalBinary(data[:33])
data = data[33:]
count, data := data[0], data[1:]
e.ExtIDs = make([][]byte, count, count)
for i:=uint8(0); i<count; i++{
length := binary.BigEndian.Uint32(data[0:4])
data = data[4:]
e.ExtIDs[i] = data[:length]
data = data[length:]
}
e.Data = data
return nil
}
func (e *EntryInfo) MarshalBinary() (data []byte, err error) {
var buf bytes.Buffer
data, _ = e.EntryHash.MarshalBinary()
buf.Write(data)
data, _ = e.EBHash.MarshalBinary()
buf.Write(data)
binary.Write(&buf, binary.BigEndian, e.EBBlockNum)
return buf.Bytes(), nil
}
func (e *EntryInfo) MarshalledSize() uint64 {
var size uint64 = 0
size += 33 //e.EntryHash
size += 33 //e.EBHash
size += 8 //e.EBBlockNum
return size
}
func (e *EntryInfo) UnmarshalBinary(data []byte) (err error) {
e.EntryHash = new(Hash)
e.EntryHash.UnmarshalBinary(data[:33])
data = data[33:]
e.EBHash = new(Hash)
e.EBHash.UnmarshalBinary(data[:33])
data = data[33:]
e.EBBlockNum = binary.BigEndian.Uint64(data[0:8])
return nil
}