From e8ef4ad2a20e1f5e817068bc2101090f49a2124d Mon Sep 17 00:00:00 2001 From: Dhruba Basu <7675102+dhrubabasu@users.noreply.github.com> Date: Wed, 15 Nov 2023 14:15:17 -0800 Subject: [PATCH] Move `AddUnverifiedTx` logic to `network.IssueTx` (#2310) --- vms/platformvm/block/builder/builder.go | 21 +---------------- vms/platformvm/block/builder/network.go | 30 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/vms/platformvm/block/builder/builder.go b/vms/platformvm/block/builder/builder.go index 4d8ad96d132a..63ae6dcb6fea 100644 --- a/vms/platformvm/block/builder/builder.go +++ b/vms/platformvm/block/builder/builder.go @@ -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. diff --git a/vms/platformvm/block/builder/network.go b/vms/platformvm/block/builder/network.go index d263561bfe74..30896e146ff8 100644 --- a/vms/platformvm/block/builder/network.go +++ b/vms/platformvm/block/builder/network.go @@ -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 { @@ -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, @@ -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 }