|
| 1 | +# Timeboost Smart Contracts |
| 2 | + |
| 3 | +This directory contains the smart contracts that power the decentralized Timeboost protocol. These contracts run on Ethereum-compatible blockchains and provide the foundation for secure, decentralized time synchronization. |
| 4 | + |
| 5 | +## Background |
| 6 | +Smart contracts are executable code, deployed on blockchains that |
| 7 | +can be read from / written to anyone with an internet connection. |
| 8 | +Transaction data and smart contract storage is public and can be |
| 9 | +accessed in blockchain explorers. In decentralized Timeboost, |
| 10 | +smart contracts are used to allow anyone to interact with various |
| 11 | +of the protocol. This readme is directed at developers who are |
| 12 | +contributing to or making use of this decentralized timeboost |
| 13 | +implementation. |
| 14 | + |
| 15 | +## What Are These Contracts For? |
| 16 | + |
| 17 | +Timeboost needs a way to coordinate cryptographic keys and committee members across a decentralized network. These smart contracts provide: |
| 18 | + |
| 19 | +- **Key Management**: Store and manage public keys for the protocol |
| 20 | +- **Committee Coordination**: Track which nodes are part of the consensus committee |
| 21 | +- **Access Control**: Ensure only authorized parties can update critical protocol parameters |
| 22 | +- **Transparency**: All changes are recorded on-chain for public verification |
| 23 | + |
| 24 | +These contracts act as the "coordination layer" that allows the Timeboost network to operate without a central authority. |
| 25 | + |
| 26 | +## Handling Upgradeability |
| 27 | + |
| 28 | +### The Upgrade Problem |
| 29 | +Once deployed, smart contracts can't be changed. To solve this, we use a proxy solution that performs functionally as upgradeable contracts. |
| 30 | + |
| 31 | +### The Proxy Solution |
| 32 | +We use a "proxy pattern" that works like this: |
| 33 | + |
| 34 | +1. **Users always interact with the proxy** - This address never changes |
| 35 | +2. **The proxy points to an implementation** - This can be updated |
| 36 | +3. **When you upgrade** - Just point the proxy to a new implementation |
| 37 | +4. **All data stays safe** - Storage is preserved across upgrades |
| 38 | + |
| 39 | +Think of it like changing the engine in a car - the car (proxy) stays the same, but you can swap out the engine (implementation) for a better one. |
| 40 | + |
| 41 | +### Our Architecture |
| 42 | +``` |
| 43 | +User → Proxy Contract → Implementation Contract |
| 44 | + ↓ |
| 45 | + Storage (persistent) |
| 46 | +``` |
| 47 | + |
| 48 | +## The Contracts |
| 49 | + |
| 50 | +### KeyManager - The Main Contract |
| 51 | + |
| 52 | +**What it stores:** |
| 53 | +- **Encryption keys** - The cryptographic keys used by the protocol |
| 54 | +- **Committee members** - Who's currently in the consensus committee |
| 55 | +- **Manager address** - Who can update the contract |
| 56 | + |
| 57 | +**What it does:** |
| 58 | +- **Sets committees** - Updates which nodes are part of the network |
| 59 | +- **Manages keys** - Stores the threshold encryption key |
| 60 | +- **Controls access** - Only the manager can make changes |
| 61 | +- **Logs everything** - All changes are recorded as events |
| 62 | + |
| 63 | +**Key functions:** |
| 64 | +- `setNextCommittee()` - Add a new committee with future members |
| 65 | +- `currentCommitteeId()` - Find which committee is active right now |
| 66 | +- `getCommitteeById()` - Get details about a specific committee |
| 67 | +- `setThresholdEncryptionKey()` - Set the encryption key for the protocol |
| 68 | + |
| 69 | +### ERC1967Proxy - The Upgrade Mechanism |
| 70 | +This is the "shell" that makes upgrades possible: |
| 71 | + |
| 72 | +- **Never changes** - Users always interact with this address |
| 73 | +- **Delegates calls** - Forwards requests to the current implementation |
| 74 | +- **Preserves data** - All storage survives upgrades |
| 75 | +- **SecOps Precautions** - it's important to call the initialize methods during deploys and upgrades so that those transactions aren't front-run |
| 76 | + |
| 77 | +## Getting Started |
| 78 | + |
| 79 | +### Prerequisites |
| 80 | +- **Foundry** - For building and testing contracts |
| 81 | + |
| 82 | +### Building the Contracts |
| 83 | +```bash |
| 84 | +# Build all contracts |
| 85 | +just build-contracts |
| 86 | + |
| 87 | +# Or use forge directly |
| 88 | +forge build |
| 89 | +``` |
| 90 | + |
| 91 | +### Testing |
| 92 | +```bash |
| 93 | +# Run all tests |
| 94 | +just test-contracts |
| 95 | + |
| 96 | +# Run with detailed output |
| 97 | +forge test -vvv |
| 98 | + |
| 99 | +# Run specific test |
| 100 | +forge test --match-test test_setThresholdEncryptionKey |
| 101 | +``` |
| 102 | + |
| 103 | +### Integration Testing |
| 104 | +For testing contract interactions from Rust code, see the [timeboost-contract README](../timeboost-contract/README.md). |
| 105 | + |
| 106 | +## Deployment |
| 107 | +You can deploy a local anvil network (as done in the test), a fork of a real network, a testnet network (e.g. Ethereum Sepolia) or a mainnet (e.g. Ethereum Mainnet). |
| 108 | + |
| 109 | +### Quick Start (Local Testing) |
| 110 | +```bash |
| 111 | +# 1. Start a local blockchain |
| 112 | +anvil |
| 113 | + |
| 114 | +# 2. Deploy the contracts |
| 115 | +cp env.example .env |
| 116 | +# Edit .env with your values |
| 117 | +./script/deploy.sh |
| 118 | +``` |
| 119 | + |
| 120 | +**📋 For detailed deployment instructions, see the [deployment script README](script/README.md)** |
| 121 | + |
| 122 | +## Security |
| 123 | + |
| 124 | +### Current Status |
| 125 | +- **ERC1967Proxy** - Audited by OpenZeppelin, widely used |
| 126 | +- **KeyManager** - Not yet audited (audit planned) |
| 127 | + |
| 128 | +### Security Considerations |
| 129 | +- **Manager privileges** - The manager can update committees and keys |
| 130 | +- **Upgrade authority** - Only the contract owner can upgrade |
| 131 | +- **Committee validation** - Contracts validate committee transitions |
| 132 | +- **Event logging** - All changes are logged onchain for transparency |
| 133 | + |
| 134 | +### Best Practices |
| 135 | +- **Use multisig wallets** - Don't use single-key wallets for manager/owner |
| 136 | +- **Test thoroughly** - Always test on testnets first |
| 137 | +- **Monitor events** - Watch for unexpected contract changes |
| 138 | +- **Keep keys secure** - Store private keys and mnemonics safely |
| 139 | + |
| 140 | +## Getting Help |
| 141 | +- Check the [deployment script README](script/README.md) for deployment issues |
| 142 | +- Review the [timeboost-contract README](../timeboost-contract/README.md) for integration questions |
0 commit comments