Skip to content

Commit d9abee6

Browse files
committed
update docu
1 parent f76aa0f commit d9abee6

File tree

5 files changed

+64
-212
lines changed

5 files changed

+64
-212
lines changed

adnl/README.md

Lines changed: 20 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -2,234 +2,42 @@
22

33
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.
44

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.
496

507
## Usage
518

529
### Basic ADNL Client
5310

5411
```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();
7318
```
7419

75-
### Lite Client for TON Blockchain
20+
### ADNL Lite Client for TON Blockchain
7621

7722
```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());
8724

88-
// Get account state
89-
JsonObject accountState = liteClient.getAccountState("EQD...");
25+
AdnlLiteClient client = AdnlLiteClient.builder().globalConfig(tonGlobalConfig).build();
26+
MasterchainInfo info = client.getMasterchainInfo();
9027

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());
9332

94-
// Close
95-
liteClient.close();
9633
```
9734

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).
22336

224-
## Contributing
37+
[maven-central-svg]: https://img.shields.io/maven-central/v/io.github.neodix42/adnl
22538

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
23240

233-
## License
41+
[ton-svg]: https://img.shields.io/badge/Based%20on-TON-blue
23442

235-
This implementation is part of the ton4j project and follows the same licensing terms.
43+
[ton]: https://ton.org
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.ton.java.adnl;
2+
3+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import java.util.Base64;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.junit.jupiter.api.Test;
9+
import org.ton.ton4j.tl.liteserver.responses.*;
10+
import org.ton.ton4j.tlb.*;
11+
12+
@Slf4j
13+
public class AdnlClientTest {
14+
15+
@Test
16+
void testAdnlClient() throws Exception {
17+
byte[] serverPublicKey =
18+
Base64.getDecoder().decode("n4VDnSCUuSpjnCyUk9e3QOOd6o0ItSWYbTnW3Wnn8wk=");
19+
20+
AdnlTcpTransport adnlTcpTransport = new AdnlTcpTransport();
21+
adnlTcpTransport.connect("5.9.10.47", 19949, serverPublicKey);
22+
assertThat(adnlTcpTransport.isConnected()).isTrue();
23+
adnlTcpTransport.close();
24+
}
25+
}

tl/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# TL Serialization
2+
3+
The implementation includes proper TL (Type Language) serialization for communicating with TON liteservers:
4+
5+
## Supported Queries
6+
7+
[queries](src/main/java/org/ton/ton4j/tl/liteserver/queries)
8+
9+
## Supported Responses
10+
11+
[responses](src/main/java/org/ton/ton4j/tl/liteserver/responses)
12+
13+
## Future improvements
14+
Other non-liteservers TL types will be added later.
File renamed without changes.

tonlib/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
Java Tonlib library uses JNA to access methods in native Tonlib shared library.
44

5+
**For pure Java implementation of ADNL protocol and lite-client see [AdnlLiteClient.java](../adnl/src/main/java/org/ton/java/adnl/AdnlLiteClient.java) in [adnl](../adnl) module**.
6+
57
Since this is Java Tonlib wrapper around the native binary, you have to specify path to the library, see the example
68
below.
79

10+
This tonlib wrapper is thread safe, which means one instance of it can be used in multiple threads.
11+
812
You can get the latest tonlib library by:
913

1014
* downloading it from the official TON Github release page [here](https://github.com/ton-blockchain/ton/releases).
1115
* by installing precompiled binaries, see instructions [here](https://github.com/ton-blockchain/packages).
16+
* by specifying URL (Utils.getTonlibGithubUrl()) in Tonlib builder.
1217

1318
## Maven [![Maven Central][maven-central-svg]][maven-central]
1419

0 commit comments

Comments
 (0)