diff --git a/logging/compat.go b/logging/compat.go new file mode 100644 index 0000000..404910c --- /dev/null +++ b/logging/compat.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: EUPL-1.2 + +package logging + +// WithComponent returns a new Logger scoped to one component. +// Deprecated: Use ComponentLogger instead. +// +// transportLogger := logger.WithComponent("transport") +func (l *Logger) WithComponent(component string) *Logger { + return l.ComponentLogger(component) +} + +// GetLevel returns the current log level. +// Preferred over Level for AX naming consistency. +// +// level := logger.GetLevel() +func (l *Logger) GetLevel() Level { + return l.Level() +} + +// GetGlobal returns the global logger instance. +// Deprecated: Use Global instead. +// +// logger := GetGlobal() +func GetGlobal() *Logger { + return Global() +} diff --git a/logging/compat_test.go b/logging/compat_test.go new file mode 100644 index 0000000..1310c17 --- /dev/null +++ b/logging/compat_test.go @@ -0,0 +1,39 @@ +package logging + +import ( + "bytes" + "testing" + + core "dappco.re/go/core" +) + +func TestGetLevel_Good(t *testing.T) { + logger := New(Config{Output: &bytes.Buffer{}, Level: LevelWarn}) + if logger.GetLevel() != LevelWarn { + t.Errorf("GetLevel() = %v, want %v", logger.GetLevel(), LevelWarn) + } + if logger.GetLevel() != logger.Level() { + t.Error("GetLevel() and Level() should return the same value") + } +} + +func TestWithComponent_Good(t *testing.T) { + var buf bytes.Buffer + parent := New(Config{Output: &buf, Level: LevelInfo}) + child := parent.WithComponent("TestChild") + child.Info("test message") + if !core.Contains(buf.String(), "[TestChild]") { + t.Error("WithComponent should set the component name") + } +} + +func TestGetGlobal_Good(t *testing.T) { + var buf bytes.Buffer + logger := New(Config{Output: &buf, Level: LevelInfo}) + SetGlobal(logger) + got := GetGlobal() + if got != Global() { + t.Error("GetGlobal() and Global() should return the same logger") + } + SetGlobal(New(DefaultConfig())) +} diff --git a/node/compat.go b/node/compat.go new file mode 100644 index 0000000..fce12ad --- /dev/null +++ b/node/compat.go @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: EUPL-1.2 + +// This file provides deprecated compatibility aliases and type aliases +// for spec compatibility. Canonical names are defined in their respective +// source files; these aliases exist so that code written against the spec +// documentation continues to compile. + +package node + +// NewNodeManagerWithPaths loads or creates a node identity store at explicit paths. +// Deprecated: Use NewNodeManagerFromPaths instead. +// +// nodeManager, err := NewNodeManagerWithPaths("/srv/p2p/private.key", "/srv/p2p/node.json") +func NewNodeManagerWithPaths(keyPath, configPath string) (*NodeManager, error) { + return NewNodeManagerFromPaths(keyPath, configPath) +} + +// NewPeerRegistryWithPath loads or creates a peer registry at an explicit path. +// Deprecated: Use NewPeerRegistryFromPath instead. +// +// peerRegistry, err := NewPeerRegistryWithPath("/srv/p2p/peers.json") +func NewPeerRegistryWithPath(peersPath string) (*PeerRegistry, error) { + return NewPeerRegistryFromPath(peersPath) +} + +// RegisterWithTransport installs the worker message handler on the transport. +// Deprecated: Use RegisterOnTransport instead. +// +// worker.RegisterWithTransport() +func (w *Worker) RegisterWithTransport() { + w.RegisterOnTransport() +} + +// ConnectedPeers returns the number of connected peers. +// Deprecated: Use ConnectedPeerCount instead. +// +// count := transport.ConnectedPeers() +func (t *Transport) ConnectedPeers() int { + return t.ConnectedPeerCount() +} + +// GetLogsPayload is an alias for LogsRequestPayload. +// Provided for spec compatibility. +// +// payload := GetLogsPayload{MinerName: "xmrig-0", Lines: 100} +type GetLogsPayload = LogsRequestPayload diff --git a/node/compat_test.go b/node/compat_test.go new file mode 100644 index 0000000..9b197aa --- /dev/null +++ b/node/compat_test.go @@ -0,0 +1,57 @@ +package node + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNewNodeManagerWithPaths_Good(t *testing.T) { + keyPath := t.TempDir() + "/private.key" + configPath := t.TempDir() + "/node.json" + + nm, err := NewNodeManagerWithPaths(keyPath, configPath) + require.NoError(t, err) + assert.NotNil(t, nm) + assert.False(t, nm.HasIdentity(), "fresh manager should have no identity") +} + +func TestNewPeerRegistryWithPath_Good(t *testing.T) { + peersPath := t.TempDir() + "/peers.json" + + pr, err := NewPeerRegistryWithPath(peersPath) + require.NoError(t, err) + assert.NotNil(t, pr) + assert.Equal(t, 0, pr.Count()) + pr.Close() +} + +func TestGetLogsPayload_Good(t *testing.T) { + var payload GetLogsPayload + payload.MinerName = "xmrig-0" + payload.Lines = 100 + payload.Since = 1234567890 + + var request LogsRequestPayload = payload + assert.Equal(t, "xmrig-0", request.MinerName) + assert.Equal(t, 100, request.Lines) + assert.Equal(t, int64(1234567890), request.Since) +} + +func TestTransportConnectedPeers_Good(t *testing.T) { + keyPath := t.TempDir() + "/private.key" + configPath := t.TempDir() + "/node.json" + peersPath := t.TempDir() + "/peers.json" + + nm, err := NewNodeManagerFromPaths(keyPath, configPath) + require.NoError(t, err) + + pr, err := NewPeerRegistryFromPath(peersPath) + require.NoError(t, err) + defer pr.Close() + + transport := NewTransport(nm, pr, DefaultTransportConfig()) + assert.Equal(t, transport.ConnectedPeerCount(), transport.ConnectedPeers()) + assert.Equal(t, 0, transport.ConnectedPeers()) +} diff --git a/node/levin/compat.go b/node/levin/compat.go new file mode 100644 index 0000000..a88bdfb --- /dev/null +++ b/node/levin/compat.go @@ -0,0 +1,87 @@ +// Copyright (c) 2024-2026 Lethean Contributors +// SPDX-License-Identifier: EUPL-1.2 + +// This file provides short-form aliases for Value constructors, +// matching the naming used in the spec documentation. + +package levin + +// Uint64Val creates a Value of TypeUint64. Short-form alias for Uint64Value. +// +// value := Uint64Val(42) +func Uint64Val(v uint64) Value { return Uint64Value(v) } + +// Uint32Val creates a Value of TypeUint32. Short-form alias for Uint32Value. +// +// value := Uint32Val(42) +func Uint32Val(v uint32) Value { return Uint32Value(v) } + +// Uint16Val creates a Value of TypeUint16. Short-form alias for Uint16Value. +// +// value := Uint16Val(42) +func Uint16Val(v uint16) Value { return Uint16Value(v) } + +// Uint8Val creates a Value of TypeUint8. Short-form alias for Uint8Value. +// +// value := Uint8Val(42) +func Uint8Val(v uint8) Value { return Uint8Value(v) } + +// Int64Val creates a Value of TypeInt64. Short-form alias for Int64Value. +// +// value := Int64Val(42) +func Int64Val(v int64) Value { return Int64Value(v) } + +// Int32Val creates a Value of TypeInt32. Short-form alias for Int32Value. +// +// value := Int32Val(42) +func Int32Val(v int32) Value { return Int32Value(v) } + +// Int16Val creates a Value of TypeInt16. Short-form alias for Int16Value. +// +// value := Int16Val(42) +func Int16Val(v int16) Value { return Int16Value(v) } + +// Int8Val creates a Value of TypeInt8. Short-form alias for Int8Value. +// +// value := Int8Val(42) +func Int8Val(v int8) Value { return Int8Value(v) } + +// BoolVal creates a Value of TypeBool. Short-form alias for BoolValue. +// +// value := BoolVal(true) +func BoolVal(v bool) Value { return BoolValue(v) } + +// DoubleVal creates a Value of TypeDouble. Short-form alias for DoubleValue. +// +// value := DoubleVal(3.14) +func DoubleVal(v float64) Value { return DoubleValue(v) } + +// StringVal creates a Value of TypeString. Short-form alias for StringValue. +// +// value := StringVal([]byte("hello")) +func StringVal(v []byte) Value { return StringValue(v) } + +// ObjectVal creates a Value of TypeObject. Short-form alias for ObjectValue. +// +// value := ObjectVal(Section{"id": StringVal([]byte("peer-1"))}) +func ObjectVal(s Section) Value { return ObjectValue(s) } + +// Uint64ArrayVal creates a typed array of uint64 values. Short-form alias for Uint64ArrayValue. +// +// value := Uint64ArrayVal([]uint64{1, 2, 3}) +func Uint64ArrayVal(vs []uint64) Value { return Uint64ArrayValue(vs) } + +// Uint32ArrayVal creates a typed array of uint32 values. Short-form alias for Uint32ArrayValue. +// +// value := Uint32ArrayVal([]uint32{1, 2, 3}) +func Uint32ArrayVal(vs []uint32) Value { return Uint32ArrayValue(vs) } + +// StringArrayVal creates a typed array of byte-string values. Short-form alias for StringArrayValue. +// +// value := StringArrayVal([][]byte{[]byte("a"), []byte("b")}) +func StringArrayVal(vs [][]byte) Value { return StringArrayValue(vs) } + +// ObjectArrayVal creates a typed array of Section values. Short-form alias for ObjectArrayValue. +// +// value := ObjectArrayVal([]Section{{"id": StringVal([]byte("peer-1"))}}) +func ObjectArrayVal(vs []Section) Value { return ObjectArrayValue(vs) } diff --git a/node/levin/compat_test.go b/node/levin/compat_test.go new file mode 100644 index 0000000..72de5f4 --- /dev/null +++ b/node/levin/compat_test.go @@ -0,0 +1,66 @@ +// Copyright (c) 2024-2026 Lethean Contributors +// SPDX-License-Identifier: EUPL-1.2 + +package levin + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestShortFormAliases_Good(t *testing.T) { + assert.Equal(t, Uint64Value(42), Uint64Val(42)) + assert.Equal(t, Uint32Value(42), Uint32Val(42)) + assert.Equal(t, Uint16Value(42), Uint16Val(42)) + assert.Equal(t, Uint8Value(42), Uint8Val(42)) + assert.Equal(t, Int64Value(-42), Int64Val(-42)) + assert.Equal(t, Int32Value(-42), Int32Val(-42)) + assert.Equal(t, Int16Value(-42), Int16Val(-42)) + assert.Equal(t, Int8Value(-42), Int8Val(-42)) + assert.Equal(t, BoolValue(true), BoolVal(true)) + assert.Equal(t, DoubleValue(3.14), DoubleVal(3.14)) + assert.Equal(t, StringValue([]byte("hello")), StringVal([]byte("hello"))) + + section := Section{"key": StringValue([]byte("value"))} + assert.Equal(t, ObjectValue(section), ObjectVal(section)) + + assert.Equal(t, Uint64ArrayValue([]uint64{1, 2}), Uint64ArrayVal([]uint64{1, 2})) + assert.Equal(t, Uint32ArrayValue([]uint32{1, 2}), Uint32ArrayVal([]uint32{1, 2})) + assert.Equal(t, StringArrayValue([][]byte{[]byte("a")}), StringArrayVal([][]byte{[]byte("a")})) + assert.Equal(t, ObjectArrayValue([]Section{section}), ObjectArrayVal([]Section{section})) +} + +func TestShortFormAliasesRoundTrip_Good(t *testing.T) { + s := Section{ + "u64": Uint64Val(0xCAFEBABE), + "str": StringVal([]byte("hello")), + "flag": BoolVal(true), + "obj": ObjectVal(Section{"nested": Int32Val(42)}), + } + + data, err := EncodeStorage(s) + require.NoError(t, err) + + decoded, err := DecodeStorage(data) + require.NoError(t, err) + + u64, err := decoded["u64"].AsUint64() + require.NoError(t, err) + assert.Equal(t, uint64(0xCAFEBABE), u64) + + str, err := decoded["str"].AsString() + require.NoError(t, err) + assert.Equal(t, []byte("hello"), str) + + flag, err := decoded["flag"].AsBool() + require.NoError(t, err) + assert.True(t, flag) + + obj, err := decoded["obj"].AsSection() + require.NoError(t, err) + nested, err := obj["nested"].AsInt32() + require.NoError(t, err) + assert.Equal(t, int32(42), nested) +} diff --git a/ueps/compat.go b/ueps/compat.go new file mode 100644 index 0000000..2b81812 --- /dev/null +++ b/ueps/compat.go @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: EUPL-1.2 + +package ueps + +// Short-form tag aliases for spec compatibility. +const ( + // TagCurrentLay is a short-form alias for TagCurrentLayer. + TagCurrentLay = TagCurrentLayer + // TagTargetLay is a short-form alias for TagTargetLayer. + TagTargetLay = TagTargetLayer +) diff --git a/ueps/compat_test.go b/ueps/compat_test.go new file mode 100644 index 0000000..d57fc01 --- /dev/null +++ b/ueps/compat_test.go @@ -0,0 +1,14 @@ +package ueps + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTagAliases_Good(t *testing.T) { + assert.Equal(t, TagCurrentLayer, TagCurrentLay) + assert.Equal(t, TagTargetLayer, TagTargetLay) + assert.Equal(t, 0x02, TagCurrentLay) + assert.Equal(t, 0x03, TagTargetLay) +}