This document outlines a proposed approach for reducing service file sizes. The implementation was started but reverted due to test failures. This serves as a design reference for future work.
Some service files have grown large:
- MockStellarService.js: 857 lines
- RecurringDonationScheduler.js: 416 lines
- StellarService.js: 273 lines
Break large service files into smaller, responsibility-focused modules.
validatePublicKey()- Validate Stellar public key formatvalidateSecretKey()- Validate Stellar secret key formatvalidateAmount()- Validate transaction amounts
FailureSimulatorclass - Simulates network and transaction failures- Handles retry logic and error simulation
WalletManagerclass - Manages in-memory wallet storage- Handles wallet creation, funding, and balance management
TransactionManagerclass - Manages transaction storage- Handles transaction history and streaming
- Single Responsibility - Each module has one clear purpose
- Easier to Read - Smaller files (348 lines vs 857 lines)
- Easier to Test - Modules can be unit tested independently
- Easier to Maintain - Changes are localized
- Better Organization - Clear structure
The refactoring introduced 47 test failures due to:
- Subtle API differences in error handling
- Balance format inconsistencies
- Transaction recording differences
- Missing edge case handling
- Large refactorings need incremental approach - Should be done in smaller PRs
- Tests must pass at each step - Can't merge with failing tests
- Behavioral compatibility is critical - Even internal refactoring must maintain exact behavior
- More time needed - This refactoring needs 2-3 days of careful work
- Extract one module at a time (e.g., validation first)
- Ensure all tests pass after each extraction
- Merge each module separately
- Continue until complete
- Start with RecurringDonationScheduler.js (416 lines)
- Or StellarService.js (273 lines)
- These may be easier to refactor successfully
- Add comprehensive unit tests for MockStellarService
- Then refactor with confidence
- Tests will catch any behavioral changes
✅ All tests passing (439 passed) ✅ No changes committed - Clean state ✅ CI/CD will pass - No breaking changes
For this issue to be completed:
- ✅ Services are easier to read and maintain
- ✅ No regression in functionality (all tests must pass)
Current: Neither criteria met, work reverted.
This issue requires more time and a more careful approach. The design is sound, but the implementation needs to be done incrementally with tests passing at each step.
Recommendation: Close this issue and create smaller, focused issues for:
- Extract validation helpers from MockStellarService
- Extract failure simulator from MockStellarService
- Extract wallet manager from MockStellarService
- Extract transaction manager from MockStellarService
Each as a separate PR with full test coverage.
validatePublicKey()- Validate Stellar public key formatvalidateSecretKey()- Validate Stellar secret key formatvalidateAmount()- Validate transaction amounts
FailureSimulatorclass - Simulates network and transaction failuresenable()- Enable failure simulationdisable()- Disable failure simulationsimulate()- Trigger simulated failuresexecuteWithRetry()- Retry logic for operations
WalletManagerclass - Manages in-memory wallet storagecreateWallet()- Create new walletsgetBalance()- Get wallet balancesfundTestnetWallet()- Fund testnet walletsisAccountFunded()- Check funding statusgenerateKeypair()- Generate mock keypairs
TransactionManagerclass - Manages transaction storagerecordTransaction()- Record transactionsgetTransactionHistory()- Get transaction historyverifyTransaction()- Verify transactionsstreamTransactions()- Stream transaction updatesnotifyStreamListeners()- Notify listeners
Before: 857 lines (monolithic) After: 348 lines (59% reduction)
The main service now:
- Delegates to focused sub-modules
- Maintains public API compatibility
- Handles orchestration and coordination
- Manages rate limiting and network delays
| File | Before | After | Change |
|---|---|---|---|
| MockStellarService.js | 857 lines | 348 lines | -59% |
| New Modules | - | 606 lines | +606 |
| Total | 857 lines | 954 lines | +11% |
While total lines increased slightly, the code is now:
- More maintainable - Each module has single responsibility
- More testable - Modules can be tested independently
- More readable - Smaller, focused files
- Better organized - Clear separation of concerns
- Input validation
- Format checking
- Error throwing for invalid inputs
- Network failure simulation
- Retry logic
- Error type management
- Auto-recovery mechanisms
- Wallet storage (Map)
- Balance management
- Funding operations
- Keypair generation
- Transaction storage (Map)
- Transaction history
- Stream listeners
- Transaction verification
- ✅ Single Responsibility - Each module has one clear purpose
- ✅ Easier to Read - Smaller files are easier to understand
- ✅ Easier to Test - Modules can be unit tested independently
- ✅ Easier to Maintain - Changes are localized to specific modules
- ✅ Better Organization - Clear structure with
/mocksubdirectory - ✅ Reusability - Modules can be reused in other contexts
All existing public methods remain unchanged:
createWallet()getBalance(publicKey)fundTestnetWallet(publicKey)isAccountFunded(publicKey)sendDonation({sourceSecret, destinationPublic, amount, memo})sendPayment(sourcePublicKey, destinationPublic, amount, memo)getTransactionHistory(publicKey, limit)verifyTransaction(transactionHash)streamTransactions(publicKey, onTransaction)enableFailureSimulation(type, probability)disableFailureSimulation()setMaxConsecutiveFailures(max)
- Minor API differences in error handling
- Balance format inconsistencies
- Transaction recording differences
These need to be resolved by:
- Reviewing original implementation details
- Adjusting module behavior to match exactly
- Updating tests if behavior improvements are intentional
- Fix remaining test failures
- Consider refactoring RecurringDonationScheduler.js (416 lines)
- Consider refactoring StellarService.js (273 lines)
- Add unit tests for new modules
- Update documentation
✅ Services are easier to read and maintain - 59% size reduction, clear modules
The refactoring successfully demonstrates the approach for breaking large service files into smaller, focused modules. The structure is in place and provides a solid foundation, but additional work is needed to ensure complete behavioral compatibility with the original implementation.