Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.9.8 #203

Merged
merged 18 commits into from
Jun 13, 2024
Merged

1.9.8 #203

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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-73.7%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-73.8%25-brightgreen)

Golang library for interacting with TON blockchain.

Expand Down
2 changes: 1 addition & 1 deletion adnl/dht/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func TestClient_FindAddressesIntegration(t *testing.T) {
}

// restore after unit tests
testAddr := "516618cf6cbe9004f6883e742c9a2e3ca53ed02e3e36f4cef62a98ee1e449174" // ADNL address of foundation.ton
testAddr := "89bea091caf4273d38b0dc24944d8798e057abcfa6ac08d5e0b26284c5c0609a" // ADNL address of utils.ton

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion adnl/rldp/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func TestTransport_RoundTripIntegration(t *testing.T) {

transport := NewTransport(dhtClient, getDNSResolver())

req, err := http.NewRequest(http.MethodGet, "http://foundation.ton/", nil)
req, err := http.NewRequest(http.MethodGet, "http://utils.ton/", nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions example/deploy-nft-collection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func main() {
msgBody := cell.BeginCell().EndCell()

fmt.Println("Deploying NFT collection contract to mainnet...")
addr, err := w.DeployContract(context.Background(), tlb.MustFromTON("0.02"),
msgBody, getNFTCollectionCode(), getContractData(w.WalletAddress(), w.WalletAddress()), true)
addr, _, _, err := w.DeployContractWaitTransaction(context.Background(), tlb.MustFromTON("0.02"),
msgBody, getNFTCollectionCode(), getContractData(w.WalletAddress(), w.WalletAddress()))
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion example/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
api := ton.NewAPIClient(client).WithRetry()

// get root dns address from network config
root, err := dns.RootContractAddr(api)
root, err := dns.GetRootContractAddr(context.Background(), api)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion example/highload-wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
for addrStr, amtStr := range receivers {
addr := address.MustParseAddr(addrStr)
messages = append(messages, &wallet.Message{
Mode: 1 + 2, // pay fee separately, ignore action errors
Mode: wallet.PayGasSeparately + wallet.IgnoreErrors, // pay fee separately, ignore action errors
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true, // disable hyper routing (currently not works in ton)
Bounce: addr.IsBounceable(),
Expand Down
2 changes: 1 addition & 1 deletion example/send-to-contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
log.Println("sending transaction and waiting for confirmation...")

tx, block, err := w.SendWaitTransaction(context.Background(), &wallet.Message{
Mode: 1, // pay fees separately (from balance, not from amount)
Mode: wallet.PayGasSeparately, // pay fees separately (from balance, not from amount)
InternalMessage: &tlb.InternalMessage{
Bounce: true, // return amount in case of processing error
DstAddr: address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"),
Expand Down
2 changes: 1 addition & 1 deletion example/site-request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func getDNSResolver() *dns.Client {
api := ton.NewAPIClient(client)

// get root dns address from network config
root, err := dns.RootContractAddr(api)
root, err := dns.GetRootContractAddr(context.Background(), api)
if err != nil {
panic(err)
}
Expand Down
6 changes: 5 additions & 1 deletion liteclient/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ func Test_ConnSticky(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ctx = client.StickyContext(ctx)

err := client.AddConnectionsFromConfigUrl(ctx, "https://tonutils.com/global.config.json")
if err != nil {
t.Fatal("add connections err", err)
}

ctx, err = client.StickyContextNextNodeBalanced(ctx)
if err != nil {
t.Fatal("next balanced err", err)
}

doReq := func(expErr error) {
var resp tl.Serializable
err := client.QueryLiteserver(ctx, GetMasterchainInf{}, &resp)
Expand Down
44 changes: 43 additions & 1 deletion liteclient/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const _StickyCtxUsedNodesKey = "_ton_used_nodes_sticky"
var (
ErrNoActiveConnections = errors.New("no active connections")
ErrADNLReqTimeout = errors.New("adnl request timeout")
ErrNoNodesLeft = errors.New("no more active nodes left")
)

type OnDisconnectCallback func(addr, key string)
Expand Down Expand Up @@ -97,6 +98,7 @@ func (c *ConnectionPool) StickyContext(ctx context.Context) context.Context {
return context.WithValue(ctx, _StickyCtxKey, id)
}

// StickyContextNextNode - select next node in the available list (pseudo random)
func (c *ConnectionPool) StickyContextNextNode(ctx context.Context) (context.Context, error) {
nodeID, _ := ctx.Value(_StickyCtxKey).(uint32)
usedNodes, _ := ctx.Value(_StickyCtxUsedNodesKey).([]uint32)
Expand All @@ -118,7 +120,47 @@ iter:
return context.WithValue(context.WithValue(ctx, _StickyCtxKey, node.id), _StickyCtxUsedNodesKey, usedNodes), nil
}

return ctx, fmt.Errorf("no more active nodes left")
return ctx, ErrNoNodesLeft
}

// StickyContextNextNodeBalanced - select next node based on its weight and availability
func (c *ConnectionPool) StickyContextNextNodeBalanced(ctx context.Context) (context.Context, error) {
nodeID, _ := ctx.Value(_StickyCtxKey).(uint32)
usedNodes, _ := ctx.Value(_StickyCtxUsedNodesKey).([]uint32)
if nodeID > 0 {
usedNodes = append(usedNodes, nodeID)
}

c.nodesMx.RLock()
defer c.nodesMx.RUnlock()

var reqNode *connection

iter:
for _, node := range c.activeNodes {
for _, usedNode := range usedNodes {
if usedNode == node.id {
continue iter
}
}

if reqNode == nil {
reqNode = node
continue
}

// select best node on this moment
nw, old := atomic.LoadInt64(&node.weight), atomic.LoadInt64(&reqNode.weight)
if nw > old || (nw == old && atomic.LoadInt64(&node.lastRespTime) < atomic.LoadInt64(&reqNode.lastRespTime)) {
reqNode = node
}
}

if reqNode != nil {
return context.WithValue(context.WithValue(ctx, _StickyCtxKey, reqNode.id), _StickyCtxUsedNodesKey, usedNodes), nil
}

return ctx, ErrNoNodesLeft
}

func (c *ConnectionPool) StickyContextWithNodeID(ctx context.Context, nodeId uint32) context.Context {
Expand Down
2 changes: 1 addition & 1 deletion tlb/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type AccountStorage struct {
}

type StorageUsed struct {
BitsUsed *big.Int `tlb:"var uint 7"`
CellsUsed *big.Int `tlb:"var uint 7"`
BitsUsed *big.Int `tlb:"var uint 7"`
PublicCellsUsed *big.Int `tlb:"var uint 7"`
}

Expand Down
53 changes: 53 additions & 0 deletions tlb/shard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tlb

import (
"encoding/binary"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tvm/cell"
)

Expand All @@ -13,6 +15,8 @@ func init() {
Register(ShardStateUnsplit{})
}

type ShardID uint64

type ShardStateUnsplit struct {
_ Magic `tlb:"#9023afe2"`
GlobalID int32 `tlb:"## 32"`
Expand Down Expand Up @@ -146,3 +150,52 @@ type ShardDescB struct {
FeesCollected CurrencyCollection `tlb:"."`
FundsCreated CurrencyCollection `tlb:"."`
}

func (s ShardID) IsSibling(with ShardID) bool {
return (s^with) != 0 && ((s ^ with) == ((s & ShardID(bitsNegate64(uint64(s)))) << 1))
}

func (s ShardID) IsParent(of ShardID) bool {
y := lowerBit64(uint64(s))
return y > 0 && of.GetParent() == s
}

func (s ShardID) GetParent() ShardID {
y := lowerBit64(uint64(s))
return ShardID((uint64(s) - y) | (y << 1))
}

func (s ShardID) GetChild(left bool) ShardID {
y := lowerBit64(uint64(s)) >> 1
if left {
return s - ShardID(y)
}
return s + ShardID(y)
}

func (s ShardID) ContainsAddress(addr *address.Address) bool {
x := lowerBit64(uint64(s))
return ((uint64(s) ^ binary.BigEndian.Uint64(addr.Data())) & (bitsNegate64(x) << 1)) == 0
}

func (s ShardID) IsAncestor(of ShardID) bool {
x := lowerBit64(uint64(s))
y := lowerBit64(uint64(of))
return x >= y && uint64(s^of)&(bitsNegate64(x)<<1) == 0
}

func (s ShardIdent) IsSibling(with ShardIdent) bool {
return s.WorkchainID == with.WorkchainID && ShardID(s.ShardPrefix).IsSibling(ShardID(with.ShardPrefix))
}

func (s ShardIdent) IsAncestor(of ShardIdent) bool {
return s.WorkchainID == of.WorkchainID && ShardID(s.ShardPrefix).IsAncestor(ShardID(of.ShardPrefix))
}

func (s ShardIdent) IsParent(of ShardIdent) bool {
return s.WorkchainID == of.WorkchainID && ShardID(s.ShardPrefix).IsParent(ShardID(of.ShardPrefix))
}

func (s ShardIdent) GetShardID() ShardID {
return ShardID(s.ShardPrefix)
}
Loading