From 96da7ceb4fc9a2bd245d8ed804f516f284417482 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 14 Jul 2021 12:43:59 +0300 Subject: [PATCH] [#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 --- pkg/innerring/processors/governance/list.go | 13 +++++++++---- pkg/innerring/processors/governance/list_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/innerring/processors/governance/list.go b/pkg/innerring/processors/governance/list.go index 6a76a08267..c85a764c26 100644 --- a/pkg/innerring/processors/governance/list.go +++ b/pkg/innerring/processors/governance/list.go @@ -44,19 +44,24 @@ func newAlphabetList(sidechain, mainnet keys.PublicKeys) (keys.PublicKeys, error newNodes := 0 newNodeLimit := (ln - 1) / 3 - for i := 0; i < ln; i++ { - if newNodes == newNodeLimit { + for _, node := range mainnet { + if len(result) == ln { break } - mainnetAddr := mainnet[i].Address() + limitReached := newNodes == newNodeLimit + + mainnetAddr := node.Address() if _, ok := hmap[mainnetAddr]; !ok { + if limitReached { + continue + } newNodes++ } else { hmap[mainnetAddr] = true } - result = append(result, mainnet[i]) + result = append(result, node) } if newNodes == 0 { diff --git a/pkg/innerring/processors/governance/list_test.go b/pkg/innerring/processors/governance/list_test.go index 3b41372042..e0121ab625 100644 --- a/pkg/innerring/processors/governance/list_test.go +++ b/pkg/innerring/processors/governance/list_test.go @@ -68,6 +68,15 @@ func TestNewAlphabetList(t *testing.T) { require.NoError(t, err) 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) {