Skip to content

Commit

Permalink
add in progress events into show events method
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Jul 19, 2023
1 parent b186e19 commit 0cd4ad6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
62 changes: 48 additions & 14 deletions pkg/api/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ func (h Handler) GetTrace(ctx context.Context, params oas.GetTraceParams) (r oas
testTrace := getTestTrace()
return &testTrace, nil
}
t, err := h.storage.GetTrace(ctx, hash)
if errors.Is(err, core.ErrEntityNotFound) {
txHash, err2 := h.storage.SearchTransactionByMessageHash(ctx, hash)
if err2 != nil {
return &oas.NotFound{Error: err.Error()}, nil
t, ok := h.mempoolEmulateCache.tracesCache.Get(hash.Hex())
if !ok {
t, err = h.storage.GetTrace(ctx, hash)
if errors.Is(err, core.ErrEntityNotFound) {
txHash, err2 := h.storage.SearchTransactionByMessageHash(ctx, hash)
if err2 != nil {
return &oas.NotFound{Error: err.Error()}, nil
}
t, err = h.storage.GetTrace(ctx, *txHash)
}
if err != nil {
return nil, err
}
t, err = h.storage.GetTrace(ctx, *txHash)
}
if err != nil {
return nil, err
}
trace := convertTrace(*t, h.addressBook)
return &trace, nil
Expand Down Expand Up @@ -95,13 +98,33 @@ func (h Handler) GetEventsByAccount(ctx context.Context, params oas.GetEventsByA
if err != nil {
return &oas.BadRequest{Error: err.Error()}, nil
}
traceIDs, err := h.storage.SearchTraces(ctx, account, params.Limit, optIntToPointer(params.BeforeLt), optIntToPointer(params.StartDate), optIntToPointer(params.EndDate))
tracesID, err := h.storage.SearchTraces(ctx, account, params.Limit, optIntToPointer(params.BeforeLt), optIntToPointer(params.StartDate), optIntToPointer(params.EndDate))
if err != nil && !errors.Is(err, core.ErrEntityNotFound) {
return &oas.InternalError{Error: err.Error()}, nil
}
events := make([]oas.AccountEvent, len(traceIDs))
mapOfTracesID := make(map[string]bool)
for _, traceID := range tracesID {
mapOfTracesID[traceID.Hex()] = true
}
var (
memTraces []*core.Trace
memActualHexTraces []string
)
memTracesHex, ok := h.mempoolEmulateCache.accountsTracesCache.Get(account)
if ok {
for _, traceHex := range memTracesHex {
memTrace, ok := h.mempoolEmulateCache.tracesCache.Get(traceHex)
if !ok || mapOfTracesID[traceHex] {
continue
}
memTraces = append(memTraces, memTrace)
memActualHexTraces = append(memActualHexTraces, traceHex)
}
h.mempoolEmulateCache.accountsTracesCache.Set(account, memActualHexTraces)
}
events := make([]oas.AccountEvent, len(tracesID))
var lastLT uint64
for i, traceID := range traceIDs {
for i, traceID := range tracesID {
trace, err := h.storage.GetTrace(ctx, traceID)
if err != nil {
return &oas.InternalError{Error: err.Error()}, nil
Expand All @@ -116,6 +139,17 @@ func (h Handler) GetEventsByAccount(ctx context.Context, params oas.GetEventsByA
}
lastLT = trace.Lt
}
for _, trace := range memTraces {
result, err := bath.FindActions(ctx, trace, bath.ForAccount(account), bath.WithInformationSource(h.storage))
if err != nil {
return &oas.InternalError{Error: err.Error()}, nil
}
event, err := h.toAccountEvent(ctx, account, trace, result, params.AcceptLanguage, params.SubjectOnly.Value)
if err != nil {
return &oas.InternalError{Error: err.Error()}, nil
}
events = slices.Insert(events, 0, event)
}
if account.ToRaw() == testEventAccount {
events = slices.Insert(events, 0, getTestAccountEvent())
}
Expand Down Expand Up @@ -328,10 +362,10 @@ func (h Handler) addToMempool(ctx context.Context, bytesBoc []byte) {
}
}
traverse(trace)
h.mempoolEmulateCache.tracesCache.Set(trace.Hash, trace, cache.WithExpiration(time.Second*20))
h.mempoolEmulateCache.tracesCache.Set(trace.Hash.Hex(), 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)
traces = slices.Insert(traces, 0, trace.Hash.Hex())
h.mempoolEmulateCache.accountsTracesCache.Set(account, traces)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
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"),
tracesCache: cache.NewLRUCache[string, *core.Trace](10000, "mempool_traces_cache"),
accountsTracesCache: cache.NewLRUCache[tongo.AccountID, []string](10000, "accounts_traces_cache"),
},
tonConnect: tonConnect,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ type metadataCache struct {
}

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

0 comments on commit 0cd4ad6

Please sign in to comment.