github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/bucket_index_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package staking
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/iotex-address/address"
    16  
    17  	"github.com/iotexproject/iotex-core/state"
    18  	"github.com/iotexproject/iotex-core/test/identityset"
    19  	"github.com/iotexproject/iotex-core/testutil/testdb"
    20  )
    21  
    22  func TestBucketIndices(t *testing.T) {
    23  	require := require.New(t)
    24  
    25  	bis := make(BucketIndices, 0)
    26  
    27  	bis.addBucketIndex(uint64(1))
    28  	bis.addBucketIndex(uint64(2))
    29  	bis.addBucketIndex(uint64(3))
    30  
    31  	data, err := bis.Serialize()
    32  	require.NoError(err)
    33  	bis1 := BucketIndices{}
    34  	require.NoError(bis1.Deserialize(data))
    35  	require.Equal(3, len(bis1))
    36  
    37  	require.Equal(uint64(1), bis1[0])
    38  	require.Equal(uint64(2), bis1[1])
    39  	require.Equal(uint64(3), bis1[2])
    40  }
    41  
    42  func TestGetPutBucketIndex(t *testing.T) {
    43  	testGetPut := func(t *testing.T) {
    44  		require := require.New(t)
    45  		ctrl := gomock.NewController(t)
    46  		sm := testdb.NewMockStateManager(ctrl)
    47  		csm := newCandidateStateManager(sm)
    48  		csr := newCandidateStateReader(sm)
    49  
    50  		tests := []struct {
    51  			index          uint64
    52  			voterAddr      address.Address
    53  			candAddr       address.Address
    54  			voterIndexSize int
    55  			candIndexSize  int
    56  		}{
    57  			{
    58  				uint64(1),
    59  				identityset.Address(1),
    60  				identityset.Address(1),
    61  				1,
    62  				1,
    63  			},
    64  			{
    65  				uint64(2),
    66  				identityset.Address(1),
    67  				identityset.Address(2),
    68  				2,
    69  				1,
    70  			},
    71  			{
    72  				uint64(3),
    73  				identityset.Address(1),
    74  				identityset.Address(3),
    75  				3,
    76  				1,
    77  			},
    78  			{
    79  				uint64(4),
    80  				identityset.Address(2),
    81  				identityset.Address(1),
    82  				1,
    83  				2,
    84  			},
    85  			{
    86  				uint64(5),
    87  				identityset.Address(2),
    88  				identityset.Address(2),
    89  				2,
    90  				2,
    91  			},
    92  			{
    93  				uint64(6),
    94  				identityset.Address(2),
    95  				identityset.Address(3),
    96  				3,
    97  				2,
    98  			},
    99  			{
   100  				uint64(7),
   101  				identityset.Address(3),
   102  				identityset.Address(1),
   103  				1,
   104  				3,
   105  			},
   106  			{
   107  				uint64(8),
   108  				identityset.Address(3),
   109  				identityset.Address(2),
   110  				2,
   111  				3,
   112  			},
   113  			{
   114  				uint64(9),
   115  				identityset.Address(3),
   116  				identityset.Address(3),
   117  				3,
   118  				3,
   119  			},
   120  		}
   121  		// after adding above, each voter and candidate will have a total of 3 indices
   122  		indexSize := 3
   123  
   124  		// put buckets and get
   125  		for i, e := range tests {
   126  			_, _, err := csr.voterBucketIndices(e.voterAddr)
   127  			if i == 0 {
   128  				require.Equal(state.ErrStateNotExist, errors.Cause(err))
   129  			}
   130  			_, _, err = csr.candBucketIndices(e.candAddr)
   131  			if i == 0 {
   132  				require.Equal(state.ErrStateNotExist, errors.Cause(err))
   133  			}
   134  
   135  			// put voter bucket index
   136  			require.NoError(csm.putVoterBucketIndex(e.voterAddr, e.index))
   137  			bis, _, err := csr.voterBucketIndices(e.voterAddr)
   138  			require.NoError(err)
   139  			bucketIndices := *bis
   140  			require.Equal(e.voterIndexSize, len(bucketIndices))
   141  			require.Equal(bucketIndices[e.voterIndexSize-1], e.index)
   142  
   143  			// put candidate bucket index
   144  			require.NoError(csm.putCandBucketIndex(e.candAddr, e.index))
   145  			bis, _, err = csr.candBucketIndices(e.candAddr)
   146  			require.NoError(err)
   147  			bucketIndices = *bis
   148  			require.Equal(e.candIndexSize, len(bucketIndices))
   149  			require.Equal(bucketIndices[e.candIndexSize-1], e.index)
   150  		}
   151  
   152  		for _, e := range tests {
   153  			// delete voter bucket index
   154  			require.NoError(csm.delVoterBucketIndex(e.voterAddr, e.index))
   155  			bis, _, err := csr.voterBucketIndices(e.voterAddr)
   156  			if e.voterIndexSize != indexSize {
   157  				bucketIndices := *bis
   158  				require.Equal(indexSize-e.voterIndexSize, len(bucketIndices))
   159  			} else {
   160  				require.Equal(state.ErrStateNotExist, errors.Cause(err))
   161  			}
   162  
   163  			// delete candidate bucket index
   164  			require.NoError(csm.delCandBucketIndex(e.candAddr, e.index))
   165  			bis, _, err = csr.candBucketIndices(e.candAddr)
   166  			if e.candIndexSize != indexSize {
   167  				bucketIndices := *bis
   168  				require.Equal(indexSize-e.candIndexSize, len(bucketIndices))
   169  			} else {
   170  				require.Equal(state.ErrStateNotExist, errors.Cause(err))
   171  			}
   172  		}
   173  	}
   174  
   175  	t.Run("test put and get bucket index", testGetPut)
   176  }