-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4baa94d
commit b001238
Showing
12 changed files
with
720 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
|
||
"github.com/ava-labs/avalanchego/network/p2p/gossip" | ||
|
||
"github.com/ava-labs/coreth/core" | ||
"github.com/ava-labs/coreth/core/txpool" | ||
"github.com/ava-labs/coreth/core/types" | ||
"github.com/ava-labs/coreth/plugin/evm/message" | ||
) | ||
|
||
var ( | ||
_ gossip.Gossipable = (*GossipEthTx)(nil) | ||
_ gossip.Gossipable = (*GossipAtomicTx)(nil) | ||
_ gossip.Set[*GossipEthTx] = (*GossipEthTxPool)(nil) | ||
) | ||
|
||
type GossipAtomicTx struct { | ||
Tx *Tx `serialize:"true"` | ||
} | ||
|
||
func (tx *GossipAtomicTx) GetHash() gossip.Hash { | ||
id := tx.Tx.ID() | ||
hash := gossip.Hash{} | ||
copy(hash[:], id[:]) | ||
|
||
return hash | ||
} | ||
|
||
func (tx *GossipAtomicTx) Marshal() ([]byte, error) { | ||
return Codec.Marshal(message.Version, tx) | ||
} | ||
|
||
func (tx *GossipAtomicTx) Unmarshal(bytes []byte) error { | ||
_, err := Codec.Unmarshal(bytes, tx) | ||
return err | ||
} | ||
|
||
func NewGossipEthTxPool(mempool *txpool.TxPool) (*GossipEthTxPool, error) { | ||
bloom, err := gossip.NewBloomFilter(txGossipBloomMaxItems, txGossipBloomFalsePositiveRate) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize bloom filter: %w", err) | ||
} | ||
|
||
return &GossipEthTxPool{ | ||
mempool: mempool, | ||
pendingTxs: make(chan core.NewTxsEvent), | ||
bloom: bloom, | ||
}, nil | ||
} | ||
|
||
type GossipEthTxPool struct { | ||
mempool *txpool.TxPool | ||
pendingTxs chan core.NewTxsEvent | ||
|
||
bloom *gossip.BloomFilter | ||
lock sync.RWMutex | ||
} | ||
|
||
func (g *GossipEthTxPool) Subscribe(shutdownChan chan struct{}, shutdownWg *sync.WaitGroup) { | ||
defer shutdownWg.Done() | ||
g.mempool.SubscribeNewTxsEvent(g.pendingTxs) | ||
|
||
for { | ||
select { | ||
case <-shutdownChan: | ||
log.Debug("shutting down subscription") | ||
return | ||
case pendingTxs := <-g.pendingTxs: | ||
g.lock.Lock() | ||
for _, pendingTx := range pendingTxs.Txs { | ||
tx := &GossipEthTx{Tx: pendingTx} | ||
g.bloom.Add(tx) | ||
if gossip.ResetBloomFilterIfNeeded(g.bloom, txGossipBloomMaxFilledRatio) { | ||
log.Debug("resetting bloom filter", "reason", "reached max filled ratio") | ||
|
||
pending := g.mempool.Pending(false) | ||
for _, pendingTxs := range pending { | ||
for _, pendingTx := range pendingTxs { | ||
g.bloom.Add(&GossipEthTx{Tx: pendingTx}) | ||
} | ||
} | ||
} | ||
} | ||
g.lock.Unlock() | ||
} | ||
} | ||
} | ||
|
||
// Add enqueues the transaction to the mempool. Subscribe should be called | ||
// to receive an event if tx is actually added to the mempool or not. | ||
func (g *GossipEthTxPool) Add(tx *GossipEthTx) error { | ||
if err := g.mempool.AddRemotes([]*types.Transaction{tx.Tx})[0]; err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *GossipEthTxPool) Get(filter func(tx *GossipEthTx) bool) []*GossipEthTx { | ||
limit := 1000 | ||
resultSize := 0 | ||
result := make([]*GossipEthTx, 0) | ||
|
||
g.mempool.IteratePending(func(tx *types.Transaction) bool { | ||
resultSize += int(tx.Size()) | ||
if resultSize > limit { | ||
return false | ||
} | ||
|
||
gossipTx := &GossipEthTx{ | ||
Tx: tx, | ||
} | ||
result = append(result, gossipTx) | ||
return true | ||
}) | ||
|
||
return result | ||
} | ||
|
||
func (g *GossipEthTxPool) GetFilter() ([]byte, []byte, error) { | ||
g.lock.RLock() | ||
defer g.lock.RUnlock() | ||
|
||
bloom, err := g.bloom.Bloom.MarshalBinary() | ||
return bloom, g.bloom.Salt[:], err | ||
} | ||
|
||
type GossipEthTx struct { | ||
Tx *types.Transaction | ||
} | ||
|
||
func (tx *GossipEthTx) GetHash() gossip.Hash { | ||
txHash := tx.Tx.Hash() | ||
hash := gossip.Hash{} | ||
copy(hash[:], txHash[:]) | ||
|
||
return hash | ||
} | ||
|
||
func (tx *GossipEthTx) Marshal() ([]byte, error) { | ||
return tx.Tx.MarshalBinary() | ||
} | ||
|
||
func (tx *GossipEthTx) Unmarshal(bytes []byte) error { | ||
tx.Tx = &types.Transaction{} | ||
return tx.Tx.UnmarshalBinary(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
) | ||
|
||
func TestAtomicMempoolAddTx(t *testing.T) { | ||
txs := []*GossipAtomicTx{ | ||
{ | ||
Tx: &Tx{ | ||
UnsignedAtomicTx: &TestUnsignedTx{ | ||
IDV: ids.GenerateTestID(), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Tx: &Tx{ | ||
UnsignedAtomicTx: &TestUnsignedTx{ | ||
IDV: ids.GenerateTestID(), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
add []*GossipAtomicTx | ||
filter func(tx *GossipAtomicTx) bool | ||
expected []*GossipAtomicTx | ||
}{ | ||
{ | ||
name: "empty", | ||
}, | ||
{ | ||
name: "filter matches nothing", | ||
add: txs, | ||
filter: func(*GossipAtomicTx) bool { | ||
return false | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "filter matches all", | ||
add: txs, | ||
filter: func(*GossipAtomicTx) bool { | ||
return true | ||
}, | ||
expected: txs, | ||
}, | ||
{ | ||
name: "filter matches subset", | ||
add: txs, | ||
filter: func(tx *GossipAtomicTx) bool { | ||
return tx.Tx == txs[0].Tx | ||
}, | ||
expected: txs[:1], | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
m, err := NewMempool(ids.Empty, 10) | ||
require.NoError(err) | ||
|
||
for _, add := range tt.add { | ||
require.NoError(m.Add(add)) | ||
} | ||
|
||
txs := m.Get(tt.filter) | ||
require.Len(txs, len(tt.expected)) | ||
|
||
for _, expected := range tt.expected { | ||
require.Contains(txs, expected) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.