Skip to content

Commit

Permalink
Add Config Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
erokhinav committed Aug 16, 2024
1 parent 2c9be7c commit 53a67dd
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.9.0
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20240812140315-a2b21a866ae6
github.com/tonkeeper/tongo v1.9.1
github.com/tonkeeper/tongo v1.9.2-0.20240816140152-3ac76957f802
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/metric v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ github.com/tonkeeper/scam_backoffice_rules v0.0.0-20240812140315-a2b21a866ae6 h1
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20240812140315-a2b21a866ae6/go.mod h1:SqZXYO9vbID8ku+xnnaKXeNGmehxigODGrk5V1KqbRA=
github.com/tonkeeper/tongo v1.9.1 h1:RaTHi7zvAhclv9EU9/9HLdGUZwq9iUc8EO9xtpuPwus=
github.com/tonkeeper/tongo v1.9.1/go.mod h1:MjgIgAytFarjCoVjMLjYEtpZNN1f2G/pnZhKjr28cWs=
github.com/tonkeeper/tongo v1.9.2-0.20240816140152-3ac76957f802 h1:X2EhvlPs8zzenlz1uUtFCmcsWNq1ST7U+rwXxZGnt50=
github.com/tonkeeper/tongo v1.9.2-0.20240816140152-3ac76957f802/go.mod h1:MjgIgAytFarjCoVjMLjYEtpZNN1f2G/pnZhKjr28cWs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
Expand Down
31 changes: 21 additions & 10 deletions pkg/api/account_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"errors"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -21,18 +22,18 @@ var precompileUsageMc = promauto.NewCounterVec(prometheus.CounterOpts{
}, []string{"status", "hash"})

type shardsAccountExecutor struct {
accounts map[tongo.AccountID]tlb.ShardAccount
configBase64 string
resolver core.LibraryResolver
executor executor
accounts map[tongo.AccountID]tlb.ShardAccount
resolver core.LibraryResolver
executor executor
configPool *sync.Pool
}

