Skip to content

Commit

Permalink
Merge pull request #129 from xssnick/v1.8-dev
Browse files Browse the repository at this point in the history
v1.8 RC
  • Loading branch information
xssnick authored Aug 16, 2023
2 parents d9f8c2c + 4110def commit 3b936db
Show file tree
Hide file tree
Showing 103 changed files with 4,505 additions and 2,965 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img align="right" width="425px" src="https://github.com/xssnick/props/blob/master/logoimg.png?raw=true">

[![Based on TON][ton-svg]][ton]
![Coverage](https://img.shields.io/badge/Coverage-72.3%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-73.4%25-brightgreen)

Golang library for interacting with TON blockchain.

Expand Down Expand Up @@ -63,7 +63,7 @@ You can also join our **[Telegram group](https://t.me/tonutils)** and ask any qu

### Connection
You can get list of public lite servers from official TON configs:
* Mainnet - `https://ton-blockchain.github.io/global.config.json`
* Mainnet - `https://ton.org/global.config.json`
* Testnet - `https://ton-blockchain.github.io/testnet-global.config.json`

from liteservers section, you need to convert int to ip and take port and key.
Expand Down Expand Up @@ -108,7 +108,7 @@ if err != nil {
panic(err)
}

if balance.NanoTON().Uint64() >= 3000000 {
if balance.Nano().Uint64() >= 3000000 {
addr := address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N")
err = w.Transfer(context.Background(), addr, tlb.MustFromTON("0.003"), "Hey bro, happy birthday!")
if err != nil {
Expand Down Expand Up @@ -203,7 +203,7 @@ if err != nil {
// Balance: ACTIVE
fmt.Printf("Status: %s\n", account.State.Status)
// Balance: 66559946.09 TON
fmt.Printf("Balance: %s TON\n", account.State.Balance.TON())
fmt.Printf("Balance: %s TON\n", account.State.Balance.String())
if account.Data != nil { // Can be nil if account is not active
// Data: [0000003829a9a31772c9ed6b62a6e2eba14a93b90462e7a367777beb8a38fb15b9f33844d22ce2ff]
fmt.Printf("Data: %s\n", account.Data.Dump())
Expand Down Expand Up @@ -478,9 +478,10 @@ client.SetOnDisconnect(func(addr, serverKey string) {
* ✅ Overlays
* ✅ TL Parser/Serializer
* ✅ TL-B Parser/Serializer
* ✅ Payment channels
* ✅ Liteserver proofs automatic validation
* DHT Server
* Payment channels
* Merkle proofs automatic validation
* TVM

<!-- Badges -->
[ton-svg]: https://img.shields.io/badge/Based%20on-TON-blue
Expand Down
2 changes: 2 additions & 0 deletions address/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
VarAddress AddrType = 3
)

const MasterchainID int32 = -1

type Address struct {
flags flags
addrType AddrType
Expand Down
53 changes: 32 additions & 21 deletions adnl/adnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func initADNL(key ed25519.PrivateKey) *ADNL {
}

func (a *ADNL) Close() {
a.mx.Lock()
defer a.mx.Unlock()
trigger := false

a.mx.Lock()
if !a.closed {
a.closed = true

Expand All @@ -111,10 +111,13 @@ func (a *ADNL) Close() {
con.Close()
}

if a.onDisconnect != nil {
// do it async to not get accidental deadlock
go a.onDisconnect(a.addr, a.peerKey)
}
trigger = true
}
a.mx.Unlock()

disc := a.onDisconnect
if trigger && disc != nil {
disc(a.addr, a.peerKey)
}
}

Expand Down Expand Up @@ -159,6 +162,11 @@ func (a *ADNL) processPacket(packet *PacketContent, ch *Channel) (err error) {
}

if packet.ReinitDate != nil && *packet.ReinitDate > a.dstReinit {
// reset their seqno even if it is lower,
// because other side could lose counter
a.confirmSeqno = seqno
a.loss = 0

// a.dstReinit = *packet.ReinitDate
// a.seqno = 0
// a.channel = nil
Expand Down Expand Up @@ -361,7 +369,7 @@ func (a *ADNL) processAnswer(id string, query any) {
res <- query
}
} else {
Logger("unknown response with id", id, a.addr, reflect.TypeOf(query).String())
// Logger("unknown response with id", id, a.addr, reflect.TypeOf(query).String())
}
}

Expand Down Expand Up @@ -397,23 +405,26 @@ func (a *ADNL) query(ctx context.Context, ch *Channel, req, result tl.Serializab
a.activeQueries[reqID] = res
a.mx.Unlock()

if err = a.sendRequestMaySplit(ctx, ch, q); err != nil {
a.mx.Lock()
delete(a.activeQueries, reqID)
a.mx.Unlock()
for {
if err = a.sendRequestMaySplit(ctx, ch, q); err != nil {
a.mx.Lock()
delete(a.activeQueries, reqID)
a.mx.Unlock()

return fmt.Errorf("request failed: %w", err)
}
return fmt.Errorf("request failed: %w", err)
}

select {
case resp := <-res:
if err, ok := resp.(error); ok {
return err
select {
case resp := <-res:
if err, ok := resp.(error); ok {
return err
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(resp))
return nil
case <-ctx.Done():
return fmt.Errorf("deadline exceeded, addr %s %s, err: %w", a.addr, hex.EncodeToString(a.peerKey), ctx.Err())
case <-time.After(250 * time.Millisecond):
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(resp))
return nil
case <-ctx.Done():
return fmt.Errorf("deadline exceeded, addr %s %s, err: %w", a.addr, hex.EncodeToString(a.peerKey), ctx.Err())
}
}

Expand Down
99 changes: 94 additions & 5 deletions adnl/adnl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestADNL_ClientServer(t *testing.T) {
gotSrvDiscon := make(chan any, 1)

s := NewGateway(srvKey)
err = s.StartServer("127.0.0.1:9055")
err = s.StartServer("127.0.0.1:9155")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestADNL_ClientServer(t *testing.T) {

time.Sleep(1 * time.Second)

cli, err := Connect(context.Background(), "127.0.0.1:9055", srvPub, nil)
cli, err := Connect(context.Background(), "127.0.0.1:9155", srvPub, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestADNL_ClientServer(t *testing.T) {
t.Fatal(err)
}

cliBad, err := Connect(context.Background(), "127.0.0.1:9055", rndPub, nil)
cliBad, err := Connect(context.Background(), "127.0.0.1:9155", rndPub, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -130,7 +130,7 @@ func TestADNL_ClientServer(t *testing.T) {
t.Fatal(err)
}

cliBadQuery, err := Connect(context.Background(), "127.0.0.1:9055", srvPub, rndOur)
cliBadQuery, err := Connect(context.Background(), "127.0.0.1:9155", srvPub, rndOur)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestADNL_ClientServer(t *testing.T) {
})

t.Run("custom msg channel reinited", func(t *testing.T) {
cli, err = Connect(context.Background(), "127.0.0.1:9055", srvPub, nil)
cli, err = Connect(context.Background(), "127.0.0.1:9155", srvPub, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -236,3 +236,92 @@ func TestADNL_Connect(t *testing.T) {
}
adnl.Close()
}

func TestADNL_ClientServerStartStop(t *testing.T) {
_, aPriv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}

bPub, bPriv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}

a := NewGateway(aPriv)
err = a.StartServer("127.0.0.1:9055")
if err != nil {
t.Fatal(err)
}
a.SetConnectionHandler(connHandler)

b := NewGateway(bPriv)
err = b.StartServer("127.0.0.1:9065")
if err != nil {
t.Fatal(err)
}
b.SetConnectionHandler(connHandler)

p, err := a.RegisterClient("127.0.0.1:9065", bPub)
if err != nil {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

var res MessagePong
err = p.Query(ctx, &MessagePing{7755}, &res)
if err != nil {
t.Fatal(err)
}

if res.Value != 7755 {
t.Fatal("value not eq")
}

_ = b.Close()
b = NewGateway(bPriv)
err = b.StartServer("127.0.0.1:9065")
if err != nil {
t.Fatal(err)
}
b.SetConnectionHandler(connHandler)

p.Close()
p, err = a.RegisterClient("127.0.0.1:9065", bPub)
if err != nil {
t.Fatal(err)
}

err = p.Query(ctx, &MessagePing{1111}, &res)
if err != nil {
t.Fatal(err)
}
}

func connHandler(client Peer) error {
client.SetQueryHandler(func(msg *MessageQuery) error {
switch m := msg.Data.(type) {
case MessagePing:
if m.Value == 9999 {
client.Close()
return fmt.Errorf("handle mock err")
}

err := client.Answer(context.Background(), msg.ID, MessagePong{
Value: m.Value,
})
if err != nil {
panic(err)
}
}
return nil
})
client.SetCustomMessageHandler(func(msg *MessageCustom) error {
return client.SendCustomMessage(context.Background(), TestMsg{Data: make([]byte, 1280)})
})
client.SetDisconnectHandler(func(addr string, key ed25519.PublicKey) {
})
return nil
}
16 changes: 2 additions & 14 deletions adnl/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/sha256"
"fmt"
"github.com/oasisprotocol/curve25519-voi/curve"
ed25519crv "github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/oasisprotocol/curve25519-voi/primitives/x25519"
Expand Down Expand Up @@ -65,15 +63,5 @@ func NewCipherCtr(key, iv []byte) (cipher.Stream, error) {
return cipher.NewCTR(c, iv), nil
}

func ToKeyID(key any) ([]byte, error) {
data, err := tl.Serialize(key, true)
if err != nil {
return nil, fmt.Errorf("key serialize err: %w", err)
}

hash := sha256.New()
hash.Write(data)
s := hash.Sum(nil)

return s, nil
}
// Deprecated: use tl.Hash
var ToKeyID = tl.Hash
Loading

0 comments on commit 3b936db

Please sign in to comment.