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

Add recentTxsLock to platform network struct #2294

Merged
merged 4 commits into from
Nov 14, 2023
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: 0 additions & 2 deletions vms/avm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ func (n *network) issueTx(tx *txs.Tx) error {
}

func (n *network) gossipTx(ctx context.Context, txID ids.ID, msgBytes []byte) {
// This lock is just to ensure there isn't racy behavior between checking if
// the tx was gossiped and marking the tx as gossiped.
n.recentTxsLock.Lock()
_, has := n.recentTxs.Get(txID)
n.recentTxs.Put(txID, struct{}{})
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (b *builder) AddUnverifiedTx(tx *txs.Tx) error {
return err
}
}
return b.GossipTx(tx)
return b.GossipTx(context.TODO(), tx)
dhrubabasu marked this conversation as resolved.
Show resolved Hide resolved
}

// BuildBlock builds a block to be added to consensus.
Expand Down
45 changes: 32 additions & 13 deletions vms/platformvm/block/builder/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package builder

import (
"context"
"fmt"
"sync"

"go.uber.org/zap"

Expand All @@ -29,7 +29,7 @@ type Network interface {
common.AppHandler

// GossipTx gossips the transaction to some of the connected peers
GossipTx(tx *txs.Tx) error
GossipTx(ctx context.Context, tx *txs.Tx) error
}

type network struct {
Expand All @@ -38,10 +38,11 @@ type network struct {

ctx *snow.Context
blkBuilder *builder
appSender common.AppSender

// gossip related attributes
appSender common.AppSender
recentTxs *cache.LRU[ids.ID, struct{}]
recentTxsLock sync.Mutex
recentTxs *cache.LRU[ids.ID, struct{}]
}

func NewNetwork(
Expand Down Expand Up @@ -120,22 +121,40 @@ func (n *network) AppGossip(_ context.Context, nodeID ids.NodeID, msgBytes []byt
return nil
}

func (n *network) GossipTx(tx *txs.Tx) error {
func (n *network) GossipTx(ctx context.Context, tx *txs.Tx) error {
txBytes := tx.Bytes()
msg := &message.Tx{
Tx: txBytes,
}
msgBytes, err := message.Build(msg)
if err != nil {
return err
}

txID := tx.ID()
n.gossipTx(ctx, txID, msgBytes)
return nil
}

func (n *network) gossipTx(ctx context.Context, txID ids.ID, msgBytes []byte) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just get rid of this function and in-line this code into GossipTx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a premature helper function. AddUnverifiedTx (called in AppGossip in this file) also calls GossipTx. The end state (from #2296) will use this helper in two places: https://github.com/ava-labs/avalanchego/blob/remove-add-unverified-tx/vms/platformvm/network/network.go

I'd like to keep it just to decrease the diff size later

n.recentTxsLock.Lock()
_, has := n.recentTxs.Get(txID)
n.recentTxs.Put(txID, struct{}{})
n.recentTxsLock.Unlock()

// Don't gossip a transaction if it has been recently gossiped.
if _, has := n.recentTxs.Get(txID); has {
return nil
if has {
return
}
n.recentTxs.Put(txID, struct{}{})

n.ctx.Log.Debug("gossiping tx",
zap.Stringer("txID", txID),
)

msg := &message.Tx{Tx: tx.Bytes()}
msgBytes, err := message.Build(msg)
if err != nil {
return fmt.Errorf("GossipTx: failed to build Tx message: %w", err)
if err := n.appSender.SendAppGossip(ctx, msgBytes); err != nil {
n.ctx.Log.Error("failed to gossip tx",
zap.Stringer("txID", txID),
zap.Error(err),
)
}
return n.appSender.SendAppGossip(context.TODO(), msgBytes)
}
Loading