Skip to content

Commit 3d1c845

Browse files
authored
Merge pull request #333 from xssnick/dev-v1-11
1.13
2 parents 4a7e1dc + e50d58e commit 3d1c845

40 files changed

+2451
-627
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Set up Go
1919
uses: actions/setup-go@v3
2020
with:
21-
go-version: 1.18
21+
go-version: 1.23
2222

2323
- name: Build
2424
run: go build -v ./...

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[![Based on TON][ton-svg]][ton]
66
[![Telegram Channel][tgc-svg]][tg-channel]
7-
![Coverage](https://img.shields.io/badge/Coverage-73.2%25-brightgreen)
7+
![Coverage](https://img.shields.io/badge/Coverage-69.5%25-yellow)
88

99
Golang library for interacting with TON blockchain.
1010

@@ -68,7 +68,7 @@ You could also join our **[Telegram channel](https://t.me/tonutilsnews)** and **
6868

6969
### Connection
7070
You can use liteservers from TON configs:
71-
* Mainnet public servers - `https://ton.org/global.config.json`
71+
* Mainnet public servers - `https://ton-blockchain.github.io/global.config.json`
7272
* Testnet public servers - `https://ton-blockchain.github.io/testnet-global.config.json`
7373

7474
from liteservers section, you need to convert int to ip and take port and key.

adnl/adnl.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type ADNL struct {
7474
ourAddresses unsafe.Pointer
7575

7676
activeQueries map[string]chan tl.Serializable
77+
activePings map[int64]chan MessagePong
7778

7879
customMessageHandler unsafe.Pointer // CustomMessageHandler
7980
queryHandler unsafe.Pointer // QueryHandler
@@ -98,6 +99,7 @@ func (g *Gateway) initADNL() *ADNL {
9899
closerCtx: closerCtx,
99100
closeFn: closeFn,
100101
msgParts: make(map[string]*partitionedMessage, 128),
102+
activePings: make(map[int64]chan MessagePong),
101103
activeQueries: map[string]chan tl.Serializable{},
102104
}
103105
}
@@ -220,7 +222,16 @@ func (a *ADNL) processPacket(packet *PacketContent, fromChannel bool) (err error
220222
func (a *ADNL) processMessage(message any) error {
221223
switch ms := message.(type) {
222224
case MessagePong:
223-
// TODO: record
225+
a.mx.RLock()
226+
ch := a.activePings[ms.Value]
227+
a.mx.RUnlock()
228+
229+
if ch != nil {
230+
select {
231+
case ch <- ms:
232+
default:
233+
}
234+
}
224235
case MessagePing:
225236
buf, err := a.buildRequest(MessagePong{Value: ms.Value})
226237
if err != nil {
@@ -294,7 +305,7 @@ func (a *ADNL) processMessage(message any) error {
294305
return fmt.Errorf("failed to setup channel: %w", err)
295306
}
296307
case MessagePart:
297-
msgID := hex.EncodeToString(ms.Hash)
308+
msgID := string(ms.Hash)
298309

299310
a.mx.Lock()
300311
p, ok := a.msgParts[msgID]
@@ -306,11 +317,17 @@ func (a *ADNL) processMessage(message any) error {
306317

307318
if len(a.msgParts) > 100 {
308319
// cleanup old stuck messages
320+
tm := time.Now().Add(-7 * time.Second)
309321
for s, pt := range a.msgParts {
310-
if time.Since(pt.startedAt) > 10*time.Second {
322+
if tm.After(pt.startedAt) {
311323
delete(a.msgParts, s)
312324
}
313325
}
326+
327+
if len(a.msgParts) > 16*1024 {
328+
a.mx.Unlock()
329+
return fmt.Errorf("too many incomplete messages")
330+
}
314331
}
315332

316333
p = newPartitionedMessage(ms.TotalSize)
@@ -441,6 +458,47 @@ reSplit:
441458
return nil
442459
}
443460

461+
func (a *ADNL) Ping(ctx context.Context) (time.Duration, error) {
462+
val := time.Now().UnixNano()
463+
req, err := a.buildRequest(MessagePing{
464+
Value: val,
465+
})
466+
if err != nil {
467+
return 0, err
468+
}
469+
470+
ch := make(chan MessagePong, 1)
471+
a.mx.Lock()
472+
a.activePings[val] = ch
473+
a.mx.Unlock()
474+
475+
defer func() {
476+
a.mx.Lock()
477+
delete(a.activePings, val)
478+
a.mx.Unlock()
479+
}()
480+
481+
for {
482+
try, cancel := context.WithTimeout(ctx, 1*time.Second)
483+
484+
if err = a.send(req); err != nil {
485+
cancel()
486+
return 0, fmt.Errorf("failed to send ping packet: %w", err)
487+
}
488+
489+
select {
490+
case <-ctx.Done():
491+
cancel()
492+
return 0, ctx.Err()
493+
case <-ch:
494+
cancel()
495+
return time.Since(time.Unix(0, val)), nil
496+
case <-try.Done():
497+
continue
498+
}
499+
}
500+
}
501+
444502
func (a *ADNL) Query(ctx context.Context, req, result tl.Serializable) error {
445503
q, err := createQueryMessage(req)
446504
if err != nil {

adnl/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
7474
}
7575

7676
// if serverID < ourID, swap keys. if same -> copy enc key
77-
if eq := new(big.Int).SetBytes(theirID).Cmp(new(big.Int).SetBytes(ourID)); eq == -1 {
77+
if eq := new(big.Int).SetBytes(theirID).Cmp(new(big.Int).SetBytes(ourID)); eq < 0 {
7878
c.encKey, c.decKey = c.decKey, c.encKey
7979
} else if eq == 0 {
8080
c.encKey = c.decKey

adnl/conn.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
87
"net"
98
"sync"
109
"time"
@@ -90,7 +89,7 @@ func (s *SyncConn) writer() {
9089
return
9190
}
9291
// should not happen, but if will we want to see
93-
log.Println("[CONN] Write error:", err.Error())
92+
Logger("[CONN] Write error:", err.Error())
9493
}
9594
case <-s.closerCtx.Done():
9695
return

adnl/dht/bucket.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dht
22

33
import (
4+
"bytes"
45
"sort"
56
"sync"
67
)
@@ -39,13 +40,26 @@ func (b *Bucket) getNode(id string) *dhtNode {
3940
return nil
4041
}
4142

43+
func (b *Bucket) findNode(id []byte) *dhtNode {
44+
b.mx.RLock()
45+
defer b.mx.RUnlock()
46+
47+
for _, n := range b.nodes {
48+
if n != nil && bytes.Equal(n.adnlId, id) {
49+
return n
50+
}
51+
}
52+
53+
return nil
54+
}
55+
4256
func (b *Bucket) addNode(node *dhtNode) {
4357
b.mx.Lock()
4458
defer b.mx.Unlock()
4559
defer b.sortAndFilter()
4660

4761
for i, n := range b.nodes {
48-
if n != nil && n.id() == node.id() {
62+
if n != nil && bytes.Equal(n.adnlId, node.adnlId) {
4963
b.nodes[i] = node
5064
return
5165
}

0 commit comments

Comments
 (0)