Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Sep 19, 2024
1 parent 6c09f42 commit b815bc9
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 268 deletions.
3 changes: 2 additions & 1 deletion internal/state/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/go-multierror"

abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/libs/math"
"github.com/dashpay/tenderdash/types"
)

Expand Down Expand Up @@ -87,7 +88,7 @@ func (e *EventSet) WithTxs(block *types.Block, txResults []*abci.ExecTxResult) *
e.Txs[i] = types.EventDataTx{
TxResult: abci.TxResult{
Height: block.Height,
Index: uint32(i),
Index: math.MustConvertUint32(i),
Tx: tx,
Result: *(txResults[i]),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
}

var validatorSet *types.ValidatorSet
if genDoc.Validators == nil || len(genDoc.Validators) == 0 {
if len(genDoc.Validators) == 0 {
validatorSet = types.NewValidatorSet(nil, nil, genDoc.QuorumType, nil, false)
} else {
validators := make([]*types.Validator, len(genDoc.Validators))
Expand Down
145 changes: 0 additions & 145 deletions internal/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"github.com/dashpay/tenderdash/crypto/merkle"
"github.com/dashpay/tenderdash/dash"
"github.com/dashpay/tenderdash/dash/llmq"
selectproposer "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
"github.com/dashpay/tenderdash/internal/evidence/mocks"
sm "github.com/dashpay/tenderdash/internal/state"
statefactory "github.com/dashpay/tenderdash/internal/state/test/factory"
"github.com/dashpay/tenderdash/libs/log"
tmstate "github.com/dashpay/tenderdash/proto/tendermint/state"
"github.com/dashpay/tenderdash/types"
)
Expand Down Expand Up @@ -384,149 +382,6 @@ func TestEmptyValidatorUpdates(t *testing.T) {
assert.Equal(t, expectValidators, types.ValidatorListString(changes.NextValidators.Validators), "validator should not change")
}

//func TestProposerFrequency(t *testing.T) {
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
//
// // some explicit test cases
// testCases := []struct {
// powers []int64
// }{
// // 2 vals
// {[]int64{1, 1}},
// {[]int64{1, 2}},
// {[]int64{1, 100}},
// {[]int64{5, 5}},
// {[]int64{5, 100}},
// {[]int64{50, 50}},
// {[]int64{50, 100}},
// {[]int64{1, 1000}},
//
// // 3 vals
// {[]int64{1, 1, 1}},
// {[]int64{1, 2, 3}},
// {[]int64{1, 2, 3}},
// {[]int64{1, 1, 10}},
// {[]int64{1, 1, 100}},
// {[]int64{1, 10, 100}},
// {[]int64{1, 1, 1000}},
// {[]int64{1, 10, 1000}},
// {[]int64{1, 100, 1000}},
//
// // 4 vals
// {[]int64{1, 1, 1, 1}},
// {[]int64{1, 2, 3, 4}},
// {[]int64{1, 1, 1, 10}},
// {[]int64{1, 1, 1, 100}},
// {[]int64{1, 1, 1, 1000}},
// {[]int64{1, 1, 10, 100}},
// {[]int64{1, 1, 10, 1000}},
// {[]int64{1, 1, 100, 1000}},
// {[]int64{1, 10, 100, 1000}},
// }
//
// for caseNum, testCase := range testCases {
// // run each case 5 times to sample different
// // initial priorities
// for i := 0; i < 5; i++ {
// valSet := genValSetWithPowers(testCase.powers)
// testProposerFreq(t, caseNum, valSet)
// }
// }
//
// // some random test cases with up to 100 validators
// maxVals := 100
// maxPower := 1000
// nTestCases := 5
// for i := 0; i < nTestCases; i++ {
// N := mrand.Int()%maxVals + 1
// vals := make([]*types.Validator, N)
// totalVotePower := int64(0)
// for j := 0; j < N; j++ {
// // make sure votePower > 0
// votePower := int64(mrand.Int()%maxPower) + 1
// totalVotePower += votePower
// privVal := types.NewMockPV()
// pubKey, err := privVal.GetPubKey(ctx)
// require.NoError(t, err)
// val := types.NewValidator(pubKey, votePower)
// val.ProposerPriority = mrand.Int63()
// vals[j] = val
// }
// valSet := types.NewValidatorSet(vals)
// valSet.RescalePriorities(totalVotePower)
// testProposerFreq(t, i, valSet)
// }
//}
//
//// new val set with given powers and random initial priorities
//func genValSetWithPowers(powers []int64) *types.ValidatorSet {
// size := len(powers)
// vals := make([]*types.Validator, size)
// totalVotePower := int64(0)
// for i := 0; i < size; i++ {
// totalVotePower += powers[i]
// val := types.NewValidator(ed25519.GenPrivKey().PubKey(), powers[i])
// val.ProposerPriority = mrand.Int63()
// vals[i] = val
// }
// valSet := types.NewValidatorSet(vals)
// valSet.RescalePriorities(totalVotePower)
// return valSet
//}
//
//// test a proposer appears as frequently as expected
//func testProposerFreq(t *testing.T, caseNum int, valSet *types.ValidatorSet) {
// N := valSet.Size()
// totalPower := valSet.TotalVotingPower()
//
// // run the proposer selection and track frequencies
// runMult := 1
// runs := int(totalPower) * runMult
// freqs := make([]int, N)
// for i := 0; i < runs; i++ {
// prop := valSet.GetProposer()
// idx, _ := valSet.GetByAddress(prop.Address)
// freqs[idx]++
// valSet.IncrementProposerPriority(1)
// }
//
// // assert frequencies match expected (max off by 1)
// for i, freq := range freqs {
// _, val := valSet.GetByIndex(int32(i))
// expectFreq := int(val.VotingPower) * runMult
// gotFreq := freq
// abs := int(math.Abs(float64(expectFreq - gotFreq)))
//
// // max bound on expected vs seen freq was proven
// // to be 1 for the 2 validator case in
// // https://github.com/cwgoes/tm-proposer-idris
// // and inferred to generalize to N-1
// bound := N - 1
// require.True(
// t,
// abs <= bound,
// fmt.Sprintf("Case %d val %d (%d): got %d, expected %d", caseNum, i, N, gotFreq, expectFreq),
// )
// }
//}

func valsetScoresNewHeight(t *testing.T, state *sm.State) *types.Validator {
ps, err := selectproposer.NewProposerSelector(
state.ConsensusParams,
state.Validators,
state.LastBlockHeight,
state.LastBlockRound,
nil,
log.NewTestingLogger(t))

require.NoError(t, err)
err = ps.UpdateHeightRound(state.LastBlockHeight+1, 0)
require.NoError(t, err)

return ps.MustGetProposer(state.LastBlockHeight+1, 0)
}

func TestFourAddFourMinusOneGenesisValidators(t *testing.T) {
tearDown, _, state := setupTestCase(t)
defer tearDown(t)
Expand Down
4 changes: 3 additions & 1 deletion light/provider/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ func (p *http) validatorSet(ctx context.Context, height *int64, proposer types.P
return nil, provider.ErrBadLightBlock{Reason: fmt.Errorf("invalid validator set retrieved: %w", err)}
}

valSet.SetProposer(proposer)
if err := valSet.SetProposer(proposer); err != nil {
return nil, provider.ErrBadLightBlock{Reason: fmt.Errorf("failed to determine proposer: %w", err)}
}

return valSet, nil
}
Expand Down
2 changes: 1 addition & 1 deletion types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
malleateEvidence func(*DuplicateVoteEvidence)
expectErr bool
}{
{"Good DuplicateVoteEvidence", func(ev *DuplicateVoteEvidence) {}, false},
{"Good DuplicateVoteEvidence", func(_ *DuplicateVoteEvidence) {}, false},
{"Nil vote A", func(ev *DuplicateVoteEvidence) { ev.VoteA = nil }, true},
{"Nil vote B", func(ev *DuplicateVoteEvidence) { ev.VoteB = nil }, true},
{"Nil votes", func(ev *DuplicateVoteEvidence) {
Expand Down
119 changes: 0 additions & 119 deletions types/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,6 @@ func randValidatorInQuorum(ctx context.Context, t *testing.T, quorumHash crypto.
return val, privVal
}

func (vals *ValidatorSet) toBytes(t *testing.T) []byte {
pbvs, err := vals.ToProto()
require.NoError(t, err)

bz, err := pbvs.Marshal()
require.NoError(t, err)

return bz
}

func (vals *ValidatorSet) fromBytes(t *testing.T, b []byte) *ValidatorSet {
pbvs := new(tmproto.ValidatorSet)
err := pbvs.Unmarshal(b)
require.NoError(t, err)

vs, err := ValidatorSetFromProto(pbvs)
require.NoError(t, err)

return vs
}

func TestEmptySet(t *testing.T) {

var valList []*Validator
Expand Down Expand Up @@ -792,82 +771,6 @@ func TestValSetApplyUpdatesTestsExecute(t *testing.T) {
}
}

type testVSetCfg struct {
startVals []testVal
deletedVals []testVal
updatedVals []testVal
addedVals []testVal
expectedVals []testVal
}

func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
if nBase <= 0 || nAddMax < 0 {
t.Fatalf("bad parameters %v %v", nBase, nAddMax)
}

var nOld, nDel, nChanged, nAdd int

nOld = int(uint(rand.Int())%uint(nBase)) + 1
if nBase-nOld > 0 {
nDel = int(uint(rand.Int()) % uint(nBase-nOld))
}
nChanged = nBase - nOld - nDel

if nAddMax > 0 {
nAdd = rand.Int()%nAddMax + 1
}

cfg := testVSetCfg{}

cfg.startVals = make([]testVal, nBase)
cfg.deletedVals = make([]testVal, nDel)
cfg.addedVals = make([]testVal, nAdd)
cfg.updatedVals = make([]testVal, nChanged)
cfg.expectedVals = make([]testVal, nBase-nDel+nAdd)

for i := 0; i < nBase; i++ {
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), DefaultDashVotingPower}
if i < nOld {
cfg.expectedVals[i] = cfg.startVals[i]
}
if i >= nOld && i < nOld+nChanged {
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), DefaultDashVotingPower}
cfg.expectedVals[i] = cfg.updatedVals[i-nOld]
}
if i >= nOld+nChanged {
cfg.deletedVals[i-nOld-nChanged] = testVal{fmt.Sprintf("v%d", i), 0}
}
}

