Skip to content

Commit e166aeb

Browse files
committed
Add architecture overview
1 parent e00b6ba commit e166aeb

9 files changed

+118
-2
lines changed

book/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [TON Pool](build-your-staking-dapp/ton/ton-pool/README.md)
2929
- [Overview](build-your-staking-dapp/ton/ton-pool/overview.md)
3030
- [Methods](build-your-staking-dapp/ton/ton-pool/methods.md)
31+
- [Architecture](build-your-staking-dapp/ton/ton-pool/architecture.md)
3132
- [Nominator](build-your-staking-dapp/ton/nominator/README.md)
3233
- [Overview](build-your-staking-dapp/ton/nominator/overview.md)
3334
- [Methods](build-your-staking-dapp/ton/nominator/methods.md)
Loading
Loading
Loading
Loading
Loading

book/build-your-staking-dapp/ton/overview.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Before you start developing your staking application, you need to decide which p
1818
| - | - | - |- |
1919
| Recommended || | |
2020
| Best for | Most use cases | Large delegators | Single delegators |
21-
| Delegators | | 40 Max | 1 |
21+
| Delegators | Unlimited | 40 Max | 1 |
2222
| Minimal Stake | 10 TON | 10,000 TON | 400,000 TON |
2323
| Partial Withdrawal || ❌ Must withdraw all funds ||
2424
| Pool creation | ✅ Not needed | ❌ Per request | ❌ Per request |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# TON Pool: Architecture
2+
3+
**TON Pool** provides efficient staking solution on the TON blockchain by utilizing the two-queue smart contract system from the [TON Whales](https://tonwhales.com/) project.
4+
By using two queues, the TON Pool ensures optimal resource use and enables 100% validator efficiency (With a single queue, double the number of validators would be needed).
5+
6+
To support these smart contracts, **TON Pool** employs a set of controllers that run directly on the node. These include the **Pool Controller** for managing customer interactions, the **Election Controller** for validator participation, and the **Validator Controller** for secure key management. Together, they enable efficient staking and ensure secure, reliable performance across each validation cycle.
7+
8+
![](../../../assets/ton-pool-architecture/ton-whales-contract-first-iteration.png)
9+
10+
## Controllers
11+
12+
The solution includes several key components for managing staking workflows:
13+
14+
- **Pool controller**: Responsible for accepting stakes and withdrawals from customers.
15+
- **Election controller**: Responsible for participating in validator elections to get our
16+
validators into the top 100 validator list. This controller interacts with the Validator Controller to secure the necessary public keys.
17+
- **Validator controller**: Responsible for generating new key pairs for each election and
18+
integrating with the TON validator software on each individual validator.
19+
20+
The **Pool** and **Election controllers** can be run together in the same binary, this makes it easier to accept stakes and withdrawals before we start participating in an election.
21+
22+
## Validators
23+
24+
Validators, as full nodes, automatically start validating when their public key is included in the current validator set. This information is retrieved from the Elector contract or Chain Config. The pool and election controllers run independently of the validators.
25+
26+
The [validation cycle](https://tonscan.com/validation) is split into **odd** and **even** cycles. Each cycle has four phases:
27+
28+
- **Election**: 6-7 hours
29+
- **Delay**: 2-3 hours
30+
- **Validation**: 18 hours
31+
- **Hold**: 9 hours (overlaps with the Election phase of the next cycle)
32+
33+
## Workflows
34+
35+
**TON Pool** includes four main workflows using the TON Whales Pool contract:
36+
37+
1. **Staking**: Customers deposit TON to the pool contract.
38+
2. **Unstaking**: Customers withdraw TON from the pool contract.
39+
3. **Stake Submission**: Validator bids are sent to participate in the upcoming election.
40+
4. **Stake Retrieval**: Previous bids and rewards are reclaimed from the last validation cycle.
41+
42+
### Staking
43+
44+
1. A customer submits a stake message to the Pool contract through the **TON Pool** frontend, which adds the deposited TON as a pending stake.
45+
2. At the start of the next election cycle, when the pool unlocks, the controller processes the pending stake. Only one contract can accept new stakes during each cycle, determined by alternating between contracts.
46+
- The controller first checks both contracts’ statuses to identify which one has ProxyStakeAt set to 0, indicating it can accept stakes.
47+
- It then verifies if there are pending deposits for this contract. If so, the pool contract processes these deposits by sending an acceptance message.
48+
49+
![](../../../assets/ton-pool-architecture/workflow-stake.png)
50+
51+
### Unstaking
52+
53+
1. **Unstake Request**: Customers initiate an unstake request through a **TON Pool** frontend.
54+
- If the contract is **unlocked**, customers receive their full stake immediately.
55+
- If the contract is **locked**, they receive as much of their stake as possible based on the available balance in the Pending Stake. Any remaining amount is added as a pending request.
56+
2. **Processing Pending Withdrawals**: The controller processes any pending withdrawals at the start of the next election cycle.
57+
3. **Final Withdrawal**: Once processed, customers can retrieve any remaining stake by sending another withdrawal request.
58+
59+
![](../../../assets/ton-pool-architecture/workflow-withdraw.png)
60+
61+
### Stake Submission
62+
63+
This process occurs every other cycle for each **TON Pool** contract.
64+
65+
1. The Validator Controller generates validator keys for the next election.
66+
2. The Election Controller creates and sends an election bid to the Pool contract.
67+
3. The Pool contract processes and forwards the bid to the proxy contract, locking the pool.
68+
4. The Proxy contract submits the bid to the Elector contract.
69+
5. The Elector contract approves or rejects the bid
70+
6. The Elector contract updates the Config contract with the new validator set after elections.
71+
72+
![](../../../assets/ton-pool-architecture/workflow-send-stake.png)
73+
74+
### Stake Retrieval
75+
76+
This process also occurs every other cycle for each **TON Pool** contract.
77+
78+
1. The Election Controller sends a recover stake message to the Pool contract.
79+
2. The Pool contract relays this message to the Elector contract through the proxy.
80+
3. The Elector contract returns the stake and rewards to the pool through the proxy.
81+
82+
![](../../../assets/ton-pool-architecture/workflow-recover-stake.png)
83+
84+
## Efficient On-Chain Operations
85+
86+
To optimize gas costs, all main operations are consolidated to occur once per election cycle:
87+
88+
- Stake and withdrawal actions are processed once per election cycle.
89+
- Election bids (stake bids) are placed once per cycle.
90+
- This approach includes tracking the long-term ADNL key for election bids and generating election keys once per cycle.
91+
- Stake and reward retrieval are also conducted once per cycle.
92+
93+
94+
## Further Reading
95+
96+
- [TON Docs](https://docs.ton.org/develop/overview)
97+
- [TON Whales contract](https://github.com/tonwhales/ton-nominators/)
98+
- [FunC standard library](https://docs.ton.org/develop/func/stdlib)
99+
- [TVM exit codes](https://docs.ton.org/learn/tvm-instructions/tvm-exit-codes)
100+
- [TL-B (Type Language - Binary) definitions](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb)
101+
- [TON Elector contract](https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/elector-code.fc)
102+
- [TON Config](https://tonviewer.com/config)
103+
- [Validation Cycle Live](https://tonscan.com/validation)

book/build-your-staking-dapp/ton/ton-pool/overview.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ The methods provided in this documentation are compatible with popular TON libra
1919

2020
The **TON Pool** is powered by [TON Whales contracts](https://tonwhales.com/), a robust and proven technology that has operated reliably in the TON ecosystem for several years. These contracts have been tested extensively in real-world conditions, providing a solid foundation for secure and efficient staking operations.
2121

22+
![](../../../assets/ton-pool-architecture/ton-whales-contract-first-iteration.png)
23+
24+
25+
{% hint style="info" %}
26+
27+
**Architecture Overview**
28+
29+
To learn more about the architecture of the TON Pool and the underlying TON Whales contracts, refer to the [Architecture](architecture.md) section.
30+
31+
{% endhint %}
32+
33+
2234
## Two Validator Pool Addresses
2335

2436
To ensure uninterrupted network participation, TON Pool utilizes two validator pool addresses: one for odd cycles and another for even cycles. These pools alternate between cycles to enable seamless staking and validation without downtime. This design ensures continuous operation and smooth participation in the TON blockchain’s validation process.
@@ -174,6 +186,6 @@ In this section you learned how to set up the Chorus One SDK for the TON network
174186
- To learn more about the available methods on `TonPoolStaker`, continue to the [Methods](methods.md) section.
175187

176188
## Further Reading
177-
189+
- [TON Pool Architecture](architecture.md)
178190
- [TonPoolStaker API Reference](../../../docs/classes/ton_src.TonPoolStaker.md)
179191
- [What is a Signer?](../../../signers-explained/what-is-a-signer.md)

0 commit comments

Comments
 (0)