Skip to content

Commit

Permalink
Sanitize ValidatorIndex conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusFranco99 committed Sep 23, 2024
1 parent eb52b63 commit 4020fbb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func MissingSomeShares() tests.SpecTest {
msgID := testingutils.CommitteeMsgID(ks)

// Committee's validator indexes
committeeShareValidators := []int{1, 3, 5, 7, 9}
committeeShareValidators := []phase0.ValidatorIndex{1, 3, 5, 7, 9}
// KeySet and Share map for Committee
committeeShareKSMap := testingutils.KeySetMapForValidatorIndexList(committeeShareValidators)
committeeShareMap := testingutils.ShareMapFromKeySetMap(committeeShareKSMap)
Expand Down
19 changes: 15 additions & 4 deletions types/testingutils/share_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ func ValidatorIndexList(limit int) []int {
return ret
}

func KeySetMapForValidatorIndexList(valIndexes []int) map[phase0.ValidatorIndex]*TestKeySet {
func KeySetMapForValidatorIndexList(valIndexes []phase0.ValidatorIndex) map[phase0.ValidatorIndex]*TestKeySet {
ret := make(map[phase0.ValidatorIndex]*TestKeySet)
for _, valIdx := range valIndexes {
ks, exists := TestingKeySetMap[phase0.ValidatorIndex(valIdx)]
ks, exists := TestingKeySetMap[valIdx]
if !exists {
panic(fmt.Sprintf("Validator index %v does not exist in TestingKeySetMap", valIdx))
}
ret[phase0.ValidatorIndex(valIdx)] = ks
ret[valIdx] = ks
}
return ret
}

func KeySetMapForValidators(limit int) map[phase0.ValidatorIndex]*TestKeySet {
return KeySetMapForValidatorIndexList(ValidatorIndexList(limit))
if limit <= 0 {
return map[phase0.ValidatorIndex]*TestKeySet{}
}
validators := make([]phase0.ValidatorIndex, limit)
for i := 0; i < limit; i++ {
validatorIndex := (i + 1)
if validatorIndex < 0 {
panic("Invalid validator index")
}
validators[i] = phase0.ValidatorIndex(validatorIndex)
}
return KeySetMapForValidatorIndexList(validators)
}

func ShareMapFromKeySetMap(keySetMap map[phase0.ValidatorIndex]*TestKeySet) map[phase0.ValidatorIndex]*types.Share {
Expand Down

0 comments on commit 4020fbb

Please sign in to comment.