Skip to content

Commit d95e4bd

Browse files
committed
skip duplicated message
1 parent c09631e commit d95e4bd

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pkg/api/event_handlers.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"encoding/base64"
67
"encoding/hex"
78
"errors"
@@ -59,6 +60,10 @@ func (h *Handler) SendBlockchainMessage(ctx context.Context, request *oas.SendBl
5960
if err != nil {
6061
return toError(http.StatusBadRequest, fmt.Errorf("boc must be a base64 encoded string"))
6162
}
63+
checksum := sha256.Sum256(payload)
64+
if _, prs := h.blacklistedBocCache.Get(checksum); prs {
65+
return toError(http.StatusBadRequest, fmt.Errorf("duplicate message"))
66+
}
6267
msgCopy := blockchain.ExtInMsgCopy{
6368
MsgBoc: request.Boc.Value,
6469
Payload: payload,
@@ -67,11 +72,13 @@ func (h *Handler) SendBlockchainMessage(ctx context.Context, request *oas.SendBl
6772
mempoolMessageCounter.Inc()
6873
if err := h.msgSender.SendMessage(ctx, msgCopy); err != nil {
6974
if strings.Contains(err.Error(), "cannot apply external message to current state") {
75+
h.blacklistedBocCache.Set(checksum, struct{}{}, cache.WithExpiration(time.Minute))
7076
return toError(http.StatusNotAcceptable, err)
7177
}
7278
sentry.Send("sending message", sentry.SentryInfoData{"payload": request.Boc}, sentry.LevelError)
7379
return toError(http.StatusInternalServerError, err)
7480
}
81+
h.blacklistedBocCache.Set(checksum, struct{}{}, cache.WithExpiration(time.Minute))
7582
go func() {
7683
defer func() {
7784
if err := recover(); err != nil {

pkg/api/handler.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type Handler struct {
5353
// mempoolEmulateIgnoreAccounts, we don't track pending transactions for this list of accounts.
5454
mempoolEmulateIgnoreAccounts map[tongo.AccountID]struct{}
5555

56+
// need to blacklist BoCs for avoiding spamming
57+
blacklistedBocCache cache.Cache[[32]byte, struct{}]
58+
5659
// mu protects "dns".
5760
mu sync.Mutex
5861
dns *dns.DNS // todo: update when blockchain config changes
@@ -216,7 +219,8 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
216219
mempoolEmulateIgnoreAccounts: map[tongo.AccountID]struct{}{
217220
tongo.MustParseAddress("0:0000000000000000000000000000000000000000000000000000000000000000").ID: {},
218221
},
219-
tonConnect: tonConnect,
222+
blacklistedBocCache: cache.NewLRUCache[[32]byte, struct{}](100000, "blacklisted_boc_cache"),
223+
tonConnect: tonConnect,
220224
}, nil
221225
}
222226

0 commit comments

Comments
 (0)