|
2 | 2 | title: "Contracts" |
3 | 3 | date: 2025-04-13 |
4 | 4 | draft: false |
5 | | -weight: 2 |
6 | 5 | --- |
7 | 6 |
|
8 | | -## Contracts |
9 | | - |
10 | 7 | Freenet is essentially a global decentralized key-value store where keys are |
11 | 8 | WebAssembly code called Contracts. Contracts are stored in the network, |
12 | 9 | along with their data or "state". The contract controls what state is permitted |
@@ -116,19 +113,41 @@ Rust contracts implement the [`ContractInterface`](https://docs.rs/freenet-stdli |
116 | 113 | functions that the core calls to interact with the contract. |
117 | 114 |
|
118 | 115 | ```rust |
119 | | -// TODO: Replace this comment with the actual code snippet for 'contractifce'. |
120 | | -// Option 1: Manually paste the code here. |
121 | | -// Option 2: Use Hugo's readFile function if the file is accessible |
122 | | -// (e.g., {{ readFile "assets/code/contract_interface.rs" | safeHTML }}) |
123 | | -// Note: readFile includes the *entire* file. |
124 | | -// Option 3: Implement a custom shortcode for dynamic section inclusion. |
125 | | - |
126 | 116 | pub trait ContractInterface { |
127 | | - // ... (Paste or dynamically include the relevant interface definition here) |
| 117 | + /// Verify that the state is valid, given the parameters. |
| 118 | + fn validate_state( |
| 119 | + parameters: Parameters<'static>, |
| 120 | + state: State<'static>, |
| 121 | + related: RelatedContracts<'static>, |
| 122 | + ) -> Result<ValidateResult, ContractError>; |
| 123 | + |
| 124 | + /// Update the state to account for the new data |
| 125 | + fn update_state( |
| 126 | + parameters: Parameters<'static>, |
| 127 | + state: State<'static>, |
| 128 | + data: Vec<UpdateData<'static>>, |
| 129 | + ) -> Result<UpdateModification<'static>, ContractError>; |
| 130 | + |
| 131 | + /// Generate a concise summary of a state that can be used to create deltas |
| 132 | + /// relative to this state. |
| 133 | + fn summarize_state( |
| 134 | + parameters: Parameters<'static>, |
| 135 | + state: State<'static>, |
| 136 | + ) -> Result<StateSummary<'static>, ContractError>; |
| 137 | + |
| 138 | + /// Generate a state delta using a summary from the current state. |
| 139 | + /// This along with [`Self::summarize_state`] allows flexible and efficient |
| 140 | + /// state synchronization between peers. |
| 141 | + fn get_state_delta( |
| 142 | + parameters: Parameters<'static>, |
| 143 | + state: State<'static>, |
| 144 | + summary: StateSummary<'static>, |
| 145 | + ) -> Result<StateDelta<'static>, ContractError>; |
128 | 146 | } |
129 | 147 | ``` |
130 | 148 |
|
131 | | -#### Flexibility versus Convenience |
| 149 | + |
| 150 | +### Flexibility versus Convenience |
132 | 151 |
|
133 | 152 | The `ContractInterface` trait is a low-level "Layer 0" API that provides direct |
134 | 153 | access to the contract's state and parameters. This API is useful for contracts |
|
0 commit comments