-
Notifications
You must be signed in to change notification settings - Fork 114
/
timestamp.go
158 lines (127 loc) · 4.96 KB
/
timestamp.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
package statement
import (
"fmt"
"strconv"
"github.com/iotaledger/goshimmer/packages/tangle"
"github.com/iotaledger/hive.go/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
"golang.org/x/xerrors"
)
const (
// TimestampLength defines the Timestamp length in bytes.
TimestampLength = tangle.MessageIDLength + OpinionLength
)
// region Timestamp /////////////////////////////////////////////////////////////////////////////////////////////////////
// Timestamp holds the message ID and its timestamp opinion.
type Timestamp struct {
ID tangle.MessageID
Opinion
}
// Bytes returns the timestamp statement encoded as bytes.
func (t Timestamp) Bytes() (bytes []byte) {
return marshalutil.New(TimestampLength).
Write(t.ID).
Write(t.Opinion).
Bytes()
}
// TimestampFromBytes parses a conflict statement from a byte slice.
func TimestampFromBytes(bytes []byte) (timestamp Timestamp, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if timestamp, err = TimestampFromMarshalUtil(marshalUtil); err != nil {
err = xerrors.Errorf("failed to parse Timestamp from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// TimestampFromMarshalUtil is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.
func TimestampFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (timestamp Timestamp, err error) {
readStartOffset := marshalUtil.ReadOffset()
timestamp = Timestamp{}
bytesID, err := marshalUtil.ReadBytes(int(tangle.MessageIDLength))
if err != nil {
err = xerrors.Errorf("failed to parse ID from timestamp: %w", err)
return
}
timestamp.ID, _, err = tangle.MessageIDFromBytes(bytesID)
if err != nil {
err = xerrors.Errorf("failed to parse ID from bytes: %w", err)
return
}
timestamp.Opinion, err = OpinionFromMarshalUtil(marshalUtil)
if err != nil {
err = xerrors.Errorf("failed to parse opinion from timestamp: %w", err)
return
}
// return the number of bytes we processed
parsedBytes := marshalUtil.ReadOffset() - readStartOffset
if parsedBytes != TimestampLength {
err = xerrors.Errorf("parsed bytes (%d) did not match expected size (%d): %w", parsedBytes, TimestampLength, cerrors.ErrParseBytesFailed)
return
}
return
}
// String returns a human readable version of the Conflict.
func (t Timestamp) String() string {
structBuilder := stringify.StructBuilder("Timestamp:")
structBuilder.AddField(stringify.StructField("ID", t.ID.String()))
structBuilder.AddField(stringify.StructField("Opinion", t.Opinion))
return structBuilder.String()
}
// endregion /////////////////////////////////////////////////////////////////////////////////////////////////////
// region Timestamps /////////////////////////////////////////////////////////////////////////////////////////////////////
// Timestamps is a slice of Timestamp.
type Timestamps []Timestamp
// Bytes returns the timestamps statements encoded as bytes.
func (t Timestamps) Bytes() (bytes []byte) {
// initialize helper
marshalUtil := marshalutil.New(TimestampLength * len(t))
for _, timestamp := range t {
marshalUtil.Write(timestamp)
}
return marshalUtil.Bytes()
}
// TimestampsFromBytes parses a slice of timestamp statements from a byte slice.
func TimestampsFromBytes(bytes []byte, n uint32) (timestamps Timestamps, consumedBytes int, err error) {
if len(bytes)/TimestampLength < int(n) {
err = xerrors.Errorf("not enough bytes to parse %d timestamps", n)
return
}
marshalUtil := marshalutil.New(bytes)
if timestamps, err = TimestampsFromMarshalUtil(marshalUtil, n); err != nil {
err = xerrors.Errorf("failed to parse Timestamps from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// TimestampsFromMarshalUtil is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.
func TimestampsFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil, n uint32) (timestamps Timestamps, err error) {
readStartOffset := marshalUtil.ReadOffset()
timestamps = Timestamps{}
for i := 0; i < int(n); i++ {
timestamp, e := TimestampFromMarshalUtil(marshalUtil)
if e != nil {
err = fmt.Errorf("failed to parse timestamp from marshalutil: %w", e)
return
}
timestamps = append(timestamps, timestamp)
}
// return the number of bytes we processed
parsedBytes := marshalUtil.ReadOffset() - readStartOffset
if parsedBytes != int(TimestampLength*n) {
err = xerrors.Errorf("parsed bytes (%d) did not match expected size (%d): %w", parsedBytes, TimestampLength*n, cerrors.ErrParseBytesFailed)
return
}
return
}
// String returns a human readable version of the Timestamps.
func (t Timestamps) String() string {
structBuilder := stringify.StructBuilder("Timestamps")
for i, timestamp := range t {
structBuilder.AddField(stringify.StructField(strconv.Itoa(i), timestamp))
}
return structBuilder.String()
}
// endregion /////////////////////////////////////////////////////////////////////////////////////////////////////