Skip to content

Commit d4afeb1

Browse files
authored
Merge pull request #255 from xssnick/dev-v19
v1.10.0
2 parents 9db1a13 + ed8b367 commit d4afeb1

29 files changed

+641
-205
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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.8%25-brightgreen)
7+
![Coverage](https://img.shields.io/badge/Coverage-73.5%25-brightgreen)
88

99
Golang library for interacting with TON blockchain.
1010

adnl/adnl.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ func (c *Channel) process(buf []byte) error {
146146

147147
func (a *ADNL) processPacket(packet *PacketContent, ch *Channel) (err error) {
148148
a.mx.Lock()
149+
150+
if packet.DstReinitDate != nil && *packet.DstReinitDate > 0 && *packet.DstReinitDate < a.reinitTime {
151+
if packet.ReinitDate != nil {
152+
a.dstReinit = *packet.ReinitDate
153+
}
154+
a.mx.Unlock()
155+
156+
buf, err := a.buildRequest(ch, MessageNop{})
157+
if err != nil {
158+
return fmt.Errorf("failed to create packet: %w", err)
159+
}
160+
if err = a.send(context.Background(), buf); err != nil {
161+
return fmt.Errorf("failed to send ping reinit: %w", err)
162+
}
163+
164+
return nil
165+
}
166+
149167
seqno := uint64(*packet.Seqno)
150168
a.lastReceiveAt = time.Now()
151169

@@ -161,17 +179,14 @@ func (a *ADNL) processPacket(packet *PacketContent, ch *Channel) (err error) {
161179
a.confirmSeqno = seqno
162180
}
163181

164-
if packet.ReinitDate != nil && *packet.ReinitDate > a.dstReinit {
182+
if (packet.ReinitDate != nil && *packet.ReinitDate > a.dstReinit) &&
183+
(packet.DstReinitDate != nil && *packet.DstReinitDate == a.reinitTime) {
165184
// reset their seqno even if it is lower,
166185
// because other side could lose counter
167186
a.confirmSeqno = seqno
168187
a.loss = 0
169188

170-
// a.dstReinit = *packet.ReinitDate
171-
// a.seqno = 0
172-
// a.channel = nil
173-
// a.confirmSeqno = 0
174-
// a.reinitTime = a.dstReinit
189+
a.dstReinit = *packet.ReinitDate
175190
}
176191

177192
if packet.RecvPriorityAddrListVersion != nil {
@@ -329,6 +344,8 @@ func (a *ADNL) processMessage(message any, ch *Channel) error {
329344
return fmt.Errorf("failed to handle custom message: %w", err)
330345
}
331346
}
347+
case MessageNop:
348+
return nil
332349
default:
333350
return fmt.Errorf("skipped unprocessable message of type %s", reflect.TypeOf(message).String())
334351
}

adnl/gateway.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (g *Gateway) listen(rootId []byte) {
279279
g.mx.RUnlock()
280280

281281
if proc == nil {
282-
Logger("no processor for ADNL packet from", hex.EncodeToString(id))
282+
Logger("no processor for ADNL packet from", addr.String(), hex.EncodeToString(id))
283283
continue
284284
}
285285

@@ -384,18 +384,17 @@ func (g *Gateway) registerClient(addr net.Addr, key ed25519.PublicKey, id string
384384
closer: ch.adnl.Close,
385385
}
386386
g.mx.Unlock()
387+
})
387388

388-
if oldId == "" { // connection = first channel initialisation
389-
connHandler := g.connHandler
390-
if connHandler != nil {
391-
err := connHandler(peer)
392-
if err != nil {
393-
// close connection if connection handler reports an error
394-
ch.adnl.Close()
395-
}
389+
connHandler := g.connHandler
390+
if connHandler != nil {
391+
go func() {
392+
if err := connHandler(peer); err != nil {
393+
// close connection if connection handler reports an error
394+
a.Close()
396395
}
397-
}
398-
})
396+
}()
397+
}
399398

400399
return peer, nil
401400
}

adnl/packet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ func parsePacket(data []byte) (_ *PacketContent, err error) {
162162
data = data[4:]
163163
packet.ReinitDate = &reinit
164164

165-
reinit = int32(binary.LittleEndian.Uint32(data))
165+
dstReinit := int32(binary.LittleEndian.Uint32(data))
166166
data = data[4:]
167-
packet.DstReinitDate = &reinit
167+
packet.DstReinitDate = &dstReinit
168168
}
169169

