github.com/lino-network/lino@v0.6.11/x/validator/model/storage_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 6 "github.com/cosmos/cosmos-sdk/store" 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 "github.com/lino-network/lino/types" 9 "github.com/stretchr/testify/assert" 10 abci "github.com/tendermint/tendermint/abci/types" 11 "github.com/tendermint/tendermint/crypto/secp256k1" 12 "github.com/tendermint/tendermint/libs/log" 13 dbm "github.com/tendermint/tm-db" 14 ) 15 16 var ( 17 TestKVStoreKey = sdk.NewKVStoreKey("validator") 18 ) 19 20 func setup(t *testing.T) (sdk.Context, ValidatorStorage) { 21 db := dbm.NewMemDB() 22 ms := store.NewCommitMultiStore(db) 23 ms.MountStoreWithDB(TestKVStoreKey, sdk.StoreTypeIAVL, db) 24 err := ms.LoadLatestVersion() 25 if err != nil { 26 panic(err) 27 } 28 ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger()) 29 vs := NewValidatorStorage(TestKVStoreKey) 30 return ctx, vs 31 } 32 33 func TestValidator(t *testing.T) { 34 ctx, vs := setup(t) 35 36 priv := secp256k1.GenPrivKey() 37 testCases := []struct { 38 testName string 39 power int64 40 user types.AccountKey 41 votes types.Coin 42 }{ 43 { 44 testName: "user as validator", 45 power: 1000, 46 user: types.AccountKey("user"), 47 votes: types.NewCoinFromInt64(10), 48 }, 49 { 50 testName: "user as validator again", 51 power: 10000, 52 user: types.AccountKey("user"), 53 votes: types.NewCoinFromInt64(10), 54 }, 55 { 56 testName: "user2 as validator", 57 power: 1, 58 user: types.AccountKey("user2"), 59 votes: types.NewCoinFromInt64(10), 60 }, 61 } 62 63 for _, tc := range testCases { 64 validator := Validator{ 65 ABCIValidator: abci.Validator{ 66 Address: priv.PubKey().Address(), 67 Power: 1000}, 68 Username: tc.user, 69 ReceivedVotes: tc.votes, 70 } 71 vs.SetValidator(ctx, tc.user, &validator) 72 73 valPtr, err := vs.GetValidator(ctx, tc.user) 74 if err != nil { 75 t.Errorf("%s: failed to get validator, got err %v", tc.testName, err) 76 } 77 if !assert.Equal(t, validator, *valPtr) { 78 t.Errorf("%s: diff result, got %v, want %v", tc.testName, *valPtr, validator) 79 } 80 } 81 } 82 83 func TestValidatorList(t *testing.T) { 84 ctx, vs := setup(t) 85 86 testCases := []struct { 87 testName string 88 valList ValidatorList 89 }{ 90 { 91 testName: "normal case", 92 valList: ValidatorList{ 93 Oncall: []types.AccountKey{ 94 types.AccountKey("user1"), 95 }, 96 Standby: []types.AccountKey{ 97 types.AccountKey("user2"), 98 }, 99 Candidates: []types.AccountKey{ 100 types.AccountKey("user2"), 101 }, 102 Jail: []types.AccountKey{ 103 types.AccountKey("user2"), 104 }, 105 PreBlockValidators: []types.AccountKey{ 106 types.AccountKey("user2"), 107 }, 108 LowestOncallVotes: types.NewCoinFromInt64(100), 109 LowestOncall: types.AccountKey("user2"), 110 LowestStandbyVotes: types.NewCoinFromInt64(100), 111 LowestStandby: types.AccountKey("user2"), 112 }, 113 }, 114 } 115 116 for _, tc := range testCases { 117 vs.SetValidatorList(ctx, &tc.valList) 118 119 valListPtr := vs.GetValidatorList(ctx) 120 if !assert.Equal(t, tc.valList, *valListPtr) { 121 t.Errorf("%s: diff result, got %v, want %v", tc.testName, *valListPtr, tc.valList) 122 } 123 } 124 } 125 126 func TestElectionVoteList(t *testing.T) { 127 ctx, vs := setup(t) 128 129 testCases := []struct { 130 testName string 131 lst ElectionVoteList 132 user types.AccountKey 133 }{ 134 { 135 testName: "normal case", 136 user: types.AccountKey("user"), 137 lst: ElectionVoteList{ 138 ElectionVotes: []ElectionVote{ 139 { 140 ValidatorName: types.AccountKey("test"), 141 Vote: types.NewCoinFromInt64(100), 142 }, 143 }, 144 }, 145 }, 146 } 147 148 for _, tc := range testCases { 149 vs.SetElectionVoteList(ctx, tc.user, &tc.lst) 150 151 lstPtr := vs.GetElectionVoteList(ctx, tc.user) 152 if !assert.Equal(t, tc.lst, *lstPtr) { 153 t.Errorf("%s: diff result, got %v, want %v", tc.testName, *lstPtr, tc.lst) 154 } 155 } 156 }