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

Fix bloom reset #1049

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin/evm/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (g *GossipEthTxPool) Subscribe(ctx context.Context) {
log.Debug("resetting bloom filter", "reason", "reached max filled ratio")

g.mempool.IteratePending(func(tx *types.Transaction) bool {
g.bloom.Add(&GossipEthTx{Tx: pendingTx})
g.bloom.Add(&GossipEthTx{Tx: tx})
return true
})
}
Expand Down
68 changes: 68 additions & 0 deletions plugin/evm/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
package evm

import (
"context"
"math/big"
"testing"
"time"

"github.com/ava-labs/avalanchego/network/p2p/gossip"
"github.com/ava-labs/subnet-evm/consensus/dummy"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/txpool"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,3 +36,59 @@ func TestGossipEthTxMarshaller(t *testing.T) {
require.NoError(err)
require.Equal(want.GossipID(), got.GossipID())
}

func TestGossipSubscribe(t *testing.T) {
require := require.New(t)
key, err := crypto.GenerateKey()
require.NoError(err)
addr := crypto.PubkeyToAddress(key.PublicKey)

require.NoError(err)
txPool := setupPoolWithConfig(t, params.TestChainConfig, addr)
defer txPool.Stop()
txPool.SetGasPrice(common.Big1)
txPool.SetMinFee(common.Big0)

gossipTxPool, err := NewGossipEthTxPool(txPool)
require.NoError(err)

// use a custom bloom filter to test the bloom filter reset
gossipTxPool.bloom, err = gossip.NewBloomFilter(1, 0.01, 0.0000000000000001) // maxCount =1
require.NoError(err)
ctx, cancel := context.WithCancel(context.TODO())
go func() {
gossipTxPool.Subscribe(ctx)
}()

// create eth txs
ethTxs := getValidEthTxs(key, 10, big.NewInt(226*params.GWei))

// Notify mempool about txs
errs := txPool.AddRemotesSync(ethTxs)
for _, err := range errs {
require.NoError(err, "failed adding subnet-evm tx to remote mempool")
}
time.Sleep(1 * time.Second)
cancel()
for i, tx := range ethTxs {
gossipable := &GossipEthTx{Tx: tx}
require.Truef(gossipTxPool.bloom.Has(gossipable), "expected tx to be in bloom filter: index %d", i)
}
}

func setupPoolWithConfig(t *testing.T, config *params.ChainConfig, fundedAddress common.Address) *txpool.TxPool {
diskdb := rawdb.NewMemoryDatabase()
engine := dummy.NewETHFaker()

var gspec = &core.Genesis{
Config: config,
Alloc: core.GenesisAlloc{fundedAddress: core.GenesisAccount{Balance: big.NewInt(1000000000000000000)}},
}
chain, err := core.NewBlockChain(diskdb, core.DefaultCacheConfig, gspec, engine, vm.Config{}, common.Hash{}, false)
require.NoError(t, err)
testTxPoolConfig := txpool.DefaultConfig
testTxPoolConfig.Journal = ""
pool := txpool.NewTxPool(testTxPoolConfig, config, chain)

return pool
}
4 changes: 2 additions & 2 deletions plugin/evm/gossipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func getValidEthTxs(key *ecdsa.PrivateKey, count int, gasPrice *big.Int) []*type
res := make([]*types.Transaction, count)

to := common.Address{}
amount := big.NewInt(10000)
gasLimit := uint64(100000)
amount := big.NewInt(0)
gasLimit := uint64(37000)

for i := 0; i < count; i++ {
tx, _ := types.SignTx(
Expand Down
Loading