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

Move context lock into network.issueTx #2525

Merged
merged 12 commits into from
Dec 22, 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
8 changes: 8 additions & 0 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func TestBuildBlockBasic(t *testing.T) {
txID := tx.ID()

// Issue the transaction
env.ctx.Lock.Unlock()
require.NoError(env.network.IssueTx(context.Background(), tx))
env.ctx.Lock.Lock()
_, ok := env.mempool.Get(txID)
require.True(ok)

Expand Down Expand Up @@ -125,7 +127,9 @@ func TestBuildBlockShouldReward(t *testing.T) {
txID := tx.ID()

// Issue the transaction
env.ctx.Lock.Unlock()
require.NoError(env.network.IssueTx(context.Background(), tx))
env.ctx.Lock.Lock()
_, ok := env.mempool.Get(txID)
require.True(ok)

Expand Down Expand Up @@ -249,7 +253,9 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {
txID := tx.ID()

// Issue the transaction
env.ctx.Lock.Unlock()
require.NoError(env.network.IssueTx(context.Background(), tx))
env.ctx.Lock.Lock()
_, ok := env.mempool.Get(txID)
require.True(ok)

Expand Down Expand Up @@ -471,6 +477,7 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
env.ctx.Lock.Lock()
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()
Expand Down Expand Up @@ -500,6 +507,7 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
require.ErrorIs(reason, errTestingDropped)

// Issue the transaction
env.ctx.Lock.Unlock()
require.NoError(env.network.IssueTx(context.Background(), tx))
_, ok := env.mempool.Get(txID)
require.True(ok)
Expand Down
18 changes: 7 additions & 11 deletions vms/platformvm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ type Network interface {

// 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.
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
IssueTx(context.Context, *txs.Tx) error
}

Expand Down Expand Up @@ -109,14 +107,6 @@ func (n *network) AppGossip(ctx context.Context, nodeID ids.NodeID, msgBytes []b
}
txID := tx.ID()

// We need to grab the context lock here to avoid racy behavior with
// transaction verification + mempool modifications.
//
// Invariant: tx should not be referenced again without the context lock
// held to avoid any data races.
n.ctx.Lock.Lock()
defer n.ctx.Lock.Unlock()

if reason := n.mempool.GetDropReason(txID); reason != nil {
// If the tx is being dropped - just ignore it
return nil
Expand Down Expand Up @@ -155,7 +145,13 @@ func (n *network) issueTx(tx *txs.Tx) error {
}

// Verify the tx at the currently preferred state
if err := n.manager.VerifyTx(tx); err != nil {
//
// We need to grab the context lock here to avoid racy behavior with
// transaction verification + mempool modifications.
n.ctx.Lock.Lock()
err := n.manager.VerifyTx(tx)
n.ctx.Lock.Unlock()
if err != nil {
n.ctx.Log.Debug("tx failed verification",
zap.Stringer("txID", txID),
zap.Error(err),
Expand Down
Loading
Loading