Skip to content

Commit 753df23

Browse files
committed
feat: implement comprehensive configuration management
- Centralize 45 scattered pub const values from 13 contract modules into contracts/teachlink/src/config.rs (15 sections, fully documented) - Each module re-exports its constants from config via pub use aliases preserving backward compatibility - Add mod config to lib.rs - Add indexer ConfigManager: typed IndexerConfig snapshot, startup validation (URL format, port range, poll interval, batch size), hot-reload via reload() method - Add AppConfigModule wrapping NestJS ConfigModule + ConfigManager - Add GET /analytics/config and POST /analytics/config/reload endpoints - Wire AppConfigModule into AppModule and ReportingModule
1 parent c91ae0f commit 753df23

22 files changed

Lines changed: 481 additions & 94 deletions

contracts/teachlink/src/analytics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::storage::{BRIDGE_METRICS, CHAIN_METRICS, DAILY_VOLUMES};
88
use crate::types::{BridgeMetrics, ChainMetrics};
99
use soroban_sdk::{Address, Bytes, Env, Map, Vec};
1010

11-
/// Metrics update interval (1 hour)
12-
pub const METRICS_UPDATE_INTERVAL: u64 = 3_600;
11+
/// Metrics update interval — re-exported from config for backward compatibility.
12+
pub use crate::config::METRICS_UPDATE_INTERVAL;
1313

1414
/// Analytics Manager
1515
pub struct AnalyticsManager;

contracts/teachlink/src/atomic_swap.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ use crate::storage::{ATOMIC_SWAPS, SWAP_COUNTER, SWAP_GUARD, SWAP_TIMELOCK_SEQ};
1010
use crate::types::{AtomicSwap, SwapStatus};
1111
use soroban_sdk::{symbol_short, vec, Address, Bytes, Env, IntoVal, Map, Vec};
1212

13-
/// Minimum timelock duration (1 hour)
14-
pub const MIN_TIMELOCK: u64 = 3_600;
15-
16-
/// Maximum timelock duration (7 days)
17-
pub const MAX_TIMELOCK: u64 = 604_800;
18-
19-
/// Hash length (32 bytes for SHA256)
20-
pub const HASH_LENGTH: u32 = 32;
13+
/// Minimum timelock duration — re-exported from config.
14+
pub use crate::config::SWAP_MIN_TIMELOCK as MIN_TIMELOCK;
15+
/// Maximum timelock duration — re-exported from config.
16+
pub use crate::config::SWAP_MAX_TIMELOCK as MAX_TIMELOCK;
17+
/// Required hash length — re-exported from config.
18+
pub use crate::config::SWAP_HASH_LENGTH as HASH_LENGTH;
2119

2220
/// Atomic Swap Manager
2321
pub struct AtomicSwapManager;

contracts/teachlink/src/audit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use crate::storage::{AUDIT_COUNTER, AUDIT_RECORDS, COMPLIANCE_REPORTS};
99
use crate::types::{AuditRecord, ComplianceReport, OperationType};
1010
use soroban_sdk::{Address, Bytes, Env, Map, Vec};
1111

12-
/// Maximum audit records to store
13-
pub const MAX_AUDIT_RECORDS: u64 = 100_000;
14-
15-
/// Compliance report period (7 days)
16-
pub const COMPLIANCE_PERIOD: u64 = 604_800;
12+
/// Maximum audit records to store — re-exported from config.
13+
pub use crate::config::AUDIT_MAX_RECORDS as MAX_AUDIT_RECORDS;
14+
/// Compliance report period — re-exported from config.
15+
pub use crate::config::AUDIT_COMPLIANCE_PERIOD as COMPLIANCE_PERIOD;
1716

1817
/// Audit Manager
1918
pub struct AuditManager;

contracts/teachlink/src/bft_consensus.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@ use crate::types::{
1717
};
1818
use soroban_sdk::{Address, Env, Map, Vec};
1919

