Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save serialized traces #547

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
242 changes: 151 additions & 91 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/tonkeeper/tongo/abi"
"go.uber.org/zap"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -351,29 +353,43 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
hash, err := c.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
txemulator.WithLimit(1100),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
}
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
return nil, toError(http.StatusBadRequest, err)
}
tree, err := emulator.Run(ctx, m)
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
if err != nil {
return nil, toProperEmulationError(err)
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
if trace == nil {
configBase64, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
txemulator.WithLimit(1100),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
}
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
err = h.storage.SaveTraceWithState(ctx, hash, trace, []abi.MethodInvocation{}, 24*time.Hour)
if err != nil {
fmt.Println("trace not saved: ", err)
}
}
actions, err := bath.FindActions(ctx, trace, bath.WithInformationSource(h.storage))
if err != nil {
Expand All @@ -398,33 +414,47 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
}
trace, prs := h.mempoolEmulate.traces.Get(hash)
if !prs {
var m tlb.Message
if err := tlb.Unmarshal(c, &m); err != nil {
hs, err := c.HashString()
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
if trace == nil {
var m tlb.Message
if err := tlb.Unmarshal(c, &m); err != nil {
return nil, toError(http.StatusBadRequest, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
}

emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
err = h.storage.SaveTraceWithState(ctx, hs, trace, []abi.MethodInvocation{}, 24*time.Hour)
if err != nil {
fmt.Println("trace not saved: ", err)
}
}
}
actions, err := bath.FindActions(ctx, trace, bath.WithInformationSource(h.storage))
Expand All @@ -450,34 +480,48 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
}
trace, prs := h.mempoolEmulate.traces.Get(hash)
if !prs {
var m tlb.Message
err = tlb.Unmarshal(c, &m)
hs, err := c.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
if trace == nil {
var m tlb.Message
err = tlb.Unmarshal(c, &m)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
options := []txemulator.TraceOption{
txemulator.WithAccountsSource(h.storage),
txemulator.WithConfigBase64(configBase64),
}
if !params.IgnoreSignatureCheck.Value {
options = append(options, txemulator.WithSignatureCheck())
}

emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
err = h.storage.SaveTraceWithState(ctx, hs, trace, []abi.MethodInvocation{}, 24*time.Hour)
if err != nil {
fmt.Println("trace not saved: ", err)
}
}
}
t := convertTrace(trace, h.addressBook)
Expand Down Expand Up @@ -564,44 +608,60 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
configBase64, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}

options := []txemulator.TraceOption{
txemulator.WithConfigBase64(configBase64),
txemulator.WithAccountsSource(h.storage),
txemulator.WithLimit(1100),
}
accounts, err := convertEmulationParameters(request.Params)
hash, err := msgCell.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var states []tlb.ShardAccount
for accountID, balance := range accounts {
originalState, err := h.storage.GetAccountState(ctx, accountID)
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
if err != nil {
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
if trace == nil {
configBase64, err := h.storage.TrimmedConfigBase64()
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
state, err := prepareAccountState(*walletAddress, originalState, balance)

options := []txemulator.TraceOption{
txemulator.WithConfigBase64(configBase64),
txemulator.WithAccountsSource(h.storage),
txemulator.WithLimit(1100),
}
accounts, err := convertEmulationParameters(request.Params)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var states []tlb.ShardAccount
for accountID, balance := range accounts {
originalState, err := h.storage.GetAccountState(ctx, accountID)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
state, err := prepareAccountState(*walletAddress, originalState, balance)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
states = append(states, state)
}

options = append(options, txemulator.WithAccounts(states...))
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
states = append(states, state)
}
options = append(options, txemulator.WithAccounts(states...))
emulator, err := txemulator.NewTraceBuilder(options...)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err := emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
tree, err := emulator.Run(ctx, m)
if err != nil {
return nil, toProperEmulationError(err)
}
trace, err = emulatedTreeToTrace(ctx, h.executor, h.storage, tree, emulator.FinalStates(), nil, h.configPool)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
err = h.storage.SaveTraceWithState(ctx, hash, trace, []abi.MethodInvocation{}, 24*time.Hour)
if err != nil {
fmt.Println("trace not saved: ", err)
}
}
t := convertTrace(trace, h.addressBook)
actions, err := bath.FindActions(ctx, trace, bath.ForAccount(*walletAddress), bath.WithInformationSource(h.storage))
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"crypto/ed25519"
"time"

"github.com/tonkeeper/opentonapi/pkg/gasless"
"github.com/tonkeeper/opentonapi/pkg/oas"
Expand Down Expand Up @@ -110,6 +111,9 @@ type storage interface {
GetAccountMultisigs(ctx context.Context, accountID ton.AccountID) ([]core.Multisig, error)
GetMultisigByID(ctx context.Context, accountID ton.AccountID) (*core.Multisig, error)

SaveTraceWithState(ctx context.Context, msgHash string, trace *core.Trace, getMethods []abi.MethodInvocation, ttl time.Duration) error
GetTraceWithState(ctx context.Context, msgHash string) (*core.Trace, []abi.MethodInvocation, error)

liteStorageRaw
}

Expand Down
54 changes: 54 additions & 0 deletions pkg/core/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package core

import (
"context"
"encoding/json"
"errors"
"github.com/tonkeeper/tongo/ton"
"sync"

"github.com/shopspring/decimal"
Expand Down Expand Up @@ -65,6 +67,58 @@ func (t *Trace) SetAdditionalInfo(info *TraceAdditionalInfo) {
t.additionalInfo = info
}

func (t *TraceAdditionalInfo) MarshalJSON() ([]byte, error) {
type Alias struct {
JettonMasters map[string]string `json:",omitempty"`
NftSaleContract *NftSaleContract `json:",omitempty"`
STONfiPool *STONfiPool `json:",omitempty"`
EmulatedTeleitemNFT *EmulatedTeleitemNFT `json:",omitempty"`
}

masters := make(map[string]string)
if t.JettonMasters != nil {
for k, v := range t.JettonMasters {
masters[k.String()] = v.String()
}
}

return json.Marshal(&Alias{
JettonMasters: masters,
NftSaleContract: t.NftSaleContract,
STONfiPool: t.STONfiPool,
EmulatedTeleitemNFT: t.EmulatedTeleitemNFT,
})
}

func (t *TraceAdditionalInfo) UnmarshalJSON(data []byte) error {
type Alias struct {
JettonMasters map[string]string `json:",omitempty"`
NftSaleContract *NftSaleContract `json:",omitempty"`
STONfiPool *STONfiPool `json:",omitempty"`
EmulatedTeleitemNFT *EmulatedTeleitemNFT `json:",omitempty"`
}

aux := &Alias{}
if err := json.Unmarshal(data, aux); err != nil {
return err
}

if aux.JettonMasters != nil {
t.JettonMasters = make(map[tongo.AccountID]tongo.AccountID)
for kStr, vStr := range aux.JettonMasters {
key := ton.MustParseAccountID(kStr)
val := ton.MustParseAccountID(vStr)
t.JettonMasters[key] = val
}
}

t.NftSaleContract = aux.NftSaleContract
t.STONfiPool = aux.STONfiPool
t.EmulatedTeleitemNFT = aux.EmulatedTeleitemNFT

return nil
}

func (t *Trace) InProgress() bool {
return t.countUncompleted() != 0
}
Expand Down
Loading
Loading