Skip to content

Code quality, backup, external APIs, and cross-chain improvements - #125

Merged
LaGodxy merged 6 commits into
MettaChain:mainfrom
OpenSourceCOntr:feat/issues-102-106-109-110-combined
Mar 26, 2026
Merged

Code quality, backup, external APIs, and cross-chain improvements#125
LaGodxy merged 6 commits into
MettaChain:mainfrom
OpenSourceCOntr:feat/issues-102-106-109-110-combined

Conversation

@Uchechukwu-Ekezie

Copy link
Copy Markdown
Contributor

Summary

This PR addresses four related issues improving code quality, operations, integrations, and cross-chain support.


Issue #102: Replace Magic Numbers with Named Constants

  • Create contracts/traits/src/constants.rs with documented named constants for all magic numbers across oracle, bridge, IPFS, token, and analytics contracts
  • Replace inline literals in oracle constructor (staleness: 3600s, min sources: 2, outlier threshold: 2) and bridge constructor (gas multiplier: 100, confirmation blocks: 6) with constant references
  • Export constants module from traits crate

Issue #106: Automated Backup and Disaster Recovery

  • Add scripts/backup.sh: timestamped backups with SHA-256 checksums, automatic rotation (30 backups), cron-compatible for 6-hour scheduling
  • Add docs/DISASTER_RECOVERY.md: backup strategy, step-by-step recovery procedures for contracts/config/bridge/oracle, data integrity checks, severity-based escalation matrix

Issue #109: External API Integration with Fallback

  • Implement fetch_from_external_endpoint() adapter pattern for Chainlink, Pyth, Substrate, and Custom oracle sources
  • Implement get_latest_manual_price() with freshness check from valuation history
  • Add automatic fallback: if primary oracle source fails, attempt manual price before returning error
  • Document adapter contract interface for future cross-contract call implementations

Issue #110: Cross-Chain Bridge Tests and Documentation

  • Add tests/bridge_tests.rs with 12 test cases: chain registration, signature threshold, duplicate rejection, expiration, recovery actions (unlock/retry/cancel), gas multiplier, metadata preservation, inactive chain rejection, admin guards
  • Add docs/BRIDGE_GUIDE.md with architecture diagram, multi-sig flow, security measures, monitoring metrics, and new chain setup guide

Files Changed

File Change
contracts/traits/src/constants.rs New: centralized constants module
contracts/traits/src/lib.rs Add constants module export
contracts/oracle/src/lib.rs Replace magic numbers + add fallback logic
contracts/bridge/src/lib.rs Replace magic numbers with constants
scripts/backup.sh New: automated backup script
docs/DISASTER_RECOVERY.md New: recovery procedures
docs/BRIDGE_GUIDE.md New: bridge documentation
tests/bridge_tests.rs New: 12 cross-chain test cases

How to test

  • cargo build --workspace compiles cleanly
  • ./scripts/backup.sh ./test-backups creates a backup archive
  • cargo test --test bridge_tests runs cross-chain tests

Closes #102
Closes #106
Closes #109
Closes #110

Create centralized constants module (contracts/traits/src/constants.rs)
with documented named constants for all magic numbers:

Oracle: DEFAULT_MAX_PRICE_STALENESS (3600s), DEFAULT_MIN_SOURCES_REQUIRED (2),
  DEFAULT_OUTLIER_THRESHOLD (2), reputation scores (500/1000/200/10/50)
Bridge: DEFAULT_GAS_MULTIPLIER (100), DEFAULT_CONFIRMATION_BLOCKS (6),
  BRIDGE_BASE_GAS (100000)
IPFS: MAX_LOCATION_LENGTH (500), MAX_FILE_SIZE (100MB), etc.
Token: TOKEN_SCALING_FACTOR (1e12)
Analytics: BASIS_POINTS_DENOMINATOR (10000)

Replace inline magic numbers in oracle and bridge constructors with
constant references.

Closes MettaChain#102
Add scripts/backup.sh:
- Timestamped backups of contracts, deployments, and config
- SHA-256 checksums for integrity verification
- Automatic rotation (keeps last 30 backups)
- Redacts sensitive .env values in backups
- Cron-compatible for 6-hour scheduling

Add docs/DISASTER_RECOVERY.md:
- Backup strategy and schedule
- Step-by-step recovery for contracts, config, bridge, oracle
- Data integrity verification procedure
- Severity-based escalation matrix

Closes MettaChain#106
Replace stub oracle source implementations with structured integration:
- Chainlink, Pyth, Substrate, Custom sources delegate to
  fetch_from_external_endpoint() which will make cross-contract calls
  to oracle adapter contracts (TODO: wire actual adapters)
- Manual source reads from valuation_history with freshness check
- Fallback: if primary source fails, attempt manual price before error
- Add get_latest_manual_price() helper for history lookup
- Add fetch_from_external_endpoint() with documented adapter pattern

This unblocks oracle functionality by providing a working fallback
path through manual prices while external adapters are deployed.

Closes MettaChain#109
Add tests/bridge_tests.rs with 12 test cases covering:
- Chain registration on init
- Unknown chain rejection
- Signature threshold enforcement
- Duplicate signature rejection
- Expired bridge detection
- Recovery actions (unlock, retry, cancel)
- Gas multiplier per chain
- Metadata preservation across transfer
- Inactive chain rejection
- Admin-only recovery guard

Add docs/BRIDGE_GUIDE.md documenting:
- Multi-sig bridge architecture and flow
- Initiate, sign, execute, recover procedures
- Security measures (multi-sig, timelock, deactivation)
- Monitoring metrics and alert thresholds
- Guide for adding new chains

Closes MettaChain#110
@drips-wave

drips-wave Bot commented Mar 26, 2026

Copy link
Copy Markdown

@Uchechukwu-Ekezie Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

The historical_valuations mapping stores PropertyValuation structs,
not PriceData. Convert valuation/last_updated fields to price/timestamp
before freshness check.
@LaGodxy
LaGodxy merged commit 0f86d26 into MettaChain:main Mar 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Insufficient Cross-Chain Support Missing External API Integration Missing Backup and Recovery Magic Numbers and Hardcoded Values

2 participants