20-
/// Minimum stake required to become a validator
21-
pub const MIN_VALIDATOR_STAKE: i128 = 100_000_000; // 100 tokens with 6 decimals
22-
23-
/// Proposal timeout in seconds (24 hours)
24-
pub const PROPOSAL_TIMEOUT: u64 = 86_400;
25-
26-
/// Number of consensus rounds per rotation epoch.
27-
/// After this many rounds, the active validator set is re-evaluated and
28-
/// low-reputation validators may be rotated out.
29-
pub const ROTATION_EPOCH_ROUNDS: u64 = 100;
30-
31-
/// Minimum reputation score required to remain in the active validator set.
32-
/// Validators below this threshold are rotated out during epoch transitions.
33-
pub const MIN_ACTIVE_REPUTATION: u32 = 40;
20+
/// Minimum stake required to become a validator — re-exported from config.
21+
pub use crate::config::BFT_MIN_VALIDATOR_STAKE as MIN_VALIDATOR_STAKE;
22+
/// Proposal timeout — re-exported from config.
23+
pub use crate::config::BFT_PROPOSAL_TIMEOUT as PROPOSAL_TIMEOUT;
24+
/// Rotation epoch rounds — re-exported from config.
25+
pub use crate::config::BFT_ROTATION_EPOCH_ROUNDS as ROTATION_EPOCH_ROUNDS;
26+
/// Minimum active reputation — re-exported from config.
27+
pub use crate::config::BFT_MIN_ACTIVE_REPUTATION as MIN_ACTIVE_REPUTATION;
3428

3529
/// BFT Consensus Manager
3630
pub struct BFTConsensus;

