refactor: buffer, channel, rate, and resolver packages to introduce constants for magic numbers

This commit is contained in:
2025-12-29 22:38:39 -06:00
parent d465f103ec
commit b60d91aa17
4 changed files with 76 additions and 29 deletions

View File

@@ -10,6 +10,17 @@ import (
"git.quad4.io/Networks/Reticulum-Go/pkg/identity"
)
const (
// Hash length conversion (bits to bytes)
BitsPerByte = 8
// Known destination data index
KnownDestIdentityIndex = 2
// Minimum name parts for hierarchical resolution
MinNameParts = 2
)
type Resolver struct {
cache map[string]*identity.Identity
cacheLock sync.RWMutex
@@ -36,12 +47,12 @@ func (r *Resolver) ResolveIdentity(fullName string) (*identity.Identity, error)
// Hash the full name to create a deterministic identity
h := sha256.New()
h.Write([]byte(fullName))
nameHash := h.Sum(nil)[:identity.NAME_HASH_LENGTH/8]
nameHash := h.Sum(nil)[:identity.NAME_HASH_LENGTH/BitsPerByte]
hashStr := hex.EncodeToString(nameHash)
// Check if this identity is known
if knownData, exists := identity.GetKnownDestination(hashStr); exists {
if id, ok := knownData[2].(*identity.Identity); ok {
if id, ok := knownData[KnownDestIdentityIndex].(*identity.Identity); ok {
r.cacheLock.Lock()
r.cache[fullName] = id
r.cacheLock.Unlock()
@@ -51,7 +62,7 @@ func (r *Resolver) ResolveIdentity(fullName string) (*identity.Identity, error)
// Split name into parts for hierarchical resolution
parts := strings.Split(fullName, ".")
if len(parts) < 2 {
if len(parts) < MinNameParts {
return nil, errors.New("invalid identity name format")
}