[#421] governance: Don't construct alphabet list with empty sidechain

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-03-25 21:13:05 +03:00 committed by Alex Vanin
parent f4e39678f1
commit 58119e6065
2 changed files with 16 additions and 5 deletions

View file

@ -8,16 +8,22 @@ import (
)
var (
errNotEnoughKeys = errors.New("alphabet list in mainnet is too short")
errNotEqualLen = errors.New("old and new alphabet lists have different length")
errNotEnoughKeys = errors.New("alphabet list in mainnet is too short")
errNotEqualLen = errors.New("old and new alphabet lists have different length")
errEmptySidechain = errors.New("sidechain list is empty")
)
// newAlphabetList returns updated list of sidechain keys with no more than 1\3
// of new keys from mainnet list. Function returns `errNotEnoughKeys` if
// mainnet list contains less keys than sidechain list. Function returns
// (nil, nil) if mainnet list contains all keys from sidechain list.
// of new keys from mainnet list. Function returns `errEmptySidechain` if
// sidechain list is empty. Function returns `errNotEnoughKeys` if mainnet
// list contains less keys than sidechain list. Function returns (nil, nil) if
// mainnet list contains all keys from sidechain list.
func newAlphabetList(sidechain, mainnet keys.PublicKeys) (keys.PublicKeys, error) {
ln := len(sidechain)
if ln == 0 {
return nil, errEmptySidechain
}
if len(mainnet) < ln {
return nil, errors.Wrapf(errNotEnoughKeys, "expecting %d keys", ln)
}

View file

@ -14,6 +14,11 @@ func TestNewAlphabetList(t *testing.T) {
orig := keys.PublicKeys{k[0], k[1], k[2], k[3], k[4], k[5], k[6]}
t.Run("no sidechain keys", func(t *testing.T) {
_, err := newAlphabetList(nil, orig)
require.Error(t, err)
})
t.Run("same keys", func(t *testing.T) {
list, err := newAlphabetList(orig, orig)
require.NoError(t, err)