refactor(node): clarify AX filesystem and message names
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
48cd87e081
commit
ca885ff386
9 changed files with 83 additions and 81 deletions
|
|
@ -55,7 +55,7 @@ type Logger struct {
|
|||
|
||||
// Config holds configuration for creating a new Logger.
|
||||
//
|
||||
// cfg := Config{Output: io.Discard, Level: LevelDebug, Component: "sync"}
|
||||
// config := Config{Output: io.Discard, Level: LevelDebug, Component: "sync"}
|
||||
type Config struct {
|
||||
Output io.Writer
|
||||
Level Level
|
||||
|
|
@ -64,7 +64,7 @@ type Config struct {
|
|||
|
||||
// DefaultConfig returns the default logger configuration.
|
||||
//
|
||||
// cfg := DefaultConfig()
|
||||
// config := DefaultConfig()
|
||||
func DefaultConfig() Config {
|
||||
return Config{
|
||||
Output: defaultOutput,
|
||||
|
|
@ -76,14 +76,14 @@ func DefaultConfig() Config {
|
|||
// New creates a logger from an explicit configuration.
|
||||
//
|
||||
// logger := New(DefaultConfig())
|
||||
func New(cfg Config) *Logger {
|
||||
if cfg.Output == nil {
|
||||
cfg.Output = defaultOutput
|
||||
func New(config Config) *Logger {
|
||||
if config.Output == nil {
|
||||
config.Output = defaultOutput
|
||||
}
|
||||
return &Logger{
|
||||
output: cfg.Output,
|
||||
level: cfg.Level,
|
||||
component: cfg.Component,
|
||||
output: config.Output,
|
||||
level: config.Level,
|
||||
component: config.Component,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -144,7 +144,7 @@ func (stderrWriter) Write(p []byte) (int, error) {
|
|||
var defaultOutput io.Writer = stderrWriter{}
|
||||
|
||||
// log writes a log message at the specified level.
|
||||
func (l *Logger) log(level Level, msg string, fields Fields) {
|
||||
func (l *Logger) log(level Level, message string, fields Fields) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
|
|
@ -167,7 +167,7 @@ func (l *Logger) log(level Level, msg string, fields Fields) {
|
|||
}
|
||||
|
||||
sb.WriteString(" ")
|
||||
sb.WriteString(msg)
|
||||
sb.WriteString(message)
|
||||
|
||||
// Add fields if present
|
||||
if len(fields) > 0 {
|
||||
|
|
@ -185,23 +185,23 @@ func (l *Logger) log(level Level, msg string, fields Fields) {
|
|||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
func (l *Logger) Debug(msg string, fields ...Fields) {
|
||||
l.log(LevelDebug, msg, mergeFields(fields))
|
||||
func (l *Logger) Debug(message string, fields ...Fields) {
|
||||
l.log(LevelDebug, message, mergeFields(fields))
|
||||
}
|
||||
|
||||
// Info logs an informational message.
|
||||
func (l *Logger) Info(msg string, fields ...Fields) {
|
||||
l.log(LevelInfo, msg, mergeFields(fields))
|
||||
func (l *Logger) Info(message string, fields ...Fields) {
|
||||
l.log(LevelInfo, message, mergeFields(fields))
|
||||
}
|
||||
|
||||
// Warn logs a warning message.
|
||||
func (l *Logger) Warn(msg string, fields ...Fields) {
|
||||
l.log(LevelWarn, msg, mergeFields(fields))
|
||||
func (l *Logger) Warn(message string, fields ...Fields) {
|
||||
l.log(LevelWarn, message, mergeFields(fields))
|
||||
}
|
||||
|
||||
// Error logs an error message.
|
||||
func (l *Logger) Error(msg string, fields ...Fields) {
|
||||
l.log(LevelError, msg, mergeFields(fields))
|
||||
func (l *Logger) Error(message string, fields ...Fields) {
|
||||
l.log(LevelError, message, mergeFields(fields))
|
||||
}
|
||||
|
||||
// Debugf logs a formatted debug message.
|
||||
|
|
@ -280,29 +280,29 @@ func SetGlobalLevel(level Level) {
|
|||
// Debug logs a debug message using the global logger.
|
||||
//
|
||||
// Debug("connected", Fields{"peer_id": "node-1"})
|
||||
func Debug(msg string, fields ...Fields) {
|
||||
Global().Debug(msg, fields...)
|
||||
func Debug(message string, fields ...Fields) {
|
||||
Global().Debug(message, fields...)
|
||||
}
|
||||
|
||||
// Info logs an informational message using the global logger.
|
||||
//
|
||||
// Info("worker started", Fields{"component": "transport"})
|
||||
func Info(msg string, fields ...Fields) {
|
||||
Global().Info(msg, fields...)
|
||||
func Info(message string, fields ...Fields) {
|
||||
Global().Info(message, fields...)
|
||||
}
|
||||
|
||||
// Warn logs a warning message using the global logger.
|
||||
//
|
||||
// Warn("peer rate limited", Fields{"peer_id": "node-1"})
|
||||
func Warn(msg string, fields ...Fields) {
|
||||
Global().Warn(msg, fields...)
|
||||
func Warn(message string, fields ...Fields) {
|
||||
Global().Warn(message, fields...)
|
||||
}
|
||||
|
||||
// Error logs an error message using the global logger.
|
||||
//
|
||||
// Error("send failed", Fields{"peer_id": "node-1"})
|
||||
func Error(msg string, fields ...Fields) {
|
||||
Global().Error(msg, fields...)
|
||||
func Error(message string, fields ...Fields) {
|
||||
Global().Error(message, fields...)
|
||||
}
|
||||
|
||||
// Debugf logs a formatted debug message using the global logger.
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ func testNodeManagerPaths(dir string) (string, string) {
|
|||
|
||||
func testWriteFile(t *testing.T, path string, content []byte, mode fs.FileMode) {
|
||||
t.Helper()
|
||||
require.NoError(t, fsResultError(localFS.WriteMode(path, string(content), mode)))
|
||||
require.NoError(t, filesystemResultError(localFileSystem.WriteMode(path, string(content), mode)))
|
||||
}
|
||||
|
||||
func testReadFile(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
content, err := fsRead(path)
|
||||
content, err := filesystemRead(path)
|
||||
require.NoError(t, err)
|
||||
return []byte(content)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ func CreateProfileBundleUnencrypted(profileJSON []byte, name string) (*Bundle, e
|
|||
// bundle, err := CreateMinerBundle("/srv/miners/xmrig", profileJSON, "xmrig", "password")
|
||||
func CreateMinerBundle(minerPath string, profileJSON []byte, name string, password string) (*Bundle, error) {
|
||||
// Read miner binary
|
||||
minerContent, err := fsRead(minerPath)
|
||||
minerContent, err := filesystemRead(minerPath)
|
||||
if err != nil {
|
||||
return nil, core.E("CreateMinerBundle", "failed to read miner binary", err)
|
||||
}
|
||||
|
|
@ -286,7 +286,7 @@ func extractTarball(tarData []byte, destDir string) (string, error) {
|
|||
absDestDir = core.CleanPath(absDestDir, pathSeparator)
|
||||
}
|
||||
|
||||
if err := fsEnsureDir(absDestDir); err != nil {
|
||||
if err := filesystemEnsureDir(absDestDir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
|
@ -329,12 +329,12 @@ func extractTarball(tarData []byte, destDir string) (string, error) {
|
|||
|
||||
switch hdr.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if err := fsEnsureDir(fullPath); err != nil {
|
||||
if err := filesystemEnsureDir(fullPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
// Ensure parent directory exists
|
||||
if err := fsEnsureDir(core.PathDir(fullPath)); err != nil {
|
||||
if err := filesystemEnsureDir(core.PathDir(fullPath)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
|
@ -346,10 +346,10 @@ func extractTarball(tarData []byte, destDir string) (string, error) {
|
|||
return "", core.E("extractTarball", "failed to write file "+hdr.Name, err)
|
||||
}
|
||||
if int64(len(content)) > maxFileSize {
|
||||
fsDelete(fullPath)
|
||||
filesystemDelete(fullPath)
|
||||
return "", core.E("extractTarball", "file "+hdr.Name+" exceeds maximum size", nil)
|
||||
}
|
||||
if err := fsResultError(localFS.WriteMode(fullPath, string(content), fs.FileMode(hdr.Mode))); err != nil {
|
||||
if err := filesystemResultError(localFileSystem.WriteMode(fullPath, string(content), fs.FileMode(hdr.Mode))); err != nil {
|
||||
return "", core.E("extractTarball", "failed to create file "+hdr.Name, err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -430,7 +430,7 @@ func TestBundle_ExtractTarball_PathTraversal_Bad(t *testing.T) {
|
|||
|
||||
// Verify symlink was not created
|
||||
linkPath := testJoinPath(tmpDir, "link")
|
||||
if fsExists(linkPath) {
|
||||
if filesystemExists(linkPath) {
|
||||
t.Error("symlink should not be created")
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,43 +4,45 @@ package node
|
|||
|
||||
import core "dappco.re/go/core"
|
||||
|
||||
var localFS = (&core.Fs{}).New("/")
|
||||
// localFileSystem is the package-scoped filesystem rooted at `/` so node code
|
||||
// can use Core file operations without os helpers.
|
||||
var localFileSystem = (&core.Fs{}).New("/")
|
||||
|
||||
func fsEnsureDir(path string) error {
|
||||
return fsResultError(localFS.EnsureDir(path))
|
||||
func filesystemEnsureDir(path string) error {
|
||||
return filesystemResultError(localFileSystem.EnsureDir(path))
|
||||
}
|
||||
|
||||
func fsWrite(path, content string) error {
|
||||
return fsResultError(localFS.Write(path, content))
|
||||
func filesystemWrite(path, content string) error {
|
||||
return filesystemResultError(localFileSystem.Write(path, content))
|
||||
}
|
||||
|
||||
func fsRead(path string) (string, error) {
|
||||
result := localFS.Read(path)
|
||||
func filesystemRead(path string) (string, error) {
|
||||
result := localFileSystem.Read(path)
|
||||
if !result.OK {
|
||||
return "", fsResultError(result)
|
||||
return "", filesystemResultError(result)
|
||||
}
|
||||
|
||||
content, ok := result.Value.(string)
|
||||
if !ok {
|
||||
return "", core.E("node.fsRead", "filesystem read returned non-string content", nil)
|
||||
return "", core.E("node.filesystemRead", "filesystem read returned non-string content", nil)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func fsDelete(path string) error {
|
||||
return fsResultError(localFS.Delete(path))
|
||||
func filesystemDelete(path string) error {
|
||||
return filesystemResultError(localFileSystem.Delete(path))
|
||||
}
|
||||
|
||||
func fsRename(oldPath, newPath string) error {
|
||||
return fsResultError(localFS.Rename(oldPath, newPath))
|
||||
func filesystemRename(oldPath, newPath string) error {
|
||||
return filesystemResultError(localFileSystem.Rename(oldPath, newPath))
|
||||
}
|
||||
|
||||
func fsExists(path string) bool {
|
||||
return localFS.Exists(path)
|
||||
func filesystemExists(path string) bool {
|
||||
return localFileSystem.Exists(path)
|
||||
}
|
||||
|
||||
func fsResultError(result core.Result) error {
|
||||
func filesystemResultError(result core.Result) error {
|
||||
if result.OK {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ func NewNodeManagerFromPaths(keyPath, configPath string) (*NodeManager, error) {
|
|||
}
|
||||
|
||||
// Missing files indicate a first run; anything else is a load failure.
|
||||
if !fsExists(keyPath) && !fsExists(configPath) {
|
||||
if !filesystemExists(keyPath) && !filesystemExists(configPath) {
|
||||
return nm, nil
|
||||
}
|
||||
|
||||
|
|
@ -237,12 +237,12 @@ func (n *NodeManager) DeriveSharedSecret(peerPubKeyBase64 string) ([]byte, error
|
|||
func (n *NodeManager) savePrivateKey() error {
|
||||
// Ensure directory exists
|
||||
dir := core.PathDir(n.keyPath)
|
||||
if err := fsEnsureDir(dir); err != nil {
|
||||
if err := filesystemEnsureDir(dir); err != nil {
|
||||
return core.E("NodeManager.savePrivateKey", "failed to create key directory", err)
|
||||
}
|
||||
|
||||
// Write private key
|
||||
if err := fsWrite(n.keyPath, string(n.privateKey)); err != nil {
|
||||
if err := filesystemWrite(n.keyPath, string(n.privateKey)); err != nil {
|
||||
return core.E("NodeManager.savePrivateKey", "failed to write private key", err)
|
||||
}
|
||||
|
||||
|
|
@ -253,7 +253,7 @@ func (n *NodeManager) savePrivateKey() error {
|
|||
func (n *NodeManager) saveIdentity() error {
|
||||
// Ensure directory exists
|
||||
dir := core.PathDir(n.configPath)
|
||||
if err := fsEnsureDir(dir); err != nil {
|
||||
if err := filesystemEnsureDir(dir); err != nil {
|
||||
return core.E("NodeManager.saveIdentity", "failed to create config directory", err)
|
||||
}
|
||||
|
||||
|
|
@ -263,7 +263,7 @@ func (n *NodeManager) saveIdentity() error {
|
|||
}
|
||||
data := result.Value.([]byte)
|
||||
|
||||
if err := fsWrite(n.configPath, string(data)); err != nil {
|
||||
if err := filesystemWrite(n.configPath, string(data)); err != nil {
|
||||
return core.E("NodeManager.saveIdentity", "failed to write identity", err)
|
||||
}
|
||||
|
||||
|
|
@ -273,7 +273,7 @@ func (n *NodeManager) saveIdentity() error {
|
|||
// loadIdentity loads the node identity from disk.
|
||||
func (n *NodeManager) loadIdentity() error {
|
||||
// Load identity config
|
||||
content, err := fsRead(n.configPath)
|
||||
content, err := filesystemRead(n.configPath)
|
||||
if err != nil {
|
||||
return core.E("NodeManager.loadIdentity", "failed to read identity", err)
|
||||
}
|
||||
|
|
@ -285,7 +285,7 @@ func (n *NodeManager) loadIdentity() error {
|
|||
}
|
||||
|
||||
// Load private key
|
||||
keyContent, err := fsRead(n.keyPath)
|
||||
keyContent, err := filesystemRead(n.keyPath)
|
||||
if err != nil {
|
||||
return core.E("NodeManager.loadIdentity", "failed to read private key", err)
|
||||
}
|
||||
|
|
@ -312,15 +312,15 @@ func (n *NodeManager) Delete() error {
|
|||
defer n.mu.Unlock()
|
||||
|
||||
// Remove private key (ignore if already absent)
|
||||
if fsExists(n.keyPath) {
|
||||
if err := fsDelete(n.keyPath); err != nil {
|
||||
if filesystemExists(n.keyPath) {
|
||||
if err := filesystemDelete(n.keyPath); err != nil {
|
||||
return core.E("NodeManager.Delete", "failed to remove private key", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove identity config (ignore if already absent)
|
||||
if fsExists(n.configPath) {
|
||||
if err := fsDelete(n.configPath); err != nil {
|
||||
if filesystemExists(n.configPath) {
|
||||
if err := filesystemDelete(n.configPath); err != nil {
|
||||
return core.E("NodeManager.Delete", "failed to remove identity", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ func IsProtocolVersionSupported(version string) bool {
|
|||
|
||||
// MessageType defines the type of P2P message.
|
||||
//
|
||||
// msgType := MsgPing
|
||||
// messageType := MsgPing
|
||||
type MessageType string
|
||||
|
||||
const (
|
||||
|
|
@ -92,7 +92,7 @@ const (
|
|||
|
||||
// Message represents a P2P message between nodes.
|
||||
//
|
||||
// msg, err := NewMessage(MsgPing, "controller", "worker", PingPayload{SentAt: time.Now().UnixMilli()})
|
||||
// message, err := NewMessage(MsgPing, "controller", "worker", PingPayload{SentAt: time.Now().UnixMilli()})
|
||||
type Message struct {
|
||||
ID string `json:"id"` // UUID
|
||||
Type MessageType `json:"type"`
|
||||
|
|
@ -105,8 +105,8 @@ type Message struct {
|
|||
|
||||
// NewMessage builds a message with a generated ID and timestamp.
|
||||
//
|
||||
// msg, err := NewMessage(MsgPing, "controller", "worker-1", PingPayload{SentAt: 42})
|
||||
func NewMessage(msgType MessageType, from, to string, payload any) (*Message, error) {
|
||||
// message, err := NewMessage(MsgPing, "controller", "worker-1", PingPayload{SentAt: 42})
|
||||
func NewMessage(messageType MessageType, from, to string, payload any) (*Message, error) {
|
||||
var payloadBytes RawMessage
|
||||
if payload != nil {
|
||||
data, err := MarshalJSON(payload)
|
||||
|
|
@ -118,7 +118,7 @@ func NewMessage(msgType MessageType, from, to string, payload any) (*Message, er
|
|||
|
||||
return &Message{
|
||||
ID: uuid.New().String(),
|
||||
Type: msgType,
|
||||
Type: messageType,
|
||||
From: from,
|
||||
To: to,
|
||||
Timestamp: time.Now(),
|
||||
|
|
@ -128,9 +128,9 @@ func NewMessage(msgType MessageType, from, to string, payload any) (*Message, er
|
|||
|
||||
// Reply creates a response message that points back to the original.
|
||||
//
|
||||
// reply, err := msg.Reply(MsgPong, PongPayload{SentAt: 42, ReceivedAt: 43})
|
||||
func (m *Message) Reply(msgType MessageType, payload any) (*Message, error) {
|
||||
reply, err := NewMessage(msgType, m.To, m.From, payload)
|
||||
// reply, err := message.Reply(MsgPong, PongPayload{SentAt: 42, ReceivedAt: 43})
|
||||
func (m *Message) Reply(messageType MessageType, payload any) (*Message, error) {
|
||||
reply, err := NewMessage(messageType, m.To, m.From, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -141,12 +141,12 @@ func (m *Message) Reply(msgType MessageType, payload any) (*Message, error) {
|
|||
// ParsePayload decodes the payload into the supplied target.
|
||||
//
|
||||
// var ping PingPayload
|
||||
// err := msg.ParsePayload(&ping)
|
||||
func (m *Message) ParsePayload(v any) error {
|
||||
// err := message.ParsePayload(&ping)
|
||||
func (m *Message) ParsePayload(target any) error {
|
||||
if m.Payload == nil {
|
||||
return nil
|
||||
}
|
||||
result := core.JSONUnmarshal(m.Payload, v)
|
||||
result := core.JSONUnmarshal(m.Payload, target)
|
||||
if !result.OK {
|
||||
return result.Value.(error)
|
||||
}
|
||||
|
|
|
|||
12
node/peer.go
12
node/peer.go
|
|
@ -150,7 +150,7 @@ func NewPeerRegistryFromPath(peersPath string) (*PeerRegistry, error) {
|
|||
}
|
||||
|
||||
// Missing files indicate a first run; any existing file must parse cleanly.
|
||||
if !fsExists(peersPath) {
|
||||
if !filesystemExists(peersPath) {
|
||||
pr.rebuildKDTree()
|
||||
return pr, nil
|
||||
}
|
||||
|
|
@ -710,7 +710,7 @@ func (r *PeerRegistry) scheduleSave() {
|
|||
func (r *PeerRegistry) saveNow() error {
|
||||
// Ensure directory exists
|
||||
dir := core.PathDir(r.path)
|
||||
if err := fsEnsureDir(dir); err != nil {
|
||||
if err := filesystemEnsureDir(dir); err != nil {
|
||||
return core.E("PeerRegistry.saveNow", "failed to create peers directory", err)
|
||||
}
|
||||
|
||||
|
|
@ -725,12 +725,12 @@ func (r *PeerRegistry) saveNow() error {
|
|||
|
||||
// Use atomic write pattern: write to temp file, then rename
|
||||
tmpPath := r.path + ".tmp"
|
||||
if err := fsWrite(tmpPath, string(data)); err != nil {
|
||||
if err := filesystemWrite(tmpPath, string(data)); err != nil {
|
||||
return core.E("PeerRegistry.saveNow", "failed to write peers temp file", err)
|
||||
}
|
||||
|
||||
if err := fsRename(tmpPath, r.path); err != nil {
|
||||
fsDelete(tmpPath) // Clean up temp file
|
||||
if err := filesystemRename(tmpPath, r.path); err != nil {
|
||||
filesystemDelete(tmpPath) // Clean up temp file
|
||||
return core.E("PeerRegistry.saveNow", "failed to rename peers file", err)
|
||||
}
|
||||
|
||||
|
|
@ -773,7 +773,7 @@ func (r *PeerRegistry) save() error {
|
|||
|
||||
// load reads peers from disk.
|
||||
func (r *PeerRegistry) load() error {
|
||||
content, err := fsRead(r.path)
|
||||
content, err := filesystemRead(r.path)
|
||||
if err != nil {
|
||||
return core.E("PeerRegistry.load", "failed to read peers", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -885,7 +885,7 @@ func TestPeer_Registry_SaveNow_Good(t *testing.T) {
|
|||
}
|
||||
|
||||
// Verify the file was written
|
||||
if !fsExists(peersPath) {
|
||||
if !filesystemExists(peersPath) {
|
||||
t.Error("peers.json should exist after saveNow")
|
||||
}
|
||||
}
|
||||
|
|
@ -908,7 +908,7 @@ func TestPeer_Registry_ScheduleSave_TimerFires_Ugly(t *testing.T) {
|
|||
time.Sleep(6 * time.Second)
|
||||
|
||||
// The file should have been saved by the timer
|
||||
if !fsExists(peersPath) {
|
||||
if !filesystemExists(peersPath) {
|
||||
t.Error("peers.json should exist after debounce timer fires")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue