Skip to content

Commit

Permalink
add trace in mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Jul 19, 2023
1 parent 1078a2f commit b186e19
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
43 changes: 43 additions & 0 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"encoding/base64"
"errors"
"fmt"
"time"

"github.com/tonkeeper/opentonapi/pkg/bath"
"github.com/tonkeeper/opentonapi/pkg/cache"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/opentonapi/pkg/wallet"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/txemulator"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

Expand All @@ -28,6 +31,7 @@ func (h Handler) SendMessage(ctx context.Context, req oas.SendMessageReq) (r oas
if err := h.msgSender.SendMessage(ctx, payload); err != nil {
return &oas.InternalError{Error: err.Error()}, nil
}
go h.addToMempool(ctx, payload)
return &oas.SendMessageOK{}, nil
}

Expand Down Expand Up @@ -293,6 +297,45 @@ func (h Handler) EmulateWalletMessage(ctx context.Context, req oas.EmulateWallet
return &consequences, nil
}

func (h Handler) addToMempool(ctx context.Context, bytesBoc []byte) {
msgCell, err := boc.DeserializeBoc(bytesBoc)
if err != nil {
return
}
var message tlb.Message
err = tlb.Unmarshal(msgCell[0], &message)
if err != nil {
return
}
emulator, err := txemulator.NewTraceBuilder()
if err != nil {
return
}
tree, err := emulator.Run(ctx, message)
if err != nil {
return
}
trace, err := emulatedTreeToTrace(tree, emulator.FinalStates())
if err != nil {
return
}
accounts := make(map[tongo.AccountID]bool)
var traverse func(*core.Trace)
traverse = func(node *core.Trace) {
accounts[node.Account] = true
for _, child := range node.Children {
traverse(child)
}
}
traverse(trace)
h.mempoolEmulateCache.tracesCache.Set(trace.Hash, trace, cache.WithExpiration(time.Second*20))
for _, account := range maps.Keys(accounts) {
traces, _ := h.mempoolEmulateCache.accountsTracesCache.Get(account)
traces = slices.Insert(traces, 0, trace.Hash)
h.mempoolEmulateCache.accountsTracesCache.Set(account, traces)
}
}

func emulatedTreeToTrace(tree *txemulator.TxTree, accounts map[tongo.AccountID]tlb.ShardAccount) (*core.Trace, error) {
if !tree.TX.Msgs.InMsg.Exists {
return nil, errors.New("there is no incoming message in emulation result")
Expand Down
30 changes: 18 additions & 12 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/go-faster/errors"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/rates"
rules "github.com/tonkeeper/scam_backoffice_rules"
"github.com/tonkeeper/tongo"
Expand All @@ -27,18 +28,19 @@ var _ oas.Handler = (*Handler)(nil)
type Handler struct {
oas.UnimplementedHandler // automatically implement all methods

addressBook addressBook
storage storage
state chainState
msgSender messageSender
previewGenerator previewGenerator
executor executor
dns *dns.DNS
limits Limits
spamRules func() rules.Rules
ratesSource ratesSource
metaCache metadataCache
tonConnect *tonconnect.Server
addressBook addressBook
storage storage
state chainState
msgSender messageSender
previewGenerator previewGenerator
executor executor
dns *dns.DNS
limits Limits
spamRules func() rules.Rules
ratesSource ratesSource
metaCache metadataCache
mempoolEmulateCache mempoolEmulateCache
tonConnect *tonconnect.Server
}

// Options configures behavior of a Handler instance.
Expand Down Expand Up @@ -170,6 +172,10 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
jettonsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "jetton_metadata_cache"),
storage: options.storage,
},
mempoolEmulateCache: mempoolEmulateCache{
tracesCache: cache.NewLRUCache[tongo.Bits256, *core.Trace](10000, "mempool_traces_cache"),
accountsTracesCache: cache.NewLRUCache[tongo.AccountID, []tongo.Bits256](10000, "accounts_traces_cache"),
},
tonConnect: tonConnect,
}, nil
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,8 @@ type metadataCache struct {
GetNftCollectionByCollectionAddress(ctx context.Context, address tongo.AccountID) (core.NftCollection, error)
}
}

type mempoolEmulateCache struct {
tracesCache cache.Cache[tongo.Bits256, *core.Trace]
accountsTracesCache cache.Cache[tongo.AccountID, []tongo.Bits256]
}

0 comments on commit b186e19

Please sign in to comment.