Skip to content

Commit

Permalink
Add negative path testcases for validator transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
charithabandi committed Mar 9, 2024
1 parent 91e926b commit 2ad0062
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/integration/kwild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ func TestKwildValidatorRemoval(t *testing.T) {
// In this test, we will have a set of 4 validators, where 3 of the
// validators are required to remove one.
const numVals, numNonVals = 4, 0
const blockInterval = time.Second

opts := []integration.HelperOpt{
integration.WithValidators(numVals),
integration.WithNonValidators(numNonVals),
integration.WithBlockInterval(blockInterval),
}

testDrivers := strings.Split(*drivers, ",")
Expand All @@ -126,6 +129,7 @@ func TestKwildValidatorRemoval(t *testing.T) {
node0Driver := helper.GetOperatorDriver(ctx, "node0", driverType)
node1Driver := helper.GetOperatorDriver(ctx, "node1", driverType)
node2Driver := helper.GetOperatorDriver(ctx, "node2", driverType)
node3Driver := helper.GetOperatorDriver(ctx, "node3", driverType)
targetPubKey := helper.NodePrivateKey("node3").PubKey().Bytes()

/* Remove node 3 (4 validators, nodes 0, 1, and 2 remove node 3)
Expand All @@ -137,6 +141,10 @@ func TestKwildValidatorRemoval(t *testing.T) {
- node 3 is no longer a validator
*/
specifications.ValidatorNodeRemoveSpecificationV4R1(ctx, t, node0Driver, node1Driver, node2Driver, targetPubKey) // joiner is a validator at node

//
// Node 3 is no longer a validator, removal should fail
specifications.RemoveNonValidatorSpecification(ctx, t, node3Driver, targetPubKey)
})
}
}
Expand Down Expand Up @@ -172,6 +180,7 @@ func TestKwildValidatorUpdatesIntegration(t *testing.T) {
helper.Setup(ctx, allServices)

node0Driver := helper.GetOperatorDriver(ctx, "node0", driverType)
node0PubKey := helper.NodePrivateKey("node0").PubKey().Bytes()
node1Driver := helper.GetOperatorDriver(ctx, "node1", driverType)
joinerDriver := helper.GetOperatorDriver(ctx, "node3", driverType)
joinerPrivKey := helper.NodePrivateKey("node3")
Expand All @@ -183,6 +192,12 @@ func TestKwildValidatorUpdatesIntegration(t *testing.T) {
// Start the network with 3 validators & 1 Non-validator
specifications.CurrentValidatorsSpecification(ctx, t, node0Driver, 3)

// Reject Joins from existing validators
specifications.JoinExistingValidatorSpecification(ctx, t, node0Driver, node0PubKey)

// Reject leaves from non-validators
specifications.NonValidatorLeaveSpecification(ctx, t, joinerDriver, joinerPubKey)

/*
Join Expiry:
- Node3 requests to join
Expand All @@ -199,6 +214,10 @@ func TestKwildValidatorUpdatesIntegration(t *testing.T) {
*/
specifications.ValidatorNodeJoinSpecification(ctx, t, joinerDriver, joinerPubKey, 3)
time.Sleep(2 * time.Second)

// Node3 cant self approve
specifications.ValidatorNodeSelfApproveSpecification(ctx, t, joinerDriver, joinerPubKey)

// Node 0,1 approves
specifications.ValidatorNodeApproveSpecification(ctx, t, node0Driver, joinerPubKey, 3, 3, false)
specifications.ValidatorNodeApproveSpecification(ctx, t, node1Driver, joinerPubKey, 3, 4, true)
Expand Down
77 changes: 77 additions & 0 deletions test/specifications/validator_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,63 @@ func ValidatorNodeJoinSpecification(ctx context.Context, t *testing.T, netops Va
assert.Equal(t, valCount, len(vals))
}

func JoinExistingValidatorSpecification(ctx context.Context, t *testing.T, netops ValidatorOpsDsl, joiner []byte) {
t.Log("Executing existing validator join specification")

// Validator issues a Join request
rec, err := netops.ValidatorNodeJoin(ctx)
require.NoError(t, err)

// Ensure that the Tx is mined.
expectTxFail(t, netops, ctx, rec, defaultTxQueryTimeout)()

// Get Request status, #approvals = 0, #board = valCount
joinStatus, err := netops.ValidatorJoinStatus(ctx, joiner)
require.Error(t, err)
require.Nil(t, joinStatus)
}

// NonValidatorLeaveSpecification tests the validator remove process on a non-validator node
func NonValidatorLeaveSpecification(ctx context.Context, t *testing.T, netops ValidatorOpsDsl, target []byte) {
t.Log("Executing non validator leave specification")

// Ensure that the validator set precondition for this spec test is met.
preVals, err := netops.ValidatorsList(ctx)
require.NoError(t, err)

// non validator node tries to leave
rec, err := netops.ValidatorNodeLeave(ctx)
require.NoError(t, err)

// Ensure that the Validator Leave Tx is mined.
expectTxFail(t, netops, ctx, rec, defaultTxQueryTimeout)()

// ValidatorSet count should remain same
postVals, err := netops.ValidatorsList(ctx)
require.NoError(t, err)
assert.Equal(t, len(preVals), len(postVals))
}

func RemoveNonValidatorSpecification(ctx context.Context, t *testing.T, netops ValidatorRemoveDsl, target []byte) {
t.Log("Executing remove non-validator specification")

// Ensure that the validator set precondition for this spec test is met.
preVals, err := netops.ValidatorsList(ctx)
require.NoError(t, err)

// non validator node tries to remove a validator
rec, err := netops.ValidatorNodeRemove(ctx, target)
require.NoError(t, err)

// Ensure that the Validator Remove Tx is mined.
expectTxFail(t, netops, ctx, rec, defaultTxQueryTimeout)()

// ValidatorSet count should remain same
postVals, err := netops.ValidatorsList(ctx)
require.NoError(t, err)
assert.Equal(t, len(preVals), len(postVals))
}

// ValidatorNodeRemoveSpecificationN4R1 tests the validator remove process on a
// network with 4 validators, where 3 nodes target the last.
func ValidatorNodeRemoveSpecificationV4R1(ctx context.Context, t *testing.T, n0, n1, n2 ValidatorRemoveDsl, target []byte) {
Expand Down Expand Up @@ -148,6 +205,26 @@ func ValidatorNodeApproveSpecification(ctx context.Context, t *testing.T, netops
assert.Equal(t, postCnt, len(vals))
}

func ValidatorNodeSelfApproveSpecification(ctx context.Context, t *testing.T, netops ValidatorOpsDsl, joiner []byte) {
// Get Join Request status, #board = preCnt
joinStatus, err := netops.ValidatorJoinStatus(ctx, joiner)
require.NoError(t, err)
preApprovalCnt := approvalCount(joinStatus)

// Approval Request
rec, err := netops.ValidatorNodeApprove(ctx, joiner)
require.NoError(t, err)

// TX should fail as validator cannot approve its own join request
expectTxFail(t, netops, ctx, rec, defaultTxQueryTimeout)()

// Check Join Request Status: #approvals = preApprovalCnt
joinStatus, err = netops.ValidatorJoinStatus(ctx, joiner)
require.NoError(t, err)
postApprovalCnt := approvalCount(joinStatus)
assert.Equal(t, preApprovalCnt, postApprovalCnt)
}

func ValidatorNodeLeaveSpecification(ctx context.Context, t *testing.T, netops ValidatorOpsDsl) {
t.Log("Executing network node leave specification")

Expand Down

0 comments on commit 2ad0062

Please sign in to comment.