Skip to content

Commit de14e07

Browse files
committed
refactor(core)!: remove PrepareNamespace
1 parent 8f996eb commit de14e07

2 files changed

Lines changed: 39 additions & 133 deletions

File tree

core/da/namespace.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,3 @@ func ParseHexNamespace(hexStr string) (*Namespace, error) {
125125

126126
return NamespaceFromBytes(b)
127127
}
128-
129-
// PrepareNamespace converts a namespace identifier (string or bytes) into a proper Celestia namespace
130-
// This is the main function to be used when preparing namespaces for DA operations
131-
func PrepareNamespace(identifier []byte) []byte {
132-
// If the identifier is already a valid namespace (29 bytes), validate and return it
133-
if len(identifier) == NamespaceSize {
134-
ns, err := NamespaceFromBytes(identifier)
135-
if err == nil {
136-
return ns.Bytes()
137-
}
138-
// If it's not a valid namespace, treat it as a string identifier
139-
}
140-
141-
// Convert the identifier to a string and create a namespace from it
142-
ns := NamespaceFromString(string(identifier))
143-
return ns.Bytes()
144-
}

core/da/namespace_test.go

Lines changed: 39 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestNamespaceV0Creation(t *testing.T) {
4242
for _, tt := range tests {
4343
t.Run(tt.name, func(t *testing.T) {
4444
ns, err := NewNamespaceV0(tt.data)
45-
45+
4646
if tt.expectError {
4747
if err == nil {
4848
t.Errorf("%s: expected error but got nil", tt.description)
@@ -57,32 +57,32 @@ func TestNamespaceV0Creation(t *testing.T) {
5757
if ns == nil {
5858
t.Fatal("expected non-nil namespace but got nil")
5959
}
60-
60+
6161
// Verify version is 0
6262
if ns.Version != NamespaceVersionZero {
6363
t.Errorf("Version should be 0, got %d", ns.Version)
6464
}
65-
65+
6666
// Verify first 18 bytes of ID are zeros
6767
for i := 0; i < NamespaceVersionZeroPrefixSize; i++ {
6868
if ns.ID[i] != byte(0) {
6969
t.Errorf("First 18 bytes should be zero, but byte %d is %d", i, ns.ID[i])
7070
}
7171
}
72-
72+
7373
// Verify data is in the last 10 bytes
7474
expectedData := make([]byte, NamespaceVersionZeroDataSize)
7575
copy(expectedData, tt.data)
7676
actualData := ns.ID[NamespaceVersionZeroPrefixSize:]
7777
if !bytes.Equal(expectedData, actualData) {
7878
t.Errorf("Data should match in last 10 bytes, expected %v, got %v", expectedData, actualData)
7979
}
80-
80+
8181
// Verify total size
8282
if len(ns.Bytes()) != NamespaceSize {
8383
t.Errorf("Total namespace size should be 29 bytes, got %d", len(ns.Bytes()))
8484
}
85-
85+
8686
// Verify it's valid for version 0
8787
if !ns.IsValidForVersion0() {
8888
t.Error("Should be valid for version 0")
@@ -128,7 +128,7 @@ func TestNamespaceFromBytes(t *testing.T) {
128128
for _, tt := range tests {
129129
t.Run(tt.name, func(t *testing.T) {
130130
ns, err := NamespaceFromBytes(tt.input)
131-
131+
132132
if tt.expectError {
133133
if err == nil {
134134
t.Errorf("%s: expected error but got nil", tt.description)
@@ -170,7 +170,7 @@ func TestNamespaceFromString(t *testing.T) {
170170
if len(ns.Bytes()) != NamespaceSize {
171171
t.Errorf("expected namespace size %d, got %d", NamespaceSize, len(ns.Bytes()))
172172
}
173-
173+
174174
// The hash should be deterministic
175175
ns2 := NamespaceFromString("rollkit-headers")
176176
if !bytes.Equal(ns.Bytes(), ns2.Bytes()) {
@@ -191,7 +191,7 @@ func TestNamespaceFromString(t *testing.T) {
191191
if len(ns.Bytes()) != NamespaceSize {
192192
t.Errorf("expected namespace size %d, got %d", NamespaceSize, len(ns.Bytes()))
193193
}
194-
194+
195195
// Different strings should produce different namespaces
196196
ns2 := NamespaceFromString("rollkit-headers")
197197
if bytes.Equal(ns.Bytes(), ns2.Bytes()) {
@@ -227,89 +227,12 @@ func TestNamespaceFromString(t *testing.T) {
227227
}
228228
}
229229

230-
func TestPrepareNamespace(t *testing.T) {
231-
tests := []struct {
232-
name string
233-
input []byte
234-
description string
235-
verify func(t *testing.T, result []byte)
236-
}{
237-
{
238-
name: "string identifier",
239-
input: []byte("rollkit-headers"),
240-
description: "Should convert string to valid namespace",
241-
verify: func(t *testing.T, result []byte) {
242-
if len(result) != NamespaceSize {
243-
t.Errorf("expected result size %d, got %d", NamespaceSize, len(result))
244-
}
245-
if result[0] != byte(0) {
246-
t.Errorf("Should be version 0, got %d", result[0])
247-
}
248-
249-
// Verify first 18 bytes of ID are zeros
250-
for i := 1; i <= 18; i++ {
251-
if result[i] != byte(0) {
252-
t.Errorf("First 18 bytes of ID should be zero, but byte %d is %d", i, result[i])
253-
}
254-
}
255-
},
256-
},
257-
{
258-
name: "already valid namespace",
259-
input: append([]byte{0}, append(make([]byte, 18), []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...)...),
260-
description: "Should return already valid namespace unchanged",
261-
verify: func(t *testing.T, result []byte) {
262-
if len(result) != NamespaceSize {
263-
t.Errorf("expected result size %d, got %d", NamespaceSize, len(result))
264-
}
265-
if result[0] != byte(0) {
266-
t.Errorf("expected version 0, got %d", result[0])
267-
}
268-
// Should pass through unchanged
269-
expected := append([]byte{0}, append(make([]byte, 18), []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...)...)
270-
if !bytes.Equal(expected, result) {
271-
t.Errorf("Should pass through unchanged, expected %v, got %v", expected, result)
272-
}
273-
},
274-
},
275-
{
276-
name: "invalid 29-byte input",
277-
input: append([]byte{0}, append([]byte{1}, make([]byte, 27)...)...), // Invalid: non-zero in prefix
278-
description: "Should treat invalid 29-byte input as string and hash it",
279-
verify: func(t *testing.T, result []byte) {
280-
if len(result) != NamespaceSize {
281-
t.Errorf("expected result size %d, got %d", NamespaceSize, len(result))
282-
}
283-
if result[0] != byte(0) {
284-
t.Errorf("expected version 0, got %d", result[0])
285-
}
286-
287-
// Should be hashed, not passed through
288-
invalidNs := append([]byte{0}, append([]byte{1}, make([]byte, 27)...)...)
289-
if bytes.Equal(invalidNs, result) {
290-
t.Error("Should not pass through invalid namespace")
291-
}
292-
},
293-
},
294-
}
295-
296-
for _, tt := range tests {
297-
t.Run(tt.name, func(t *testing.T) {
298-
result := PrepareNamespace(tt.input)
299-
if result == nil {
300-
t.Fatal("expected non-nil result but got nil")
301-
}
302-
tt.verify(t, result)
303-
})
304-
}
305-
}
306-
307230
func TestHexStringConversion(t *testing.T) {
308231
ns, err := NewNamespaceV0([]byte{1, 2, 3, 4, 5})
309232
if err != nil {
310233
t.Fatalf("unexpected error: %v", err)
311234
}
312-
235+
313236
// Test HexString
314237
hexStr := ns.HexString()
315238
if len(hexStr) <= 2 {
@@ -318,7 +241,7 @@ func TestHexStringConversion(t *testing.T) {
318241
if hexStr[:2] != "0x" {
319242
t.Errorf("Should have 0x prefix, got %s", hexStr[:2])
320243
}
321-
244+
322245
// Test ParseHexNamespace
323246
parsed, err := ParseHexNamespace(hexStr)
324247
if err != nil {
@@ -327,7 +250,7 @@ func TestHexStringConversion(t *testing.T) {
327250
if !bytes.Equal(ns.Bytes(), parsed.Bytes()) {
328251
t.Error("Should round-trip through hex")
329252
}
330-
253+
331254
// Test without 0x prefix
332255
parsed2, err := ParseHexNamespace(hexStr[2:])
333256
if err != nil {
@@ -336,13 +259,13 @@ func TestHexStringConversion(t *testing.T) {
336259
if !bytes.Equal(ns.Bytes(), parsed2.Bytes()) {
337260
t.Error("Should work without 0x prefix")
338261
}
339-
262+
340263
// Test invalid hex
341264
_, err = ParseHexNamespace("invalid-hex")
342265
if err == nil {
343266
t.Error("Should fail with invalid hex")
344267
}
345-
268+
346269
// Test wrong size hex
347270
_, err = ParseHexNamespace("0x0011")
348271
if err == nil {
@@ -352,64 +275,64 @@ func TestHexStringConversion(t *testing.T) {
352275

353276
func TestCelestiaSpecCompliance(t *testing.T) {
354277
// Test that our implementation follows the Celestia namespace specification
355-
278+
356279
t.Run("namespace structure", func(t *testing.T) {
357280
ns, err := NewNamespaceV0([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A})
358281
if err != nil {
359282
t.Fatalf("unexpected error: %v", err)
360283
}
361-
284+
362285
nsBytes := ns.Bytes()
363-
286+
364287
// Check total size is 29 bytes (1 version + 28 ID)
365288
if len(nsBytes) != 29 {
366289
t.Errorf("Total namespace size should be 29 bytes, got %d", len(nsBytes))
367290
}
368-
291+
369292
// Check version byte is at position 0
370293
if nsBytes[0] != byte(0) {
371294
t.Errorf("Version byte should be 0, got %d", nsBytes[0])
372295
}
373-
296+
374297
// Check ID is 28 bytes starting at position 1
375298
if len(nsBytes[1:]) != 28 {
376299
t.Errorf("ID should be 28 bytes, got %d", len(nsBytes[1:]))
377300
}
378301
})
379-
302+
380303
t.Run("version 0 requirements", func(t *testing.T) {
381304
ns, err := NewNamespaceV0([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
382305
if err != nil {
383306
t.Fatalf("unexpected error: %v", err)
384307
}
385-
308+
386309
nsBytes := ns.Bytes()
387-
310+
388311
// For version 0, first 18 bytes of ID must be zero
389312
for i := 1; i <= 18; i++ {
390313
if nsBytes[i] != byte(0) {
391314
t.Errorf("Bytes 1-18 should be zero for version 0, but byte %d is %d", i, nsBytes[i])
392315
}
393316
}
394-
317+
395318
// Last 10 bytes should contain our data
396319
expectedData := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
397320
actualData := nsBytes[19:29]
398321
if !bytes.Equal(expectedData, actualData) {
399322
t.Errorf("Last 10 bytes should contain user data, expected %v, got %v", expectedData, actualData)
400323
}
401324
})
402-
325+
403326
t.Run("example from spec", func(t *testing.T) {
404327
// Create a namespace similar to the example in the spec
405328
ns, err := NewNamespaceV0([]byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01})
406329
if err != nil {
407330
t.Fatalf("unexpected error: %v", err)
408331
}
409-
332+
410333
hexStr := ns.HexString()
411334
t.Logf("Example namespace: %s", hexStr)
412-
335+
413336
// Verify it matches the expected format
414337
if len(hexStr) != 60 {
415338
t.Errorf("Hex string should be 60 chars (0x + 58 hex chars), got %d", len(hexStr))
@@ -431,37 +354,37 @@ func TestRealWorldNamespaces(t *testing.T) {
431354
"test-headers",
432355
"test-data",
433356
}
434-
357+
435358
seen := make(map[string]bool)
436-
359+
437360
for _, nsStr := range namespaces {
438361
t.Run(nsStr, func(t *testing.T) {
439362
// Convert string to namespace
440-
nsBytes := PrepareNamespace([]byte(nsStr))
441-
363+
nsBytes := NamespaceFromString(nsStr)
364+
442365
// Verify it's valid
443-
ns, err := NamespaceFromBytes(nsBytes)
366+
ns, err := NamespaceFromBytes(nsBytes.Bytes())
444367
if err != nil {
445368
t.Fatalf("unexpected error: %v", err)
446369
}
447370
if !ns.IsValidForVersion0() {
448371
t.Error("namespace should be valid for version 0")
449372
}
450-
373+
451374
// Verify uniqueness
452-
hexStr := hex.EncodeToString(nsBytes)
375+
hexStr := hex.EncodeToString(nsBytes.Bytes())
453376
if seen[hexStr] {
454377
t.Errorf("Namespace should be unique, but %s was already seen", hexStr)
455378
}
456379
seen[hexStr] = true
457-
380+
458381
// Verify deterministic
459-
nsBytes2 := PrepareNamespace([]byte(nsStr))
460-
if !bytes.Equal(nsBytes, nsBytes2) {
382+
nsBytes2 := NamespaceFromString(nsStr)
383+
if !bytes.Equal(nsBytes.Bytes(), nsBytes2.Bytes()) {
461384
t.Error("Should be deterministic")
462385
}
463-
464-
t.Logf("Namespace for '%s': %s", nsStr, hex.EncodeToString(nsBytes))
386+
387+
t.Logf("Namespace for '%s': %s", nsStr, hex.EncodeToString(nsBytes.Bytes()))
465388
})
466389
}
467-
}
390+
}

0 commit comments

Comments
 (0)