for i := nBase; i < nBase+nAdd; i++ {
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), DefaultDashVotingPower}
cfg.expectedVals[i-nDel] = cfg.addedVals[i-nBase]
}

sort.Sort(testValsByVotingPower(cfg.startVals))
sort.Sort(testValsByVotingPower(cfg.deletedVals))
sort.Sort(testValsByVotingPower(cfg.updatedVals))
sort.Sort(testValsByVotingPower(cfg.addedVals))
sort.Sort(testValsByVotingPower(cfg.expectedVals))

return cfg

}

func applyChangesToValSet(t *testing.T, expErr error, valSet *ValidatorSet, valsLists ...[]testVal) {
changes := make([]testVal, 0)
for _, valsList := range valsLists {
changes = append(changes, valsList...)
}
valList, thresholdPublicKey := addValidatorsToValidatorSet(valSet, changes)
err := valSet.UpdateWithChangeSet(valList, thresholdPublicKey, crypto.RandQuorumHash())
if expErr != nil {
assert.Equal(t, expErr, err)
} else {
assert.NoError(t, err)
}
}

func TestValidatorSetProtoBuf(t *testing.T) {
valset, _ := RandValidatorSet(10)
valset2, _ := RandValidatorSet(10)
Expand Down Expand Up @@ -910,28 +813,6 @@ func TestValidatorSetProtoBuf(t *testing.T) {
}
}

//-------------------------------------

type testValsByVotingPower []testVal

func (tvals testValsByVotingPower) Len() int {
return len(tvals)
}

// Here we need to sort by the pro_tx_hash and not the name if the power is equal, in the test the pro_tx_hash is derived
//
// from the name by applying a single SHA256
func (tvals testValsByVotingPower) Less(i, j int) bool {
if tvals[i].power == tvals[j].power {
return bytes.Compare(crypto.Checksum([]byte(tvals[i].name)), crypto.Checksum([]byte(tvals[j].name))) == -1
}
return tvals[i].power > tvals[j].power
}

func (tvals testValsByVotingPower) Swap(i, j int) {
tvals[i], tvals[j] = tvals[j], tvals[i]
}

// -------------------------------------
// Benchmark tests
func BenchmarkUpdates(b *testing.B) {
Expand Down

0 comments on commit b815bc9

Please sign in to comment.