-
Notifications
You must be signed in to change notification settings - Fork 95
/
commitment.go
84 lines (71 loc) · 2.34 KB
/
commitment.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
package iotago
import (
"context"
"fmt"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
)
type Commitment struct {
ProtocolVersion Version `serix:""`
Slot SlotIndex `serix:""`
PreviousCommitmentID CommitmentID `serix:""`
RootsID Identifier `serix:""`
CumulativeWeight uint64 `serix:""`
ReferenceManaCost Mana `serix:""`
}
func NewCommitment(version Version, slot SlotIndex, prevID CommitmentID, rootsID Identifier, cumulativeWeight uint64, rmc Mana) *Commitment {
return &Commitment{
ProtocolVersion: version,
Slot: slot,
PreviousCommitmentID: prevID,
RootsID: rootsID,
CumulativeWeight: cumulativeWeight,
ReferenceManaCost: rmc,
}
}
func NewEmptyCommitment(api API) *Commitment {
return &Commitment{
ProtocolVersion: api.ProtocolParameters().Version(),
Slot: api.ProtocolParameters().GenesisSlot(),
ReferenceManaCost: api.ProtocolParameters().CongestionControlParameters().MinReferenceManaCost,
}
}
func (c *Commitment) ID() (CommitmentID, error) {
data, err := CommonSerixAPI().Encode(context.TODO(), c)
if err != nil {
return CommitmentID{}, ierrors.Wrap(err, "failed to serialize commitment")
}
return CommitmentIDRepresentingData(c.Slot, data), nil
}
// MustID works like ID but panics if the CommitmentID can't be computed.
func (c *Commitment) MustID() CommitmentID {
id, err := c.ID()
if err != nil {
panic(err)
}
return id
}
func (c *Commitment) Type() ContextInputType {
return ContextInputCommitment
}
func (c *Commitment) Equals(other *Commitment) bool {
return c.MustID() == other.MustID() &&
c.ProtocolVersion == other.ProtocolVersion &&
c.Slot == other.Slot &&
c.PreviousCommitmentID == other.PreviousCommitmentID &&
c.RootsID == other.RootsID &&
c.CumulativeWeight == other.CumulativeWeight &&
c.ReferenceManaCost == other.ReferenceManaCost
}
func (c *Commitment) String() string {
return fmt.Sprintf("Commitment{\n\tIndex: %d\n\tPrevID: %s\n\tRootsID: %s\n\tCumulativeWeight: %d\n\tRMC: %d\n}",
c.Slot, c.PreviousCommitmentID, c.RootsID, c.CumulativeWeight, c.ReferenceManaCost)
}
func (c *Commitment) Size() int {
return serializer.OneByte +
SlotIndexLength +
CommitmentIDLength +
IdentifierLength +
serializer.UInt64ByteSize +
ManaSize
}