github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/04-channel/simulation/decoder_test.go (about)

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