gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/light/provider/http/http_test.go (about)

     1  package http_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/tendermint/tendermint/abci/example/kvstore"
    14  	"github.com/tendermint/tendermint/light/provider"
    15  	lighthttp "github.com/tendermint/tendermint/light/provider/http"
    16  	rpcclient "github.com/tendermint/tendermint/rpc/client"
    17  	rpchttp "github.com/tendermint/tendermint/rpc/client/http"
    18  	rpctest "github.com/tendermint/tendermint/rpc/test"
    19  	"github.com/tendermint/tendermint/types"
    20  )
    21  
    22  func TestNewProvider(t *testing.T) {
    23  	c, err := lighthttp.New("chain-test", "192.168.0.1:26657")
    24  	require.NoError(t, err)
    25  	require.Equal(t, fmt.Sprintf("%s", c), "http{http://192.168.0.1:26657}")
    26  
    27  	c, err = lighthttp.New("chain-test", "http://153.200.0.1:26657")
    28  	require.NoError(t, err)
    29  	require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1:26657}")
    30  
    31  	c, err = lighthttp.New("chain-test", "153.200.0.1")
    32  	require.NoError(t, err)
    33  	require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1}")
    34  }
    35  
    36  func TestProvider(t *testing.T) {
    37  	app := kvstore.NewApplication()
    38  	app.RetainBlocks = 10
    39  	node := rpctest.StartTendermint(app)
    40  
    41  	cfg := rpctest.GetConfig()
    42  	defer os.RemoveAll(cfg.RootDir)
    43  	rpcAddr := cfg.RPC.ListenAddress
    44  	genDoc, err := types.GenesisDocFromFile(cfg.GenesisFile())
    45  	require.NoError(t, err)
    46  	chainID := genDoc.ChainID
    47  
    48  	c, err := rpchttp.New(rpcAddr, "/websocket")
    49  	require.Nil(t, err)
    50  
    51  	p := lighthttp.NewWithClient(chainID, c)
    52  	require.NoError(t, err)
    53  	require.NotNil(t, p)
    54  
    55  	// let it produce some blocks
    56  	err = rpcclient.WaitForHeight(c, 10, nil)
    57  	require.NoError(t, err)
    58  
    59  	// let's get the highest block
    60  	lb, err := p.LightBlock(context.Background(), 0)
    61  	require.NoError(t, err)
    62  	require.NotNil(t, lb)
    63  	assert.True(t, lb.Height < 1000)
    64  
    65  	// let's check this is valid somehow
    66  	assert.Nil(t, lb.ValidateBasic(chainID))
    67  
    68  	// historical queries now work :)
    69  	lower := lb.Height - 3
    70  	lb, err = p.LightBlock(context.Background(), lower)
    71  	require.NoError(t, err)
    72  	assert.Equal(t, lower, lb.Height)
    73  
    74  	// fetching missing heights (both future and pruned) should return appropriate errors
    75  	lb, err = p.LightBlock(context.Background(), 1000)
    76  	require.Error(t, err)
    77  	require.Nil(t, lb)
    78  	assert.Equal(t, provider.ErrHeightTooHigh, err)
    79  
    80  	_, err = p.LightBlock(context.Background(), 1)
    81  	require.Error(t, err)
    82  	require.Nil(t, lb)
    83  	assert.Equal(t, provider.ErrLightBlockNotFound, err)
    84  
    85  	// stop the full node and check that a no response error is returned
    86  	rpctest.StopTendermint(node)
    87  	time.Sleep(10 * time.Second)
    88  	lb, err = p.LightBlock(context.Background(), lower+2)
    89  	// we should see a connection refused
    90  	require.Error(t, err)
    91  	require.Contains(t, err.Error(), "connection refused")
    92  	require.Nil(t, lb)
    93  }