|
2 | 2 |
|
3 | 3 | This module provides a complete implementation of the ADNL (Abstract Datagram Network Layer) protocol for TON blockchain, including a lite client that can communicate directly with TON liteservers. |
4 | 4 |
|
5 | | -## ⚠️ Important: Transport Selection |
6 | | - |
7 | | -**For liteclient connections to TON liteservers, use:** |
8 | | -- `AdnlTcpTransport` + `AdnlLiteClient` (TCP-based, follows ADNL-TCP specification) |
9 | | - |
10 | | -**Deprecated for liteclient use:** |
11 | | -- `AdnlTransport` (UDP-based with channel creation, incompatible with liteservers) |
12 | | - |
13 | | -The UDP-based transport uses `createChannel`/`confirmChannel` messages which are not supported by liteservers. Always use the TCP implementation for liteserver communication. |
14 | | - |
15 | | -## Features |
16 | | - |
17 | | -- **Complete ADNL Protocol Implementation**: Full support for ADNL over TCP as specified in the TON documentation |
18 | | -- **Cryptographic Security**: Ed25519 key exchange, AES-256-CTR encryption, and HMAC-SHA256 authentication |
19 | | -- **TL Serialization**: Proper Type Language (TL) serialization for liteserver communication |
20 | | -- **Lite Client**: Direct communication with TON liteservers without requiring native binaries |
21 | | -- **Connection Management**: Connection pooling and automatic reconnection |
22 | | -- **Message Types**: Support for all ADNL message types (ping, pong, query, answer, custom, etc.) |
23 | | - |
24 | | -## Architecture |
25 | | - |
26 | | -### Core Components |
27 | | - |
28 | | -1. **AdnlClient**: Main ADNL client implementation |
29 | | -2. **AdnlTcpTransport**: TCP transport layer with encryption |
30 | | -3. **AdnlLiteClient**: High-level lite client for TON blockchain queries |
31 | | -4. **CryptoUtils**: Cryptographic utilities for key generation and encryption |
32 | | -5. **Message System**: Complete message type hierarchy |
33 | | -6. **TL Serialization**: Type Language serialization for liteserver API |
34 | | - |
35 | | -### Message Types |
36 | | - |
37 | | -- `MessagePing` / `MessagePong`: Connection testing |
38 | | -- `MessageQuery` / `MessageAnswer`: Request-response pattern |
39 | | -- `MessageCustom`: Custom application messages |
40 | | -- `MessageCreateChannel` / `MessageConfirmChannel`: Channel establishment |
41 | | -- `MessagePart`: Message fragmentation support |
42 | | -- `MessageNop`: No-operation messages |
43 | | - |
44 | | -### Packet Structure |
45 | | - |
46 | | -- `PacketContent`: Encrypted packet payload |
47 | | -- `PacketFlags`: Packet metadata and flags |
48 | | -- `PublicKeyED25519`: Ed25519 public key representation |
| 5 | +[AdnlLiteClient](src/main/java/org/ton/java/adnl/AdnlLiteClient.java) is not thread safe, which means a new instance of it should be created in each thread. |
49 | 6 |
|
50 | 7 | ## Usage |
51 | 8 |
|
52 | 9 | ### Basic ADNL Client |
53 | 10 |
|
54 | 11 | ```java |
55 | | -// Create ADNL client |
56 | | -byte[] ourPrivateKey = CryptoUtils.generatePrivateKey(); |
57 | | -byte[] serverPublicKey = Base64.getDecoder().decode("server_public_key_base64"); |
58 | | - |
59 | | -AdnlClient client = new AdnlClient("server_ip", port, ourPrivateKey, serverPublicKey); |
60 | | - |
61 | | -// Connect |
62 | | -client.connect(); |
63 | | - |
64 | | -// Send ping |
65 | | -long rtt = client.ping(5000); |
66 | | -System.out.println("RTT: " + rtt + " ns"); |
67 | | - |
68 | | -// Send custom message |
69 | | -client.sendCustomMessage("Hello, TON!"); |
70 | | - |
71 | | -// Close |
72 | | -client.close(); |
| 12 | +byte[] serverPublicKey = |
| 13 | +Base64.getDecoder().decode("n4VDnSCUuSpjnCyUk9e3QOOd6o0ItSWYbTnW3Wnn8wk="); |
| 14 | +AdnlTcpTransport adnlTcpTransport = new AdnlTcpTransport(); |
| 15 | +adnlTcpTransport.connect("5.9.10.47", 19949, serverPublicKey); |
| 16 | +assertThat(adnlTcpTransport.isConnected()).isTrue(); |
| 17 | +adnlTcpTransport.close(); |
73 | 18 | ``` |
74 | 19 |
|
75 | | -### Lite Client for TON Blockchain |
| 20 | +### ADNL Lite Client for TON Blockchain |
76 | 21 |
|
77 | 22 | ```java |
78 | | -// Create lite client for testnet |
79 | | -AdnlLiteClient liteClient = AdnlLiteClient.forTestnet(); |
80 | | - |
81 | | -// Connect |
82 | | -liteClient.connect(); |
83 | | - |
84 | | -// Get masterchain info |
85 | | -MasterchainInfo info = liteClient.getMasterchainInfo(); |
86 | | -System.out.println("Last block seqno: " + info.getSeqno()); |
| 23 | +TonGlobalConfig tonGlobalConfig = TonGlobalConfig.loadFromUrl(Utils.getGlobalConfigUrlMainnetGithub()); |
87 | 24 |
|
88 | | -// Get account state |
89 | | -JsonObject accountState = liteClient.getAccountState("EQD..."); |
| 25 | +AdnlLiteClient client = AdnlLiteClient.builder().globalConfig(tonGlobalConfig).build(); |
| 26 | +MasterchainInfo info = client.getMasterchainInfo(); |
90 | 27 |
|
91 | | -// Run smart contract method |
92 | | -JsonObject result = liteClient.runGetMethod("EQD...", "get_balance"); |
| 28 | +log.info("Last block seqno: {} ", info.getLast().getSeqno()); |
| 29 | +log.info("Workchain: {}", info.getLast().getWorkchain()); |
| 30 | +log.info("Shard: {}", info.getLast().getShard()); |
| 31 | +log.info("init.wc: {}", info.getInit().getWorkchain()); |
93 | 32 |
|
94 | | -// Close |
95 | | -liteClient.close(); |
96 | 33 | ``` |
97 | 34 |
|
98 | | -### Using Global Config |
99 | | - |
100 | | -```java |
101 | | -// From downloaded config file |
102 | | -AdnlLiteClient client = AdnlLiteClient.fromGlobalConfig("global.config.json", 0); |
103 | | - |
104 | | -// For mainnet (downloads config automatically) |
105 | | -AdnlLiteClient mainnetClient = AdnlLiteClient.forMainnet(); |
106 | | - |
107 | | -// For testnet (downloads config automatically) |
108 | | -AdnlLiteClient testnetClient = AdnlLiteClient.forTestnet(); |
109 | | -``` |
110 | | - |
111 | | -### Connection Pool |
112 | | - |
113 | | -```java |
114 | | -LiteClientConnectionPool pool = new LiteClientConnectionPool(); |
115 | | - |
116 | | -// Add connections from global config |
117 | | -pool.addConnectionsFromGlobalConfig("global.config.json", 3); |
118 | | - |
119 | | -// Execute query with automatic load balancing |
120 | | -MasterchainInfo info = pool.executeQuery(client -> client.getMasterchainInfo()); |
121 | | - |
122 | | -// Close pool |
123 | | -pool.close(); |
124 | | -``` |
125 | | - |
126 | | -## TL Serialization |
127 | | - |
128 | | -The implementation includes proper TL (Type Language) serialization for communicating with TON liteservers: |
129 | | - |
130 | | -### Supported Queries |
131 | | - |
132 | | -- `GetMasterchainInfo`: Get latest masterchain block information |
133 | | -- `GetAccountState`: Get account state for a specific address |
134 | | -- More queries can be easily added by extending `LiteServerQuery` |
135 | | - |
136 | | -### Custom TL Queries |
137 | | - |
138 | | -```java |
139 | | -public class CustomQuery extends LiteServerQuery { |
140 | | - private static final int CONSTRUCTOR_ID = 0x12345678; |
141 | | - |
142 | | - @Override |
143 | | - public int getConstructorId() { |
144 | | - return CONSTRUCTOR_ID; |
145 | | - } |
146 | | - |
147 | | - @Override |
148 | | - protected void serializeData(ByteArrayOutputStream baos) throws IOException { |
149 | | - // Serialize your data here |
150 | | - writeInt32(baos, someValue); |
151 | | - writeBytes(baos, someData); |
152 | | - } |
153 | | -} |
154 | | -``` |
155 | | - |
156 | | -## Security Features |
157 | | - |
158 | | -- **Ed25519 Key Exchange**: Secure key agreement using Curve25519 |
159 | | -- **AES-256-CTR Encryption**: All messages are encrypted |
160 | | -- **HMAC-SHA256 Authentication**: Message integrity and authenticity |
161 | | -- **Replay Protection**: Sequence numbers prevent replay attacks |
162 | | -- **Perfect Forward Secrecy**: Session keys are ephemeral |
163 | | - |
164 | | -## Protocol Compliance |
165 | | - |
166 | | -This implementation follows the official ADNL protocol specification: |
167 | | -- [ADNL-TCP-Liteserver Documentation](https://github.com/xssnick/ton-deep-doc/blob/patch-1/ADNL-TCP-Liteserver.md) |
168 | | -- Compatible with reference implementations in Go and C++ |
169 | | - |
170 | | -## Testing |
171 | | - |
172 | | -Run the test suite: |
173 | | - |
174 | | -```bash |
175 | | -mvn test |
176 | | -``` |
177 | | - |
178 | | -Unit tests cover: |
179 | | -- TL serialization/deserialization |
180 | | -- Cryptographic operations |
181 | | -- Message handling |
182 | | -- Configuration parsing |
183 | | -- Connection management |
184 | | - |
185 | | -Network tests (disabled by default) can test real connections to liteservers. |
186 | | - |
187 | | -## Dependencies |
188 | | - |
189 | | -- **BouncyCastle**: Cryptographic operations |
190 | | -- **Gson**: JSON parsing for configuration files |
191 | | -- **Apache HttpClient**: HTTP operations for config download |
192 | | -- **JUnit 5**: Testing framework |
193 | | - |
194 | | -## Performance |
195 | | - |
196 | | -- **Low Latency**: Direct TCP connections to liteservers |
197 | | -- **Connection Pooling**: Multiple connections for load balancing |
198 | | -- **Efficient Serialization**: Optimized TL serialization |
199 | | -- **Memory Efficient**: Minimal object allocation in hot paths |
200 | | - |
201 | | -## Error Handling |
202 | | - |
203 | | -The implementation includes comprehensive error handling: |
204 | | -- Connection timeouts and retries |
205 | | -- Cryptographic verification failures |
206 | | -- Malformed packet detection |
207 | | -- Network interruption recovery |
208 | | - |
209 | | -## Thread Safety |
210 | | - |
211 | | -- All public APIs are thread-safe |
212 | | -- Connection pools support concurrent access |
213 | | -- Message handlers are executed in separate threads |
214 | | - |
215 | | -## Future Enhancements |
216 | | - |
217 | | -- [ ] ADNL over UDP support |
218 | | -- [ ] Channel-based communication |
219 | | -- [ ] Advanced query caching |
220 | | -- [ ] Metrics and monitoring |
221 | | -- [ ] More TL query types |
222 | | -- [ ] WebSocket transport option |
| 35 | +There are lots of examples on how to work with [AdnlLiteClient](src/test/java/org/ton/java/adnl/AdnlLiteClientTest.java). |
223 | 36 |
|
224 | | -## Contributing |
| 37 | +[maven-central-svg]: https://img.shields.io/maven-central/v/io.github.neodix42/adnl |
225 | 38 |
|
226 | | -When adding new features: |
227 | | -1. Follow the existing code style |
228 | | -2. Add comprehensive tests |
229 | | -3. Update documentation |
230 | | -4. Ensure thread safety |
231 | | -5. Maintain protocol compliance |
| 39 | +[maven-central]: https://mvnrepository.com/artifact/io.github.neodix42/adnl |
232 | 40 |
|
233 | | -## License |
| 41 | +[ton-svg]: https://img.shields.io/badge/Based%20on-TON-blue |
234 | 42 |
|
235 | | -This implementation is part of the ton4j project and follows the same licensing terms. |
| 43 | +[ton]: https://ton.org |
0 commit comments