Skip to content

Commit

Permalink
vms/platformvm: Cleanup block builder tests (#2406)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
dhrubabasu and StephenButtolph authored Dec 2, 2023
1 parent 7623ffd commit 04af33e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 91 deletions.
163 changes: 86 additions & 77 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
txbuilder "github.com/ava-labs/avalanchego/vms/platformvm/txs/builder"
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
)

var errTestingDropped = errors.New("testing dropped")

// shows that a locally generated CreateChainTx can be added to mempool and then
// removed by inclusion in a block
func TestBlockBuilderAddLocalTx(t *testing.T) {
require := require.New(t)

Expand All @@ -44,17 +40,24 @@ func TestBlockBuilderAddLocalTx(t *testing.T) {
require.NoError(shutdownEnvironment(env))
}()

// add a tx to it
tx := getValidTx(env.txBuilder, t)
// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
ids.ShortEmpty,
)
require.NoError(err)
txID := tx.ID()

env.sender.SendAppGossipF = func(context.Context, []byte) error {
return nil
}
// Issue the transaction
require.NoError(env.network.IssueTx(context.Background(), tx))
require.True(env.mempool.Has(txID))

// show that build block include that tx and removes it from mempool
// [BuildBlock] should build a block with the transaction
blkIntf, err := env.Builder.BuildBlock(context.Background())
require.NoError(err)

Expand All @@ -63,7 +66,9 @@ func TestBlockBuilderAddLocalTx(t *testing.T) {
require.Len(blk.Txs(), 1)
require.Equal(txID, blk.Txs()[0].ID())

// Mempool should not contain the transaction or have marked it as dropped
require.False(env.mempool.Has(txID))
require.NoError(env.mempool.GetDropReason(txID))
}

func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
Expand All @@ -75,45 +80,67 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
require.NoError(shutdownEnvironment(env))
}()

// create candidate tx
tx := getValidTx(env.txBuilder, t)
// Create a valid transaction
tx, err := env.txBuilder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
ids.ShortEmpty,
)
require.NoError(err)
txID := tx.ID()

// A tx simply added to mempool is obviously not marked as dropped
require.NoError(env.mempool.Add(tx))
// Issue the transaction
require.NoError(env.network.IssueTx(context.Background(), tx))
require.True(env.mempool.Has(txID))

// Transaction should not be marked as dropped when added to the mempool
reason := env.mempool.GetDropReason(txID)
require.NoError(reason)

// When a tx is marked as dropped, it is still available to allow re-issuance
// Mark the transaction as dropped
errTestingDropped := errors.New("testing dropped")
env.mempool.MarkDropped(txID, errTestingDropped)
require.True(env.mempool.Has(txID)) // still available
reason = env.mempool.GetDropReason(txID)
require.ErrorIs(reason, errTestingDropped)

// A previously dropped tx, popped then re-added to mempool,
// is not dropped anymore
// Dropped transactions should still be in the mempool
require.True(env.mempool.Has(txID))

// Remove the transaction from the mempool
env.mempool.Remove([]*txs.Tx{tx})
require.NoError(env.mempool.Add(tx))

// Issue the transaction again
require.NoError(env.network.IssueTx(context.Background(), tx))
require.True(env.mempool.Has(txID))

// When issued again, the mempool should not be marked as dropped
reason = env.mempool.GetDropReason(txID)
require.NoError(reason)
}

func TestNoErrorOnUnexpectedSetPreferenceDuringBootstrapping(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env.ctx.Lock.Lock()
env.isBootstrapped.Set(false)
env.ctx.Log = logging.NoWarn{}
defer func() {
require.NoError(t, shutdownEnvironment(env))
require.NoError(shutdownEnvironment(env))
}()

require.False(t, env.blkManager.SetPreference(ids.GenerateTestID())) // should not panic
require.False(env.blkManager.SetPreference(ids.GenerateTestID())) // should not panic
}

