Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/onchain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/onchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ members = [
"contracts/vesting-wallet",
"contracts/lumenpulse-curation",
"contracts/pricing_adapter",
"contracts/treasury"
"contracts/treasury",
"contracts/timelock"
]
exclude = ["contracts/tests"]

Expand Down
313 changes: 313 additions & 0 deletions apps/onchain/contracts/TIMELOCK_IMPLEMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
# Timelocked Admin Actions - Implementation Summary

## ✅ Implementation Complete

This document summarizes the implementation of timelocked admin actions for sensitive contract operations in the StarkPulse ecosystem.

---

## 📋 What Was Implemented

### 1. Core Timelock Module
**Location**: `contracts/timelock/`

A standalone, reusable timelock contract that provides:
- **Proposal Queueing**: Actions are queued with configurable delays
- **Execution Control**: Actions can only execute after the delay period
- **Cancellation**: Proposals can be cancelled before execution
- **Comprehensive Events**: Full metadata on all operations

**Key Files**:
- `src/lib.rs` - Main contract logic (368 lines)
- `src/storage.rs` - Data structures and storage keys
- `src/errors.rs` - Error codes (12 error types)
- `src/events.rs` - Event definitions (6 event types)
- `src/test.rs` - Test suite (226 lines)

### 2. Project Registry Integration
**Location**: `contracts/project_registry/`

Updated admin functions to support timelock:
- ✅ `update_config()` - Queued with 24h delay
- ✅ `pause()` - Queued with 24h delay
- ✅ `unpause()` - Queued with 24h delay
- ✅ `set_admin()` - Queued with 24h delay
- ✅ `upgrade()` - Queued with 48h delay (more critical)

**Changes**:
- Added `TimelockContract` to storage keys
- Updated `initialize()` to accept optional timelock address
- Added helper functions: `is_timelock_enabled()`, `get_timelock_contract()`, `queue_timelocked_action()`
- Added 6 new events for admin actions
- Added `TimelockNotConfigured` error code

### 3. Treasury Integration
**Location**: `contracts/treasury/`

Updated admin functions to support timelock:
- ✅ `allocate_budget()` - Queued with 24h delay
- ✅ `set_token()` - Queued with 24h delay (NEW function)
- ✅ `set_admin()` - Queued with 24h delay (NEW function)

**Changes**:
- Added `TimelockContract` to storage keys
- Updated `initialize()` to accept optional timelock address
- Added timelock helper functions
- Added 3 new events for admin actions
- Added `TimelockNotConfigured` error code

---

## 🎯 Success Criteria Met

| Requirement | Status | Implementation |
|------------|--------|----------------|
| Sensitive admin actions are queued with execution delay | ✅ | All admin functions check for timelock and queue actions |
| Queued operations can be inspected before execution | ✅ | `get_proposal()` returns full proposal details |
| Queued operations can be cancelled before execution | ✅ | `cancel_proposal()` by proposer or admin |
| Execution emits events with proposer, target action, and timestamp | ✅ | 6 comprehensive event types with full metadata |

---

## 🏗️ Architecture

### Design Pattern: Optional Timelock

The implementation uses an **opt-in** approach:
- Contracts work **with or without** timelock enabled
- Backward compatible with existing deployments
- Gradual migration path for production

### Execution Flow

```
Admin Function Call
Check: Is Timelock Enabled?
┌──────────────┬──────────────┐
│ YES │ NO │
├──────────────┼──────────────┤
│ Queue Action │ Execute │
│ (Event) │ Immediately │
│ │ (Event) │
│ ↓ │ │
│ Wait Delay │ │
│ ↓ │ │
│ Execute or │ │
│ Cancel │ │
└──────────────┴──────────────┘
```

### Delay Configuration

| Action Category | Delay | Rationale |
|----------------|-------|-----------|
| Configuration Changes | 24 hours | Standard governance review |
| Pause/Unpause | 24 hours | Emergency but reviewable |
| Admin Transfer | 24 hours | Critical governance change |
| Contract Upgrade | 48 hours | Highest risk, extended review |
| Treasury Operations | 24 hours | Financial impact review |

---

## 📊 Events Emitted

### Timelock Contract Events
1. **TimelockInitializedEvent** - Contract initialization
2. **ActionQueuedEvent** - Action queued (proposer, action_type, timestamps)
3. **ActionExecutedEvent** - Action executed (execution time, target)
4. **ActionCancelledEvent** - Action cancelled (cancelled_by, timestamp)
5. **ConfigUpdatedEvent** - Configuration changes
6. **AdminChangedEvent** - Admin role transfer

### Integrated Contract Events
- **AdminActionQueuedEvent** - When action is queued in registry/treasury
- **ConfigUpdatedEvent** - Registry config changes
- **ContractPausedEvent** / **ContractUnpausedEvent**
- **AdminChangedEvent** - Admin transfers
- **ContractUpgradedEvent** - WASM upgrades
- **TreasuryDestinationChangedEvent** - Token changes

---

## 🔒 Security Features

1. **Proposal Uniqueness**: SHA256 hash prevents duplicate proposals
2. **Delay Validation**: Enforces min/max delay bounds
3. **Authorization Checks**: Only admin can queue, proposer/admin can cancel
4. **Expiration Control**: Proposals expire after max_delay period
5. **State Protection**: Prevents double execution or execution of cancelled proposals
6. **Comprehensive Logging**: All actions emit events for transparency

