Skip to content
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

--txpool.gossip.disable #8800

Merged
merged 13 commits into from
Nov 23, 2023
9 changes: 5 additions & 4 deletions cmd/caplin-regression/regression/reader.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/caplin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
3 changes: 2 additions & 1 deletion cmd/rpctest/rpctest/bench_tracecallmany.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 4 additions & 0 deletions cmd/txpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var (
priceBump uint64
blobPriceBump uint64

noTxGossip bool

commitEvery time.Duration
)

Expand All @@ -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)
}

Expand Down Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ var (
Usage: "sets the port to listen for beacon api requests",
Value: 5555,
}

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,
}
)

var MetricFlags = []cli.Flag{&MetricsEnabledFlag, &MetricsHTTPFlag, &MetricsPortFlag}
Expand Down Expand Up @@ -1725,6 +1731,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
Expand Down
3 changes: 2 additions & 1 deletion erigon-lib/txpool/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 20 additions & 0 deletions erigon-lib/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions erigon-lib/txpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions erigon-lib/txpool/txpoolcfg/txpoolcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ type Config struct {
SilkwormExecution bool
SilkwormRpcDaemon bool
SilkwormSentry bool

DisableTxPoolGossip bool
}

type Sync struct {
Expand Down
3 changes: 2 additions & 1 deletion eth/ethconfig/estimate/esitmated_ram.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ var DefaultFlags = []cli.Flag{
&utils.BeaconApiIdleTimeoutFlag,

&utils.TrustedSetupFile,

&utils.TxPoolGossipDisableFlag,
}
3 changes: 2 additions & 1 deletion turbo/jsonrpc/eth_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion turbo/jsonrpc/eth_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion turbo/jsonrpc/eth_callMany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion turbo/jsonrpc/eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading