github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/proto/tendermint/statesync/message_test.go (about)

     1  package statesync_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/gogo/protobuf/proto"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/ari-anchor/sei-tendermint/crypto/ed25519"
    12  	ssproto "github.com/ari-anchor/sei-tendermint/proto/tendermint/statesync"
    13  	tmproto "github.com/ari-anchor/sei-tendermint/proto/tendermint/types"
    14  )
    15  
    16  func TestValidateMsg(t *testing.T) {
    17  	testcases := map[string]struct {
    18  		msg      proto.Message
    19  		validMsg bool
    20  		valid    bool
    21  	}{
    22  		"nil":       {nil, false, false},
    23  		"unrelated": {&tmproto.Block{}, false, false},
    24  
    25  		"ChunkRequest valid":    {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, true, true},
    26  		"ChunkRequest 0 height": {&ssproto.ChunkRequest{Height: 0, Format: 1, Index: 1}, true, false},
    27  		"ChunkRequest 0 format": {&ssproto.ChunkRequest{Height: 1, Format: 0, Index: 1}, true, true},
    28  		"ChunkRequest 0 chunk":  {&ssproto.ChunkRequest{Height: 1, Format: 1, Index: 0}, true, true},
    29  
    30  		"ChunkResponse valid": {
    31  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1}},
    32  			true,
    33  			true,
    34  		},
    35  		"ChunkResponse 0 height": {
    36  			&ssproto.ChunkResponse{Height: 0, Format: 1, Index: 1, Chunk: []byte{1}},
    37  			true,
    38  			false,
    39  		},
    40  		"ChunkResponse 0 format": {
    41  			&ssproto.ChunkResponse{Height: 1, Format: 0, Index: 1, Chunk: []byte{1}},
    42  			true,
    43  			true,
    44  		},
    45  		"ChunkResponse 0 chunk": {
    46  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 0, Chunk: []byte{1}},
    47  			true,
    48  			true,
    49  		},
    50  		"ChunkResponse empty body": {
    51  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{}},
    52  			true,
    53  			true,
    54  		},
    55  		"ChunkResponse nil body": {
    56  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil},
    57  			true,
    58  			false,
    59  		},
    60  		"ChunkResponse missing": {
    61  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true},
    62  			true,
    63  			true,
    64  		},
    65  		"ChunkResponse missing with empty": {
    66  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{}},
    67  			true,
    68  			true,
    69  		},
    70  		"ChunkResponse missing with body": {
    71  			&ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true, Chunk: []byte{1}},
    72  			true,
    73  			false,
    74  		},
    75  
    76  		"SnapshotsRequest valid": {&ssproto.SnapshotsRequest{}, true, true},
    77  
    78  		"SnapshotsResponse valid": {
    79  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{1}},
    80  			true,
    81  			true,
    82  		},
    83  		"SnapshotsResponse 0 height": {
    84  			&ssproto.SnapshotsResponse{Height: 0, Format: 1, Chunks: 2, Hash: []byte{1}},
    85  			true,
    86  			false,
    87  		},
    88  		"SnapshotsResponse 0 format": {
    89  			&ssproto.SnapshotsResponse{Height: 1, Format: 0, Chunks: 2, Hash: []byte{1}},
    90  			true,
    91  			true,
    92  		},
    93  		"SnapshotsResponse 0 chunks": {
    94  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Hash: []byte{1}},
    95  			true,
    96  			false,
    97  		},
    98  		"SnapshotsResponse no hash": {
    99  			&ssproto.SnapshotsResponse{Height: 1, Format: 1, Chunks: 2, Hash: []byte{}},
   100  			true,
   101  			false,
   102  		},
   103  	}
   104  
   105  	for name, tc := range testcases {
   106  		tc := tc
   107  		t.Run(name, func(t *testing.T) {
   108  			msg := new(ssproto.Message)
   109  
   110  			if tc.validMsg {
   111  				require.NoError(t, msg.Wrap(tc.msg))
   112  			} else {
   113  				require.Error(t, msg.Wrap(tc.msg))
   114  			}
   115  
   116  			if tc.valid {
   117  				require.NoError(t, msg.Validate())
   118  			} else {
   119  				require.Error(t, msg.Validate())
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  func TestStateSyncVectors(t *testing.T) {
   126  	testCases := []struct {
   127  		testName string
   128  		msg      proto.Message
   129  		expBytes string
   130  	}{
   131  		{
   132  			"SnapshotsRequest",
   133  			&ssproto.SnapshotsRequest{},
   134  			"0a00",
   135  		},
   136  		{
   137  			"SnapshotsResponse",
   138  			&ssproto.SnapshotsResponse{
   139  				Height:   1,
   140  				Format:   2,
   141  				Chunks:   3,
   142  				Hash:     []byte("chuck hash"),
   143  				Metadata: []byte("snapshot metadata"),
   144  			},
   145  			"1225080110021803220a636875636b20686173682a11736e617073686f74206d65746164617461",
   146  		},
   147  		{
   148  			"ChunkRequest",
   149  			&ssproto.ChunkRequest{
   150  				Height: 1,
   151  				Format: 2,
   152  				Index:  3,
   153  			},
   154  			"1a06080110021803",
   155  		},
   156  		{
   157  			"ChunkResponse",
   158  			&ssproto.ChunkResponse{
   159  				Height: 1,
   160  				Format: 2,
   161  				Index:  3,
   162  				Chunk:  []byte("it's a chunk"),
   163  			},
   164  			"2214080110021803220c697427732061206368756e6b",
   165  		},
   166  		{
   167  			"LightBlockRequest",
   168  			&ssproto.LightBlockRequest{
   169  				Height: 100,
   170  			},
   171  			"2a020864",
   172  		},
   173  		{
   174  			"LightBlockResponse",
   175  			&ssproto.LightBlockResponse{
   176  				LightBlock: nil,
   177  			},
   178  			"3200",
   179  		},
   180  		{
   181  			"ParamsRequest",
   182  			&ssproto.ParamsRequest{
   183  				Height: 9001,
   184  			},
   185  			"3a0308a946",
   186  		},
   187  		{
   188  			"ParamsResponse",
   189  			&ssproto.ParamsResponse{
   190  				Height: 9001,
   191  				ConsensusParams: tmproto.ConsensusParams{
   192  					Block: &tmproto.BlockParams{
   193  						MaxBytes: 10,
   194  						MaxGas:   20,
   195  					},
   196  					Evidence: &tmproto.EvidenceParams{
   197  						MaxAgeNumBlocks: 10,
   198  						MaxAgeDuration:  300,
   199  						MaxBytes:        100,
   200  					},
   201  					Validator: &tmproto.ValidatorParams{
   202  						PubKeyTypes: []string{ed25519.KeyType},
   203  					},
   204  					Version: &tmproto.VersionParams{
   205  						AppVersion: 11,
   206  					},
   207  					Synchrony: &tmproto.SynchronyParams{
   208  						MessageDelay: durationPtr(550),
   209  						Precision:    durationPtr(90),
   210  					},
   211  				},
   212  			},
   213  			"423008a946122b0a04080a10141209080a120310ac0218641a090a07656432353531392202080b2a090a0310a6041202105a",
   214  		},
   215  	}
   216  
   217  	for _, tc := range testCases {
   218  		tc := tc
   219  
   220  		msg := new(ssproto.Message)
   221  		require.NoError(t, msg.Wrap(tc.msg))
   222  
   223  		bz, err := msg.Marshal()
   224  		require.NoError(t, err)
   225  		require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
   226  	}
   227  }
   228  
   229  func durationPtr(t time.Duration) *time.Duration {
   230  	return &t
   231  }