Skip to content

Commit b498216

Browse files
committed
save tongo version
1 parent 220f35e commit b498216

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

pkg/api/event_handlers.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,11 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
395395
if err != nil {
396396
return nil, toError(http.StatusBadRequest, err)
397397
}
398-
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
398+
trace, version, _, err := h.storage.GetTraceWithState(ctx, hash)
399399
if err != nil {
400400
h.logger.Warn("get trace from storage: ", zap.Error(err))
401401
}
402-
if trace == nil {
402+
if trace == nil || h.tongoVersion == 0 || version > h.tongoVersion {
403403
configBase64, err := h.storage.TrimmedConfigBase64()
404404
if err != nil {
405405
return nil, toError(http.StatusInternalServerError, err)
@@ -424,7 +424,7 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
424424
if err != nil {
425425
return nil, toError(http.StatusInternalServerError, err)
426426
}
427-
err = h.storage.SaveTraceWithState(ctx, hash, trace, []abi.MethodInvocation{}, 24*time.Hour)
427+
err = h.storage.SaveTraceWithState(ctx, hash, trace, h.tongoVersion, []abi.MethodInvocation{}, 24*time.Hour)
428428
if err != nil {
429429
fmt.Println("trace not saved: ", err)
430430
}
@@ -456,11 +456,11 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
456456
if err != nil {
457457
return nil, toError(http.StatusBadRequest, err)
458458
}
459-
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
459+
trace, version, _, err := h.storage.GetTraceWithState(ctx, hs)
460460
if err != nil {
461461
h.logger.Warn("get trace from storage: ", zap.Error(err))
462462
}
463-
if trace == nil {
463+
if trace == nil || h.tongoVersion == 0 || version > h.tongoVersion {
464464
var m tlb.Message
465465
if err := tlb.Unmarshal(c, &m); err != nil {
466466
return nil, toError(http.StatusBadRequest, err)
@@ -489,7 +489,7 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
489489
if err != nil {
490490
return nil, toError(http.StatusInternalServerError, err)
491491
}
492-
err = h.storage.SaveTraceWithState(ctx, hs, trace, []abi.MethodInvocation{}, 24*time.Hour)
492+
err = h.storage.SaveTraceWithState(ctx, hs, trace, h.tongoVersion, []abi.MethodInvocation{}, 24*time.Hour)
493493
if err != nil {
494494
fmt.Println("trace not saved: ", err)
495495
}
@@ -522,11 +522,11 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
522522
if err != nil {
523523
return nil, toError(http.StatusBadRequest, err)
524524
}
525-
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
525+
trace, version, _, err := h.storage.GetTraceWithState(ctx, hs)
526526
if err != nil {
527527
h.logger.Warn("get trace from storage: ", zap.Error(err))
528528
}
529-
if trace == nil {
529+
if trace == nil || h.tongoVersion == 0 || version > h.tongoVersion {
530530
var m tlb.Message
531531
err = tlb.Unmarshal(c, &m)
532532
if err != nil {
@@ -556,7 +556,7 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
556556
if err != nil {
557557
return nil, toError(http.StatusInternalServerError, err)
558558
}
559-
err = h.storage.SaveTraceWithState(ctx, hs, trace, []abi.MethodInvocation{}, 24*time.Hour)
559+
err = h.storage.SaveTraceWithState(ctx, hs, trace, h.tongoVersion, []abi.MethodInvocation{}, 24*time.Hour)
560560
if err != nil {
561561
fmt.Println("trace not saved: ", err)
562562
}
@@ -651,11 +651,11 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
651651
if err != nil {
652652
return nil, toError(http.StatusBadRequest, err)
653653
}
654-
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
654+
trace, version, _, err := h.storage.GetTraceWithState(ctx, hash)
655655
if err != nil {
656656
h.logger.Warn("get trace from storage: ", zap.Error(err))
657657
}
658-
if trace == nil {
658+
if trace == nil || h.tongoVersion == 0 || version > h.tongoVersion {
659659
configBase64, err := h.storage.TrimmedConfigBase64()
660660
if err != nil {
661661
return nil, toError(http.StatusInternalServerError, err)
@@ -696,7 +696,7 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
696696
if err != nil {
697697
return nil, toError(http.StatusInternalServerError, err)
698698
}
699-
err = h.storage.SaveTraceWithState(ctx, hash, trace, []abi.MethodInvocation{}, 24*time.Hour)
699+
err = h.storage.SaveTraceWithState(ctx, hash, trace, h.tongoVersion, []abi.MethodInvocation{}, 24*time.Hour)
700700
if err != nil {
701701
fmt.Println("trace not saved: ", err)
702702
}

pkg/api/handler.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"fmt"
6+
"golang.org/x/exp/slog"
67
"sync"
78

89
"github.com/go-faster/errors"
@@ -51,6 +52,7 @@ type Handler struct {
5152
mempoolEmulate mempoolEmulate
5253
// ctxToDetails converts a request context to a details instance.
5354
ctxToDetails ctxToDetails
55+
tongoVersion int
5456

5557
// mempoolEmulateIgnoreAccounts, we don't track pending transactions for this list of accounts.
5658
mempoolEmulateIgnoreAccounts map[tongo.AccountID]struct{}
@@ -218,6 +220,10 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
218220
if options.score == nil {
219221
options.score = score.NewScore()
220222
}
223+
tongoVersion, err := GetPackageVersionInt("tongo")
224+
if err != nil {
225+
slog.Warn("unable to detect tongo version", "err", err)
226+
}
221227
return &Handler{
222228
logger: logger,
223229
storage: options.storage,
@@ -244,6 +250,7 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
244250
mempoolEmulateIgnoreAccounts: map[tongo.AccountID]struct{}{
245251
tongo.MustParseAddress("0:0000000000000000000000000000000000000000000000000000000000000000").ID: {},
246252
},
253+
tongoVersion: tongoVersion,
247254
blacklistedBocCache: cache.NewLRUCache[[32]byte, struct{}](100000, "blacklisted_boc_cache"),
248255
getMethodsCache: cache.NewLRUCache[string, *oas.MethodExecutionResult](100000, "get_methods_cache"),
249256
tonConnect: tonConnect,

pkg/api/interfaces.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ type storage interface {
112112
GetAccountMultisigs(ctx context.Context, accountID ton.AccountID) ([]core.Multisig, error)
113113
GetMultisigByID(ctx context.Context, accountID ton.AccountID) (*core.Multisig, error)
114114

115-
SaveTraceWithState(ctx context.Context, msgHash string, trace *core.Trace, getMethods []abi.MethodInvocation, ttl time.Duration) error
116-
GetTraceWithState(ctx context.Context, msgHash string) (*core.Trace, []abi.MethodInvocation, error)
115+
SaveTraceWithState(ctx context.Context, msgHash string, trace *core.Trace, version int, getMethods []abi.MethodInvocation, ttl time.Duration) error
116+
GetTraceWithState(ctx context.Context, msgHash string) (*core.Trace, int, []abi.MethodInvocation, error)
117117

118118
liteStorageRaw
119119
}

pkg/api/utils.go

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package api
22

33
import (
44
"fmt"
5+
"runtime/debug"
6+
"strconv"
7+
"strings"
58

69
"github.com/tonkeeper/tongo/boc"
710
)
@@ -25,3 +28,31 @@ func deserializeSingleBoc(bocStr string) (*boc.Cell, error) {
2528
}
2629
return cells[0], nil
2730
}
31+
32+
func GetPackageVersionInt(packagePath string) (int, error) {
33+
info, ok := debug.ReadBuildInfo()
34+
if !ok {
35+
return 0, fmt.Errorf("error getting build info")
36+
}
37+
38+
for _, dep := range info.Deps {
39+
if strings.Contains(dep.Path, packagePath) {
40+
version := strings.TrimPrefix(dep.Version, "v")
41+
42+
parts := strings.Split(version, ".")
43+
44+
result := 0
45+
for _, part := range parts {
46+
num, err := strconv.Atoi(part)
47+
if err != nil {
48+
return 0, fmt.Errorf("error parsing version number: %v", err)
49+
}
50+
result = result*100 + num
51+
}
52+
53+
return result, nil
54+
}
55+
}
56+
57+
return 0, fmt.Errorf("package %s not found", packagePath)
58+
}

pkg/litestorage/litestorage.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ func (s *LiteStorage) GetMultisigByID(ctx context.Context, accountID ton.Account
567567
return nil, fmt.Errorf("not implemented")
568568
}
569569

570-
func (s *LiteStorage) SaveTraceWithState(ctx context.Context, msgHash string, trace *core.Trace, getMethods []abi.MethodInvocation, ttl time.Duration) error {
570+
func (s *LiteStorage) SaveTraceWithState(ctx context.Context, msgHash string, trace *core.Trace, version int, getMethods []abi.MethodInvocation, ttl time.Duration) error {
571571
return fmt.Errorf("not implemented")
572572
}
573573

574-
func (s *LiteStorage) GetTraceWithState(ctx context.Context, msgHash string) (*core.Trace, []abi.MethodInvocation, error) {
575-
return nil, nil, fmt.Errorf("not implemented")
574+
func (s *LiteStorage) GetTraceWithState(ctx context.Context, msgHash string) (*core.Trace, int, []abi.MethodInvocation, error) {
575+
return nil, 0, nil, fmt.Errorf("not implemented")
576576
}

0 commit comments

Comments
 (0)