func newSharedAccountExecutor(accounts map[tongo.AccountID]tlb.ShardAccount, executor executor, resolver core.LibraryResolver, configBase64 string) *shardsAccountExecutor {
func newSharedAccountExecutor(accounts map[tongo.AccountID]tlb.ShardAccount, executor executor, resolver core.LibraryResolver, configPool *sync.Pool) *shardsAccountExecutor {
return &shardsAccountExecutor{
accounts: accounts,
configBase64: configBase64,
resolver: resolver,
executor: executor,
accounts: accounts,
resolver: resolver,
executor: executor,
configPool: configPool,
}
}

Expand Down Expand Up @@ -73,7 +74,17 @@ func (s shardsAccountExecutor) RunSmcMethodByID(ctx context.Context, accountID t
if err != nil {
return 0, nil, err
}
e, err := tvm.NewEmulatorFromBOCsBase64(codeBoc, dataBoc, s.configBase64, tvm.WithLibrariesBase64(librariesBase64), tvm.WithLibraryResolver(s.resolver))
configObject := s.configPool.Get().(*tvm.Config)
defer s.configPool.Put(configObject)

if configObject == nil {
return 0, nil, errors.New("error getting BlockchainConfig from the pool")
}

e, err := tvm.NewEmulatorFromBOCsBase64(codeBoc, dataBoc, "",
tvm.WithLibrariesBase64(librariesBase64),
tvm.WithLibraryResolver(s.resolver),
tvm.WithConfig(configObject))
if err != nil {
return 0, nil, err
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/api/blockchain_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,6 @@ func (h *Handler) GetBlockchainValidators(ctx context.Context) (*oas.Validators,
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
bConfig, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
configCell, err := boc.DeserializeSinglRootBase64(bConfig)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
electorAddr, ok := config.ElectorAddr()
if !ok {
return nil, toError(http.StatusInternalServerError, fmt.Errorf("can't get elector address"))
Expand Down Expand Up @@ -441,7 +433,13 @@ func (h *Handler) GetBlockchainValidators(ctx context.Context) (*oas.Validators,
code := init.Code.Value.Value
data := init.Data.Value.Value

emulator, err := tvm.NewEmulator(&code, &data, configCell)
configObject := h.configPool.Get().(*tvm.Config)
defer h.configPool.Put(configObject)
if configObject == nil {
return nil, toError(http.StatusInternalServerError, fmt.Errorf("error getting BlockchainConfig from the pool"))
}

emulator, err := tvm.NewEmulator(&code, &data, nil, tvm.WithConfig(configObject))
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/api/emulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"encoding/base64"
"errors"
"golang.org/x/exp/slices"
"math/big"
"sync"
"time"

"golang.org/x/exp/slices"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/shopspring/decimal"
Expand Down Expand Up @@ -132,7 +134,7 @@ func (h *Handler) addToMempool(ctx context.Context, bytesBoc []byte, shardAccoun
return shardAccount, err
}
newShardAccount := emulator.FinalStates()
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, config, tree, newShardAccount, nil)
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, tree, newShardAccount, nil, h.configPool)
if err != nil {
return shardAccount, err
}
Expand Down Expand Up @@ -180,10 +182,10 @@ func emulatedTreeToTrace(
ctx context.Context,
executor executor,
resolver core.LibraryResolver,
configBase64 string,
tree *txemulator.TxTree,
accounts map[tongo.AccountID]tlb.ShardAccount,
inspectionCache map[ton.AccountID]*abi.ContractDescription,
configPool *sync.Pool,
) (*core.Trace, error) {
if !tree.TX.Msgs.InMsg.Exists {
return nil, errors.New("there is no incoming message in emulation result")
Expand Down Expand Up @@ -220,7 +222,7 @@ func emulatedTreeToTrace(
}
additionalInfo := &core.TraceAdditionalInfo{}
for i := range tree.Children {
child, err := emulatedTreeToTrace(ctx, executor, resolver, configBase64, tree.Children[i], accounts, inspectionCache)
child, err := emulatedTreeToTrace(ctx, executor, resolver, tree.Children[i], accounts, inspectionCache, configPool)
if err != nil {
return nil, err
}
Expand All @@ -240,7 +242,7 @@ func emulatedTreeToTrace(
return nil, err
}
emulatedAccountCode.WithLabelValues(codeHash).Inc()
sharedExecutor := newSharedAccountExecutor(accounts, executor, resolver, configBase64)
sharedExecutor := newSharedAccountExecutor(accounts, executor, resolver, configPool)
inspectionResult, ok := inspectionCache[accountID]
if !ok {
inspectionResult, err = abi.NewContractInspector(abi.InspectWithLibraryResolver(resolver)).InspectContract(ctx, b, sharedExecutor, accountID)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates(), nil)
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates(), nil)
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates(), nil)
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -601,7 +601,7 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, configBase64, tree, emulator.FinalStates(), nil)
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down
20 changes: 18 additions & 2 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tonkeeper/tongo/tep64"
"github.com/tonkeeper/tongo/ton"
"github.com/tonkeeper/tongo/tonconnect"
"github.com/tonkeeper/tongo/tvm"
"go.uber.org/zap"

"github.com/tonkeeper/opentonapi/pkg/addressbook"
Expand Down Expand Up @@ -59,8 +60,9 @@ type Handler struct {
getMethodsCache cache.Cache[string, *oas.MethodExecutionResult]

// mu protects "dns".
mu sync.Mutex
dns *dns.DNS // todo: update when blockchain config changes
mu sync.Mutex
dns *dns.DNS // todo: update when blockchain config changes
configPool *sync.Pool
}

func (h *Handler) NewError(ctx context.Context, err error) *oas.ErrorStatusCode {
Expand Down Expand Up @@ -177,6 +179,19 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
return nil
}
}
configBase64, err := options.storage.TrimmedConfigBase64()
if err != nil {
return nil, err
}
configPool := &sync.Pool{
New: func() interface{} {
config, err := tvm.CreateConfig(configBase64)
if err != nil {
return nil
}
return config
},
}
tonConnect, err := tonconnect.NewTonConnect(options.executor, options.tonConnectSecret)
if err != nil {
return nil, fmt.Errorf("failed to init tonconnect")
Expand Down Expand Up @@ -208,6 +223,7 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
blacklistedBocCache: cache.NewLRUCache[[32]byte, struct{}](100000, "blacklisted_boc_cache"),
getMethodsCache: cache.NewLRUCache[string, *oas.MethodExecutionResult](100000, "get_methods_cache"),
tonConnect: tonConnect,
configPool: configPool,
}, nil
}

Expand Down

0 comments on commit 53a67dd

Please sign in to comment.