Skip to content

Commit

Permalink
Move AddUnverifiedTx logic to network.IssueTx (#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Nov 15, 2023
1 parent dcc6ea8 commit e8ef4ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
21 changes: 1 addition & 20 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,7 @@ func New(

// AddUnverifiedTx verifies a transaction and attempts to add it to the mempool
func (b *builder) AddUnverifiedTx(tx *txs.Tx) error {
txID := tx.ID()
if b.Mempool.Has(txID) {
// If the transaction is already in the mempool - then it looks the same
// as if it was successfully added
return nil
}

if err := b.blkManager.VerifyTx(tx); err != nil {
b.MarkDropped(txID, err)
return err
}

// If we are partially syncing the Primary Network, we should not be
// maintaining the transaction mempool locally.
if !b.txExecutorBackend.Config.PartialSyncPrimaryNetwork {
if err := b.Mempool.Add(tx); err != nil {
return err
}
}
return b.GossipTx(context.TODO(), tx)
return b.Network.IssueTx(context.TODO(), tx)
}

// BuildBlock builds a block to be added to consensus.
Expand Down
30 changes: 26 additions & 4 deletions vms/platformvm/block/builder/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ var _ Network = (*network)(nil)
type Network interface {
common.AppHandler

// GossipTx gossips the transaction to some of the connected peers
GossipTx(ctx context.Context, tx *txs.Tx) error
// IssueTx verifies the transaction at the currently preferred state, adds
// it to the mempool, and gossips it to the network.
//
// Invariant: Assumes the context lock is held.
IssueTx(ctx context.Context, tx *txs.Tx) error
}

type network struct {
Expand Down Expand Up @@ -121,7 +124,27 @@ func (n *network) AppGossip(_ context.Context, nodeID ids.NodeID, msgBytes []byt
return nil
}

func (n *network) GossipTx(ctx context.Context, tx *txs.Tx) error {
func (n *network) IssueTx(ctx context.Context, tx *txs.Tx) error {
txID := tx.ID()
if n.blkBuilder.Mempool.Has(txID) {
// If the transaction is already in the mempool - then it looks the same
// as if it was successfully added
return nil
}

if err := n.blkBuilder.blkManager.VerifyTx(tx); err != nil {
n.blkBuilder.Mempool.MarkDropped(txID, err)
return err
}

// If we are partially syncing the Primary Network, we should not be
// maintaining the transaction mempool locally.
if !n.blkBuilder.txExecutorBackend.Config.PartialSyncPrimaryNetwork {
if err := n.blkBuilder.Mempool.Add(tx); err != nil {
return err
}
}

txBytes := tx.Bytes()
msg := &message.Tx{
Tx: txBytes,
Expand All @@ -131,7 +154,6 @@ func (n *network) GossipTx(ctx context.Context, tx *txs.Tx) error {
return err
}

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

0 comments on commit e8ef4ad

Please sign in to comment.