Update and refactor core functionality
- Update common package utilities - Refactor network code for better error handling - Remove deprecated files and functionality - Enhance blacklist and filtering capabilities - Improve snapshot handling and processing
This commit is contained in:
34
util/util.go
34
util/util.go
@@ -6,14 +6,8 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
func PrintStackAndPanic(err error) {
|
||||
fmt.Printf("PANIC Error %s Stack trace:\n%s", err, debug.Stack())
|
||||
panic("PANIC")
|
||||
}
|
||||
|
||||
// SecureRandomInt returns a cryptographically secure random integer in the range [0,max).
|
||||
// Panics if max <= 0 or if there's an error reading from the system's secure
|
||||
// random number generator.
|
||||
@@ -24,14 +18,14 @@ func SecureRandomInt(max int) int {
|
||||
// Generate random number
|
||||
n, err := rand.Int(rand.Reader, maxBig)
|
||||
if err != nil {
|
||||
PrintStackAndPanic(fmt.Errorf("could not generate a random integer between 0 and %d", max))
|
||||
panic(fmt.Errorf("could not generate a random integer between 0 and %d", max))
|
||||
}
|
||||
|
||||
// Convert back to int
|
||||
return int(n.Int64())
|
||||
}
|
||||
|
||||
func PrettyJson(data string) string {
|
||||
func PrettifyJson(data string) string {
|
||||
marshalled, _ := json.MarshalIndent(data, "", " ")
|
||||
return fmt.Sprintf("%s\n", marshalled)
|
||||
}
|
||||
@@ -42,3 +36,27 @@ func GetLinesMatchingRegex(input string, pattern string) []string {
|
||||
matches := re.FindAllString(input, -1)
|
||||
return matches
|
||||
}
|
||||
|
||||
// Filter applies a predicate function to each element in a slice and returns a new slice
|
||||
// containing only the elements for which the predicate returns true.
|
||||
// Type parameter T allows this function to work with slices of any type.
|
||||
func Filter[T any](slice []T, f func(T) bool) []T {
|
||||
filtered := make([]T, 0)
|
||||
for _, v := range slice {
|
||||
if f(v) {
|
||||
filtered = append(filtered, v)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// Map applies a function to each element in a slice and returns a new slice
|
||||
// containing the results.
|
||||
// Type parameters T and R allow this function to work with different input and output types.
|
||||
func Map[T any, R any](slice []T, f func(T) R) []R {
|
||||
result := make([]R, len(slice))
|
||||
for i, v := range slice {
|
||||
result[i] = f(v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user