Repository navigation
Expand file tree
/
Copy pathid.go
More file actions
217 lines (184 loc) · 5.76 KB
/
Copy pathid.go
File metadata and controls
217 lines (184 loc) · 5.76 KB
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
package stack
import (
"bytes"
"cmp"
"errors"
"fmt"
"log/slog"
"slices"
"sort"
"github.com/ethereum-optimism/optimism/op-service/eth"
)
// Kind represents a kind of component, this is used to make each ID unique, even when encoded as text.
type Kind string
var _ slog.LogValuer = (*Kind)(nil)
func (k Kind) LogValue() slog.Value {
return slog.StringValue(string(k))
}
func (k Kind) String() string {
return string(k)
}
func (k Kind) MarshalText() ([]byte, error) {
return []byte(k), nil
}
func (k *Kind) UnmarshalText(data []byte) error {
*k = Kind(data)
return nil
}
// ChainIDProvider presents a type that provides a relevant ChainID.
type ChainIDProvider interface {
ChainID() eth.ChainID
}
// KindProvider presents a type that provides a relevant Kind. E.g. an L2BatcherKind.
type KindProvider interface {
Kind() Kind
}
// Keyed presents a type that provides a relevant string key. E.g. a named superchain.
type Keyed interface {
Key() string
}
const maxIDLength = 100
var errInvalidID = errors.New("invalid ID")
// Defined types based on idWithChain should implement this interface so they may be used as logging attributes.
type IDWithChain interface {
slog.LogValuer
ChainIDProvider
KindProvider
Keyed
}
// idWithChain is comparable, can be copied, contains a chain-ID,
// and has type-safe text encoding/decoding to prevent accidental mixups.
type idWithChain struct {
key string
chainID eth.ChainID
}
func (id idWithChain) string(kind Kind) string {
return fmt.Sprintf("%s-%s-%s", kind, id.key, id.chainID)
}
func (id idWithChain) marshalText(kind Kind) ([]byte, error) {
k := string(id.key)
if len(k) > maxIDLength {
return nil, errInvalidID
}
k = fmt.Sprintf("%s-%s-%s", kind, k, id.chainID)
return []byte(k), nil
}
func (id *idWithChain) unmarshalText(kind Kind, data []byte) error {
kindData, mainData, ok := bytes.Cut(data, []byte("-"))
if !ok {
return fmt.Errorf("expected kind-prefix, but id has none: %q", data)
}
if x := string(kindData); x != string(kind) {
return fmt.Errorf("id %q has unexpected kind %q, expected %q", string(data), x, kind)
}
before, after, ok := bytes.Cut(mainData, []byte("-"))
if !ok {
return fmt.Errorf("expected chain separator, but found none: %q", string(data))
}
var chainID eth.ChainID
if err := chainID.UnmarshalText(after); err != nil {
return fmt.Errorf("failed to unmarshal chain part: %w", err)
}
if len(before) > maxIDLength {
return errInvalidID
}
id.key = string(before)
id.chainID = chainID
return nil
}
// Defined types based on idOnlyChainID should implement this interface so they may be used as logging attributes.
type IDOnlyChainID interface {
slog.LogValuer
ChainIDProvider
KindProvider
}
// idChainID is comparable, can be copied, contains only a chain-ID,
// and has type-safe text encoding/decoding to prevent accidental mixups.
type idOnlyChainID eth.ChainID
func (id idOnlyChainID) string(kind Kind) string {
return fmt.Sprintf("%s-%s", kind, eth.ChainID(id))
}
func (id idOnlyChainID) marshalText(kind Kind) ([]byte, error) {
k := fmt.Sprintf("%s-%s", kind, eth.ChainID(id))
return []byte(k), nil
}
func (id *idOnlyChainID) unmarshalText(kind Kind, data []byte) error {
kindData, mainData, ok := bytes.Cut(data, []byte("-"))
if !ok {
return fmt.Errorf("expected kind-prefix, but id has none: %q", data)
}
if x := string(kindData); x != string(kind) {
return fmt.Errorf("id %q has unexpected kind %q, expected %q", string(data), x, kind)
}
var chainID eth.ChainID
if err := chainID.UnmarshalText(mainData); err != nil {
return fmt.Errorf("failed to unmarshal chain part: %w", err)
}
*id = idOnlyChainID(chainID)
return nil
}
// Defined types based on genericID should implement this interface so they may be used as logging attributes.
type GenericID interface {
slog.LogValuer
KindProvider
}
// genericID is comparable, can be copied,
// and has type-safe text encoding/decoding to prevent accidental mixups.
type genericID string
func (id genericID) string(kind Kind) string {
return fmt.Sprintf("%s-%s", kind, string(id))
}
func (id genericID) marshalText(kind Kind) ([]byte, error) {
if len(id) > maxIDLength {
return nil, errInvalidID
}
return []byte(fmt.Sprintf("%s-%s", kind, string(id))), nil
}
func (id *genericID) unmarshalText(kind Kind, data []byte) error {
kindData, mainData, ok := bytes.Cut(data, []byte("-"))
if !ok {
return fmt.Errorf("expected kind-prefix, but id has none: %q", data)
}
if x := string(kindData); x != string(kind) {
return fmt.Errorf("id %q has unexpected kind %q, expected %q", string(data), x, kind)
}
if len(mainData) > maxIDLength {
return errInvalidID
}
*id = genericID(mainData)
return nil
}
// copyAndSort helps copy and sort a slice of objects with the given less function
func copyAndSort[V ~[]E, E any](vs V, lessFn func(a, b E) bool) V {
out := slices.Clone(vs)
sort.Slice(out, func(i, j int) bool {
a := out[i]
b := out[j]
return lessFn(a, b)
})
return out
}
// lessIDWithChain is a helper function to compare two idWithChain objects.
// It does not use generics, since idWithChain is a concrete type with struct fields and no accessor methods in the types that wrap this type.
func lessIDWithChain(a, b idWithChain) bool {
if a.key > b.key {
return false
}
if a.key == b.key {
return a.chainID.Cmp(b.chainID) < 0
}
return true
}
// lessIDOnlyChainID is a helper function to compare two idOnlyChainID objects.
func lessIDOnlyChainID(a, b idOnlyChainID) bool {
return eth.ChainID(a).Cmp(eth.ChainID(b)) < 0
}
func lessElemOrdered[I cmp.Ordered, E Identifiable[I]](a, b E) bool {
return a.ID() < b.ID()
}
// copyAndSortCmp is a helper function to copy and sort a slice of elements that are already natively comparable.
func copyAndSortCmp[V ~[]E, E cmp.Ordered](vs V) V {
out := slices.Clone(vs)
slices.Sort(out)
return out
}