github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/light/provider/http/http_test.go (about) 1 package http_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/ari-anchor/sei-tendermint/abci/example/kvstore" 13 "github.com/ari-anchor/sei-tendermint/light/provider" 14 lighthttp "github.com/ari-anchor/sei-tendermint/light/provider/http" 15 rpcclient "github.com/ari-anchor/sei-tendermint/rpc/client" 16 rpchttp "github.com/ari-anchor/sei-tendermint/rpc/client/http" 17 rpctest "github.com/ari-anchor/sei-tendermint/rpc/test" 18 "github.com/ari-anchor/sei-tendermint/types" 19 ) 20 21 func TestNewProvider(t *testing.T) { 22 c, err := lighthttp.New("chain-test", "192.168.0.1:26657") 23 require.NoError(t, err) 24 require.Equal(t, c.ID(), "http{http://192.168.0.1:26657}") 25 26 c, err = lighthttp.New("chain-test", "http://153.200.0.1:26657") 27 require.NoError(t, err) 28 require.Equal(t, c.ID(), "http{http://153.200.0.1:26657}") 29 30 c, err = lighthttp.New("chain-test", "153.200.0.1") 31 require.NoError(t, err) 32 require.Equal(t, c.ID(), "http{http://153.200.0.1}") 33 } 34 35 func TestProvider(t *testing.T) { 36 ctx, cancel := context.WithCancel(context.Background()) 37 defer cancel() 38 cfg, err := rpctest.CreateConfig(t, t.Name()) 39 require.NoError(t, err) 40 41 // start a tendermint node in the background to test against 42 app := kvstore.NewApplication() 43 app.RetainBlocks = 9 44 _, closer, err := rpctest.StartTendermint(ctx, cfg, app) 45 require.NoError(t, err) 46 47 rpcAddr := cfg.RPC.ListenAddress 48 genDoc, err := types.GenesisDocFromFile(cfg.GenesisFile()) 49 require.NoError(t, err) 50 51 chainID := genDoc.ChainID 52 t.Log("chainID:", chainID) 53 54 c, err := rpchttp.New(rpcAddr) 55 require.NoError(t, err) 56 57 p := lighthttp.NewWithClient(chainID, c) 58 require.NoError(t, err) 59 require.NotNil(t, p) 60 61 // let it produce some blocks 62 err = rpcclient.WaitForHeight(ctx, c, 10, nil) 63 require.NoError(t, err) 64 65 // let's get the highest block 66 lb, err := p.LightBlock(ctx, 0) 67 require.NoError(t, err) 68 assert.True(t, lb.Height < 9001, "height=%d", lb.Height) 69 70 // let's check this is valid somehow 71 assert.Nil(t, lb.ValidateBasic(chainID)) 72 73 // historical queries now work :) 74 lower := lb.Height - 3 75 lb, err = p.LightBlock(ctx, lower) 76 require.NoError(t, err) 77 assert.Equal(t, lower, lb.Height) 78 79 // fetching missing heights (both future and pruned) should return appropriate errors 80 lb, err = p.LightBlock(ctx, 9001) 81 require.Error(t, err) 82 require.Nil(t, lb) 83 assert.ErrorIs(t, err, provider.ErrHeightTooHigh) 84 85 lb, err = p.LightBlock(ctx, 1) 86 require.Error(t, err) 87 require.Nil(t, lb) 88 assert.ErrorIs(t, err, provider.ErrLightBlockNotFound) 89 90 // if the provider is unable to provide four more blocks then we should return 91 // an unreliable peer error 92 for i := 0; i < 4; i++ { 93 _, err = p.LightBlock(ctx, 1) 94 } 95 assert.IsType(t, provider.ErrUnreliableProvider{}, err) 96 97 // shut down tendermint node 98 require.NoError(t, closer(ctx)) 99 cancel() 100 101 time.Sleep(10 * time.Second) 102 lb, err = p.LightBlock(ctx, lower+2) 103 // Either the connection should be refused, or the context canceled. 104 require.Error(t, err) 105 require.Nil(t, lb) 106 if !errors.Is(err, provider.ErrConnectionClosed) && !errors.Is(err, context.Canceled) { 107 assert.Fail(t, "Incorrect error", "wanted connection closed or context canceled, got %v", err) 108 } 109 }