func TestGetNextStakerToReward(t *testing.T) {
var (
now = time.Now()
txID = ids.GenerateTestID()
)

type test struct {
name string
timestamp time.Time
Expand All @@ -123,10 +150,6 @@ func TestGetNextStakerToReward(t *testing.T) {
expectedErr error
}

var (
now = time.Now()
txID = ids.GenerateTestID()
)
tests := []test{
{
name: "end of time",
Expand Down Expand Up @@ -295,49 +318,35 @@ func TestGetNextStakerToReward(t *testing.T) {
}

func TestBuildBlock(t *testing.T) {
env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(t, shutdownEnvironment(env))
}()

var (
parentID = ids.GenerateTestID()
height = uint64(1337)
output = &avax.TransferableOutput{
Asset: avax.Asset{ID: ids.GenerateTestID()},
Out: &secp256k1fx.TransferOutput{
OutputOwners: secp256k1fx.OutputOwners{
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
},
}
now = time.Now()
now = env.backend.Clk.Time()
parentID = ids.GenerateTestID()
height = uint64(1337)
parentTimestamp = now.Add(-2 * time.Second)
transactions = []*txs.Tx{{
Unsigned: &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
Ins: []*avax.TransferableInput{{
Asset: avax.Asset{ID: ids.GenerateTestID()},
In: &secp256k1fx.TransferInput{
Input: secp256k1fx.Input{
SigIndices: []uint32{0},
},
},
}},
Outs: []*avax.TransferableOutput{output},
}},
Validator: txs.Validator{
// Shouldn't be dropped
Start: uint64(now.Add(2 * txexecutor.SyncBound).Unix()),
},
StakeOuts: []*avax.TransferableOutput{output},
RewardsOwner: &secp256k1fx.OutputOwners{
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
},
Creds: []verify.Verifiable{
&secp256k1fx.Credential{
Sigs: [][secp256k1.SignatureLen]byte{{1, 3, 3, 7}},
},
},
}}
stakerTxID = ids.GenerateTestID()
stakerTxID = ids.GenerateTestID()

defaultValidatorStake = 100 * units.MilliAvax
validatorStartTime = now.Add(2 * txexecutor.SyncBound)
validatorEndTime = validatorStartTime.Add(360 * 24 * time.Hour)
)

tx, err := env.txBuilder.NewAddValidatorTx(
defaultValidatorStake,
uint64(validatorStartTime.Unix()),
uint64(validatorEndTime.Unix()),
ids.GenerateTestNodeID(),
preFundedKeys[0].PublicKey().Address(),
reward.PercentDenominator,
[]*secp256k1.PrivateKey{preFundedKeys[0]},
preFundedKeys[0].PublicKey().Address(),
)
require.NoError(t, err)

type test struct {
name string
Expand All @@ -357,7 +366,7 @@ func TestBuildBlock(t *testing.T) {

// The tx builder should be asked to build a reward tx
txBuilder := txbuilder.NewMockBuilder(ctrl)
txBuilder.EXPECT().NewRewardValidatorTx(stakerTxID).Return(transactions[0], nil)
txBuilder.EXPECT().NewRewardValidatorTx(stakerTxID).Return(tx, nil)

return &builder{
Mempool: mempool,
Expand Down Expand Up @@ -388,7 +397,7 @@ func TestBuildBlock(t *testing.T) {
parentTimestamp,
parentID,
height,
transactions[0],
tx,
)
require.NoError(err)
return expectedBlk
Expand All @@ -403,7 +412,7 @@ func TestBuildBlock(t *testing.T) {
// There are txs.
mempool.EXPECT().DropExpiredStakerTxs(gomock.Any()).Return([]ids.ID{})
mempool.EXPECT().HasTxs().Return(true)
mempool.EXPECT().PeekTxs(targetBlockSize).Return(transactions)
mempool.EXPECT().PeekTxs(targetBlockSize).Return([]*txs.Tx{tx})
return &builder{
Mempool: mempool,
}
Expand Down Expand Up @@ -435,7 +444,7 @@ func TestBuildBlock(t *testing.T) {
parentTimestamp,
parentID,
height,
transactions,
[]*txs.Tx{tx},
)
require.NoError(err)
return expectedBlk
Expand Down Expand Up @@ -553,7 +562,7 @@ func TestBuildBlock(t *testing.T) {
// There is a tx.
mempool.EXPECT().DropExpiredStakerTxs(gomock.Any()).Return([]ids.ID{})
mempool.EXPECT().HasTxs().Return(true)
mempool.EXPECT().PeekTxs(targetBlockSize).Return([]*txs.Tx{transactions[0]})
mempool.EXPECT().PeekTxs(targetBlockSize).Return([]*txs.Tx{tx})

clk := &mockable.Clock{}
clk.Set(now)
Expand Down Expand Up @@ -591,7 +600,7 @@ func TestBuildBlock(t *testing.T) {
parentTimestamp,
parentID,
height,
[]*txs.Tx{transactions[0]},
[]*txs.Tx{tx},
)
require.NoError(err)
return expectedBlk
Expand All @@ -607,7 +616,7 @@ func TestBuildBlock(t *testing.T) {
// There is a staker tx.
mempool.EXPECT().DropExpiredStakerTxs(gomock.Any()).Return([]ids.ID{})
mempool.EXPECT().HasTxs().Return(true)
mempool.EXPECT().PeekTxs(targetBlockSize).Return([]*txs.Tx{transactions[0]})
mempool.EXPECT().PeekTxs(targetBlockSize).Return([]*txs.Tx{tx})

clk := &mockable.Clock{}
clk.Set(now)
Expand Down Expand Up @@ -645,7 +654,7 @@ func TestBuildBlock(t *testing.T) {
parentTimestamp,
parentID,
height,
[]*txs.Tx{transactions[0]},
[]*txs.Tx{tx},
)
require.NoError(err)
return expectedBlk
Expand Down
14 changes: 0 additions & 14 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,3 @@ func shutdownEnvironment(env *environment) error {
env.baseDB.Close(),
)
}

func getValidTx(txBuilder txbuilder.Builder, t *testing.T) *txs.Tx {
tx, err := txBuilder.NewCreateChainTx(
testSubnet1.ID(),
nil,
constants.AVMID,
nil,
"chain name",
[]*secp256k1.PrivateKey{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},
ids.ShortEmpty,
)
require.NoError(t, err)
return tx
}

0 comments on commit 04af33e

Please sign in to comment.