github.com/okex/exchain@v1.8.0/libs/ibc-go/modules/core/04-channel/simulation/decoder_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"fmt"
     5  	tmkv "github.com/okex/exchain/libs/tendermint/libs/kv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
    11  	"github.com/okex/exchain/libs/cosmos-sdk/types/kv"
    12  	"github.com/okex/exchain/libs/ibc-go/modules/core/04-channel/simulation"
    13  	"github.com/okex/exchain/libs/ibc-go/modules/core/04-channel/types"
    14  	host "github.com/okex/exchain/libs/ibc-go/modules/core/24-host"
    15  	"github.com/okex/exchain/libs/ibc-go/testing/simapp"
    16  )
    17  
    18  func TestDecodeStore(t *testing.T) {
    19  	app := simapp.Setup(false)
    20  	cdc := app.AppCodec()
    21  
    22  	channelID := "channelidone"
    23  	portID := "portidone"
    24  
    25  	channel := types.Channel{
    26  		State:   types.OPEN,
    27  		Version: "1.0",
    28  	}
    29  
    30  	bz := []byte{0x1, 0x2, 0x3}
    31  
    32  	kvPairs := kv.Pairs{
    33  		Pairs: []kv.Pair{
    34  			{
    35  				Key:   host.ChannelKey(portID, channelID),
    36  				Value: cdc.GetProtocMarshal().MustMarshalBinaryBare(&channel),
    37  			},
    38  			{
    39  				Key:   host.NextSequenceSendKey(portID, channelID),
    40  				Value: sdk.Uint64ToBigEndian(1),
    41  			},
    42  			{
    43  				Key:   host.NextSequenceRecvKey(portID, channelID),
    44  				Value: sdk.Uint64ToBigEndian(1),
    45  			},
    46  			{
    47  				Key:   host.NextSequenceAckKey(portID, channelID),
    48  				Value: sdk.Uint64ToBigEndian(1),
    49  			},
    50  			{
    51  				Key:   host.PacketCommitmentKey(portID, channelID, 1),
    52  				Value: bz,
    53  			},
    54  			{
    55  				Key:   host.PacketAcknowledgementKey(portID, channelID, 1),
    56  				Value: bz,
    57  			},
    58  			{
    59  				Key:   []byte{0x99},
    60  				Value: []byte{0x99},
    61  			},
    62  		},
    63  	}
    64  	tests := []struct {
    65  		name        string
    66  		expectedLog string
    67  	}{
    68  		{"Channel", fmt.Sprintf("Channel A: %v\nChannel B: %v", channel, channel)},
    69  		{"NextSeqSend", "NextSeqSend A: 1\nNextSeqSend B: 1"},
    70  		{"NextSeqRecv", "NextSeqRecv A: 1\nNextSeqRecv B: 1"},
    71  		{"NextSeqAck", "NextSeqAck A: 1\nNextSeqAck B: 1"},
    72  		{"CommitmentHash", fmt.Sprintf("CommitmentHash A: %X\nCommitmentHash B: %X", bz, bz)},
    73  		{"AckHash", fmt.Sprintf("AckHash A: %X\nAckHash B: %X", bz, bz)},
    74  		{"other", ""},
    75  	}
    76  
    77  	for i, tt := range tests {
    78  		i, tt := i, tt
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			// res, found := simulation.NewDecodeStore(cdc, kvPairs.Pairs[i], kvPairs.Pairs[i])
    81  			kvA := tmkv.Pair{
    82  				Key:   kvPairs.Pairs[i].GetKey(),
    83  				Value: kvPairs.Pairs[i].GetValue(),
    84  			}
    85  			res, found := simulation.NewDecodeStore(cdc, kvA, kvA)
    86  			if i == len(tests)-1 {
    87  				require.False(t, found, string(kvPairs.Pairs[i].Key))
    88  				require.Empty(t, res, string(kvPairs.Pairs[i].Key))
    89  			} else {
    90  				require.True(t, found, string(kvPairs.Pairs[i].Key))
    91  				require.Equal(t, tt.expectedLog, res, string(kvPairs.Pairs[i].Key))
    92  			}
    93  		})
    94  	}
    95  }