github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/utils_test.go (about)

     1  // Copyright 2017 The Celo Authors
     2  // This file is part of the celo library.
     3  //
     4  // The celo library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The celo library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the celo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package istanbul
    18  
    19  import (
    20  	blscrypto "github.com/ethereum/go-ethereum/crypto/bls"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  func TestValSetDiff(t *testing.T) {
    28  	tests := []struct {
    29  		inputOldValset      []common.Address
    30  		inputNewValset      []common.Address
    31  		expectedAddedVals   []common.Address
    32  		expectedRemovedVals *big.Int
    33  	}{
    34  		{
    35  			// Test validator sets that are both empty
    36  			inputOldValset:      []common.Address{},
    37  			inputNewValset:      []common.Address{},
    38  			expectedAddedVals:   []common.Address{},
    39  			expectedRemovedVals: big.NewInt(0),
    40  		},
    41  
    42  		{
    43  			// Test validator sets that are the same
    44  			inputOldValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    45  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    46  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825")},
    47  			inputNewValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    48  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    49  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825")},
    50  			expectedAddedVals:   []common.Address{},
    51  			expectedRemovedVals: big.NewInt(0),
    52  		},
    53  
    54  		{
    55  			// Test validator sets where one is empty
    56  			inputOldValset: []common.Address{},
    57  			inputNewValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    58  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    59  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825")},
    60  			expectedAddedVals: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    61  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    62  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825")},
    63  			expectedRemovedVals: big.NewInt(0),
    64  		},
    65  
    66  		{
    67  			// Test validator sets where other is empty
    68  			inputOldValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    69  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    70  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825")},
    71  			inputNewValset:      []common.Address{},
    72  			expectedAddedVals:   []common.Address{},
    73  			expectedRemovedVals: big.NewInt(7), // 111, all were removed
    74  		},
    75  
    76  		{
    77  			// Test validator sets that have some common elements
    78  			inputOldValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    79  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    80  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825"),
    81  				common.HexToAddress("0x18a00A3b357F7c309f0025dAe883170140527F76"),
    82  				common.HexToAddress("0xaF6532a62c7c7c951129cd55078B19216E81Dad9"),
    83  				common.HexToAddress("0x48Fa44872054C1426bdAB29834972c45D207D9DE")},
    84  			inputNewValset: []common.Address{common.HexToAddress("0x64DB1B94A0304E4c27De2E758B2f962d09dFE503"),
    85  				common.HexToAddress("0xC257274276a4E539741Ca11b590B9447B26A8051"),
    86  				common.HexToAddress("0x2140eFD7Ba31169c69dfff6CDC66C542f0211825"),
    87  				common.HexToAddress("0x31722d8C03e18a84891f45A4ECDe4444C8bE0907"),
    88  				common.HexToAddress("0xB55A183bF5db01665f9fC5DfbA71Fc6f8b5e42e6"),
    89  				common.HexToAddress("0x5B570EA42eBE010df95670389b93fd17d9Db9F23")},
    90  			expectedAddedVals: []common.Address{common.HexToAddress("0x31722d8C03e18a84891f45A4ECDe4444C8bE0907"),
    91  				common.HexToAddress("0xB55A183bF5db01665f9fC5DfbA71Fc6f8b5e42e6"),
    92  				common.HexToAddress("0x5B570EA42eBE010df95670389b93fd17d9Db9F23")},
    93  			expectedRemovedVals: big.NewInt(56),
    94  		},
    95  
    96  		{
    97  			// Test validator sets that have no common elements
    98  			inputOldValset: []common.Address{common.HexToAddress("0x18a00A3b357F7c309f0025dAe883170140527F76"),
    99  				common.HexToAddress("0xaF6532a62c7c7c951129cd55078B19216E81Dad9"),
   100  				common.HexToAddress("0x48Fa44872054C1426bdAB29834972c45D207D9DE")},
   101  			inputNewValset: []common.Address{common.HexToAddress("0x31722d8C03e18a84891f45A4ECDe4444C8bE0907"),
   102  				common.HexToAddress("0xB55A183bF5db01665f9fC5DfbA71Fc6f8b5e42e6"),
   103  				common.HexToAddress("0x5B570EA42eBE010df95670389b93fd17d9Db9F23")},
   104  			expectedAddedVals: []common.Address{common.HexToAddress("0x31722d8C03e18a84891f45A4ECDe4444C8bE0907"),
   105  				common.HexToAddress("0xB55A183bF5db01665f9fC5DfbA71Fc6f8b5e42e6"),
   106  				common.HexToAddress("0x5B570EA42eBE010df95670389b93fd17d9Db9F23")},
   107  			expectedRemovedVals: big.NewInt(7), // 111, all were removed
   108  		},
   109  	}
   110  
   111  	for i, tt := range tests {
   112  		convertedInputOldValSet := []ValidatorData{}
   113  		for _, addr := range tt.inputOldValset {
   114  			convertedInputOldValSet = append(convertedInputOldValSet, ValidatorData{
   115  				addr,
   116  				blscrypto.SerializedPublicKey{},
   117  			})
   118  		}
   119  		convertedInputNewValSet := []ValidatorData{}
   120  		for _, addr := range tt.inputNewValset {
   121  			convertedInputNewValSet = append(convertedInputNewValSet, ValidatorData{
   122  				addr,
   123  				blscrypto.SerializedPublicKey{},
   124  			})
   125  		}
   126  		addedVals, removedVals := ValidatorSetDiff(convertedInputOldValSet, convertedInputNewValSet)
   127  		addedValsAddresses, _ := SeparateValidatorDataIntoIstanbulExtra(addedVals)
   128  
   129  		if !CompareValidatorSlices(addedValsAddresses, tt.expectedAddedVals) || removedVals.Cmp(tt.expectedRemovedVals) != 0 {
   130  			t.Errorf("test %d failed - have: addedVals %v, removedVals %v; want: addedVals %v, removedVals %v", i, addedValsAddresses, removedVals, tt.expectedAddedVals, tt.expectedRemovedVals)
   131  		}
   132  	}
   133  
   134  }