170170
if flags&_FlagSignature != 0 {

adnl/rldp/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ func (r *RLDP) sendMessageParts(ctx context.Context, transferId, data []byte) er
357357

358358
if symbolsSent > fastSymbols {
359359
x := (symbolsSent - fastSymbols) / 2
360+
if x > 70 { // 7 ms max delay
361+
x = 70
362+
}
360363

361364
select {
362365
case <-ctx.Done():
@@ -365,7 +368,7 @@ func (r *RLDP) sendMessageParts(ctx context.Context, transferId, data []byte) er
365368
case <-ch:
366369
// we got complete from receiver, finish sending
367370
return nil
368-
case <-time.After(time.Duration(x) * _PacketWaitTime):
371+
case <-time.After(time.Duration(x) * (time.Millisecond / 10)):
369372
// send additional FEC recovery parts until complete
370373
}
371374
}

adnl/rldp/client_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,14 @@ func TestRDLP_sendMessageParts(t *testing.T) {
377377
}
378378

379379
decoded, receivData, err := tDecoder.Decode()
380-
if err != nil || decoded != true {
380+
if err != nil {
381381
t.Fatal("failed to decode received test data, err: ", err)
382382
}
383383

384+
if decoded != true {
385+
return nil
386+
}
387+
384388
if !bytes.Equal(data, receivData) {
385389
t.Fatal("bad data received in 'sendCustomMessage'")
386390
}
@@ -479,10 +483,14 @@ func TestRLDP_DoQuery(t *testing.T) {
479483
}
480484

481485
decoded, receivData, err := tDecoder.Decode()
482-
if err != nil || decoded != true {
486+
if err != nil {
483487
t.Fatal("failed to decode received test data, err: ", err)
484488
}
485489

490+
if decoded != true {
491+
return nil
492+
}
493+
486494
var checkReq Query
487495
_, err = tl.Parse(&checkReq, receivData, true)
488496
if err != nil {
@@ -568,10 +576,14 @@ func TestRLDP_SendAnswer(t *testing.T) {
568576
}
569577

570578
decoded, receivData, err := tDecoder.Decode()
571-
if err != nil || decoded != true {
579+
if err != nil {
572580
t.Fatal("failed to decode received test data, err: ", err)
573581
}
574582

583+
if decoded != true {
584+
return nil
585+
}
586+
575587
var checkAnswer Answer
576588
_, err = tl.Parse(&checkAnswer, receivData, true)
577589
if err != nil {

adnl/rldp/http/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (t *Transport) connectRLDP(ctx context.Context, key ed25519.PublicKey, addr
112112
}
113113

114114
rCap := GetCapabilities{
115-
Capabilities: CapabilityRLDP2,
115+
Capabilities: 0,
116116
}
117117

118118
var caps Capabilities
@@ -126,7 +126,7 @@ func (t *Transport) connectRLDP(ctx context.Context, key ed25519.PublicKey, addr
126126
switch query.Data.(type) {
127127
case GetCapabilities:
128128
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
129-
err := a.Answer(ctx, query.ID, &Capabilities{Value: CapabilityRLDP2})
129+
err := a.Answer(ctx, query.ID, &Capabilities{Value: 0})
130130
cancel()
131131
if err != nil {
132132
return fmt.Errorf("failed to send capabilities answer: %w", err)
@@ -284,7 +284,7 @@ func (t *Transport) RoundTrip(request *http.Request) (_ *http.Response, err erro
284284
req := Request{
285285
ID: qid,
286286
Method: request.Method,
287-
URL: request.URL.String(),
287+
URL: request.URL.RequestURI(),
288288
Version: "HTTP/1.1",
289289
Headers: []Header{
290290
{

adnl/rldp/http/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (s *Server) ListenAndServe(listenAddr string) error {
157157
switch query.Data.(type) {
158158
case GetCapabilities:
159159
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
160-
err := client.Answer(ctx, query.ID, &Capabilities{Value: CapabilityRLDP2})
160+
err := client.Answer(ctx, query.ID, &Capabilities{Value: 0}) // CapabilityRLDP2
161161
cancel()
162162
if err != nil {
163163
return fmt.Errorf("failed to send capabilities answer: %w", err)
@@ -236,11 +236,15 @@ func (s *Server) handle(client RLDP, adnlId, addr string) func(transferId []byte
236236
if err != nil {
237237
return fmt.Errorf("failed to parse url `%s`: %w", uri, err)
238238
}
239+
uri.Scheme = "http"
239240

240241
contentLen := int64(-1)
241242
headers := http.Header{}
242243
for _, header := range req.Headers {
243-
if header.Name == "Content-Length" {
244+
name := http.CanonicalHeaderKey(header.Name)
245+
if name == "Host" {
246+
uri.Host = header.Value
247+
} else if name == "Content-Length" {
244248
contentLen, err = strconv.ParseInt(header.Value, 10, 64)
245249
if err != nil {
246250
return fmt.Errorf("failed to parse content len `%s`: %w", header.Value, err)
@@ -250,7 +254,8 @@ func (s *Server) handle(client RLDP, adnlId, addr string) func(transferId []byte
250254
return fmt.Errorf("failed to parse content len: should be >= 0")
251255
}
252256
}
253-
headers[header.Name] = append(headers[header.Name], header.Value)
257+
258+
headers[name] = append(headers[name], header.Value)
254259
}
255260
headers.Set("X-Adnl-Ip", netAddr.IP.String())
256261
headers.Set("X-Adnl-Id", adnlId)

example/nft-info/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
case *nft.ContentOffchain:
4545
fmt.Println(" content offchain :", content.URI)
4646
case *nft.ContentOnchain:
47-
fmt.Println(" content onchain :", content.Name)
47+
fmt.Println(" content onchain :", content.GetAttribute("name"))
4848
}
4949
fmt.Println(" owner :", collectionData.OwnerAddress.String())
5050
fmt.Println(" minted items num :", collectionData.NextItemIndex)
@@ -69,7 +69,7 @@ func main() {
6969
fmt.Println(" full content :", nftContent.(*nft.ContentOffchain).URI)
7070
}
7171
case *nft.ContentOnchain:
72-
fmt.Println(" content name :", content.Name)
72+
fmt.Println(" content name :", content.GetAttribute("name"))
7373
}
7474
} else {
7575
fmt.Println(" empty content")

tlb/coins.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,15 @@ func (g *Coins) UnmarshalJSON(data []byte) error {
222222

223223
return nil
224224
}
225+
226+
func (g *Coins) Compare(coins *Coins) int {
227+
if g.decimals != coins.decimals {
228+
panic("invalid comparsion")
229+
}
230+
231+
return g.Nano().Cmp(coins.Nano())
232+
}
233+
234+
func (g *Coins) Decimals() int {
235+
return g.decimals
236+
}

0 commit comments

Comments
 (0)