Skip to content

count finished tx #650

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

Open
wants to merge 9 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
7 changes: 7 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5950,6 +5950,13 @@
},
"type": "array"
},
"progress": {
"example": 0.5,
"format": "float",
"maximum": 1,
"minimum": 0,
"type": "number"
},
"transaction": {
"$ref": "#/components/schemas/Transaction"
}
Expand Down
6 changes: 6 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6913,6 +6913,12 @@ components:
emulated:
type: boolean
example: false
progress:
type: number
format: float
minimum: 0
maximum: 1
example: 0.5
MessageConsequences:
type: object
required:
Expand Down
11 changes: 10 additions & 1 deletion pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ func distinctAccounts(skip *tongo.AccountID, book addressBook, accounts ...*tong
}

func convertTrace(t *core.Trace, book addressBook) oas.Trace {
trace := oas.Trace{Transaction: convertTransaction(t.Transaction, t.AccountInterfaces, book), Interfaces: g.ToStrings(t.AccountInterfaces)}
trace := oas.Trace{
Transaction: convertTransaction(t.Transaction, t.AccountInterfaces, book),
Interfaces: g.ToStrings(t.AccountInterfaces),
Emulated: oas.OptBool{Set: true, Value: t.Emulated},
}

// if root transaction
if t.InMsg.IsExternal() {
trace.SetProgress(oas.OptFloat32{Set: true, Value: t.CalculateProgress()})
}

sort.Slice(t.Children, func(i, j int) bool {
if t.Children[i].InMsg == nil || t.Children[j].InMsg == nil {
Expand Down
63 changes: 38 additions & 25 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func (h *Handler) GetTrace(ctx context.Context, params oas.GetTraceParams) (*oas
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
if trace.InProgress() {
traceId := trace.Hash.Hex()
traceEmulated, _, _, err := h.storage.GetTraceWithState(ctx, traceId[0:len(traceId)/2])
if err != nil {
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
if traceEmulated != nil {
traceEmulated = core.CopyTraceData(ctx, trace, traceEmulated)
trace = traceEmulated
}
}
convertedTrace := convertTrace(trace, h.addressBook)
if emulated {
setRecursiveEmulated(&convertedTrace)
Expand Down Expand Up @@ -193,6 +204,20 @@ func (h *Handler) GetEvent(ctx context.Context, params oas.GetEventParams) (*oas
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}

if trace.InProgress() {
hash := trace.Hash.Hex()
traceEmulated, _, _, err := h.storage.GetTraceWithState(ctx, hash[0:len(hash)/2])
if err != nil {
h.logger.Warn("get trace from storage: ", zap.Error(err))
}
if traceEmulated != nil {
// we copy data from finished transactions. for emulated it's provided while emulation
traceEmulated = core.CopyTraceData(ctx, trace, traceEmulated)
trace = traceEmulated
}
}

actions, err := bath.FindActions(ctx, trace, bath.WithInformationSource(h.storage))
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -399,10 +424,7 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
hash, err := c.HashString()
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
hash := m.Hash(true).Hex()
trace, version, _, err := h.storage.GetTraceWithState(ctx, hash)
if err != nil {
h.logger.Warn("get trace from storage: ", zap.Error(err))
Expand Down Expand Up @@ -466,12 +488,13 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var m tlb.Message
if err := tlb.Unmarshal(c, &m); err != nil {
return nil, toError(http.StatusBadRequest, err)
}
trace, prs := h.mempoolEmulate.traces.Get(hash)
if !prs {
hs, err := c.HashString()
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
hs := m.Hash(true).Hex()
var version int
trace, version, _, err = h.storage.GetTraceWithState(ctx, hs)
if err != nil {
Expand All @@ -482,10 +505,6 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
if version > h.tongoVersion {
savedEmulatedTraces.WithLabelValues("expired").Inc()
}
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)
Expand Down Expand Up @@ -541,12 +560,14 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var m tlb.Message
err = tlb.Unmarshal(c, &m)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
trace, prs := h.mempoolEmulate.traces.Get(hash)
if !prs {
hs, err := c.HashString()
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
hs := m.Hash(true).Hex()
var version int
trace, version, _, err = h.storage.GetTraceWithState(ctx, hs)
if err != nil {
Expand All @@ -557,11 +578,6 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
if version > h.tongoVersion {
savedEmulatedTraces.WithLabelValues("expired").Inc()
}
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)
Expand Down Expand Up @@ -682,10 +698,7 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
return nil, toError(http.StatusInternalServerError, err)
}

hash, err := msgCell.HashString()
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
hash := m.Hash(true).Hex()
trace, version, _, err := h.storage.GetTraceWithState(ctx, hash)
if err != nil {
h.logger.Warn("get trace from storage: ", zap.Error(err))
Expand Down
65 changes: 65 additions & 0 deletions pkg/core/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (t *Trace) SetAdditionalInfo(info *TraceAdditionalInfo) {
t.additionalInfo = info
}

func (t *Trace) SetAccountInterfaces(ifaces []abi.ContractInterface) {
t.mu.Lock()
defer t.mu.Unlock()
t.AccountInterfaces = ifaces
}

func (t *TraceAdditionalInfo) MarshalJSON() ([]byte, error) {
type Alias struct {
JettonMasters map[string]string `json:",omitempty"`
Expand Down Expand Up @@ -137,6 +143,29 @@ func (t *Trace) countUncompleted() int {
return c
}

func (t *Trace) CalculateProgress() float32 {
var calculateProgress func(t *Trace)
var finished, all int
calculateProgress = func(t *Trace) {
if t == nil {
return
}
if !t.Emulated {
finished += 1
}
all += 1
for _, child := range t.Children {
calculateProgress(child)
}
}
calculateProgress(t)

if all == 0 {
return 0
}
return float32(finished) / float32(all)
}

type EmulatedTeleitemNFT struct {
Index decimal.Decimal
CollectionAddress *tongo.AccountID
Expand Down Expand Up @@ -210,6 +239,42 @@ func DistinctAccounts(trace *Trace) []tongo.AccountID {
return maps.Keys(accounts)
}

// CopyTraceData copies additional data and transaction data from one trace to another.
// It copies TraceAdditionalInfo, AccountInterfaces, and the embedded Transaction.
func CopyTraceData(ctx context.Context, fromTrace *Trace, toTrace *Trace) *Trace {
additionalDataByHash := make(map[tongo.Bits256]*TraceAdditionalInfo)
interfacesByHash := make(map[tongo.Bits256][]abi.ContractInterface)
transactionByHash := make(map[tongo.Bits256]Transaction)

Visit(fromTrace, func(trace *Trace) {
if trace.Hash != (tongo.Bits256{}) {
if additionalInfo := trace.AdditionalInfo(); additionalInfo != nil {
additionalDataByHash[trace.Hash] = additionalInfo
}
if len(trace.AccountInterfaces) > 0 {
interfacesByHash[trace.Hash] = trace.AccountInterfaces
}
transactionByHash[trace.Hash] = trace.Transaction
}
})

Visit(toTrace, func(trace *Trace) {
if trace.Hash != (tongo.Bits256{}) {
if additionalInfo, exists := additionalDataByHash[trace.Hash]; exists {
trace.SetAdditionalInfo(additionalInfo)
}
if interfaces, exists := interfacesByHash[trace.Hash]; exists {
trace.SetAccountInterfaces(interfaces)
}
if transaction, exists := transactionByHash[trace.Hash]; exists {
trace.Transaction = transaction
}
}
})

return toTrace
}

// CollectAdditionalInfo goes over the whole trace
// and populates trace.TraceAdditionalInfo based on information
// provided by InformationSource.
Expand Down
54 changes: 53 additions & 1 deletion pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading