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