github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/simulation/decoder_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
    11  	sdk "github.com/cosmos/cosmos-sdk/types"
    12  	"github.com/cosmos/cosmos-sdk/types/kv"
    13  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    14  	"github.com/cosmos/cosmos-sdk/x/slashing"
    15  	"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
    16  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    17  )
    18  
    19  var (
    20  	delPk1    = ed25519.GenPrivKey().PubKey()
    21  	delAddr1  = sdk.AccAddress(delPk1.Address())
    22  	consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
    23  )
    24  
    25  func TestDecodeStore(t *testing.T) {
    26  	encodingConfig := moduletestutil.MakeTestEncodingConfig(slashing.AppModuleBasic{})
    27  	cdc := encodingConfig.Codec
    28  	dec := simulation.NewDecodeStore(cdc)
    29  
    30  	info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0)
    31  	missed := []byte{1} // we want to display the bytes for simulation diffs
    32  	bz, err := cdc.MarshalInterface(delPk1)
    33  	require.NoError(t, err)
    34  
    35  	kvPairs := kv.Pairs{
    36  		Pairs: []kv.Pair{
    37  			{Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshal(&info)},
    38  			{Key: types.ValidatorMissedBlockBitmapKey(consAddr1, 6), Value: missed},
    39  			{Key: types.AddrPubkeyRelationKey(delAddr1), Value: bz},
    40  			{Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic
    41  		},
    42  	}
    43  
    44  	tests := []struct {
    45  		name        string
    46  		expectedLog string
    47  		panics      bool
    48  	}{
    49  		{"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info), false},
    50  		{"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v\n", missed, missed), false},
    51  		{"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1), false},
    52  		{"other", "", true},
    53  	}
    54  	for i, tt := range tests {
    55  		i, tt := i, tt
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			if tt.panics {
    58  				require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name)
    59  			} else {
    60  				require.Contains(t, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.expectedLog, tt.name)
    61  			}
    62  		})
    63  	}
    64  }