From 1147994c65796428d74f1074d0e3ccc4e3313da4 Mon Sep 17 00:00:00 2001 From: SHIVAM SHARMA Date: Wed, 13 Jul 2022 14:51:29 +0530 Subject: [PATCH] Added valSet testcases (#452) * Added valSet testcases * Added Negative Assertions * Minor Changes * Added varying valset length * Minor changes * Linting --- consensus/bor/valset/validator_set_test.go | 196 +++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 consensus/bor/valset/validator_set_test.go diff --git a/consensus/bor/valset/validator_set_test.go b/consensus/bor/valset/validator_set_test.go new file mode 100644 index 0000000000..d48fd3d626 --- /dev/null +++ b/consensus/bor/valset/validator_set_test.go @@ -0,0 +1,196 @@ +package valset + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + + "gotest.tools/assert" +) + +func NewValidatorFromKey(key string, votingPower int64) *Validator { + privKey, _ := crypto.HexToECDSA(key) + return NewValidator(crypto.PubkeyToAddress(privKey.PublicKey), votingPower) +} + +func GetValidators() [4]*Validator { + const ( + // addr0 = 0x96C42C56fdb78294F96B0cFa33c92bed7D75F96a + signer0 = "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd9" + + // addr1 = 0x98925BE497f6dFF6A5a33dDA8B5933cA35262d69 + signer1 = "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd8" + + //addr2 = 0x648Cf2A5b119E2c04061021834F8f75735B1D36b + signer2 = "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd7" + + //addr3 = 0x168f220B3b313D456eD4797520eFdFA9c57E6C45 + signer3 = "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd6" + ) + + return [4]*Validator{ + NewValidatorFromKey(signer0, 100), + NewValidatorFromKey(signer1, 200), + NewValidatorFromKey(signer2, 300), + NewValidatorFromKey(signer3, 400), + } +} + +func TestIncrementProposerPriority(t *testing.T) { + t.Parallel() + + vals := GetValidators() + + // Validator set length = 1 + valSet := NewValidatorSet(vals[:1]) + + expectedPropsers := []*Validator{vals[0], vals[0], vals[0], vals[0], vals[0], vals[0], vals[0], vals[0], vals[0], vals[0]} + + for i := 0; i < 10; i++ { + valSet.IncrementProposerPriority(1) + + assert.Equal(t, expectedPropsers[i].Address, valSet.GetProposer().Address) + } + + // Validator set length = 2 + valSet = NewValidatorSet(vals[:2]) + + expectedPropsers = []*Validator{vals[0], vals[1], vals[1], vals[0], vals[1], vals[1], vals[0], vals[1], vals[1], vals[0]} + + for i := 0; i < 10; i++ { + valSet.IncrementProposerPriority(1) + + assert.Equal(t, expectedPropsers[i].Address, valSet.GetProposer().Address) + } + + // Validator set length = 3 + valSet = NewValidatorSet(vals[:3]) + + expectedPropsers = []*Validator{vals[1], vals[2], vals[0], vals[1], vals[2], vals[2], vals[1], vals[2], vals[0], vals[1]} + + for i := 0; i < 10; i++ { + valSet.IncrementProposerPriority(1) + + assert.Equal(t, expectedPropsers[i].Address, valSet.GetProposer().Address) + } + + // Validator set length = 4 + valSet = NewValidatorSet(vals[:4]) + + expectedPropsers = []*Validator{vals[2], vals[1], vals[3], vals[2], vals[0], vals[3], vals[1], vals[2], vals[3], vals[3]} + + for i := 0; i < 10; i++ { + valSet.IncrementProposerPriority(1) + + assert.Equal(t, expectedPropsers[i].Address, valSet.GetProposer().Address) + } +} + +func TestRescalePriorities(t *testing.T) { + t.Parallel() + + vals := GetValidators() + + // Validator set length = 1 + valSet := NewValidatorSet(vals[:1]) + + valSet.RescalePriorities(10) + + expectedPriorities := []int64{0} + for i, val := range valSet.Validators { + assert.Equal(t, expectedPriorities[i], val.ProposerPriority) + } + + // Validator set length = 2 + + valSet = NewValidatorSet(vals[:2]) + + valSet.RescalePriorities(100) + + expectedPriorities = []int64{50, -50} + for i, val := range valSet.Validators { + assert.Equal(t, expectedPriorities[i], val.ProposerPriority) + } + + // Validator set length = 3 + + valSet = NewValidatorSet(vals[:3]) + + valSet.RescalePriorities(30) + + expectedPriorities = []int64{-17, 5, 11} + for i, val := range valSet.Validators { + assert.Equal(t, expectedPriorities[i], val.ProposerPriority) + } + + // Validator set length = 4 + + valSet = NewValidatorSet(vals[:4]) + + valSet.RescalePriorities(10) + + expectedPriorities = []int64{-6, 3, 1, 2} + for i, val := range valSet.Validators { + assert.Equal(t, expectedPriorities[i], val.ProposerPriority) + } +} + +func TestGetValidatorByAddressAndIndex(t *testing.T) { + t.Parallel() + + vals := GetValidators() + valSet := NewValidatorSet(vals[:4]) + + for _, val := range valSet.Validators { + idx, valByAddress := valSet.GetByAddress(val.Address) + addr, valByIndex := valSet.GetByIndex(idx) + + assert.DeepEqual(t, val, valByIndex) + assert.DeepEqual(t, val, valByAddress) + assert.DeepEqual(t, val.Address, common.BytesToAddress(addr)) + } + + tempAddress := common.HexToAddress("0x12345") + + // Negative Testcase + idx, _ := valSet.GetByAddress(tempAddress) + assert.Equal(t, idx, -1) + + // checking for validator index out of range + addr, _ := valSet.GetByIndex(100) + assert.Equal(t, common.BytesToAddress(addr), common.Address{}) +} + +func TestUpdateWithChangeSet(t *testing.T) { + t.Parallel() + + vals := GetValidators() + valSet := NewValidatorSet(vals[:4]) + + // halved the power of vals[2] and doubled the power of vals[3] + val2 := NewValidatorFromKey("c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd7", 150) // signer2 + val3 := NewValidatorFromKey("c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd6", 800) // signer3 + + // Adding new temp validator in the set + tempSigner := "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd5" + tempVal := NewValidatorFromKey(tempSigner, 250) + + // check totalVotingPower before updating validator set + assert.Equal(t, int64(1000), valSet.TotalVotingPower()) + + err := valSet.UpdateWithChangeSet([]*Validator{val2, val3, tempVal}) + assert.NilError(t, err) + + // check totalVotingPower after updating validator set + assert.Equal(t, int64(1500), valSet.TotalVotingPower()) + + _, updatedVal2 := valSet.GetByAddress(vals[2].Address) + assert.Equal(t, int64(150), updatedVal2.VotingPower) + + _, updatedVal3 := valSet.GetByAddress(vals[3].Address) + assert.Equal(t, int64(800), updatedVal3.VotingPower) + + _, updatedTempVal := valSet.GetByAddress(tempVal.Address) + assert.Equal(t, int64(250), updatedTempVal.VotingPower) +}