Skip to content

Commit c3271c2

Browse files
committed
nit
1 parent b001238 commit c3271c2

File tree

8 files changed

+82
-85
lines changed

8 files changed

+82
-85
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/VictoriaMetrics/fastcache v1.10.0
7-
github.com/ava-labs/avalanchego v1.10.10-rc.0
7+
github.com/ava-labs/avalanchego v1.10.10-rc.1
88
github.com/cespare/cp v0.1.0
99
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
1010
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ github.com/ava-labs/avalanchego v1.10.9-rc.4 h1:vtavPfRiF6r1Zc6RV8/arEfVpe9GQsLW
6161
github.com/ava-labs/avalanchego v1.10.9-rc.4/go.mod h1:vTBLl1zK36olfLRA7IUfdbvphWqlkuarIoXxvZTHZVw=
6262
github.com/ava-labs/avalanchego v1.10.10-rc.0 h1:6VjkpwhAJ0tDNJK+UIUD8WIb5VelgH3w61mgk7JAkDQ=
6363
github.com/ava-labs/avalanchego v1.10.10-rc.0/go.mod h1:C8R5uiltpc8MQ62ixxgODR+15mesWF0aAw3H+Qrl9Iw=
64+
github.com/ava-labs/avalanchego v1.10.10-rc.1 h1:dPJISEWqL3tdUShe6RuB8CFuXl3rsH8617sXbLBjkIE=
65+
github.com/ava-labs/avalanchego v1.10.10-rc.1/go.mod h1:C8R5uiltpc8MQ62ixxgODR+15mesWF0aAw3H+Qrl9Iw=
6466
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
6567
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
6668
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

