--txpool.gossip.disable (#8800)

Co-authored-by: cby3149 <cby3149@gmail.com>
This commit is contained in:
Alex Sharov 2023-11-23 12:00:41 +07:00 committed by GitHub
parent e390b0e182
commit 55e05c440f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 167 additions and 13 deletions

View File

@ -1,15 +1,16 @@
package regression
import (
clparams2 "github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/utils"
"io/fs"
"os"
"path"
"path/filepath"
"sort"
clparams2 "github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/utils"
)
func (r *RegressionTester) readStartingState() (*state.CachingBeaconState, error) {

View File

@ -34,7 +34,7 @@ import (
"github.com/ledgerwatch/erigon/cmd/caplin/caplin1"
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel/cli"
"github.com/ledgerwatch/erigon/cmd/sentinel/cli/flags"
app "github.com/ledgerwatch/erigon/turbo/app"
"github.com/ledgerwatch/erigon/turbo/app"
"github.com/ledgerwatch/erigon/turbo/debug"
)

View File

@ -3,11 +3,12 @@ package rpctest
import (
"bufio"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"net/http"
"os"
"time"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
)

View File

@ -55,6 +55,8 @@ var (
priceBump uint64
blobPriceBump uint64
noTxGossip bool
commitEvery time.Duration
)
@ -80,6 +82,7 @@ func init() {
rootCmd.PersistentFlags().Uint64Var(&priceBump, "txpool.pricebump", txpoolcfg.DefaultConfig.PriceBump, "Price bump percentage to replace an already existing transaction")
rootCmd.PersistentFlags().Uint64Var(&blobPriceBump, "txpool.blobpricebump", txpoolcfg.DefaultConfig.BlobPriceBump, "Price bump percentage to replace an existing blob (type-3) transaction")
rootCmd.PersistentFlags().DurationVar(&commitEvery, utils.TxPoolCommitEveryFlag.Name, utils.TxPoolCommitEveryFlag.Value, utils.TxPoolCommitEveryFlag.Usage)
rootCmd.PersistentFlags().BoolVar(&noTxGossip, utils.TxPoolGossipDisableFlag.Name, utils.TxPoolGossipDisableFlag.Value, utils.TxPoolGossipDisableFlag.Usage)
rootCmd.Flags().StringSliceVar(&traceSenders, utils.TxPoolTraceSendersFlag.Name, []string{}, utils.TxPoolTraceSendersFlag.Usage)
}
@ -146,6 +149,7 @@ func doTxpool(ctx context.Context, logger log.Logger) error {
cfg.BlobSlots = blobSlots
cfg.PriceBump = priceBump
cfg.BlobPriceBump = blobPriceBump
cfg.NoGossip = noTxGossip
cacheConfig := kvcache.DefaultCoherentConfig
cacheConfig.MetricsLabel = "txpool"

View File

@ -148,6 +148,11 @@ var (
Name: "txpool.disable",
Usage: "Experimental external pool and block producer, see ./cmd/txpool/readme.md for more info. Disabling internal txpool and block producer.",
}
TxPoolGossipDisableFlag = cli.BoolFlag{
Name: "txpool.gossip.disable",
Usage: "Disabling p2p gossip of txs. Any txs received by p2p - will be dropped.K Some networks like 'Optimism execution engine'/'Optimistic Rollup' - using it to protect against MEV attacks",
Value: txpoolcfg.DefaultConfig.NoGossip,
}
TxPoolLocalsFlag = cli.StringFlag{
Name: "txpool.locals",
Usage: "Comma separated accounts to treat as locals (no flush, priority inclusion)",
@ -1741,6 +1746,10 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C
if ctx.IsSet(TrustedSetupFile.Name) {
libkzg.SetTrustedSetupFilePath(ctx.String(TrustedSetupFile.Name))
}
if ctx.IsSet(TxPoolGossipDisableFlag.Name) {
cfg.DisableTxPoolGossip = ctx.Bool(TxPoolGossipDisableFlag.Name)
}
}
// SetDNSDiscoveryDefaults configures DNS discovery with the given URL if

View File

@ -20,10 +20,11 @@ import (
"context"
"errors"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"sync"
"time"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/direct"

View File

@ -711,6 +711,13 @@ func (p *TxPool) CountContent() (int, int, int) {
return p.pending.Len(), p.baseFee.Len(), p.queued.Len()
}
func (p *TxPool) AddRemoteTxs(_ context.Context, newTxs types.TxSlots) {
if p.cfg.NoGossip {
// if no gossip, then
// disable adding remote transactions
// consume remote tx from fetch
return
}
defer addRemoteTxsTimer.UpdateDuration(time.Now())
p.lock.Lock()
defer p.lock.Unlock()
@ -1701,6 +1708,14 @@ func MainLoop(ctx context.Context, db kv.RwDB, coreDB kv.RoDB, p *TxPool, newTxs
notifyMiningAboutNewSlots()
if p.cfg.NoGossip {
// drain newTxs for emptying newTx channel
// newTx channel will be filled only with local transactions
// early return to avoid outbound transaction propagation
log.Debug("[txpool] tx gossip disabled", "state", "drain new transactions")
return
}
var localTxTypes []byte
var localTxSizes []uint32
var localTxHashes types.Hashes
@ -1778,6 +1793,11 @@ func MainLoop(ctx context.Context, db kv.RwDB, coreDB kv.RoDB, p *TxPool, newTxs
if len(newPeers) == 0 {
continue
}
if p.cfg.NoGossip {
// avoid transaction gossiping for new peers
log.Debug("[txpool] tx gossip disabled", "state", "sync new peers")
continue
}
t := time.Now()
var hashes types.Hashes
var types []byte

View File

@ -940,3 +940,106 @@ func makeBlobTx() types.TxSlot {
blobTx.BlobFeeCap = *blobFeeCap
return blobTx
}
func TestDropRemoteAtNoGossip(t *testing.T) {
assert, require := assert.New(t), require.New(t)
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := txpoolcfg.DefaultConfig
cfg.NoGossip = true
logger := log.New()
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
txPool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, big.NewInt(0), big.NewInt(0), nil, fixedgas.DefaultMaxBlobsPerBlock, logger)
assert.NoError(err)
require.True(txPool != nil)
ctx := context.Background()
var stateVersionID uint64 = 0
pendingBaseFee := uint64(1_000_000)
// start blocks from 0, set empty hash - then kvcache will also work on this
h1 := gointerfaces.ConvertHashToH256([32]byte{})
change := &remote.StateChangeBatch{
StateVersionId: stateVersionID,
PendingBlockBaseFee: pendingBaseFee,
BlockGasLimit: 1000000,
ChangeBatch: []*remote.StateChange{
{BlockHeight: 0, BlockHash: h1},
},
}
var addr [20]byte
addr[0] = 1
v := make([]byte, types.EncodeSenderLengthForStorage(2, *uint256.NewInt(1 * common.Ether)))
types.EncodeSender(2, *uint256.NewInt(1 * common.Ether), v)
change.ChangeBatch[0].Changes = append(change.ChangeBatch[0].Changes, &remote.AccountChange{
Action: remote.Action_UPSERT,
Address: gointerfaces.ConvertAddressToH160(addr),
Data: v,
})
tx, err := db.BeginRw(ctx)
require.NoError(err)
defer tx.Rollback()
err = txPool.OnNewBlock(ctx, change, types.TxSlots{}, types.TxSlots{}, tx)
assert.NoError(err)
// 1. Try Local Tx
{
var txSlots types.TxSlots
txSlot := &types.TxSlot{
Tip: *uint256.NewInt(500_000),
FeeCap: *uint256.NewInt(3_000_000),
Gas: 100000,
Nonce: 3,
}
txSlot.IDHash[0] = 1
txSlots.Append(txSlot, addr[:], true)
reasons, err := txPool.AddLocalTxs(ctx, txSlots, tx)
assert.NoError(err)
for _, reason := range reasons {
assert.Equal(txpoolcfg.Success, reason, reason.String())
}
}
select {
case annoucements := <-ch:
for i := 0; i < annoucements.Len(); i++ {
_, _, hash := annoucements.At(i)
fmt.Printf("propagated hash %x\n", hash)
}
default:
}
// 2. Try same Tx, but as remote; tx must be dropped
{
var txSlots types.TxSlots
txSlot := &types.TxSlot{
Tip: *uint256.NewInt(500_000),
FeeCap: *uint256.NewInt(3_000_000),
Gas: 100000,
Nonce: 3,
}
txSlot.IDHash[0] = 1
txSlots.Append(txSlot, addr[:], true)
txPool.AddRemoteTxs(ctx, txSlots)
}
// empty because AddRemoteTxs logic is intentionally empty
assert.Equal(0, len(txPool.unprocessedRemoteByHash))
assert.Equal(0, len(txPool.unprocessedRemoteTxs.Txs))
assert.NoError(txPool.processRemoteTxs(ctx))
checkAnnouncementEmpty := func() bool {
select {
case <-ch:
return false
default:
return true
}
}
// no announcement because unprocessedRemoteTxs is already empty
assert.True(checkAnnouncementEmpty())
}

View File

@ -51,6 +51,8 @@ type Config struct {
MdbxPageSize datasize.ByteSize
MdbxDBSizeLimit datasize.ByteSize
MdbxGrowthStep datasize.ByteSize
NoGossip bool // this mode doesn't broadcast any txs, and if receive remote-txn - skip it
}
var DefaultConfig = Config{
@ -68,6 +70,8 @@ var DefaultConfig = Config{
BlobSlots: 48, // Default for a total of 8 txs for 6 blobs each - for hive tests
PriceBump: 10, // Price bump percentage to replace an already existing transaction
BlobPriceBump: 100,
NoGossip: false,
}
type DiscardReason uint8

View File

@ -589,6 +589,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
return nil, err
}
config.TxPool.NoGossip = config.DisableTxPoolGossip
var miningRPC txpool_proto.MiningServer
stateDiffClient := direct.NewStateDiffClientDirect(kvRPC)
if config.DeprecatedTxPool.Disable {

View File

@ -260,6 +260,8 @@ type Config struct {
SilkwormExecution bool
SilkwormRpcDaemon bool
SilkwormSentry bool
DisableTxPoolGossip bool
}
type Sync struct {

View File

@ -1,10 +1,11 @@
package estimate
import (
"runtime"
"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/mmap"
"runtime"
)
type estimatedRamPerWorker datasize.ByteSize

View File

@ -179,4 +179,6 @@ var DefaultFlags = []cli.Flag{
&utils.BeaconApiIdleTimeoutFlag,
&utils.TrustedSetupFile,
&utils.TxPoolGossipDisableFlag,
}

View File

@ -3,9 +3,10 @@ package jsonrpc
import (
"context"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"testing"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/stretchr/testify/assert"

View File

@ -2,10 +2,11 @@ package jsonrpc
import (
"context"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math/big"
"testing"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"

View File

@ -4,9 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math/big"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/kv/membatchwithdb"
"github.com/ledgerwatch/log/v3"

View File

@ -4,11 +4,12 @@ import (
"context"
"encoding/hex"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math/big"
"strconv"
"testing"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"

View File

@ -3,11 +3,12 @@ package jsonrpc
import (
"context"
"fmt"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math/big"
"testing"
"time"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/holiman/uint256"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"