Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ For more information on Jettons compatibility, see [Jettons compatibility](/jett
|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `LITESERVER` | IP and port of lite server, example: `185.86.76.183:5815` |
| `LITESERVER_KEY` | public key of lite server `5v2dHtSclsGsZVbNVwTj4hQDso5xvQjzL/yPEHJevHk=`. <br/>Be careful with base64 encoding and ENV var. Use '' |
| `LITESERVER_RATE_LIMIT` | If you have a rented node with an RPS limit, set the RPS value here equal to (or preferably slightly less than) the limit. Default: 100. |
| `SEED` | seed phrase for main hot wallet. 24 words compatible with standard TON wallets |
| `DB_URI` | URI for DB connection, example: <br/>`postgresql://db_user:db_password@localhost:5432/payment_processor` |
| `POSTGRES_DB` | name of database for storing payments data |
Expand Down
8 changes: 5 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type contract struct {
}

// NewConnection creates new Blockchain connection
func NewConnection(addr, key string) (*Connection, error) {
func NewConnection(addr, key string, rateLimit int) (*Connection, error) {

client := liteclient.NewConnectionPool()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
Expand All @@ -64,6 +64,8 @@ func NewConnection(addr, key string) (*Connection, error) {
return nil, fmt.Errorf("connection err: %v", err.Error())
}

limitedClient := newLimitedClient(client, rateLimit)

var wrappedClient ton.APIClientWrapped

if config.Config.ProofCheckEnabled {
Expand All @@ -77,7 +79,7 @@ func NewConnection(addr, key string) (*Connection, error) {
return nil, fmt.Errorf("get network config from url err: %s", err.Error())
}

wrappedClient = ton.NewAPIClient(client, ton.ProofCheckPolicySecure).WithRetry()
wrappedClient = ton.NewAPIClient(limitedClient, ton.ProofCheckPolicySecure).WithRetry()
wrappedClient.SetTrustedBlockFromConfig(cfg)

log.Infof("Fetching and checking proofs since config init block ...")
Expand All @@ -88,7 +90,7 @@ func NewConnection(addr, key string) (*Connection, error) {
log.Infof("Proof checks are completed")

} else {
wrappedClient = ton.NewAPIClient(client, ton.ProofCheckPolicyUnsafe).WithRetry()
wrappedClient = ton.NewAPIClient(limitedClient, ton.ProofCheckPolicyUnsafe).WithRetry()
}

// TODO: replace after tonutils fix
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func connect(t *testing.T) *Connection {
if key == "" {
t.Fatal("empty key var")
}
c, err := NewConnection(server, key)
c, err := NewConnection(server, key, 100)
if err != nil {
t.Fatal("connections err: ", err)
}
Expand Down
46 changes: 46 additions & 0 deletions blockchain/limited_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package blockchain

import (
"context"
"fmt"

"github.com/xssnick/tonutils-go/tl"
"github.com/xssnick/tonutils-go/ton"
"golang.org/x/time/rate"
)

type limitedLiteClient struct {
limiter *rate.Limiter
original ton.LiteClient
}

func newLimitedClient(lc ton.LiteClient, rateLimit int) *limitedLiteClient {
return &limitedLiteClient{
original: lc,
limiter: rate.NewLimiter(rate.Limit(rateLimit), 1),
}
}

func (w *limitedLiteClient) QueryLiteserver(ctx context.Context, payload tl.Serializable, result tl.Serializable) error {
err := w.limiter.Wait(ctx)
if err != nil {
return fmt.Errorf("limiter err: %w", err)
}
return w.original.QueryLiteserver(ctx, payload, result)
}

func (w *limitedLiteClient) StickyContext(ctx context.Context) context.Context {
return w.original.StickyContext(ctx)
}

func (w *limitedLiteClient) StickyNodeID(ctx context.Context) uint32 {
return w.original.StickyNodeID(ctx)
}

func (w *limitedLiteClient) StickyContextNextNode(ctx context.Context) (context.Context, error) {
return w.original.StickyContextNextNode(ctx)
}

func (w *limitedLiteClient) StickyContextNextNodeBalanced(ctx context.Context) (context.Context, error) {
return w.original.StickyContextNextNodeBalanced(ctx)
}
2 changes: 1 addition & 1 deletion cmd/processor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
wg := new(sync.WaitGroup)

bcClient, err := blockchain.NewConnection(config.Config.LiteServer, config.Config.LiteServerKey)
bcClient, err := blockchain.NewConnection(config.Config.LiteServer, config.Config.LiteServerKey, config.Config.LiteServerRateLimit)
if err != nil {
log.Fatalf("blockchain connection error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/testutil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
log.Fatalf("invalid HOT_WALLET_B env var")
}

bcClient, err := blockchain.NewConnection(config.Config.LiteServer, config.Config.LiteServerKey)
bcClient, err := blockchain.NewConnection(config.Config.LiteServer, config.Config.LiteServerKey, config.Config.LiteServerRateLimit)
if err != nil {
log.Fatalf("blockchain connection error: %v", err)
}
Expand Down
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package config

import (
"log"
"math/big"
"strings"
"time"

"github.com/caarlos0/env/v6"
"github.com/shopspring/decimal"
"github.com/tonkeeper/tongo/boc"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"log"
"math/big"
"strings"
"time"
)

const MaxJettonForwardTonAmount = 20_000_000
Expand Down Expand Up @@ -39,6 +40,7 @@ const MaxCommentLength = 1000 // qty in chars
var Config = struct {
LiteServer string `env:"LITESERVER,required"`
LiteServerKey string `env:"LITESERVER_KEY,required"`
LiteServerRateLimit int `env:"LITESERVER_RATE_LIMIT" envDefault:"100"`
Seed string `env:"SEED,required"`
DatabaseURI string `env:"DB_URI,required"`
APIPort int `env:"API_PORT,required"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/tonkeeper/tongo v1.9.9
github.com/xssnick/tonutils-go v1.10.2
golang.org/x/time v0.10.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.10.0 h1:3usCWA8tQn0L8+hFJQNgzpWbd89begxN66o1Ojdn5L4=
golang.org/x/time v0.10.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down