rpc/client: split out client packages (#4628)

* rpc/client: initial split into directories

* lite2: split out test package

* rpc/client: simplify client constructurs

* updated docs

* updated changelog
This commit is contained in:
Erik Grinaker
2020-04-02 15:25:30 +02:00
committed by GitHub
parent 6c88d2ba1f
commit fdf9c7ae64
19 changed files with 159 additions and 145 deletions

View File

@@ -1,4 +1,4 @@
package lite
package lite_test
import (
"testing"
@@ -7,6 +7,7 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/tendermint/tendermint/libs/log"
lite "github.com/tendermint/tendermint/lite2"
"github.com/tendermint/tendermint/lite2/provider"
mockp "github.com/tendermint/tendermint/lite2/provider/mock"
dbs "github.com/tendermint/tendermint/lite2/store/db"
@@ -25,9 +26,9 @@ var (
)
func BenchmarkSequence(b *testing.B) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 24 * time.Hour,
Height: 1,
Hash: genesisHeader.Hash(),
@@ -35,8 +36,8 @@ func BenchmarkSequence(b *testing.B) {
benchmarkFullNode,
[]provider.Provider{benchmarkFullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
SequentialVerification(),
lite.Logger(log.TestingLogger()),
lite.SequentialVerification(),
)
if err != nil {
b.Fatal(err)
@@ -52,9 +53,9 @@ func BenchmarkSequence(b *testing.B) {
}
func BenchmarkBisection(b *testing.B) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 24 * time.Hour,
Height: 1,
Hash: genesisHeader.Hash(),
@@ -62,7 +63,7 @@ func BenchmarkBisection(b *testing.B) {
benchmarkFullNode,
[]provider.Provider{benchmarkFullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
if err != nil {
b.Fatal(err)
@@ -79,9 +80,9 @@ func BenchmarkBisection(b *testing.B) {
func BenchmarkBackwards(b *testing.B) {
trustedHeader, _ := benchmarkFullNode.SignedHeader(0)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 24 * time.Hour,
Height: trustedHeader.Height,
Hash: trustedHeader.Hash(),
@@ -89,7 +90,7 @@ func BenchmarkBackwards(b *testing.B) {
benchmarkFullNode,
[]provider.Provider{benchmarkFullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
if err != nil {
b.Fatal(err)

View File

@@ -1,4 +1,4 @@
package lite
package lite_test
import (
"sync"
@@ -11,6 +11,7 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/tendermint/tendermint/libs/log"
lite "github.com/tendermint/tendermint/lite2"
"github.com/tendermint/tendermint/lite2/provider"
mockp "github.com/tendermint/tendermint/lite2/provider/mock"
dbs "github.com/tendermint/tendermint/lite2/store/db"
@@ -34,7 +35,7 @@ var (
h3 = keys.GenSignedHeaderLastBlockID(chainID, 3, bTime.Add(1*time.Hour), nil, vals, vals,
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys), types.BlockID{Hash: h2.Hash()})
trustPeriod = 4 * time.Hour
trustOptions = TrustOptions{
trustOptions = lite.TrustOptions{
Period: 4 * time.Hour,
Height: 1,
Hash: h1.Hash(),
@@ -140,7 +141,7 @@ func TestClient_SequentialVerification(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
mockp.New(
@@ -154,7 +155,7 @@ func TestClient_SequentialVerification(t *testing.T) {
tc.vals,
)},
dbs.New(dbm.NewMemDB(), chainID),
SequentialVerification(),
lite.SequentialVerification(),
)
if tc.initErr {
@@ -263,7 +264,7 @@ func TestClient_SkippingVerification(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
mockp.New(
@@ -277,7 +278,7 @@ func TestClient_SkippingVerification(t *testing.T) {
tc.vals,
)},
dbs.New(dbm.NewMemDB(), chainID),
SkippingVerification(DefaultTrustLevel),
lite.SkippingVerification(lite.DefaultTrustLevel),
)
if tc.initErr {
require.Error(t, err)
@@ -297,13 +298,13 @@ func TestClient_SkippingVerification(t *testing.T) {
}
func TestClient_Cleanup(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
_, err = c.TrustedHeader(1)
@@ -330,13 +331,13 @@ func TestClientRestoresTrustedHeaderAfterStartup1(t *testing.T) {
err := trustedStore.SaveSignedHeaderAndValidatorSet(h1, vals)
require.NoError(t, err)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -372,9 +373,9 @@ func TestClientRestoresTrustedHeaderAfterStartup1(t *testing.T) {
valSet,
)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 4 * time.Hour,
Height: 1,
Hash: header1.Hash(),
@@ -382,7 +383,7 @@ func TestClientRestoresTrustedHeaderAfterStartup1(t *testing.T) {
primary,
[]provider.Provider{primary},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -409,9 +410,9 @@ func TestClientRestoresTrustedHeaderAfterStartup2(t *testing.T) {
err := trustedStore.SaveSignedHeaderAndValidatorSet(h1, vals)
require.NoError(t, err)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 4 * time.Hour,
Height: 2,
Hash: h2.Hash(),
@@ -419,7 +420,7 @@ func TestClientRestoresTrustedHeaderAfterStartup2(t *testing.T) {
fullNode,
[]provider.Provider{fullNode},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -460,9 +461,9 @@ func TestClientRestoresTrustedHeaderAfterStartup2(t *testing.T) {
valSet,
)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 4 * time.Hour,
Height: 2,
Hash: diffHeader2.Hash(),
@@ -470,7 +471,7 @@ func TestClientRestoresTrustedHeaderAfterStartup2(t *testing.T) {
primary,
[]provider.Provider{primary},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -498,13 +499,13 @@ func TestClientRestoresTrustedHeaderAfterStartup3(t *testing.T) {
err = trustedStore.SaveSignedHeaderAndValidatorSet(h2, vals)
require.NoError(t, err)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -555,9 +556,9 @@ func TestClientRestoresTrustedHeaderAfterStartup3(t *testing.T) {
valSet,
)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 4 * time.Hour,
Height: 1,
Hash: header1.Hash(),
@@ -565,7 +566,7 @@ func TestClientRestoresTrustedHeaderAfterStartup3(t *testing.T) {
primary,
[]provider.Provider{primary},
trustedStore,
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -594,13 +595,13 @@ func TestClientRestoresTrustedHeaderAfterStartup3(t *testing.T) {
}
func TestClient_Update(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -619,13 +620,13 @@ func TestClient_Update(t *testing.T) {
}
func TestClient_Concurrency(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -663,14 +664,14 @@ func TestClient_Concurrency(t *testing.T) {
}
func TestClientReplacesPrimaryWithWitnessIfPrimaryIsUnavailable(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
deadNode,
[]provider.Provider{fullNode, fullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
MaxRetryAttempts(1),
lite.Logger(log.TestingLogger()),
lite.MaxRetryAttempts(1),
)
require.NoError(t, err)
@@ -684,9 +685,9 @@ func TestClientReplacesPrimaryWithWitnessIfPrimaryIsUnavailable(t *testing.T) {
func TestClient_BackwardsVerification(t *testing.T) {
{
trustHeader, _ := largeFullNode.SignedHeader(6)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 4 * time.Minute,
Height: trustHeader.Height,
Hash: trustHeader.Hash(),
@@ -694,7 +695,7 @@ func TestClient_BackwardsVerification(t *testing.T) {
largeFullNode,
[]provider.Provider{largeFullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -769,9 +770,9 @@ func TestClient_BackwardsVerification(t *testing.T) {
}
for _, tc := range testCases {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 1 * time.Hour,
Height: 3,
Hash: h3.Hash(),
@@ -779,7 +780,7 @@ func TestClient_BackwardsVerification(t *testing.T) {
tc.provider,
[]provider.Provider{tc.provider},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)
@@ -795,7 +796,7 @@ func TestClient_NewClientFromTrustedStore(t *testing.T) {
err := db.SaveSignedHeaderAndValidatorSet(h1, vals)
require.NoError(t, err)
c, err := NewClientFromTrustedStore(
c, err := lite.NewClientFromTrustedStore(
chainID,
trustPeriod,
deadNode,
@@ -819,14 +820,14 @@ func TestClient_NewClientFromTrustedStore(t *testing.T) {
}
func TestNewClientErrorsIfAllWitnessesUnavailable(t *testing.T) {
_, err := NewClient(
_, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{deadNode, deadNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
MaxRetryAttempts(1),
lite.Logger(log.TestingLogger()),
lite.MaxRetryAttempts(1),
)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "awaiting response from all witnesses exceeded dropout time")
@@ -862,14 +863,14 @@ func TestClientRemovesWitnessIfItSendsUsIncorrectHeader(t *testing.T) {
},
)
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{badProvider1, badProvider2},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
MaxRetryAttempts(1),
lite.Logger(log.TestingLogger()),
lite.MaxRetryAttempts(1),
)
// witness should have behaved properly -> no error
require.NoError(t, err)
@@ -889,13 +890,13 @@ func TestClientRemovesWitnessIfItSendsUsIncorrectHeader(t *testing.T) {
}
func TestClientTrustedValidatorSet(t *testing.T) {
c, err := NewClient(
c, err := lite.NewClient(
chainID,
trustOptions,
fullNode,
[]provider.Provider{fullNode},
dbs.New(dbm.NewMemDB(), chainID),
Logger(log.TestingLogger()),
lite.Logger(log.TestingLogger()),
)
require.NoError(t, err)

View File

@@ -1,4 +1,4 @@
package lite
package lite_test
import (
"fmt"
@@ -11,6 +11,7 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/tendermint/tendermint/abci/example/kvstore"
lite "github.com/tendermint/tendermint/lite2"
"github.com/tendermint/tendermint/lite2/provider"
httpp "github.com/tendermint/tendermint/lite2/provider/http"
dbs "github.com/tendermint/tendermint/lite2/store/db"
@@ -48,9 +49,9 @@ func ExampleClient_Update() {
stdlog.Fatal(err)
}
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 504 * time.Hour, // 21 days
Height: 2,
Hash: header.Hash(),
@@ -117,9 +118,9 @@ func ExampleClient_VerifyHeaderAtHeight() {
stdlog.Fatal(err)
}
c, err := NewClient(
c, err := lite.NewClient(
chainID,
TrustOptions{
lite.TrustOptions{
Period: 504 * time.Hour, // 21 days
Height: 2,
Hash: header.Hash(),

View File

@@ -1,4 +1,4 @@
package lite
package lite_test
import (
"time"

View File

@@ -7,6 +7,7 @@ import (
"github.com/tendermint/tendermint/lite2/provider"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/tendermint/tendermint/types"
)
@@ -21,14 +22,14 @@ type SignStatusClient interface {
// http provider uses an RPC client (or SignStatusClient more generally) to
// obtain the necessary information.
type http struct {
chainID string
client SignStatusClient
SignStatusClient // embed so interface can be converted to SignStatusClient for tests
chainID string
}
// New creates a HTTP provider, which is using the rpcclient.HTTP
// New creates a HTTP provider, which is using the rpchttp.HTTP
// client under the hood.
func New(chainID, remote string) (provider.Provider, error) {
httpClient, err := rpcclient.NewHTTP(remote, "/websocket")
httpClient, err := rpchttp.New(remote, "/websocket")
if err != nil {
return nil, err
}
@@ -38,8 +39,8 @@ func New(chainID, remote string) (provider.Provider, error) {
// NewWithClient allows you to provide custom SignStatusClient.
func NewWithClient(chainID string, client SignStatusClient) provider.Provider {
return &http{
chainID: chainID,
client: client,
SignStatusClient: client,
chainID: chainID,
}
}
@@ -49,7 +50,7 @@ func (p *http) ChainID() string {
}
func (p *http) String() string {
return fmt.Sprintf("http{%s}", p.client.Remote())
return fmt.Sprintf("http{%s}", p.Remote())
}
// SignedHeader fetches a SignedHeader at the given height and checks the
@@ -60,7 +61,7 @@ func (p *http) SignedHeader(height int64) (*types.SignedHeader, error) {
return nil, err
}
commit, err := p.client.Commit(h)
commit, err := p.SignStatusClient.Commit(h)
if err != nil {
// TODO: standartise errors on the RPC side
if strings.Contains(err.Error(), "height must be less than or equal") {
@@ -90,7 +91,7 @@ func (p *http) ValidatorSet(height int64) (*types.ValidatorSet, error) {
}
const maxPerPage = 100
res, err := p.client.Validators(h, 0, maxPerPage)
res, err := p.SignStatusClient.Validators(h, 0, maxPerPage)
if err != nil {
// TODO: standartise errors on the RPC side
if strings.Contains(err.Error(), "height must be less than or equal") {
@@ -106,7 +107,7 @@ func (p *http) ValidatorSet(height int64) (*types.ValidatorSet, error) {
// Check if there are more validators.
for len(res.Validators) == maxPerPage {
res, err = p.client.Validators(h, page, maxPerPage)
res, err = p.SignStatusClient.Validators(h, page, maxPerPage)
if err != nil {
return nil, err
}

View File

@@ -1,4 +1,4 @@
package http
package http_test
import (
"os"
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/abci/example/kvstore"
litehttp "github.com/tendermint/tendermint/lite2/provider/http"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpctest "github.com/tendermint/tendermint/rpc/test"
"github.com/tendermint/tendermint/types"
@@ -33,12 +34,12 @@ func TestProvider(t *testing.T) {
}
chainID := genDoc.ChainID
t.Log("chainID:", chainID)
p, err := New(chainID, rpcAddr)
p, err := litehttp.New(chainID, rpcAddr)
require.Nil(t, err)
require.NotNil(t, p)
// let it produce some blocks
err = rpcclient.WaitForHeight(p.(*http).client, 6, nil)
err = rpcclient.WaitForHeight(p.(rpcclient.StatusClient), 6, nil)
require.Nil(t, err)
// let's get the highest block

View File

@@ -1,4 +1,4 @@
package lite
package lite_test
import (
"fmt"
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
tmmath "github.com/tendermint/tendermint/libs/math"
lite "github.com/tendermint/tendermint/lite2"
"github.com/tendermint/tendermint/types"
)
@@ -117,7 +118,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
ErrInvalidHeader{Reason: types.ErrNotEnoughVotingPowerSigned{Got: 50, Needed: 93}},
lite.ErrInvalidHeader{Reason: types.ErrNotEnoughVotingPowerSigned{Got: 50, Needed: 93}},
"",
},
// vals does not match with what we have -> error
@@ -155,7 +156,7 @@ func TestVerifyAdjacentHeaders(t *testing.T) {
for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
err := VerifyAdjacent(chainID, header, tc.newHeader, tc.newVals, tc.trustingPeriod, tc.now, maxClockDrift)
err := lite.VerifyAdjacent(chainID, header, tc.newHeader, tc.newVals, tc.trustingPeriod, tc.now, maxClockDrift)
switch {
case tc.expErr != nil && assert.Error(t, err):
assert.Equal(t, tc.expErr, err)
@@ -231,7 +232,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
vals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
ErrInvalidHeader{types.ErrNotEnoughVotingPowerSigned{Got: 50, Needed: 93}},
lite.ErrInvalidHeader{types.ErrNotEnoughVotingPowerSigned{Got: 50, Needed: 93}},
"",
},
// 3/3 new vals signed, 2/3 old vals present -> no error
@@ -261,7 +262,7 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
lessThanOneThirdVals,
3 * time.Hour,
bTime.Add(2 * time.Hour),
ErrNewValSetCantBeTrusted{types.ErrNotEnoughVotingPowerSigned{Got: 20, Needed: 46}},
lite.ErrNewValSetCantBeTrusted{types.ErrNotEnoughVotingPowerSigned{Got: 20, Needed: 46}},
"",
},
}
@@ -269,9 +270,9 @@ func TestVerifyNonAdjacentHeaders(t *testing.T) {
for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
err := VerifyNonAdjacent(chainID, header, vals, tc.newHeader, tc.newVals, tc.trustingPeriod,
err := lite.VerifyNonAdjacent(chainID, header, vals, tc.newHeader, tc.newVals, tc.trustingPeriod,
tc.now, maxClockDrift,
DefaultTrustLevel)
lite.DefaultTrustLevel)
switch {
case tc.expErr != nil && assert.Error(t, err):
@@ -300,7 +301,7 @@ func TestVerifyReturnsErrorIfTrustLevelIsInvalid(t *testing.T) {
[]byte("app_hash"), []byte("cons_hash"), []byte("results_hash"), 0, len(keys))
)
err := Verify(chainID, header, vals, header, vals, 2*time.Hour, time.Now(), maxClockDrift,
err := lite.Verify(chainID, header, vals, header, vals, 2*time.Hour, time.Now(), maxClockDrift,
tmmath.Fraction{Numerator: 2, Denominator: 1})
assert.Error(t, err)
}
@@ -327,7 +328,7 @@ func TestValidateTrustLevel(t *testing.T) {
}
for _, tc := range testCases {
err := ValidateTrustLevel(tc.lvl)
err := lite.ValidateTrustLevel(tc.lvl)
if !tc.valid {
assert.Error(t, err)
} else {