Expand description
Utility functions for CHIE Protocol.
Structs§
- Bucketed
Time Series - Bucketed time series for aggregating metrics into fixed time intervals.
- Circuit
Breaker - Circuit breaker for fault tolerance
- Exponential
Backoff - Exponential backoff calculator for retry logic.
- Histogram
- Histogram for tracking value distributions (e.g., latency).
- Sliding
Window - Sliding window for time-series analytics.
- Sliding
Window Rate Limiter - Rate limiter using sliding window algorithm.
- Streaming
Stats - Streaming statistics calculator using Welford’s online algorithm. Computes mean, variance, and standard deviation in a single pass without storing all values in memory.
- Time
Bucket - Fixed-size time bucket for aggregating metrics over time periods.
- Time
Window - Time window for tracking events within a specific time period.
Enums§
- Circuit
State - Circuit breaker states
Functions§
- actual_
chunk_ size - Calculate the actual size of a specific chunk (last chunk may be smaller).
- average
- Calculate the average of two values.
- batch_
by_ size - Batch items into groups where the size of each batch is determined by a size function. Ensures no batch exceeds max_size.
- bps_
to_ mbps - Convert bits per second to megabits per second.
- byte_
to_ chunk_ index - Calculate which chunk contains a given byte offset. Returns the chunk index that contains the specified byte.
- bytes_
to_ gb_ f64 - Convert bytes to gigabytes with precision (f64 version for non-const contexts).
- calculate_
bandwidth_ mbps - Calculate bandwidth in Mbps from bytes and duration.
- calculate_
content_ price - Calculate content pricing based on size and demand.
- calculate_
creator_ share - Calculate creator share from total points.
- calculate_
demand_ multiplier - Calculate reward multiplier based on demand/supply ratio.
- calculate_
ema - Calculate exponential moving average.
- calculate_
growth_ rate - Calculate compound growth rate.
- calculate_
latency_ ms - Calculate latency from start and end timestamps.
- calculate_
mean - Calculate mean of values.
- calculate_
median - Calculate median of a slice of values.
- calculate_
moving_ average - Calculate moving average over a window.
- calculate_
percentage - Calculate percentage with proper rounding.
- calculate_
percentage_ change - Calculate percentage change between two values. Returns positive for increase, negative for decrease. Returns 0.0 if old_value is 0.
- calculate_
percentile - Calculate percentile of a slice of values (p should be between 0.0 and 1.0).
- calculate_
platform_ fee - Calculate platform fee from total points.
- calculate_
provider_ earnings - Calculate provider earnings after fees and creator share.
- calculate_
rate - Calculate rate (events per time unit). Returns events per second.
- calculate_
reputation_ bonus - Calculate reputation boost for consistent good behavior.
- calculate_
reputation_ decay - Calculate reputation score with decay over time.
- calculate_
reward_ with_ penalty - Calculate reward with multiplier and penalty based on latency.
- calculate_
sliding_ window_ count - Calculate sliding window rate limit.
- calculate_
stats - Calculate min, max, and average from a slice of values.
- calculate_
std_ dev - Calculate standard deviation from a slice of values.
- calculate_
storage_ cost - Calculate storage cost per month (points per GB).
- calculate_
token_ bucket - Calculate tokens available in a token bucket.
- calculate_
uptime_ percentage - Calculate uptime percentage from total time and downtime.
- calculate_
z_ score - Calculate z-score for anomaly detection.
- chunk_
byte_ range - Get the byte range (start, end) for a specific chunk. Returns (start_byte, end_byte_exclusive) for the chunk.
- chunk_
offset - Calculate the byte offset for a chunk index. Returns the starting byte position in the content for the given chunk.
- chunk_
vec - Split a vector into chunks of specified size.
Returns a vector of vectors, where each inner vector has at most
chunk_sizeelements. - cid_
to_ short_ id - Generate a short identifier (8 chars) from a CID for display purposes. Takes first 8 characters after “Qm” prefix if present, otherwise first 8 chars.
- clamp
- Clamp a value between a minimum and maximum.
- constant_
time_ eq - Constant-time comparison of two byte slices to prevent timing attacks. Returns true if slices are equal, false otherwise. This is critical for comparing signatures, hashes, and nonces securely.
- constant_
time_ eq_ 32 - Constant-time comparison of two 32-byte arrays (common for hashes/keys). Optimized version for fixed-size arrays.
- count_
set_ bits - Count the number of set bits (1s) in a byte slice.
- decode_
hex - Decode hexadecimal string to bytes.
- deduplicate_
preserve_ order - Deduplicate a vector while preserving order.
- encode_
hex - Encode bytes as hexadecimal string.
- estimate_
transfer_ time - Calculate estimated transfer time in seconds.
- extract_
peer_ id_ from_ multiaddr - Extract the peer ID from a libp2p multiaddress string. Example: “/ip4/127.0.0.1/tcp/4001/p2p/QmPeerId” -> “QmPeerId”
- find_
duplicates - Find duplicate items in a vector. Returns a vector of items that appear more than once.
- flatten
- Flatten a vector of vectors into a single vector.
- format_
bandwidth - Format bandwidth as string.
- format_
bytes - Format bytes as human-readable string (KB, MB, GB, TB).
- format_
duration_ ms - Format duration in milliseconds as human-readable string.
- format_
points - Format points with thousands separator.
- format_
ratio_ as_ percentage - Format a ratio as a percentage string.
- format_
timestamp - Format Unix timestamp (milliseconds) as human-readable string. Returns format like “2024-12-16 14:30:45 UTC”.
- gb_
to_ bytes_ f64 - Convert gigabytes to bytes (f64 version for non-const contexts).
- generate_
nonce - Generate a random nonce for challenge-response.
- generate_
session_ id - Generate a human-readable session ID.
- generate_
slug - Generate URL-friendly slug from a string. Converts to lowercase, replaces spaces/special chars with hyphens, removes consecutive hyphens.
- get_
file_ extension - Get file extension from filename. Returns the extension without the dot, or empty string if no extension.
- group_
by - Group items by a key extraction function. Returns a HashMap where keys are the result of the key function and values are vectors of items.
- has_
valid_ extension - Check if a filename has a valid extension (non-empty and alphanumeric).
- is_
all_ zeros - Check if all bytes in a slice are zero.
- is_
outlier_ iqr - Check if value is an outlier using IQR method.
- is_
private_ ipv4 - Check if an IPv4 address is in a private range. Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8.
- is_
rate_ limit_ allowed - Check if an action is allowed under rate limiting (token bucket).
- is_
safe_ string - Check if a string contains only safe characters (no control chars except whitespace).
- is_
timestamp_ valid - Check if a timestamp is within the valid range (not too old, not in future).
- is_
valid_ chunk_ index - Check if a chunk index is valid for the given content size.
- is_
valid_ cid - Parse content CID and validate basic format.
- is_
valid_ email - Validate email format (basic check).
- is_
valid_ hex - Verify hex string is valid.
- is_
valid_ ipv4 - Validate IPv4 address format.
- is_
valid_ ipv6 - Validate IPv6 address format (basic check).
- is_
valid_ multiaddr - Parse multiaddr and check if it’s valid (basic check).
- is_
valid_ peer_ id - Check if a peer ID format is valid (basic check).
- is_
valid_ peer_ id_ format - Validate peer ID format (basic check for IPFS/libp2p peer IDs). Checks if it starts with common prefixes and has reasonable length.
- is_
valid_ port - Validate port number.
- is_
valid_ url - Validate HTTP/HTTPS URL format.
- is_
valid_ username - Validate username (alphanumeric, underscore, hyphen, 3-20 chars).
- is_
within_ tolerance - Check if a value is within a tolerance of a target.
- lerp
- Linear interpolation between two values.
- mbps_
to_ bps - Convert megabits per second to bits per second.
- merge_
sorted - Merge two sorted vectors into a single sorted vector.
- mime_
to_ category_ hint - Detect common file types from MIME type strings. Returns a category hint that can help with content classification.
- ms_
to_ secs - Convert milliseconds to seconds.
- normalize
- Normalize a value to 0.0-1.0 range based on min and max bounds. Values outside bounds are clamped to 0.0 or 1.0.
- normalize_
tag - Normalize tag for search/comparison.
- now_ms
- Get current Unix timestamp in milliseconds.
- now_
secs - Get current Unix timestamp in seconds.
- parse_
bandwidth_ str - Parse bandwidth from string (e.g., “100 Mbps”, “1 Gbps”).
- parse_
duration_ str - Parse human-readable duration string to milliseconds. Supports formats like “1h30m”, “45s”, “2h”, “500ms”. Returns None if the format is invalid.
- partition
- Partition a vector into two vectors based on a predicate. Returns (matching, non_matching).
- random_
jitter - Apply random jitter to a value for backoff/retry timing.
- rotate_
bytes_ left - Rotate bytes left by n positions. Wraps around: [1,2,3,4] rotated left by 1 = [2,3,4,1]
- rotate_
bytes_ right - Rotate bytes right by n positions. Wraps around: [1,2,3,4] rotated right by 1 = [4,1,2,3]
- round_
down_ to_ multiple - Round down to the nearest multiple of n.
- round_
up_ to_ multiple - Round up to the nearest multiple of n.
- sanitize_
string - Sanitize string for display (remove control characters).
- sanitize_
tag - Sanitize a tag by trimming whitespace and converting to lowercase.
- sanitize_
tags - Validate and sanitize a list of tags.
- secs_
to_ ms - Convert seconds to milliseconds.
- skip
- Skip the first N items and return the rest.
- take
- Take the first N items from a vector.
- truncate_
string - Truncate a string to a maximum length, adding “…” if truncated.
- update_
reputation - Update reputation based on success/failure events.
- validate_
and_ sanitize_ tag - Sanitize and validate tag.
- validate_
bandwidth_ reasonable - Validate bandwidth is not suspiciously high.
- validate_
blake3_ hash - Validate BLAKE3 hash format (32 bytes).
- validate_
challenge_ nonce - Validate challenge nonce format (32 bytes).
- validate_
chunk_ indices_ batch - Batch validate chunk indices are within bounds.
- validate_
chunk_ size - Validate chunk size is within acceptable range.
- validate_
cids_ batch - Batch validate CIDs and return invalid ones.
- validate_
content_ size_ in_ range - Validate content size is within platform limits.
- validate_
ed25519_ public_ key - Validate Ed25519 public key format (32 bytes).
- validate_
ed25519_ signature - Validate Ed25519 signature format (64 bytes).
- validate_
emails_ batch - Batch validate emails and return invalid ones.
- validate_
hash_ length - Validate hash has correct length.
- validate_
latency - Validate latency is within acceptable range.
- validate_
nonce_ length - Validate nonce has correct length.
- validate_
price_ range - Validate price is within acceptable range.
- validate_
proof_ freshness - Validate proof timestamp is recent (within tolerance).
- validate_
public_ key_ length - Validate public key has correct length.
- validate_
signature_ length - Validate signature has correct length.
- validate_
tags_ list - Validate all tags in a list and return valid ones.
- validate_
usernames_ batch - Batch validate usernames and return invalid ones.
- xor_
bytes - XOR two byte slices into a new vector. Returns a vector with the XOR of each byte pair. If slices have different lengths, uses the shorter length.
- zip_
with - Zip two vectors together, stopping at the length of the shorter vector.