-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joshua Kim <[email protected]>
- Loading branch information
1 parent
a277f31
commit 82b8655
Showing
9 changed files
with
527 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/network/p2p" | ||
"github.com/ava-labs/avalanchego/network/p2p/gossip" | ||
blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/txs" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" | ||
) | ||
|
||
var ( | ||
_ p2p.Handler = (*txGossipHandler)(nil) | ||
_ gossip.Set[*txs.Tx] = (*verifierMempool)(nil) | ||
) | ||
|
||
// txGossipHandler is the handler called when serving gossip messages | ||
type txGossipHandler struct { | ||
p2p.NoOpHandler | ||
appGossipHandler p2p.Handler | ||
appRequestHandler p2p.Handler | ||
} | ||
|
||
func (t txGossipHandler) AppGossip( | ||
ctx context.Context, | ||
nodeID ids.NodeID, | ||
gossipBytes []byte, | ||
) { | ||
t.appGossipHandler.AppGossip(ctx, nodeID, gossipBytes) | ||
} | ||
|
||
func (t txGossipHandler) AppRequest( | ||
ctx context.Context, | ||
nodeID ids.NodeID, | ||
deadline time.Time, | ||
requestBytes []byte, | ||
) ([]byte, error) { | ||
return t.appRequestHandler.AppRequest(ctx, nodeID, deadline, requestBytes) | ||
} | ||
|
||
func newVerifierMempool( | ||
mempool mempool.Mempool, | ||
verifier blockexecutor.Manager, | ||
bloomMaxItems uint64, | ||
bloomFalsePositiveRate float64, | ||
bloomMaxFalsePositiveRate float64, | ||
) (*verifierMempool, error) { | ||
bloomFilter, err := gossip.NewBloomFilter( | ||
bloomMaxItems, | ||
bloomFalsePositiveRate, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &verifierMempool{ | ||
mempool: mempool, | ||
verifier: verifier, | ||
bloomFilter: bloomFilter, | ||
bloomMaxFalsePositiveRate: bloomMaxFalsePositiveRate, | ||
}, nil | ||
} | ||
|
||
type Verifier interface { | ||
Verify(tx *txs.Tx) error | ||
} | ||
|
||
// verifierMempool performs verification before adding something to the mempool | ||
type verifierMempool struct { | ||
mempool mempool.Mempool | ||
verifier blockexecutor.Manager | ||
bloomFilter *gossip.BloomFilter | ||
bloomMaxFalsePositiveRate float64 | ||
} | ||
|
||
func (v *verifierMempool) Add(tx *txs.Tx) error { | ||
if err := v.verifier.VerifyTx(tx); err != nil { | ||
return fmt.Errorf("tx %s failed verification: %w", tx.ID(), err) | ||
} | ||
|
||
if err := v.mempool.Add(tx); err != nil { | ||
return fmt.Errorf("could not add tx %s to mempool: %w", tx.ID(), err) | ||
} | ||
|
||
v.bloomFilter.Add(tx) | ||
|
||
ok, err := gossip.ResetBloomFilterIfNeeded(v.bloomFilter, v.bloomMaxFalsePositiveRate) | ||
if ok { | ||
v.Iterate(func(tx *txs.Tx) bool { | ||
v.bloomFilter.Add(tx) | ||
return true | ||
}) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (v *verifierMempool) Iterate(f func(tx *txs.Tx) bool) { | ||
v.mempool.Iterate(f) | ||
} | ||
|
||
func (v *verifierMempool) GetFilter() (bloom []byte, salt []byte, err error) { | ||
bloomBytes, err := v.bloomFilter.Bloom.MarshalBinary() | ||
return bloomBytes, v.bloomFilter.Salt[:], err | ||
} |
Oops, something went wrong.