Browse Source

rpc: move evidence tests to shared fixtures (#7119)

This is follow on to the work in #7112.
pull/7122/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
164de91842
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 63 deletions
  1. +0
    -63
      rpc/client/evidence_test.go
  2. +55
    -0
      rpc/client/rpc_test.go
  3. +1
    -0
      rpc/test/helpers.go

+ 0
- 63
rpc/client/evidence_test.go View File

@ -1,17 +1,12 @@
package client_test
import (
"bytes"
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/tmhash"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/privval"
@ -113,64 +108,6 @@ func makeEvidences(
return correct, fakes
}
func TestBroadcastEvidence_DuplicateVoteEvidence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
n, config := NodeSuite(t)
chainID := config.ChainID()
pv, err := privval.LoadOrGenFilePV(config.PrivValidator.KeyFile(), config.PrivValidator.StateFile())
require.NoError(t, err)
for i, c := range GetClients(t, n, config) {
correct, fakes := makeEvidences(t, pv, chainID)
t.Logf("client %d", i)
// make sure that the node has produced enough blocks
waitForBlock(ctx, t, c, 2)
result, err := c.BroadcastEvidence(ctx, correct)
require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
status, err := c.Status(ctx)
require.NoError(t, err)
err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
require.NoError(t, err)
ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
rawpub := ed25519pub.Bytes()
result2, err := c.ABCIQuery(ctx, "/val", rawpub)
require.NoError(t, err)
qres := result2.Response
require.True(t, qres.IsOK())
var v abci.ValidatorUpdate
err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
require.NoError(t, err, "Error reading query result, value %v", qres.Value)
pk, err := encoding.PubKeyFromProto(v.PubKey)
require.NoError(t, err)
require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
for _, fake := range fakes {
_, err := c.BroadcastEvidence(ctx, fake)
require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
}
}
}
func TestBroadcastEmptyEvidence(t *testing.T) {
n, conf := NodeSuite(t)
for _, c := range GetClients(t, n, conf) {
_, err := c.BroadcastEvidence(context.Background(), nil)
assert.Error(t, err)
}
}
func waitForBlock(ctx context.Context, t *testing.T, c client.Client, height int64) {
timer := time.NewTimer(0 * time.Millisecond)
defer timer.Stop()


+ 55
- 0
rpc/client/rpc_test.go View File

@ -1,6 +1,7 @@
package client_test
import (
"bytes"
"context"
"encoding/base64"
"fmt"
@ -16,11 +17,14 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/internal/mempool"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmmath "github.com/tendermint/tendermint/libs/math"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
rpclocal "github.com/tendermint/tendermint/rpc/client/local"
@ -193,8 +197,13 @@ func TestClientMethodCalls(t *testing.T) {
defer cancel()
n, conf := NodeSuite(t)
// for broadcast tx tests
pool := getMempool(t, n)
// for evidence tests
pv, err := privval.LoadOrGenFilePV(conf.PrivValidator.KeyFile(), conf.PrivValidator.StateFile())
require.NoError(t, err)
for i, c := range GetClients(t, n, conf) {
t.Run(fmt.Sprintf("%T", c), func(t *testing.T) {
t.Run("Status", func(t *testing.T) {
@ -503,6 +512,52 @@ func TestClientMethodCalls(t *testing.T) {
testTxEventsSent(ctx, t, "sync", c)
})
})
t.Run("Evidence", func(t *testing.T) {
t.Run("BraodcastDuplicateVote", func(t *testing.T) {
chainID := conf.ChainID()
correct, fakes := makeEvidences(t, pv, chainID)
// make sure that the node has produced enough blocks
waitForBlock(ctx, t, c, 2)
result, err := c.BroadcastEvidence(ctx, correct)
require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
status, err := c.Status(ctx)
require.NoError(t, err)
err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
require.NoError(t, err)
ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
rawpub := ed25519pub.Bytes()
result2, err := c.ABCIQuery(ctx, "/val", rawpub)
require.NoError(t, err)
qres := result2.Response
require.True(t, qres.IsOK())
var v abci.ValidatorUpdate
err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
require.NoError(t, err, "Error reading query result, value %v", qres.Value)
pk, err := encoding.PubKeyFromProto(v.PubKey)
require.NoError(t, err)
require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
for _, fake := range fakes {
_, err := c.BroadcastEvidence(ctx, fake)
require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
}
})
t.Run("BroadcastEmpty", func(t *testing.T) {
_, err := c.BroadcastEvidence(ctx, nil)
assert.Error(t, err)
})
})
})
}
}


+ 1
- 0
rpc/test/helpers.go View File

@ -73,6 +73,7 @@ func CreateConfig(testName string) *config.Config {
tm, rpc, grpc := makeAddrs()
c.P2P.ListenAddress = tm
c.RPC.ListenAddress = rpc
c.Consensus.WalPath = "rpc-test"
c.RPC.CORSAllowedOrigins = []string{"https://tendermint.com/"}
c.RPC.GRPCListenAddress = grpc
return c


Loading…
Cancel
Save