-
Notifications
You must be signed in to change notification settings - Fork 95
/
block_id.gen.go
220 lines (170 loc) · 5.37 KB
/
block_id.gen.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package iotago
// Code generated by go generate; DO NOT EDIT. Check gen/ directory instead.
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"sort"
"sync"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
BlockIDLength = IdentifierLength + SlotIndexLength
)
var (
EmptyBlockID = BlockID{}
)
// BlockID is a 32 byte hash value together with an 4 byte slot index.
type BlockID [BlockIDLength]byte
// BlockIDRepresentingData returns a new BlockID for the given data by hashing it with blake2b and associating it with the given slot index.
func BlockIDRepresentingData(slot SlotIndex, data []byte) BlockID {
return NewBlockID(slot, blake2b.Sum256(data))
}
func NewBlockID(slot SlotIndex, idBytes Identifier) BlockID {
b := BlockID{}
copy(b[:], idBytes[:])
binary.LittleEndian.PutUint32(b[IdentifierLength:], uint32(slot))
return b
}
// BlockIDFromHexString converts the hex to a BlockID representation.
func BlockIDFromHexString(hex string) (BlockID, error) {
b, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyBlockID, err
}
s, _, err := BlockIDFromBytes(b)
return s, err
}
// IsValidBlockID returns an error if the passed bytes are not a valid BlockID, otherwise nil.
func IsValidBlockID(b []byte) error {
if len(b) != BlockIDLength {
return ierrors.Errorf("invalid blockID length: expected %d bytes, got %d bytes", BlockIDLength, len(b))
}
return nil
}
// BlockIDFromBytes returns a new BlockID represented by the passed bytes.
func BlockIDFromBytes(b []byte) (BlockID, int, error) {
if len(b) < BlockIDLength {
return EmptyBlockID, 0, ierrors.Errorf("invalid length for blockID, expected at least %d bytes, got %d bytes", BlockIDLength, len(b))
}
return BlockID(b), BlockIDLength, nil
}
// MustBlockIDFromHexString converts the hex to a BlockID representation.
func MustBlockIDFromHexString(hex string) BlockID {
b, err := BlockIDFromHexString(hex)
if err != nil {
panic(err)
}
return b
}
func (b BlockID) Bytes() ([]byte, error) {
return b[:], nil
}
func (b BlockID) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyBlockID)))
hex.Encode(dst, b[:])
return dst, nil
}
func (b *BlockID) UnmarshalText(text []byte) error {
_, err := hex.Decode(b[:], text)
return err
}
// Empty tells whether the BlockID is empty.
func (b BlockID) Empty() bool {
return b == EmptyBlockID
}
// ToHex converts the Identifier to its hex representation.
func (b BlockID) ToHex() string {
return hexutil.EncodeHex(b[:])
}
func (b BlockID) String() string {
return fmt.Sprintf("BlockID(%s:%d)", b.Alias(), b.Slot())
}
func (b BlockID) Slot() SlotIndex {
return SlotIndex(binary.LittleEndian.Uint32(b[IdentifierLength:]))
}
// Index returns a slot index to conform with hive's IndexedID interface.
func (b BlockID) Index() SlotIndex {
return b.Slot()
}
func (b BlockID) Identifier() Identifier {
return Identifier(b[:IdentifierLength])
}
var (
// BlockIDAliases contains a dictionary of identifiers associated to their human-readable alias.
BlockIDAliases = make(map[BlockID]string)
// blockIDAliasesMutex is the mutex that is used to synchronize access to the previous map.
blockIDAliasesMutex = sync.RWMutex{}
)
// RegisterAlias allows to register a human-readable alias for the Identifier which will be used as a replacement for
// the String method.
func (b BlockID) RegisterAlias(alias string) {
blockIDAliasesMutex.Lock()
defer blockIDAliasesMutex.Unlock()
BlockIDAliases[b] = alias
}
// Alias returns the human-readable alias of the Identifier (or the base58 encoded bytes of no alias was set).
func (b BlockID) Alias() (alias string) {
blockIDAliasesMutex.RLock()
defer blockIDAliasesMutex.RUnlock()
if existingAlias, exists := BlockIDAliases[b]; exists {
return existingAlias
}
return b.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (b BlockID) UnregisterAlias() {
blockIDAliasesMutex.Lock()
defer blockIDAliasesMutex.Unlock()
delete(BlockIDAliases, b)
}
// Compare compares two BlockIDs.
func (b BlockID) Compare(other BlockID) int {
return bytes.Compare(b[:], other[:])
}
type BlockIDs []BlockID
// ToHex converts the BlockIDs to their hex representation.
func (ids BlockIDs) ToHex() []string {
hexIDs := make([]string, len(ids))
for i, b := range ids {
hexIDs[i] = hexutil.EncodeHex(b[:])
}
return hexIDs
}
// RemoveDupsAndSort removes duplicated BlockIDs and sorts the slice by the lexical ordering.
func (ids BlockIDs) RemoveDupsAndSort() BlockIDs {
sorted := append(BlockIDs{}, ids...)
sort.Slice(sorted, func(i, j int) bool {
return bytes.Compare(sorted[i][:], sorted[j][:]) == -1
})
var result BlockIDs
var prev BlockID
for i, b := range sorted {
if i == 0 || !bytes.Equal(prev[:], b[:]) {
result = append(result, b)
}
prev = b
}
return result
}
// Sort sorts the BlockIDs lexically and in-place.
func (ids BlockIDs) Sort() {
sort.Slice(ids, func(i, j int) bool {
return ids[i].Compare(ids[j]) < 0
})
}
// BlockIDsFromHexString converts the given block IDs from their hex to BlockID representation.
func BlockIDsFromHexString(BlockIDsHex []string) (BlockIDs, error) {
result := make(BlockIDs, len(BlockIDsHex))
for i, hexString := range BlockIDsHex {
BlockID, err := BlockIDFromHexString(hexString)
if err != nil {
return nil, err
}
result[i] = BlockID
}
return result, nil
}