Location: packages/rules/src/stellar/upgradeability/
Detects unsafe serialization changes during Soroban contract upgrades that could corrupt contract state.
Purpose: Extract and analyze struct definitions
let schemas = SchemaAnalyzer::extract_schemas(source);
let issues = SchemaAnalyzer::detect_incompatibilities(&old, &new);Detects:
- Field additions/removals
- Type changes
- Serde derive modifications
Purpose: Perform detailed upgrade compatibility checks
let rule = SerializationUpgradeCompatibilityRule::new(old_code);
let violations = rule.check_upgrade(new_code, "contract.rs");Returns: RuleViolation objects with severity and suggestions
Purpose: Detect dangerous patterns via heuristics
let violations = UnsafeSerializationPatternRule::check(source, "contract.rs");Purpose: High-level compatibility interface
let checker = DefaultUpgradeChecker;
let safe = checker.is_upgrade_safe(old_code, new_code);
let issues = checker.get_incompatibilities(old_code, new_code);| Issue | Severity | Example |
|---|---|---|
| Field Removed | Critical | paused: bool → removed |
| Type Changed | Critical | balance: i128 → u64 |
| Required Field Added | High | New version: u32 field |
| Serde Derive Removed | High | Lost #[derive(Serialize)] |
| Made Required | High | paused: Option<bool> → bool |
- ✅ Adding optional fields:
Option<T> - ✅ Making field optional:
T→Option<T> - ✅ Adding with default:
#[serde(default)] - ✅ Migration function present:
fn migrate()detected
gasguard check-serialization \
--old-code previous.rs \
--new-code current.rs \
--fail-on critical,highlet diagnostics = UnsafeSerializationPatternRule::check(source, file_path);
editor.show_diagnostics(diagnostics);let rule = SerializationUpgradeCompatibilityRule::new(old_code);
let violations = rule.check_upgrade(new_code, file_path);packages/rules/src/stellar/upgradeability/
├── mod.rs # Module root, exports, traits
├── schema_analyzer.rs # Struct parsing & analysis (210 lines)
├── serialization_rules.rs # Detection rules (240 lines)
└── tests.rs # Integration tests & examples (150 lines)
- SERIALIZATION_UPGRADE_DETECTION.md - Complete feature guide
- SERIALIZATION_UPGRADE_IMPLEMENTATION.md - Implementation details
- [This file] - Quick reference
cargo test --lib stellar::upgradeabilityIncludes tests for:
- Schema extraction
- Compatibility detection
- Field removal detection
- Type change detection
- Pattern matching
// Extract all structs from source
SchemaAnalyzer::extract_schemas(source: &str) -> Vec<StructSchema>
// Compare old and new schemas
SchemaAnalyzer::detect_incompatibilities(old: &StructSchema, new: &StructSchema)
-> Vec<SerializationIssue>// Create with old code
SerializationUpgradeCompatibilityRule::new(old_code: String)
// Check upgrade compatibility
check_upgrade(new_code: &str, file_path: &str) -> Vec<RuleViolation>// Check for dangerous patterns
UnsafeSerializationPatternRule::check(source: &str, file_path: &str)
-> Vec<RuleViolation>- Critical (🔴): Upgrade will fail or corrupt state
- High (🟠): Needs manual migration
- Medium (🟡): Review recommended
- Low (🔵): Minor concerns
- Warning (⚪): Informational
- Info (ℹ️): Informational only
pub struct RuleViolation {
pub rule_name: String, // "soroban-serialization-compatibility"
pub description: String, // What's wrong
pub severity: ViolationSeverity, // Critical, High, etc.
pub line_number: usize,
pub column_number: usize,
pub variable_name: String, // Struct or field name
pub suggestion: String, // How to fix
}pub struct SerializationIssue {
pub issue_type: SerializationIssueType,
pub struct_name: String,
pub field_name: Option<String>,
pub old_type: Option<String>,
pub new_type: Option<String>,
pub description: String,
pub impact: String,
}- Always keep Serde derives on persistent structs
- Document why fields are being removed
- Implement migration functions for complex changes
- Test upgrades between versions
- Use
#[serde(default)]for new optional fields - Version your contract schemas
- Regex-based parsing (may not catch all Rust syntax)
- Static analysis only (no runtime checks)
- Assumes standard serialization format
- Won't detect custom serde implementations
Uses only already-present dependencies:
regex(1.10)serde(1.0)
To use this in your project:
- Review SERIALIZATION_UPGRADE_DETECTION.md
- Import from
gasguard_rules::stellar::upgradeability - Run in CI/CD pipeline before deploys
- Review violations and implement safe upgrade patterns
Scope: rules/stellar/upgradeability/
Created: June 2, 2026
Status: Ready for Integration