From 58119e606583e8a29ecac16da34cdc176dc7b6c8 Mon Sep 17 00:00:00 2001
From: Alex Vanin <alexey@nspcc.ru>
Date: Thu, 25 Mar 2021 21:13:05 +0300
Subject: [PATCH] [#421] governance: Don't construct alphabet list with empty
 sidechain

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
---
 pkg/innerring/processors/governance/list.go      | 16 +++++++++++-----
 pkg/innerring/processors/governance/list_test.go |  5 +++++
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/pkg/innerring/processors/governance/list.go b/pkg/innerring/processors/governance/list.go
index f5e21d753..a181c5969 100644
--- a/pkg/innerring/processors/governance/list.go
+++ b/pkg/innerring/processors/governance/list.go
@@ -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)
 	}
diff --git a/pkg/innerring/processors/governance/list_test.go b/pkg/innerring/processors/governance/list_test.go
index 8cc64cd0e..2b67e0bed 100644
--- a/pkg/innerring/processors/governance/list_test.go
+++ b/pkg/innerring/processors/governance/list_test.go
@@ -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)