github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/privval/grpc/server_test.go (about)

     1  package grpc_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/ari-anchor/sei-tendermint/crypto"
    12  	"github.com/ari-anchor/sei-tendermint/libs/log"
    13  	tmrand "github.com/ari-anchor/sei-tendermint/libs/rand"
    14  	tmgrpc "github.com/ari-anchor/sei-tendermint/privval/grpc"
    15  	privvalproto "github.com/ari-anchor/sei-tendermint/proto/tendermint/privval"
    16  	tmproto "github.com/ari-anchor/sei-tendermint/proto/tendermint/types"
    17  	"github.com/ari-anchor/sei-tendermint/types"
    18  )
    19  
    20  const ChainID = "123"
    21  
    22  func TestGetPubKey(t *testing.T) {
    23  
    24  	testCases := []struct {
    25  		name string
    26  		pv   types.PrivValidator
    27  		err  bool
    28  	}{
    29  		{name: "valid", pv: types.NewMockPV(), err: false},
    30  		{name: "error on pubkey", pv: types.NewErroringMockPV(), err: true},
    31  	}
    32  
    33  	for _, tc := range testCases {
    34  		tc := tc
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			ctx, cancel := context.WithCancel(context.Background())
    37  			defer cancel()
    38  			logger := log.NewTestingLogger(t)
    39  
    40  			s := tmgrpc.NewSignerServer(logger, ChainID, tc.pv)
    41  
    42  			req := &privvalproto.PubKeyRequest{ChainId: ChainID}
    43  			resp, err := s.GetPubKey(ctx, req)
    44  			if tc.err {
    45  				require.Error(t, err)
    46  			} else {
    47  				pk, err := tc.pv.GetPubKey(ctx)
    48  				require.NoError(t, err)
    49  				assert.Equal(t, resp.PubKey.GetEd25519(), pk.Bytes())
    50  			}
    51  		})
    52  	}
    53  
    54  }
    55  
    56  func TestSignVote(t *testing.T) {
    57  
    58  	ts := time.Now()
    59  	hash := tmrand.Bytes(crypto.HashSize)
    60  	valAddr := tmrand.Bytes(crypto.AddressSize)
    61  
    62  	testCases := []struct {
    63  		name       string
    64  		pv         types.PrivValidator
    65  		have, want *types.Vote
    66  		err        bool
    67  	}{
    68  		{name: "valid", pv: types.NewMockPV(), have: &types.Vote{
    69  			Type:             tmproto.PrecommitType,
    70  			Height:           1,
    71  			Round:            2,
    72  			BlockID:          types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
    73  			Timestamp:        ts,
    74  			ValidatorAddress: valAddr,
    75  			ValidatorIndex:   1,
    76  		}, want: &types.Vote{
    77  			Type:             tmproto.PrecommitType,
    78  			Height:           1,
    79  			Round:            2,
    80  			BlockID:          types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
    81  			Timestamp:        ts,
    82  			ValidatorAddress: valAddr,
    83  			ValidatorIndex:   1,
    84  		},
    85  			err: false},
    86  		{name: "invalid vote", pv: types.NewErroringMockPV(), have: &types.Vote{
    87  			Type:             tmproto.PrecommitType,
    88  			Height:           1,
    89  			Round:            2,
    90  			BlockID:          types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
    91  			Timestamp:        ts,
    92  			ValidatorAddress: valAddr,
    93  			ValidatorIndex:   1,
    94  			Signature:        []byte("signed"),
    95  		}, want: &types.Vote{
    96  			Type:             tmproto.PrecommitType,
    97  			Height:           1,
    98  			Round:            2,
    99  			BlockID:          types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
   100  			Timestamp:        ts,
   101  			ValidatorAddress: valAddr,
   102  			ValidatorIndex:   1,
   103  			Signature:        []byte("signed"),
   104  		},
   105  			err: true},
   106  	}
   107  
   108  	for _, tc := range testCases {
   109  		tc := tc
   110  		t.Run(tc.name, func(t *testing.T) {
   111  			ctx, cancel := context.WithCancel(context.Background())
   112  			defer cancel()
   113  			logger := log.NewTestingLogger(t)
   114  
   115  			s := tmgrpc.NewSignerServer(logger, ChainID, tc.pv)
   116  
   117  			req := &privvalproto.SignVoteRequest{ChainId: ChainID, Vote: tc.have.ToProto()}
   118  			resp, err := s.SignVote(ctx, req)
   119  			if tc.err {
   120  				require.Error(t, err)
   121  			} else {
   122  				pbVote := tc.want.ToProto()
   123  
   124  				require.NoError(t, tc.pv.SignVote(ctx, ChainID, pbVote))
   125  
   126  				assert.Equal(t, pbVote.Signature, resp.Vote.Signature)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestSignProposal(t *testing.T) {
   133  
   134  	ts := time.Now()
   135  	hash := tmrand.Bytes(crypto.HashSize)
   136  
   137  	testCases := []struct {
   138  		name       string
   139  		pv         types.PrivValidator
   140  		have, want *types.Proposal
   141  		err        bool
   142  	}{
   143  		{name: "valid", pv: types.NewMockPV(), have: &types.Proposal{
   144  			Type:      tmproto.ProposalType,
   145  			Height:    1,
   146  			Round:     2,
   147  			POLRound:  2,
   148  			BlockID:   types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
   149  			Timestamp: ts,
   150  		}, want: &types.Proposal{
   151  			Type:      tmproto.ProposalType,
   152  			Height:    1,
   153  			Round:     2,
   154  			POLRound:  2,
   155  			BlockID:   types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
   156  			Timestamp: ts,
   157  		},
   158  			err: false},
   159  		{name: "invalid proposal", pv: types.NewErroringMockPV(), have: &types.Proposal{
   160  			Type:      tmproto.ProposalType,
   161  			Height:    1,
   162  			Round:     2,
   163  			POLRound:  2,
   164  			BlockID:   types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
   165  			Timestamp: ts,
   166  			Signature: []byte("signed"),
   167  		}, want: &types.Proposal{
   168  			Type:      tmproto.ProposalType,
   169  			Height:    1,
   170  			Round:     2,
   171  			POLRound:  2,
   172  			BlockID:   types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
   173  			Timestamp: ts,
   174  			Signature: []byte("signed"),
   175  		},
   176  			err: true},
   177  	}
   178  
   179  	for _, tc := range testCases {
   180  		tc := tc
   181  		t.Run(tc.name, func(t *testing.T) {
   182  			ctx, cancel := context.WithCancel(context.Background())
   183  			defer cancel()
   184  			logger := log.NewTestingLogger(t)
   185  
   186  			s := tmgrpc.NewSignerServer(logger, ChainID, tc.pv)
   187  
   188  			req := &privvalproto.SignProposalRequest{ChainId: ChainID, Proposal: tc.have.ToProto()}
   189  			resp, err := s.SignProposal(ctx, req)
   190  			if tc.err {
   191  				require.Error(t, err)
   192  			} else {
   193  				pbProposal := tc.want.ToProto()
   194  				require.NoError(t, tc.pv.SignProposal(ctx, ChainID, pbProposal))
   195  				assert.Equal(t, pbProposal.Signature, resp.Proposal.Signature)
   196  			}
   197  		})
   198  	}
   199  }