contracts/teachlink/src/config.rs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//! Centralized configuration for the TeachLink contract.
2+
//!
3+
//! All tunable constants live here. Modules import from this file instead of
4+
//! defining their own `pub const` values, ensuring a single source of truth.
5+
//!
6+
//! # Sections
7+
//! - [Analytics](#analytics)
8+
//! - [Atomic Swaps](#atomic-swaps)
9+
//! - [Audit & Compliance](#audit--compliance)
10+
//! - [BFT Consensus](#bft-consensus)
11+
//! - [Emergency & Circuit Breaker](#emergency--circuit-breaker)
12+
//! - [Ledger Time](#ledger-time)
13+
//! - [Liquidity & Fees](#liquidity--fees)
14+
//! - [Message Passing](#message-passing)
15+
//! - [Multichain](#multichain)
16+
//! - [Network Recovery](#network-recovery)
17+
//! - [Notifications](#notifications)
18+
//! - [Performance Cache](#performance-cache)
19+
//! - [Rate Limiting](#rate-limiting)
20+
//! - [Slashing](#slashing)
21+
//! - [Sustainability](#sustainability)
22+
//! - [Upgrade](#upgrade)
23+
24+
// ===== Analytics =====
25+
26+
/// How often bridge metrics may be updated (seconds). Default: 1 hour.
27+
pub const METRICS_UPDATE_INTERVAL: u64 = 3_600;
28+
29+
// ===== Atomic Swaps =====
30+
31+
/// Minimum timelock duration for an atomic swap (seconds). Default: 1 hour.
32+
pub const SWAP_MIN_TIMELOCK: u64 = 3_600;
33+
34+
/// Maximum timelock duration for an atomic swap (seconds). Default: 7 days.
35+
pub const SWAP_MAX_TIMELOCK: u64 = 604_800;
36+
37+
/// Required byte length of a hashlock preimage hash.
38+
pub const SWAP_HASH_LENGTH: u32 = 32;
39+
40+
// ===== Audit & Compliance =====
41+
42+
/// Maximum number of audit records retained before circular-buffer wrap.
43+
pub const AUDIT_MAX_RECORDS: u64 = 100_000;
44+
45+
/// Default compliance report period (seconds). Default: 7 days.
46+
pub const AUDIT_COMPLIANCE_PERIOD: u64 = 604_800;
47+
48+
// ===== BFT Consensus =====
49+
50+
/// Minimum validator stake (stroops, 6-decimal). Default: 100 tokens.
51+
pub const BFT_MIN_VALIDATOR_STAKE: i128 = 100_000_000;
52+
53+
/// Proposal expiry window (seconds). Default: 24 hours.
54+
pub const BFT_PROPOSAL_TIMEOUT: u64 = 86_400;
55+
56+
/// Number of consensus rounds per validator rotation epoch.
57+
pub const BFT_ROTATION_EPOCH_ROUNDS: u64 = 100;
58+
59+
/// Minimum reputation score for a validator to remain active (0-100).
60+
pub const BFT_MIN_ACTIVE_REPUTATION: u32 = 40;
61+
62+
// ===== Emergency & Circuit Breaker =====
63+
64+
/// Number of seats on the security council.
65+
pub const EMERGENCY_SECURITY_COUNCIL_SIZE: u32 = 5;
66+
67+
/// Daily volume tracking window (seconds). Default: 24 hours.
68+
pub const EMERGENCY_DAILY_VOLUME_RESET: u64 = 86_400;
69+
70+
// ===== Ledger Time =====
71+
72+
/// Estimated seconds per Stellar ledger (used for lag calculations).
73+
pub const LEDGER_EST_SECS: u64 = 5;
74+
75+
// ===== Liquidity & Fees =====
76+
77+
/// Base bridge fee in basis points. Default: 0.10%.
78+
pub const LIQUIDITY_BASE_FEE_BPS: i128 = 10;
79+
80+
/// Maximum bridge fee in basis points. Default: 5%.
81+
pub const LIQUIDITY_MAX_FEE_BPS: i128 = 500;
82+
83+
/// Minimum bridge fee in basis points. Default: 0.01%.
84+
pub const LIQUIDITY_MIN_FEE_BPS: i128 = 1;
85+
86+
/// Pool utilization threshold (basis points) above which dynamic fees apply.
87+
pub const LIQUIDITY_UTILIZATION_THRESHOLD: u32 = 8_000;
88+
89+
// ===== Message Passing =====
90+
91+
/// Default cross-chain packet timeout (seconds). Default: 24 hours.
92+
pub const MSG_DEFAULT_PACKET_TIMEOUT: u64 = 86_400;
93+
94+
/// Maximum packet delivery retry attempts.
95+
pub const MSG_MAX_RETRY_ATTEMPTS: u32 = 5;
96+
97+
/// Base delay between packet retries (seconds). Default: 5 minutes.
98+
pub const MSG_RETRY_DELAY_BASE: u64 = 300;
99+
100+
// ===== Multichain =====
101+
102+
/// Maximum number of supported external chains.
103+
pub const MULTICHAIN_MAX_CHAINS: u32 = 100;
104+
105+
/// Maximum number of registered multi-chain assets.
106+
pub const MULTICHAIN_MAX_ASSETS: u32 = 1_000;
107+
108+
// ===== Network Recovery =====
109+
110+
/// Maximum automatic retry attempts for a failed operation.
111+
pub const RECOVERY_MAX_RETRY_ATTEMPTS: u32 = 5;
112+
113+
/// Initial exponential-backoff delay (seconds). Default: 1 minute.
114+
pub const RECOVERY_INITIAL_BACKOFF_SECS: u64 = 60;
115+
116+
/// Maximum backoff delay (seconds). Default: 1 hour.
117+
pub const RECOVERY_MAX_BACKOFF_SECS: u64 = 3_600;
118+
119+
/// Backoff multiplier applied on each retry.
120+
pub const RECOVERY_BACKOFF_MULTIPLIER: u64 = 2;
121+
122+
// ===== Notifications =====
123+
124+
/// Sentinel value indicating immediate (non-scheduled) delivery.
125+
pub const NOTIF_IMMEDIATE_DELIVERY: u64 = 0;
126+
127+
/// Minimum scheduling delay for a notification (seconds). Default: 1 minute.
128+
pub const NOTIF_MIN_DELAY_SECS: u64 = 60;
129+
130+
/// Maximum scheduling delay for a notification (seconds). Default: 30 days.
131+
pub const NOTIF_MAX_DELAY_SECS: u64 = 86_400 * 30;
132+
133+
/// Maximum notifications processed per batch.
134+
pub const NOTIF_BATCH_SIZE: u32 = 100;
135+
136+
/// Default TTL for notification event storage (seconds). Default: 7 days.
137+
pub const NOTIF_DEFAULT_EVENT_TTL_SECS: u64 = 86_400 * 7;
138+
139+
// ===== Performance Cache =====
140+
141+
/// Bridge summary cache TTL (seconds). Default: 1 hour.
142+
pub const PERF_CACHE_TTL_SECS: u64 = 3_600;
143+
144+
/// Maximum chains included in the cached top-by-volume list.
145+
pub const PERF_MAX_TOP_CHAINS: u32 = 20;
146+
147+
// ===== Rate Limiting =====
148+
149+
/// Default maximum calls allowed per rate-limit window.
150+
pub const RATE_LIMIT_DEFAULT_MAX_CALLS: u32 = 100;
151+
152+
/// Default rate-limit window size in ledgers.
153+
pub const RATE_LIMIT_DEFAULT_WINDOW_LEDGERS: u32 = 600;
154+
155+
// ===== Slashing =====
156+
157+
/// Slash percentage for double-vote offence (basis points). Default: 50%.
158+
pub const SLASH_DOUBLE_VOTE_BPS: u32 = 5_000;
159+
160+
/// Slash percentage for invalid-signature offence (basis points). Default: 10%.
161+
pub const SLASH_INVALID_SIGNATURE_BPS: u32 = 1_000;
162+
163+
/// Slash percentage for inactivity offence (basis points). Default: 5%.
164+
pub const SLASH_INACTIVITY_BPS: u32 = 500;
165+
166+
/// Slash percentage for byzantine behaviour (basis points). Default: 100%.
167+
pub const SLASH_BYZANTINE_BPS: u32 = 10_000;
168+
169+
/// Slash percentage for malicious behaviour (basis points). Default: 100%.
170+
pub const SLASH_MALICIOUS_BPS: u32 = 10_000;
171+
172+
// ===== Sustainability =====
173+
174+
/// Minimum content tokens minted to reach full content-creation score.
175+
pub const SUSTAIN_CONTENT_SCORE_CAP: u64 = 1_000;
176+
177+
/// Minimum active users to reach full user-adoption score.
178+
pub const SUSTAIN_USER_SCORE_CAP: u64 = 1_000;
179+
180+
// ===== Upgrade =====
181+
182+
/// Window within which a contract upgrade may be rolled back (seconds).
183+
/// Default: 30 days.
184+
pub const UPGRADE_ROLLBACK_WINDOW_SECS: u64 = 86_400 * 30;

