[#697] governance: Make best effort traversing main chain list of keys

We should go through every key in main chain list to merget lists
as fast as possible. Previously we drop main chain traversing as
soon as we have no more new keys to add. Instead we should try
to go for old keys in the list and add it as more as we can.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-07-14 12:43:59 +03:00 committed by Alex Vanin
parent 0212f33743
commit 96da7ceb4f
2 changed files with 18 additions and 4 deletions

View file

@ -44,19 +44,24 @@ func newAlphabetList(sidechain, mainnet keys.PublicKeys) (keys.PublicKeys, error
newNodes := 0 newNodes := 0
newNodeLimit := (ln - 1) / 3 newNodeLimit := (ln - 1) / 3
for i := 0; i < ln; i++ { for _, node := range mainnet {
if newNodes == newNodeLimit { if len(result) == ln {
break break
} }
mainnetAddr := mainnet[i].Address() limitReached := newNodes == newNodeLimit
mainnetAddr := node.Address()
if _, ok := hmap[mainnetAddr]; !ok { if _, ok := hmap[mainnetAddr]; !ok {
if limitReached {
continue
}
newNodes++ newNodes++
} else { } else {
hmap[mainnetAddr] = true hmap[mainnetAddr] = true
} }
result = append(result, mainnet[i]) result = append(result, node)
} }
if newNodes == 0 { if newNodes == 0 {

View file

@ -68,6 +68,15 @@ func TestNewAlphabetList(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.True(t, equalPublicKeyLists(exp, got)) // expect {1, 2, 4, 5}, not {1, 2, 3, 5} require.True(t, equalPublicKeyLists(exp, got)) // expect {1, 2, 4, 5}, not {1, 2, 3, 5}
}) })
t.Run("new keys in the middle", func(t *testing.T) {
orig := keys.PublicKeys{k[0], k[1], k[2], k[6], k[7], k[8], k[9]}
// `exp` should contain maximum amount of new keys (2) in the middle
exp := keys.PublicKeys{k[0], k[3], k[4], k[6], k[7], k[8], k[9]}
got, err := newAlphabetList(orig, exp)
require.NoError(t, err)
require.True(t, equalPublicKeyLists(exp, got))
})
} }
func TestUpdateInnerRing(t *testing.T) { func TestUpdateInnerRing(t *testing.T) {