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 2 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
249 changes: 142 additions & 107 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/tonkeeper/tongo/abi"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -351,29 +352,37 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
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),
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)
hash, err := c.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
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(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 +407,41 @@ 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 {
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())
}
hs, _ := c.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
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(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 +467,42 @@ 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)
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())
}
hs, err := c.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
trace, _, err = h.storage.GetTraceWithState(ctx, hs)
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(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 +589,54 @@ 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)
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)
hash, err := msgCell.HashString()
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
trace, _, err := h.storage.GetTraceWithState(ctx, hash)
if trace == nil {
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)
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)
}
state, err := prepareAccountState(*walletAddress, originalState, balance)
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)
}
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)
err = h.storage.SaveTraceWithState(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(msgHash string, trace *core.Trace, getMethods []abi.MethodInvocation, ttl time.Duration) error
erokhinav marked this conversation as resolved.
Show resolved Hide resolved
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