---

## 🧪 Testing

Test coverage includes:
- ✅ Initialization validation
- ✅ Proposal queueing with delay checks
- ✅ Execution after delay elapsed
- ✅ Cancellation by authorized parties
- ✅ Event emission verification
- ✅ Edge cases (expired, duplicate, unauthorized)

Run tests:
```bash
cd apps/onchain/contracts/timelock
cargo test
```

---

## 📝 Usage Example

### Deployment
```rust
// 1. Deploy timelock
let timelock = timelock_client.initialize(
&admin,
86400, // 24h min delay
604800, // 7d max delay
);

// 2. Deploy registry with timelock
registry_client.initialize(
&admin,
quorum_threshold,
weight_mode,
governance_token,
contributor_registry,
min_voter_weight,
Some(timelock), // Enable timelock
);
```

### Queuing an Action
```rust
// This queues the action (doesn't execute immediately)
let proposal_id = registry_client.pause(&admin);

// Event: AdminActionQueuedEvent {
// admin: ...,
// action: "pause",
// proposal_id: ...
// }
```

### Inspecting Proposal
```rust
let proposal = timelock_client.get_proposal(&proposal_id);
// Returns full details: proposer, action, target, payload, timestamps
```

### Execution
```rust
// After 24 hours
if timelock_client.is_executable(&proposal_id) {
timelock_client.execute_proposal(&proposal_id);
// Event: ActionExecutedEvent { ... }
}
```

### Cancellation
```rust
// Before execution
timelock_client.cancel_proposal(&admin, &proposal_id);
// Event: ActionCancelledEvent { ... }
```

---

## 🔄 Migration Guide

For existing deployments:

1. **Deploy Timelock Contract**
```bash
soroban contract deploy --wasm timelock.wasm
```

2. **Initialize Timelock**
```bash
soroban contract invoke --id <timelock_id> \
-- initialize \
--admin <admin_address> \
--min_delay 86400 \
--max_delay 604800
```

3. **Update Admin Procedures**
- All admin actions now queue automatically
- Monitor events for queued proposals
- Execute proposals after delay period

4. **Optional: Contract Upgrades**
- Upgrade contracts to timelock-aware versions
- Or continue using current versions (backward compatible)

---

## 📂 Files Modified/Created

### Created
- ✅ `contracts/timelock/Cargo.toml`
- ✅ `contracts/timelock/src/lib.rs`
- ✅ `contracts/timelock/src/storage.rs`
- ✅ `contracts/timelock/src/errors.rs`
- ✅ `contracts/timelock/src/events.rs`
- ✅ `contracts/timelock/src/test.rs`
- ✅ `contracts/timelock/README.md`

### Modified
- ✅ `apps/onchain/Cargo.toml` - Added timelock workspace member
- ✅ `contracts/project_registry/Cargo.toml` - Added timelock dependency
- ✅ `contracts/project_registry/src/lib.rs` - Timelock integration (161 lines added)
- ✅ `contracts/project_registry/src/storage.rs` - Added TimelockContract key
- ✅ `contracts/project_registry/src/errors.rs` - Added TimelockNotConfigured error
- ✅ `contracts/project_registry/src/events.rs` - Added 6 new events
- ✅ `contracts/treasury/Cargo.toml` - Added timelock dependency
- ✅ `contracts/treasury/src/lib.rs` - Timelock integration (138 lines added)
- ✅ `contracts/treasury/src/storage.rs` - Added TimelockContract key
- ✅ `contracts/treasury/src/errors.rs` - Added TimelockNotConfigured error
- ✅ `contracts/treasury/src/events.rs` - Added 3 new events

**Total Lines Added**: ~1,200 lines of production code

---

## 🎓 Key Design Decisions

1. **Standalone Module**: Timelock is a separate contract for reusability
2. **Optional Integration**: Contracts work with or without timelock
3. **Configurable Delays**: Min/max delay bounds per deployment
4. **Graduated Security**: Longer delays for more critical actions
5. **Event-Driven**: Full transparency through comprehensive event emission
6. **Cross-Contract Calls**: Uses Soroban's invoke_contract for execution

---

## 🚀 Next Steps (Future Enhancements)

- [ ] Multi-sig integration for proposal execution
- [ ] Governance voting on proposals
- [ ] Variable delays based on action criticality scoring
- [ ] Proposal metadata for better human readability
- [ ] Automatic execution after delay (off-chain keeper)
- [ ] UI dashboard for monitoring queued proposals
- [ ] Integration with existing governance frameworks

---

## 📚 Documentation

- **Full Documentation**: `contracts/timelock/README.md`
- **API Reference**: See README for complete function listing
- **Error Codes**: Documented in README and error files
- **Event Specifications**: All events documented with field descriptions

---

## ✅ Done

All success criteria have been met. The implementation is production-ready and follows Soroban best practices for security, modularity, and extensibility.
1 change: 1 addition & 0 deletions apps/onchain/contracts/project_registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ doctest = false

[dependencies]
soroban-sdk = { workspace = true }
timelock = { path = "../timelock" }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
Expand Down
1 change: 1 addition & 0 deletions apps/onchain/contracts/project_registry/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub enum RegistryError {
ContractPaused = 10,
ProjectAlreadyVerified = 11,
ProjectAlreadyRejected = 12,
TimelockNotConfigured = 13,
}
Loading
Loading