Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Dec 20, 2023
1 parent 677629a commit a582640
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 18 deletions.
25 changes: 16 additions & 9 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func TestBuildBlockBasic(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()

// Create a valid transaction
Expand All @@ -50,13 +52,12 @@ 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)

env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

// [BuildBlock] should build a block with the transaction
blkIntf, err := env.Builder.BuildBlock(context.Background())
require.NoError(err)
Expand Down Expand Up @@ -96,8 +97,10 @@ func TestBuildBlockShouldReward(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()

var (
Expand All @@ -124,13 +127,12 @@ 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)

env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

// Build and accept a block with the tx
blk, err := env.Builder.BuildBlock(context.Background())
require.NoError(err)
Expand Down Expand Up @@ -231,8 +233,10 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()

// Create a valid transaction
Expand All @@ -249,13 +253,12 @@ 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)

env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

var (
now = env.backend.Clk.Time()
nextTime = now.Add(2 * txexecutor.SyncBound)
Expand Down Expand Up @@ -492,7 +495,9 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(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 All @@ -514,7 +519,9 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
env.mempool.Remove(tx)

// Issue the transaction again
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
8 changes: 4 additions & 4 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,16 @@ func TestGetTxStatus(t *testing.T) {
require.Equal(status.Unknown, resp.Status)
require.Zero(resp.Reason)

service.vm.ctx.Lock.Lock()

// put the chain in existing chain list
err = service.vm.Network.IssueTx(context.Background(), tx)
require.ErrorIs(err, database.ErrNotFound) // Missing shared memory UTXO
service.vm.ctx.Lock.Lock()

mutableSharedMemory.SharedMemory = sm

service.vm.ctx.Lock.Unlock()
require.NoError(service.vm.Network.IssueTx(context.Background(), tx))
service.vm.ctx.Lock.Lock()

block, err := service.vm.BuildBlock(context.Background())
require.NoError(err)
Expand Down Expand Up @@ -337,10 +338,9 @@ func TestGetTx(t *testing.T) {
err = service.GetTx(nil, arg, &response)
require.ErrorIs(err, database.ErrNotFound) // We haven't issued the tx yet

service.vm.ctx.Lock.Lock()

require.NoError(service.vm.Network.IssueTx(context.Background(), tx))

service.vm.ctx.Lock.Lock()
blk, err := service.vm.BuildBlock(context.Background())
require.NoError(err)

Expand Down
29 changes: 24 additions & 5 deletions vms/platformvm/validator_set_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,14 @@ func addSubnetValidator(vm *VM, data *validatorInputData, subnetID ids.ID) (*sta
if err != nil {
return nil, fmt.Errorf("could not create AddSubnetValidatorTx: %w", err)
}
return internalAddValidator(vm, signedTx)

vm.ctx.Lock.Unlock()
if err := vm.Network.IssueTx(context.Background(), signedTx); err != nil {
return nil, fmt.Errorf("could not add tx to mempool: %w", err)
}
vm.ctx.Lock.Lock()

return acceptBlockWithTx(vm, signedTx)
}

func addPrimaryValidatorWithBLSKey(vm *VM, data *validatorInputData) (*state.Staker, error) {
Expand Down Expand Up @@ -351,7 +358,14 @@ func addPrimaryValidatorWithBLSKey(vm *VM, data *validatorInputData) (*state.Sta
if err := signedTx.SyntacticVerify(vm.ctx); err != nil {
return nil, fmt.Errorf("failed syntax verification of AddPermissionlessValidatorTx: %w", err)
}
return internalAddValidator(vm, signedTx)

vm.ctx.Lock.Unlock()
if err := vm.Network.IssueTx(context.Background(), signedTx); err != nil {
return nil, fmt.Errorf("could not add tx to mempool: %w", err)
}
vm.ctx.Lock.Lock()

return acceptBlockWithTx(vm, signedTx)
}

func addPrimaryValidatorWithoutBLSKey(vm *VM, data *validatorInputData) (*state.Staker, error) {
Expand All @@ -369,14 +383,17 @@ func addPrimaryValidatorWithoutBLSKey(vm *VM, data *validatorInputData) (*state.
if err != nil {
return nil, fmt.Errorf("could not create AddValidatorTx: %w", err)
}
return internalAddValidator(vm, signedTx)
}

func internalAddValidator(vm *VM, signedTx *txs.Tx) (*state.Staker, error) {
vm.ctx.Lock.Unlock()
if err := vm.Network.IssueTx(context.Background(), signedTx); err != nil {
return nil, fmt.Errorf("could not add tx to mempool: %w", err)
}
vm.ctx.Lock.Lock()

return acceptBlockWithTx(vm, signedTx)
}

func acceptBlockWithTx(vm *VM, signedTx *txs.Tx) (*state.Staker, error) {
blk, err := vm.Builder.BuildBlock(context.Background())
if err != nil {
return nil, fmt.Errorf("failed building block: %w", err)
Expand Down Expand Up @@ -783,9 +800,11 @@ func buildVM(t *testing.T) (*VM, ids.ID, error) {
if err != nil {
return nil, ids.Empty, err
}
vm.ctx.Lock.Unlock()
if err := vm.Network.IssueTx(context.Background(), testSubnet1); err != nil {
return nil, ids.Empty, err
}
vm.ctx.Lock.Lock()

blk, err := vm.Builder.BuildBlock(context.Background())
if err != nil {
Expand Down
Loading

0 comments on commit a582640

Please sign in to comment.