github.com/DFWallet/tendermint-cosmos@v0.0.2/rpc/client/evidence_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	abci "github.com/DFWallet/tendermint-cosmos/abci/types"
    13  	"github.com/DFWallet/tendermint-cosmos/crypto/ed25519"
    14  	cryptoenc "github.com/DFWallet/tendermint-cosmos/crypto/encoding"
    15  	"github.com/DFWallet/tendermint-cosmos/crypto/tmhash"
    16  	tmrand "github.com/DFWallet/tendermint-cosmos/libs/rand"
    17  	"github.com/DFWallet/tendermint-cosmos/privval"
    18  	tmproto "github.com/DFWallet/tendermint-cosmos/proto/tendermint/types"
    19  	"github.com/DFWallet/tendermint-cosmos/rpc/client"
    20  	rpctest "github.com/DFWallet/tendermint-cosmos/rpc/test"
    21  	"github.com/DFWallet/tendermint-cosmos/types"
    22  )
    23  
    24  // For some reason the empty node used in tests has a time of
    25  // 2018-10-10 08:20:13.695936996 +0000 UTC
    26  // this is because the test genesis time is set here
    27  // so in order to validate evidence we need evidence to be the same time
    28  var defaultTestTime = time.Date(2018, 10, 10, 8, 20, 13, 695936996, time.UTC)
    29  
    30  func newEvidence(t *testing.T, val *privval.FilePV,
    31  	vote *types.Vote, vote2 *types.Vote,
    32  	chainID string) *types.DuplicateVoteEvidence {
    33  
    34  	var err error
    35  
    36  	v := vote.ToProto()
    37  	v2 := vote2.ToProto()
    38  
    39  	vote.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v))
    40  	require.NoError(t, err)
    41  
    42  	vote2.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v2))
    43  	require.NoError(t, err)
    44  
    45  	validator := types.NewValidator(val.Key.PubKey, 10)
    46  	valSet := types.NewValidatorSet([]*types.Validator{validator})
    47  
    48  	return types.NewDuplicateVoteEvidence(vote, vote2, defaultTestTime, valSet)
    49  }
    50  
    51  func makeEvidences(
    52  	t *testing.T,
    53  	val *privval.FilePV,
    54  	chainID string,
    55  ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
    56  	vote := types.Vote{
    57  		ValidatorAddress: val.Key.Address,
    58  		ValidatorIndex:   0,
    59  		Height:           1,
    60  		Round:            0,
    61  		Type:             tmproto.PrevoteType,
    62  		Timestamp:        defaultTestTime,
    63  		BlockID: types.BlockID{
    64  			Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
    65  			PartSetHeader: types.PartSetHeader{
    66  				Total: 1000,
    67  				Hash:  tmhash.Sum([]byte("partset")),
    68  			},
    69  		},
    70  	}
    71  
    72  	vote2 := vote
    73  	vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
    74  	correct = newEvidence(t, val, &vote, &vote2, chainID)
    75  
    76  	fakes = make([]*types.DuplicateVoteEvidence, 0)
    77  
    78  	// different address
    79  	{
    80  		v := vote2
    81  		v.ValidatorAddress = []byte("some_address")
    82  		fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
    83  	}
    84  
    85  	// different height
    86  	{
    87  		v := vote2
    88  		v.Height = vote.Height + 1
    89  		fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
    90  	}
    91  
    92  	// different round
    93  	{
    94  		v := vote2
    95  		v.Round = vote.Round + 1
    96  		fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
    97  	}
    98  
    99  	// different type
   100  	{
   101  		v := vote2
   102  		v.Type = tmproto.PrecommitType
   103  		fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
   104  	}
   105  
   106  	// exactly same vote
   107  	{
   108  		v := vote
   109  		fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
   110  	}
   111  
   112  	return correct, fakes
   113  }
   114  
   115  func TestBroadcastEvidence_DuplicateVoteEvidence(t *testing.T) {
   116  	var (
   117  		config  = rpctest.GetConfig()
   118  		chainID = config.ChainID()
   119  		pv      = privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
   120  	)
   121  
   122  	for i, c := range GetClients() {
   123  		correct, fakes := makeEvidences(t, pv, chainID)
   124  		t.Logf("client %d", i)
   125  
   126  		result, err := c.BroadcastEvidence(context.Background(), correct)
   127  		require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
   128  		assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
   129  
   130  		status, err := c.Status(context.Background())
   131  		require.NoError(t, err)
   132  		err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
   133  		require.NoError(t, err)
   134  
   135  		ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
   136  		rawpub := ed25519pub.Bytes()
   137  		result2, err := c.ABCIQuery(context.Background(), "/val", rawpub)
   138  		require.NoError(t, err)
   139  		qres := result2.Response
   140  		require.True(t, qres.IsOK())
   141  
   142  		var v abci.ValidatorUpdate
   143  		err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
   144  		require.NoError(t, err, "Error reading query result, value %v", qres.Value)
   145  
   146  		pk, err := cryptoenc.PubKeyFromProto(v.PubKey)
   147  		require.NoError(t, err)
   148  
   149  		require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
   150  		require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
   151  
   152  		for _, fake := range fakes {
   153  			_, err := c.BroadcastEvidence(context.Background(), fake)
   154  			require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
   155  		}
   156  	}
   157  }
   158  
   159  func TestBroadcastEmptyEvidence(t *testing.T) {
   160  	for _, c := range GetClients() {
   161  		_, err := c.BroadcastEvidence(context.Background(), nil)
   162  		assert.Error(t, err)
   163  	}
   164  }