peer/network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ func TestSDKRouting(t *testing.T) {
665665
protocol := 0
666666
handler := &testSDKHandler{}
667667
router := p2p.NewRouter(logging.NoLog{}, sender)
668-
_, err := router.RegisterAppProtocol(uint64(protocol), handler)
668+
_, err := router.RegisterAppProtocol(uint64(protocol), handler, &p2p.Peers{})
669669
require.NoError(err)
670670

671671
networkCodec := codec.NewManager(0)

plugin/evm/gossip_mempool.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"sync"
99

10+
"github.com/ava-labs/avalanchego/ids"
1011
"github.com/ethereum/go-ethereum/log"
1112

1213
"github.com/ava-labs/avalanchego/network/p2p/gossip"
@@ -27,12 +28,8 @@ type GossipAtomicTx struct {
2728
Tx *Tx `serialize:"true"`
2829
}
2930

30-
func (tx *GossipAtomicTx) GetHash() gossip.Hash {
31-
id := tx.Tx.ID()
32-
hash := gossip.Hash{}
33-
copy(hash[:], id[:])
34-
35-
return hash
31+
func (tx *GossipAtomicTx) GetID() ids.ID {
32+
return tx.Tx.ID()
3633
}
3734

3835
func (tx *GossipAtomicTx) Marshal() ([]byte, error) {
@@ -79,7 +76,13 @@ func (g *GossipEthTxPool) Subscribe(shutdownChan chan struct{}, shutdownWg *sync
7976
for _, pendingTx := range pendingTxs.Txs {
8077
tx := &GossipEthTx{Tx: pendingTx}
8178
g.bloom.Add(tx)
82-
if gossip.ResetBloomFilterIfNeeded(g.bloom, txGossipBloomMaxFilledRatio) {
79+
reset, err := gossip.ResetBloomFilterIfNeeded(g.bloom, txGossipBloomMaxFilledRatio)
80+
if err != nil {
81+
log.Error("failed to reset bloom filter", "err", err)
82+
continue
83+
}
84+
85+
if reset {
8386
log.Debug("resetting bloom filter", "reason", "reached max filled ratio")
8487

8588
pending := g.mempool.Pending(false)
@@ -105,25 +108,10 @@ func (g *GossipEthTxPool) Add(tx *GossipEthTx) error {
105108
return nil
106109
}
107110

108-
func (g *GossipEthTxPool) Get(filter func(tx *GossipEthTx) bool) []*GossipEthTx {
109-
limit := 1000
110-
resultSize := 0
111-
result := make([]*GossipEthTx, 0)
112-
111+
func (g *GossipEthTxPool) Iterate(f func(tx *GossipEthTx) bool) {
113112
g.mempool.IteratePending(func(tx *types.Transaction) bool {
114-
resultSize += int(tx.Size())
115-
if resultSize > limit {
116-
return false
117-
}
118-
119-
gossipTx := &GossipEthTx{
120-
Tx: tx,
121-
}
122-
result = append(result, gossipTx)
123-
return true
113+
return f(&GossipEthTx{Tx: tx})
124114
})
125-
126-
return result
127115
}
128116

129117
func (g *GossipEthTxPool) GetFilter() ([]byte, []byte, error) {
@@ -138,12 +126,8 @@ type GossipEthTx struct {
138126
Tx *types.Transaction
139127
}
140128

141-
func (tx *GossipEthTx) GetHash() gossip.Hash {
142-
txHash := tx.Tx.Hash()
143-
hash := gossip.Hash{}
144-
copy(hash[:], txHash[:])
145-
146-
return hash
129+
func (tx *GossipEthTx) GetID() ids.ID {
130+
return ids.ID(tx.Tx.Hash())
147131
}
148132

149133
func (tx *GossipEthTx) Marshal() ([]byte, error) {

plugin/evm/gossip_mempool_test.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/ava-labs/avalanchego/ids"
1212
)
1313

14-
func TestAtomicMempoolAddTx(t *testing.T) {
14+
func TestAtomicMempoolIterate(t *testing.T) {
1515
txs := []*GossipAtomicTx{
1616
{
1717
Tx: &Tx{
@@ -30,57 +30,65 @@ func TestAtomicMempoolAddTx(t *testing.T) {
3030
}
3131

3232
tests := []struct {
33-
name string
34-
add []*GossipAtomicTx
35-
filter func(tx *GossipAtomicTx) bool
36-
expected []*GossipAtomicTx
33+
name string
34+
add []*GossipAtomicTx
35+
f func(tx *GossipAtomicTx) bool
36+
possibleValues []*GossipAtomicTx
37+
expectedLen int
3738
}{
3839
{
39-
name: "empty",
40-
},
41-
{
42-
name: "filter matches nothing",
40+
name: "func matches nothing",
4341
add: txs,
44-
filter: func(*GossipAtomicTx) bool {
42+
f: func(*GossipAtomicTx) bool {
4543
return false
4644
},
47-
expected: nil,
45+
possibleValues: nil,
4846
},
4947
{
50-
name: "filter matches all",
48+
name: "func matches all",
5149
add: txs,
52-
filter: func(*GossipAtomicTx) bool {
50+
f: func(*GossipAtomicTx) bool {
5351
return true
5452
},
55-
expected: txs,
53+
possibleValues: txs,
54+
expectedLen: 2,
5655
},
5756
{
58-
name: "filter matches subset",
57+
name: "func matches subset",
5958
add: txs,
60-
filter: func(tx *GossipAtomicTx) bool {
59+
f: func(tx *GossipAtomicTx) bool {
6160
return tx.Tx == txs[0].Tx
6261
},
63-
expected: txs[:1],
62+
possibleValues: txs,
63+
expectedLen: 1,
6464
},
6565
}
6666

6767
for _, tt := range tests {
6868
t.Run(tt.name, func(t *testing.T) {
6969
require := require.New(t)
70-
7170
m, err := NewMempool(ids.Empty, 10)
7271
require.NoError(err)
7372

7473
for _, add := range tt.add {
7574
require.NoError(m.Add(add))
7675
}
7776

78-
txs := m.Get(tt.filter)
79-
require.Len(txs, len(tt.expected))
77+
matches := make([]*GossipAtomicTx, 0)
78+
f := func(tx *GossipAtomicTx) bool {
79+
match := tt.f(tx)
8080

81-
for _, expected := range tt.expected {
82-
require.Contains(txs, expected)
81+
if match {
82+
matches = append(matches, tx)
83+
}
84+
85+
return match
8386
}
87+
88+
m.Iterate(f)
89+
90+
require.Len(matches, tt.expectedLen)
91+
require.Subset(tt.possibleValues, matches)
8492
})
8593
}
8694
}

plugin/evm/mempool.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,40 +274,41 @@ func (m *Mempool) addTx(tx *Tx, force bool) error {
274274
for utxoID := range utxoSet {
275275
m.utxoSpenders[utxoID] = tx
276276
}
277-
// When adding [tx] to the mempool make sure that there is an item in Pending
278-
// to signal the VM to produce a block. Note: if the VM's buildStatus has already
279-
// been set to something other than [dontBuild], this will be ignored and won't be
280-
// reset until the engine calls BuildBlock. This case is handled in IssueCurrentTx
281-
// and CancelCurrentTx.
282-
m.newTxs = append(m.newTxs, tx)
283-
m.addPending()
284277

285278
m.bloom.Add(&GossipAtomicTx{Tx: tx})
286-
if gossip.ResetBloomFilterIfNeeded(m.bloom, txGossipBloomMaxFilledRatio) {
279+
reset, err := gossip.ResetBloomFilterIfNeeded(m.bloom, txGossipBloomMaxFilledRatio)
280+
if err != nil {
281+
return err
282+
}
283+
284+
if reset {
287285
log.Debug("resetting bloom filter", "reason", "reached max filled ratio")
288286

289287
for _, pendingTx := range m.txHeap.minHeap.items {
290288
m.bloom.Add(&GossipAtomicTx{Tx: pendingTx.tx})
291289
}
292290
}
293291

292+
// When adding [tx] to the mempool make sure that there is an item in Pending
293+
// to signal the VM to produce a block. Note: if the VM's buildStatus has already
294+
// been set to something other than [dontBuild], this will be ignored and won't be
295+
// reset until the engine calls BuildBlock. This case is handled in IssueCurrentTx
296+
// and CancelCurrentTx.
297+
m.newTxs = append(m.newTxs, tx)
298+
m.addPending()
299+
294300
return nil
295301
}
296302

297-
func (m *Mempool) Get(filter func(tx *GossipAtomicTx) bool) []*GossipAtomicTx {
303+
func (m *Mempool) Iterate(f func(tx *GossipAtomicTx) bool) {
298304
m.lock.RLock()
299305
defer m.lock.RUnlock()
300306

301-
gossipTxs := make([]*GossipAtomicTx, 0, len(m.txHeap.maxHeap.items))
302307
for _, item := range m.txHeap.maxHeap.items {
303-
gossipTx := &GossipAtomicTx{Tx: item.tx}
304-
if !filter(gossipTx) {
305-
continue
308+
if !f(&GossipAtomicTx{Tx: item.tx}) {
309+
return
306310
}
307-
gossipTxs = append(gossipTxs, gossipTx)
308311
}
309-
310-
return gossipTxs
311312
}
312313

313314
func (m *Mempool) GetFilter() ([]byte, []byte, error) {

plugin/evm/tx_gossip_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/ava-labs/avalanchego/p2p/gossip"
14-
"github.com/ava-labs/avalanchego/p2p/gossip/proto/pb"
15-
"github.com/stretchr/testify/require"
16-
"go.uber.org/mock/gomock"
17-
"google.golang.org/protobuf/proto"
18-
1913
"github.com/ava-labs/avalanchego/ids"
2014
"github.com/ava-labs/avalanchego/network/p2p"
21-
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
15+
"github.com/ava-labs/avalanchego/network/p2p/gossip"
16+
"github.com/ava-labs/avalanchego/proto/pb/sdk"
17+
"github.com/ava-labs/avalanchego/snow/engine/common"
2218
"github.com/ava-labs/avalanchego/snow/validators"
2319
"github.com/ava-labs/avalanchego/utils"
2420
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
2521
"github.com/ava-labs/avalanchego/utils/logging"
2622
"github.com/ava-labs/avalanchego/utils/set"
23+
"github.com/stretchr/testify/require"
24+
25+
"go.uber.org/mock/gomock"
26+
27+
"google.golang.org/protobuf/proto"
2728

2829
"github.com/ava-labs/coreth/core"
2930
"github.com/ava-labs/coreth/core/types"
@@ -60,7 +61,7 @@ func TestEthTxGossip(t *testing.T) {
6061

6162
// sender for the peer requesting gossip from [vm]
6263
ctrl := gomock.NewController(t)
63-
peerSender := commonEng.NewMockSender(ctrl)
64+
peerSender := common.NewMockSender(ctrl)
6465
router := p2p.NewRouter(logging.NoLog{}, peerSender)
6566

6667
// we're only making client requests, so we don't need a server handler
@@ -71,7 +72,7 @@ func TestEthTxGossip(t *testing.T) {
7172
require.NoError(err)
7273
emptyBloomFilterBytes, err := emptyBloomFilter.Bloom.MarshalBinary()
7374
require.NoError(err)
74-
request := &pb.PullGossipRequest{
75+
request := &sdk.PullGossipRequest{
7576
Filter: emptyBloomFilterBytes,
7677
Salt: utils.RandomBytes(32),
7778
}
@@ -110,7 +111,7 @@ func TestEthTxGossip(t *testing.T) {
110111
onResponse := func(nodeID ids.NodeID, responseBytes []byte, err error) {
111112
require.NoError(err)
112113

113-
response := &pb.PullGossipResponse{}
114+
response := &sdk.PullGossipResponse{}
114115
require.NoError(proto.Unmarshal(responseBytes, response))
115116
require.Empty(response.Gossip)
116117
wg.Done()
@@ -137,7 +138,7 @@ func TestEthTxGossip(t *testing.T) {
137138
onResponse = func(nodeID ids.NodeID, responseBytes []byte, err error) {
138139
require.NoError(err)
139140

140-
response := &pb.PullGossipResponse{}
141+
response := &sdk.PullGossipResponse{}
141142
require.NoError(proto.Unmarshal(responseBytes, response))
142143
require.Len(response.Gossip, 1)
143144

@@ -166,7 +167,7 @@ func TestAtomicTxGossip(t *testing.T) {
166167

167168
// sender for the peer requesting gossip from [vm]
168169
ctrl := gomock.NewController(t)
169-
peerSender := commonEng.NewMockSender(ctrl)
170+
peerSender := common.NewMockSender(ctrl)
170171
router := p2p.NewRouter(logging.NoLog{}, peerSender)
171172

172173
// we're only making client requests, so we don't need a server handler
@@ -177,7 +178,7 @@ func TestAtomicTxGossip(t *testing.T) {
177178
require.NoError(err)
178179
bloomBytes, err := emptyBloomFilter.Bloom.MarshalBinary()
179180
require.NoError(err)
180-
request := &pb.PullGossipRequest{
181+
request := &sdk.PullGossipRequest{
181182
Filter: bloomBytes,
182183
Salt: emptyBloomFilter.Salt[:],
183184
}
@@ -214,7 +215,7 @@ func TestAtomicTxGossip(t *testing.T) {
214215
onResponse := func(nodeID ids.NodeID, responseBytes []byte, err error) {
215216
require.NoError(err)
216217

217-
response := &pb.PullGossipResponse{}
218+
response := &sdk.PullGossipResponse{}
218219
require.NoError(proto.Unmarshal(responseBytes, response))
219220
require.Empty(response.Gossip)
220221
wg.Done()
@@ -237,7 +238,7 @@ func TestAtomicTxGossip(t *testing.T) {
237238
onResponse = func(nodeID ids.NodeID, responseBytes []byte, err error) {
238239
require.NoError(err)
239240

240-
response := &pb.PullGossipResponse{}
241+
response := &sdk.PullGossipResponse{}
241242
require.NoError(proto.Unmarshal(responseBytes, response))
242243
require.Len(response.Gossip, 1)
243244

plugin/evm/vm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
avalanchegoMetrics "github.com/ava-labs/avalanchego/api/metrics"
2020
"github.com/ava-labs/avalanchego/network/p2p"
21+
"github.com/ava-labs/avalanchego/network/p2p/gossip"
2122

2223
"github.com/ava-labs/coreth/consensus/dummy"
2324
corethConstants "github.com/ava-labs/coreth/constants"

0 commit comments

Comments
 (0)