Skip to content

Commit

Permalink
Merge branch 'develop' into buyback
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 authored Jul 18, 2024
2 parents fd8dd45 + 28dbb48 commit 6424e94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

- [11453](https://github.com/vegaprotocol/vega/issues/11453) - Fix margin for fully collateralised markets.
- [11462](https://github.com/vegaprotocol/vega/issues/11462) - Update the time weighted notional when publishing live game scores.

- [11474](https://github.com/vegaprotocol/vega/issues/11474) - Fail `checkTx` downstream for delayed transactions so they don't get included in more than one block.

## 0.77.0

Expand Down
7 changes: 7 additions & 0 deletions core/processor/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type TxCache interface {
NewDelayedTransaction(ctx context.Context, delayed [][]byte, height uint64) []byte
IsDelayRequired(marketID string) bool
IsDelayRequiredAnyMarket() bool
IsTxInCache(tx []byte) bool
}

type App struct {
Expand Down Expand Up @@ -1584,6 +1585,12 @@ func (app *App) removeOldCheckpoints() error {
func (app *App) OnCheckTxSpam(tx abci.Tx) tmtypes.ResponseCheckTx {
resp := tmtypes.ResponseCheckTx{}

if app.txCache.IsTxInCache(tx.Hash()) {
resp.Code = blockchain.AbciSpamError
resp.Data = []byte("delayed transaction already included in a block")
return resp
}

// verify proof of work and replay
if !app.nilPow {
if err := app.pow.CheckTx(tx); err != nil {
Expand Down
36 changes: 36 additions & 0 deletions core/txcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package txcache

import (
"context"
"encoding/hex"
"fmt"
"sort"
"sync"

"code.vegaprotocol.io/vega/core/nodewallets"
"code.vegaprotocol.io/vega/core/txn"
Expand All @@ -27,13 +29,16 @@ import (
"code.vegaprotocol.io/vega/libs/proto"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"

"github.com/cometbft/cometbft/crypto/tmhash"
)

func NewTxCache(commander *nodewallets.Commander) *TxCache {
return &TxCache{
commander: commander,
marketToDelayRequired: map[string]bool{},
heightToTxs: map[uint64][][]byte{},
cachedTxs: map[string]struct{}{},
}
}

Expand All @@ -44,6 +49,34 @@ type TxCache struct {
numBlocksToDelay uint64
// no need to include is snapshot - is updated when markets are created/updated/loaded from snapshot
marketToDelayRequired map[string]bool
// map of transactions that have been picked up for delay
cachedTxs map[string]struct{}
lock sync.RWMutex
}

func (t *TxCache) IsTxInCache(txHash []byte) bool {
t.lock.RLock()
defer t.lock.RUnlock()
_, ok := t.cachedTxs[hex.EncodeToString(txHash)]
return ok
}

func (t *TxCache) removeHeightFromCache(height uint64) {
t.lock.Lock()
defer t.lock.Unlock()
if txs, ok := t.heightToTxs[height]; ok {
for _, tx := range txs {
delete(t.cachedTxs, hex.EncodeToString(tmhash.Sum(tx)))
}
}
}

func (t *TxCache) addHeightToCache(txs [][]byte) {
t.lock.Lock()
defer t.lock.Unlock()
for _, tx := range txs {
t.cachedTxs[hex.EncodeToString(tmhash.Sum(tx))] = struct{}{}
}
}

// MarketDelayRequiredUpdated is called when the market configuration is created/updated with support for
Expand Down Expand Up @@ -84,9 +117,11 @@ func (t *TxCache) NewDelayedTransaction(ctx context.Context, delayed [][]byte, c

func (t *TxCache) SetRawTxs(rtx [][]byte, height uint64) {
if rtx == nil {
t.removeHeightFromCache(height)
delete(t.heightToTxs, height)
} else {
t.heightToTxs[height] = rtx
t.addHeightToCache(rtx)
}
}

Expand Down Expand Up @@ -139,6 +174,7 @@ func (t *TxCache) LoadState(_ context.Context, p *types.Payload) ([]types.StateP
t.heightToTxs = map[uint64][][]byte{}
for _, tx := range data.TxCache.Txs {
t.heightToTxs[tx.Height] = tx.Tx
t.addHeightToCache(tx.Tx)
}
return nil, nil
default:
Expand Down

0 comments on commit 6424e94

Please sign in to comment.