-
Notifications
You must be signed in to change notification settings - Fork 151
/
tracked_participation.go
106 lines (89 loc) · 2.66 KB
/
tracked_participation.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
package participation
import (
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hornet/pkg/model/hornet"
"github.com/iotaledger/hornet/pkg/model/milestone"
"github.com/iotaledger/hornet/pkg/model/utxo"
iotago "github.com/iotaledger/iota.go/v2"
)
// TrackedParticipation holds the information the node tracked for the participation.
type TrackedParticipation struct {
// EventID is the ID of the event the participation is made for.
EventID EventID
// OutputID is the ID of the output the participation was made.
OutputID *iotago.UTXOInputID
// MessageID is the ID of the message that included the transaction that created the output the participation was made.
MessageID hornet.MessageID
// Amount is the amount of tokens that were included in the output the participation was made.
Amount uint64
// StartIndex is the milestone index the participation started.
StartIndex milestone.Index
// EndIndex is the milestone index the participation ended. 0 if the participation is still active.
EndIndex milestone.Index
}
// ParseEventID helps to parse an EventID using marshalutil.
func ParseEventID(ms *marshalutil.MarshalUtil) (EventID, error) {
bytes, err := ms.ReadBytes(EventIDLength)
if err != nil {
return NullEventID, err
}
o := EventID{}
copy(o[:], bytes)
return o, nil
}
func TrackedParticipationFromBytes(key []byte, value []byte) (*TrackedParticipation, error) {
if len(key) != 67 {
return nil, ErrInvalidPreviouslyTrackedParticipation
}
if len(value) != 48 {
return nil, ErrInvalidPreviouslyTrackedParticipation
}
mKey := marshalutil.New(key)
// Skip prefix
if _, err := mKey.ReadByte(); err != nil {
return nil, err
}
// Read EventID
eventID, err := ParseEventID(mKey)
if err != nil {
return nil, err
}
// Read OutputID
outputID, err := utxo.ParseOutputID(mKey)
if err != nil {
return nil, err
}
mValue := marshalutil.New(value)
messageID, err := utxo.ParseMessageID(mValue)
if err != nil {
return nil, err
}
amount, err := mValue.ReadUint64()
if err != nil {
return nil, err
}
start, err := mValue.ReadUint32()
if err != nil {
return nil, err
}
end, err := mValue.ReadUint32()
if err != nil {
return nil, err
}
return &TrackedParticipation{
EventID: eventID,
OutputID: outputID,
MessageID: messageID,
Amount: amount,
StartIndex: milestone.Index(start),
EndIndex: milestone.Index(end),
}, nil
}
func (t *TrackedParticipation) ValueBytes() []byte {
m := marshalutil.New(48)
m.WriteBytes(t.MessageID) // 32 bytes
m.WriteUint64(t.Amount) // 8 bytes
m.WriteUint32(uint32(t.StartIndex)) // 4 bytes
m.WriteUint32(uint32(t.EndIndex)) // 4 bytes
return m.Bytes()
}