Skip to content

Commit

Permalink
Merge pull request #107 from xssnick/storage
Browse files Browse the repository at this point in the history
Fixed connection and serialization error determinition for retry
  • Loading branch information
xssnick authored Mar 6, 2023
2 parents 6830774 + 6d446d9 commit fb9b3fa
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 27 deletions.
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.3%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-73.1%25-brightgreen)

Golang library for interacting with TON blockchain.

Expand Down
53 changes: 32 additions & 21 deletions example/nft-info/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,47 @@ func main() {
panic(err)
}

// get info about our nft's collection
collection := nft.NewCollectionClient(api, nftData.CollectionAddress)
collectionData, err := collection.GetCollectionData(context.Background())
if err != nil {
panic(err)
}
var nftContent nft.ContentAny
if nftData.CollectionAddress.Type() != address.NoneAddress {
// get info about our nft's collection
collection := nft.NewCollectionClient(api, nftData.CollectionAddress)
collectionData, err := collection.GetCollectionData(context.Background())
if err != nil {
panic(err)
}

fmt.Println("Collection addr :", nftData.CollectionAddress.String())
switch content := collectionData.Content.(type) {
case *nft.ContentOffchain:
fmt.Println(" content offchain :", content.URI)
case *nft.ContentOnchain:
fmt.Println(" content onchain :", content.Name)
}
fmt.Println(" owner :", collectionData.OwnerAddress.String())
fmt.Println(" minted items num :", collectionData.NextItemIndex)
fmt.Println()

fmt.Println("Collection addr :", nftData.CollectionAddress.String())
switch content := collectionData.Content.(type) {
case *nft.ContentOffchain:
fmt.Println(" content offchain :", content.URI)
case *nft.ContentOnchain:
fmt.Println(" content onchain :", content.Name)
// get full nft's content url using collection method that will merge base url with nft's data
nftContent, err = collection.GetNFTContent(context.Background(), nftData.Index, nftData.Content)
if err != nil {
panic(err)
}
}
fmt.Println(" owner :", collectionData.OwnerAddress.String())
fmt.Println(" minted items num :", collectionData.NextItemIndex)
fmt.Println()
fmt.Println("NFT addr :", nftAddr.String())
fmt.Println(" initialized :", nftData.Initialized)
fmt.Println(" owner :", nftData.OwnerAddress.String())
fmt.Println(" index :", nftData.Index)

if nftData.Initialized {
// get full nft's content url using collection method that will merge base url with nft's data
nftContent, err := collection.GetNFTContent(context.Background(), nftData.Index, nftData.Content)
if err != nil {
panic(err)
switch content := nftData.Content.(type) {
case *nft.ContentOffchain:
fmt.Println(" content :", content.URI)
if nftContent != nil {
fmt.Println(" full content :", nftContent.(*nft.ContentOffchain).URI)
}
case *nft.ContentOnchain:
fmt.Println(" content name :", content.Name)
}
fmt.Println(" part content :", nftData.Content.(*nft.ContentOffchain).URI)
fmt.Println(" full content :", nftContent.(*nft.ContentOffchain).URI)
} else {
fmt.Println(" empty content")
}
Expand Down
17 changes: 16 additions & 1 deletion liteclient/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,21 @@ func (n *connection) readData(sz uint32) ([]byte, error) {
return result, nil
}

type NetworkErr struct {
error
}

func (e NetworkErr) Is(err error) bool {
if _, ok := err.(NetworkErr); ok {
return true
}
return false
}

func (e NetworkErr) Unwrap() error {
return e.error
}

func (n *connection) send(data []byte) error {
buf := make([]byte, 4)

Expand Down Expand Up @@ -394,7 +409,7 @@ func (n *connection) send(data []byte) error {
num, err := n.tcp.Write(buf)
if err != nil {
n.tcp.Close()
return err
return NetworkErr{err}
}

buf = buf[num:]
Expand Down
3 changes: 3 additions & 0 deletions liteclient/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func (c *ConnectionPool) queryWithBalancer(req *ADNLRequest) (string, error) {

host, err := reqNode.queryAdnl(req.QueryID, req.Data)
if err != nil {
if !errors.Is(err, NetworkErr{}) {
return "", err
}
nodeOffset++
continue
}
Expand Down
8 changes: 6 additions & 2 deletions tl/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,14 @@ func serializeField(tags []string, value reflect.Value) (buf []byte, err error)
switch value.Type().Elem().Kind() {
case reflect.Uint8:
if tags[0] == "int256" {
if len(value.Bytes()) != 32 {
bytes := value.Bytes()
if len(bytes) == 0 {
// consider it as 0
bytes = make([]byte, 32)
} else if len(bytes) != 32 {
return nil, fmt.Errorf("not 32 bytes for int256 value")
}
buf = append(buf, value.Bytes()...)
buf = append(buf, bytes...)
} else {
buf = append(buf, ToBytes(value.Bytes())...)
}
Expand Down
4 changes: 3 additions & 1 deletion tl/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type TestTL struct {
In *TestInner `tl:"struct boxed"`
InX any `tl:"struct boxed [in]"`
In2 []any `tl:"vector struct boxed [in]"`
KeyEmpty []byte `tl:"int256"`
Data [][]byte `tl:"vector bytes"`
}

Expand All @@ -34,12 +35,13 @@ func TestParse(t *testing.T) {
"e323006f" + "0200000000000000" + "7777777777777777777777777777777777777777777777777777777777777777" +
"e323006f" + "0800000000000000" + "7177777777777777777777777777777777777777777777777777777777777777" +
"02000000" + "e323006f" + "0700000000000000" + "7777777777777777777777777777777777777777777777777777777777777777" + "e323006f" + "0800000000000000" + "7777777777777777777777777777777777777777777777777777777777777777" +
"03000000" + "00000000" + "03112233" + "0411223344000000")
"0000000000000000000000000000000000000000000000000000000000000000" + "03000000" + "00000000" + "03112233" + "0411223344" + "000000")
var tst TestTL
_, err := Parse(&tst, data, true)
if err != nil {
panic(err)
}
tst.KeyEmpty = nil

data2, err := Serialize(tst, true)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion tlb/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/xssnick/tonutils-go/tvm/cell"
)

type smallStruct struct {
Sz uint32 `tlb:"## 8"`
}

type manualLoad struct {
Val string
}
Expand Down Expand Up @@ -37,6 +41,7 @@ type testInner struct {
Addr *address.Address `tlb:"addr"`
Manual manualLoad `tlb:"."`
Dict *cell.Dictionary `tlb:"dict 256"`
StructMaybe *smallStruct `tlb:"maybe ^"`
}

type testTLB struct {
Expand All @@ -62,13 +67,16 @@ func TestLoadFromCell(t *testing.T) {
t.Fatal(err)
}

mRef := cell.BeginCell().MustStoreUInt('y', 8).EndCell()

ref := cell.BeginCell().MustStoreUInt(0b1011, 4).
MustStoreInt(-7172, 34).
MustStoreUInt(0xCCA, 12).
MustStoreCoins(700000).
MustStoreUInt(5, 10).
MustStoreUInt(7126382921832, 176).
MustStoreBoolBit(true).MustStoreAddr(addr).MustStoreUInt('x', 8).MustStoreDict(d)
MustStoreBoolBit(true).MustStoreAddr(addr).MustStoreUInt('x', 8).MustStoreDict(d).
MustStoreMaybeRef(mRef)

a := cell.BeginCell().MustStoreUInt(0xFFAA, 16).
MustStoreUInt(0xFFBF, 16).MustStoreBoolBit(true).MustStoreUInt(0xFFBFFFAA, 32).MustStoreRef(ref.EndCell()).MustStoreMaybeRef(nil).
Expand Down

0 comments on commit fb9b3fa

Please sign in to comment.