You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/3-smart-contract-features/1-data-access.md
+19-21Lines changed: 19 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,9 @@ To achieve this, we had to:
14
14
15
15
1. Run the Ethereum Virtual Machine inside a Trusted Execution Environment (TEE) and store the state in encrypted storage. This prevents the node operator from accessing the data.
16
16
2. Disable the `getStorageAt` method, so now contracts can have secrets (as long as the `private` state variables are not exposed via public view functions). This prevents anyone with RPC access to the node from reading the data.
17
-
3. Authenticate "view function calls" so that the smart contract knows who is calling the view function. Without this feature there is no "Data Access **Control**", because the dApp developer can't write access control logic.
18
-
4.Event logs are another way to expose data from a contract to the outside world. A practical platform needs a way to configure who can read the various event logs.
19
-
5. Control access to the "Transaction Receipts", which contain the logs and status of the transaction.
17
+
3. Authenticate "view function calls" so that the smart contract knows who is calling the view function. Without this feature, there is no "Data Access **Control**", because the dApp developer can't write access control logic.
18
+
4.Configure event log visibility, since event logs are another way to expose data from a contract to the outside world. A practical platform needs a way to configure who can read the various event logs.
19
+
5. Control access to the "Transaction Receipts", which contain the logs and status of the transaction.
20
20
21
21
## Data Access Control Rules
22
22
@@ -34,14 +34,13 @@ Here, we'll list the platform rules. The examples below will showcase how exactl
34
34
- everyone, if the transaction called a transparent contract.
35
35
-`eth_getStorageAt` can be called on "transparent" contracts.
36
36
37
-
38
37
## Data Access Control Example
39
38
40
39
Let's illustrate with a basic storage dApp example where users can store and retrieve a number.
41
40
42
41
At every step, we'll add a new feature and explain the difference between `TEN` and `Ethereum`.
43
42
44
-
### Step 1: Basic contract with a Public Variable
43
+
### Step 1: Basic Contract with a Public Variable
45
44
46
45
#### Code
47
46
@@ -60,14 +59,13 @@ contract StorageExample {
60
59
61
60
#### Explanation
62
61
63
-
In this step, we created a public variable `storedValues` that maps the provided value to the address of the user who called the `storeValue` function.
62
+
In this step, we created a public variable `storedValues` that maps each user's address to their stored value.
64
63
65
64
Because the variable is public, Solidity will provide a default public getter for it.
66
65
67
66
Since there are no data access restrictions, on both Ethereum and TEN, everyone will be able to read the values of all users by just calling the default public getter.
68
67
69
-
70
-
### Step 2: Converting to a Private Variable with an explicit Getter Function
68
+
### Step 2: Converting to a Private Variable with an Explicit Getter Function
71
69
72
70
#### Code
73
71
@@ -78,7 +76,7 @@ contract StorageExample {
78
76
function storeValue(uint256 value) public {
79
77
_storedValues[tx.origin] = value;
80
78
}
81
-
79
+
82
80
function getValue(address account) public view returns (uint256) {
83
81
return _storedValues[account];
84
82
}
@@ -87,10 +85,10 @@ contract StorageExample {
87
85
88
86
#### Explanation
89
87
90
-
The `storedValues` variable is now private, and we added a basic `getValue` function for users to retrieve their value.
88
+
The `_storedValues` variable is now private, and we added a basic `getValue` function for users to retrieve their value.
91
89
92
-
On both Ethereum and TEN, anyone can call `getValue` to retrieve any value.
93
-
On Ethereum, `_storedValues` can also be accessed directly with `getStorageAt`
90
+
On both Ethereum and TEN, anyone can call `getValue` to retrieve any value.
91
+
On Ethereum, `_storedValues` can also be accessed directly with `getStorageAt`.
94
92
95
93
### Step 3: Data Access Control
96
94
@@ -115,18 +113,17 @@ contract StorageExample {
115
113
116
114
#### Explanation
117
115
118
-
The key line is: ``require(tx.origin == account, "Not authorised!");``, which ensures that the caller of the view function is the owner of the data.
116
+
The key line is: `require(tx.origin == account, "Not authorised!");`, which ensures that the caller of the view function is the owner of the data.
119
117
120
118
**When deployed on TEN, this code guarantees that all users can only access their own values, and nobody can read the `_storedValues`.**
121
119
122
120
On Ethereum, the `tx.origin` is not authenticated, so the check above is not effective and `eth_getStorageAt` is available.
123
121
124
-
125
122
### Step 4: Emitting Events - Default Visibility
126
123
127
124
Event logs notify UIs about state changes in smart contracts.
128
125
129
-
To improve our smart contract, we’ll emit an event when a user stores a value and milestone events when a specific size threshold is met.
126
+
To improve our smart contract, we’ll emit an event when a user stores a value and a milestone event when a call count threshold is reached.
130
127
131
128
#### Code
132
129
@@ -158,18 +155,18 @@ contract StorageExample {
158
155
159
156
Notice how we defined the two events: `DataChanged` and `MilestoneReached`, and are emitting them in the `storeValue` function.
160
157
161
-
In Ethereum, everyone can query and subscribe to these events. If this was possible on TEN, it would completely break the functionality because you can see all the secret values.
158
+
In Ethereum, everyone can query and subscribe to these events. If this were possible on TEN, it would completely break the functionality because anyone could see all the secret values.
162
159
163
-
Notice how in this version, we have no configuration for event log visibility, so we are relying on the default rules:
160
+
Notice how in this version, we have no configuration for event log visibility, so we are relying on the [default rules](#data-access-control-rules):
164
161
165
-
- Rule 1: Event logs that contain ("Externally owned Account") EOAs as indexed fields (topics) are only visible to those EOAs.
162
+
- Rule 1: Event logs that contain EOAs as topics are only visible to those EOAs.
166
163
- Rule 2: Event logs that don't contain any EOA are visible to everyone.
167
164
168
165
In our case, the default rules ensure that:
166
+
169
167
-`DataChanged` is visible only to the address that is storing the value.
170
168
-`MilestoneReached` is publicly visible.
171
169
172
-
173
170
### Step 5: Customising Event Visibility
174
171
175
172
The default visibility rules are a good starting point, but complex dApps require greater flexibility.
@@ -245,10 +242,11 @@ contract StorageExample is ContractTransparencyConfig {
245
242
The [`ContractTransparencyConfig`](https://github.com/ten-protocol/go-ten/blob/main/contracts/src/system/config/IContractTransparencyConfig.sol) interface is known by the TEN platform.
246
243
When a contract is deployed, the platform will call the `visibilityRules` function, and store the `VisibilityConfig`.
247
244
248
-
For each event type, you can configure which fields can access it.
245
+
For each event type, you can configure who can view it based on its topics (indexed fields).
249
246
250
-
Notice how in the `visibilityRules` above, we configure the `DataChanged` event to be visible to the first field and the sender, and the `MilestoneReached` to be visible to everyone.
247
+
Notice how in the `visibilityRules` above, we configure the `DataChanged` event to be visible to the first topic and the sender, and the `MilestoneReached` to be visible to everyone.
251
248
252
249
The other configuration: `VisibilityConfig.contractCfg` applies to the entire contract:
250
+
253
251
-`ContractCfg.TRANSPARENT`: The contracts will have public storage and events, behaving exactly like Ethereum.
254
252
-`ContractCfg.PRIVATE`: The default TEN behaviour, where the storage is not accessible and the events are individually configurable.
Copy file name to clipboardExpand all lines: docs/3-smart-contract-features/3-native-commit-reveal.md
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ sidebar_position: 3
6
6
7
7
Every on-chain game developer knows that moves that rely on entropy must be executed in two steps.
8
8
9
-
Imagine you implement an on-chain coin flip game. The player pays 0.1ETH to choose `Heads` or `Tails`.
10
-
If they win, they receive 0.2ETH, otherwise they lose the 0.1ETH.
9
+
Imagine you implement an on-chain coin flip game. The player pays 0.1 ETH to choose `Heads` or `Tails`.
10
+
If they win, they receive 0.2 ETH, otherwise they lose the 0.1 ETH.
11
11
Even if randomness is unpredictable, this simple game can be exploited in several ways:
12
12
13
-
- The attacker can create a “proxy” smart contract to play on their behalf. Using a similar mechanism to flash loans in DeFi: the proxy is programmed to make multiple actions and only “commit” if it can obtain a profit. In our case, if the coin flip is losing, the proxy can just revert. The only cost will be the gas burned.
13
+
- The attacker can create a “proxy” smart contract to play on their behalf. Using a mechanism similar to flash loans in DeFi: the proxy is programmed to make multiple actions and only “commit” if it can obtain a profit. In our case, if the coin flip is losing, the proxy can just revert. The only cost will be the gas burned.
14
14
- Transactions consume gas, and the gas cost can inadvertently reveal information. For instance, if a winning move is more computationally intensive than a losing one, players could deduce optimal moves by estimating gas costs for various actions.
15
15
16
16
The typical solution is to use an ad-hoc commit-reveal scheme. The smart contract ensures that the player commits to a move, and only afterwards reveals it to the chain.
@@ -28,8 +28,8 @@ To avoid increasing the latency, the move must be executed in the same block as
28
28
29
29
### How it works
30
30
31
-
TEN provides a "System Contract" (a contract deployed and known by the platform.)
32
-
You can get the address of the system contract for our testnet [here](https://sepolia.tenscan.io/resources/verified-data) - "Ten System Contract".
31
+
TEN provides a "System Contract" (a contract deployed and known by the platform).
32
+
You can get the address of the system contract for our testnet [here](https://sepolia.tenscan.io/resources/verified-data) - "Ten Callbacks".
33
33
34
34
The interface for registering the callback is: [IPublicCallbacks](https://github.com/ten-protocol/go-ten/blob/main/contracts/src/system/interfaces/IPublicCallbacks.sol).
0 commit comments