contracts/teachlink/src/emergency.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use crate::storage::{CIRCUIT_BREAKERS, CIRCUIT_RESET_SEQ, EMERGENCY_STATE, PAUSE
1111
use crate::types::{CircuitBreaker, EmergencyState};
1212
use soroban_sdk::{Address, Bytes, Env, Map, Vec};
1313

14-
/// Authorized pausers (admin + security council)
15-
pub const SECURITY_COUNCIL_SIZE: u32 = 5;
16-
17-
/// Daily volume reset period (24 hours)
18-
pub const DAILY_VOLUME_RESET: u64 = 86_400;
14+
/// Authorized pausers (admin + security council) — re-exported from config.
15+
pub use crate::config::EMERGENCY_SECURITY_COUNCIL_SIZE as SECURITY_COUNCIL_SIZE;
16+
/// Daily volume reset period — re-exported from config.
17+
pub use crate::config::EMERGENCY_DAILY_VOLUME_RESET as DAILY_VOLUME_RESET;
1918

2019
/// Emergency Manager
2120
pub struct EmergencyManager;

contracts/teachlink/src/ledger_time.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
66
use soroban_sdk::Env;
77

8-
/// Conservative estimate of seconds per ledger close on Stellar.
9-
///
10-
/// Used only to derive a *fallback* ledger-sequence deadline when code is otherwise
11-
/// timestamp-gated.
12-
pub const EST_SECS_PER_LEDGER: u64 = 5;
8+
/// Conservative estimate of seconds per ledger close on Stellar — re-exported from config.
9+
pub use crate::config::LEDGER_EST_SECS as EST_SECS_PER_LEDGER;
1310

1411
pub fn seconds_to_ledger_delta(seconds: u64) -> u32 {
1512
// Ceil division to avoid shortening timeouts.

contracts/teachlink/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ mod audit;
9999
mod backup;
100100
mod bft_consensus;
101101
mod bridge;
102+
mod config;
102103
// TODO: Fix collaboration module compilation errors (pre-existing issue)
103104
// mod collaboration;
104105
// TODO: Fix content_nft module compilation errors (pre-existing issue)

contracts/teachlink/src/liquidity.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ use crate::storage::{FEE_STRUCTURE, LIQUIDITY_POOLS, LP_POSITIONS};
99
use crate::types::{BridgeFeeStructure, LPPosition, LiquidityPool};
1010
use soroban_sdk::{Address, Env, Map, Vec};
1111

12-
/// Base fee in basis points (0.1%)
13-
pub const BASE_FEE_BPS: i128 = 10;
14-
15-
/// Maximum fee in basis points (5%)
16-
pub const MAX_FEE_BPS: i128 = 500;
17-
18-
/// Minimum fee in basis points (0.01%)
19-
pub const MIN_FEE_BPS: i128 = 1;
20-
21-
/// Liquidity utilization threshold for dynamic pricing (80%)
22-
pub const UTILIZATION_THRESHOLD: u32 = 8000;
12+
/// Base fee in basis points — re-exported from config.
13+
pub use crate::config::LIQUIDITY_BASE_FEE_BPS as BASE_FEE_BPS;
14+
/// Maximum fee in basis points — re-exported from config.
15+
pub use crate::config::LIQUIDITY_MAX_FEE_BPS as MAX_FEE_BPS;
16+
/// Minimum fee in basis points — re-exported from config.
17+
pub use crate::config::LIQUIDITY_MIN_FEE_BPS as MIN_FEE_BPS;
18+
/// Utilization threshold for dynamic pricing — re-exported from config.
19+
pub use crate::config::LIQUIDITY_UTILIZATION_THRESHOLD as UTILIZATION_THRESHOLD;
2320

2421
/// Congestion multiplier steps
2522
pub const CONGESTION_STEP_1: u32 = 5000; // 50% utilization

contracts/teachlink/src/message_passing.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ use crate::types::{CrossChainPacket, MessageReceipt, PacketStatus};
1212
use crate::validation::NumberValidator;
1313
use soroban_sdk::{Bytes, Env, Map, Vec};
1414

15-
/// Default packet timeout (24 hours)
16-
pub const DEFAULT_PACKET_TIMEOUT: u64 = 86_400;
17-
18-
/// Maximum retry attempts
19-
pub const MAX_RETRY_ATTEMPTS: u32 = 5;
20-
21-
/// Retry delay in seconds (exponential backoff)
22-
pub const RETRY_DELAY_BASE: u64 = 300; // 5 minutes
15+
/// Default packet timeout — re-exported from config.
16+
pub use crate::config::MSG_DEFAULT_PACKET_TIMEOUT as DEFAULT_PACKET_TIMEOUT;
17+
/// Maximum retry attempts — re-exported from config.
18+
pub use crate::config::MSG_MAX_RETRY_ATTEMPTS as MAX_RETRY_ATTEMPTS;
19+
/// Retry delay base — re-exported from config.
20+
pub use crate::config::MSG_RETRY_DELAY_BASE as RETRY_DELAY_BASE;
2321

2422
/// Message Passing Manager
2523
pub struct MessagePassing;

0 commit comments

Comments
 (0)